<?php
requireAdmin();

// 获取支付配置
$payment_config = json_decode(getSiteConfig('payment_config') ?? '{}', true);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $api_url = trim($_POST['api_url'] ?? '');
    $pid = trim($_POST['pid'] ?? '');
    $key = trim($_POST['key'] ?? '');
    $pay_alipay = isset($_POST['pay_alipay']) ? 1 : 0;
    $pay_wxpay = isset($_POST['pay_wxpay']) ? 1 : 0;
    
    // 默认配置值
$payment_config = [
    'api_url' => 'https://mzf.akwl.net/xpay/epay/',
    'pid' => '11094',
    'key' => '2LZ3NAkLVBixGcfz4oMz',
    'pay_alipay' => 1,
    'pay_wxpay' => 1
];
    
    updateSiteConfig('payment_config', json_encode($payment_config));
    $success = '支付配置已保存';
}
?>

<div class="payments-container">
    <div class="page-header">
        <h2>支付配置</h2>
        <p>配置易支付接口参数</p>
    </div>
    
    <?php if (isset($success)): ?>
        <div class="alert alert-success"><?php echo $success; ?></div>
    <?php endif; ?>
    
    <div class="form-card">
        <form method="POST">
            <div class="form-group">
                <label>支付接口地址</label>
                <input type="url" name="api_url" value="<?php echo htmlspecialchars($payment_config['api_url'] ?? 'https://z-pay.cn/'); ?>" placeholder="https://your-pay.com/">
                <small>易支付接口地址，以 / 结尾</small>
            </div>
            
            <div class="form-group">
                <label>商户ID (PID)</label>
                <input type="text" name="pid" value="<?php echo htmlspecialchars($payment_config['pid'] ?? ''); ?>">
            </div>
            
            <div class="form-group">
                <label>商户密钥 (KEY)</label>
                <input type="text" name="key" value="<?php echo htmlspecialchars($payment_config['key'] ?? ''); ?>">
            </div>
            
            <div class="form-group">
                <label>启用支付方式</label>
                <div class="checkbox-group">
                    <label class="checkbox-label">
                        <input type="checkbox" name="pay_alipay" value="1" <?php echo ($payment_config['pay_alipay'] ?? 1) == 1 ? 'checked' : ''; ?>>
                        支付宝
                    </label>
                    <label class="checkbox-label">
                        <input type="checkbox" name="pay_wxpay" value="1" <?php echo ($payment_config['pay_wxpay'] ?? 1) == 1 ? 'checked' : ''; ?>>
                        微信支付
                    </label>
                </div>
            </div>
            
            <div class="info-note">
                <h4>支付回调地址配置</h4>
                <p><strong>异步回调地址：</strong> <code><?php echo PAY_NOTIFY_URL; ?></code></p>
                <p><strong>同步回调地址：</strong> <code><?php echo PAY_RETURN_URL; ?></code></p>
                <p class="text-muted">请在支付平台后台配置上述回调地址</p>
            </div>
            
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">保存配置</button>
            </div>
        </form>
    </div>
</div>

<style>
.form-card {
    max-width: 500px;
    background: var(--bg-primary);
    border-radius: var(--radius-md);
    padding: 30px;
    border: 1px solid var(--border-color);
    margin-bottom: 20px;
}
.info-note {
    background: var(--bg-secondary);
    padding: 15px;
    border-radius: var(--radius-sm);
    margin: 20px 0;
}
.info-note code {
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 4px;
}
.checkbox-group {
    display: flex;
    gap: 20px;
    margin-top: 8px;
}
.checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
}
</style>