<?php
ob_start();
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' => $api_url,
        'pid' => $pid,
        'key' => $key,
        'pay_alipay' => $pay_alipay,
        'pay_wxpay' => $pay_wxpay
    ];
    
    updateSiteConfig('payment_config', json_encode($payment_config));
    $success = '支付配置已保存';
}
?>

<div class="payments-container">
    <div class="page-header">
        <h2>
            <img src="https://api.iconify.design/mdi:credit-card.svg?color=%23667eea" width="26" height="26" style="vertical-align: middle; margin-right: 10px;" alt="">
            支付配置
        </h2>
        <p class="text-muted">
            <img src="https://api.iconify.design/mdi:cog.svg?color=%239ca3af" width="14" height="14" style="margin-right: 4px;" alt="">
            配置易支付接口参数
        </p>
    </div>
    
    <?php if (isset($success)): ?>
        <div class="alert alert-success">
            <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="20" height="20" style="margin-right: 8px;" alt="">
            <?php echo $success; ?>
        </div>
    <?php endif; ?>
    
    <div class="form-card">
        <form method="POST">
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:web.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    支付接口地址
                </label>
                <input type="url" name="api_url" value="<?php echo htmlspecialchars($payment_config['api_url'] ?? ''); ?>" placeholder="https://your-pay.com/">
                <small>易支付接口地址，以 / 结尾</small>
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:identifier.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    商户ID (PID)
                </label>
                <input type="text" name="pid" value="<?php echo htmlspecialchars($payment_config['pid'] ?? ''); ?>" placeholder="请输入商户ID">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:key.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    商户密钥 (KEY)
                </label>
                <input type="text" name="key" value="<?php echo htmlspecialchars($payment_config['key'] ?? ''); ?>" placeholder="请输入商户密钥">
            </div>
            
            <div class="form-group">
                <label>
                    <img src="https://api.iconify.design/mdi:toggle-switch.svg?color=%23667eea" width="16" height="16" style="margin-right: 6px;" alt="">
                    启用支付方式
                </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' : ''; ?>>
                        <span class="checkbox-custom"></span>
                        <span class="checkbox-text">
                            <img src="/assets/svg/alipay-ys.svg" width="18" height="18" style="margin-right: 6px;" alt="">
                            支付宝
                        </span>
                    </label>
                    <label class="checkbox-label">
                        <input type="checkbox" name="pay_wxpay" value="1" <?php echo ($payment_config['pay_wxpay'] ?? 1) == 1 ? 'checked' : ''; ?>>
                        <span class="checkbox-custom"></span>
                        <span class="checkbox-text">
                            <img src="/assets/svg/wxpay-ys.svg" width="18" height="18" style="margin-right: 6px;" alt="">
                            微信支付
                        </span>
                    </label>
                </div>
            </div>
            
            <div class="info-note">
                <div class="info-note-header">
                    <img src="https://api.iconify.design/mdi:information.svg?color=%23667eea" width="20" height="20" style="margin-right: 8px;" alt="">
                    <h4>支付回调地址配置</h4>
                </div>
                <div class="callback-item">
                    <span class="callback-label">
                        <img src="https://api.iconify.design/mdi:arrow-right-circle.svg?color=%236b7280" width="14" height="14" style="margin-right: 6px;" alt="">
                        异步回调地址：
                    </span>
                    <code><?php echo PAY_NOTIFY_URL; ?></code>
                    <button type="button" class="copy-url-btn" onclick="copyToClipboard('<?php echo PAY_NOTIFY_URL; ?>')" title="复制">
                        <img src="https://api.iconify.design/mdi:content-copy.svg?color=%23667eea" width="14" height="14" alt="">
                    </button>
                </div>
                <div class="callback-item">
                    <span class="callback-label">
                        <img src="https://api.iconify.design/mdi:check-circle.svg?color=%236b7280" width="14" height="14" style="margin-right: 6px;" alt="">
                        同步回调地址：
                    </span>
                    <code><?php echo PAY_RETURN_URL; ?></code>
                    <button type="button" class="copy-url-btn" onclick="copyToClipboard('<?php echo PAY_RETURN_URL; ?>')" title="复制">
                        <img src="https://api.iconify.design/mdi:content-copy.svg?color=%23667eea" width="14" height="14" alt="">
                    </button>
                </div>
                <p class="text-muted">
                    <img src="https://api.iconify.design/mdi:alert-circle.svg?color=%23f59e0b" width="14" height="14" style="margin-right: 4px;" alt="">
                    请在支付平台后台配置上述回调地址
                </p>
            </div>
            
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">
                    <img src="https://api.iconify.design/mdi:content-save.svg?color=white" width="18" height="18" style="margin-right: 6px;" alt="">
                    保存配置
                </button>
            </div>
        </form>
    </div>
</div>

<style>
/* 基础变量 */
:root {
    --primary-color: #667eea;
    --primary-hover: #5a67d8;
    --danger-color: #ef4444;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --bg-primary: #ffffff;
    --bg-secondary: #f9fafb;
    --text-primary: #1f2937;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border-color: #e5e7eb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

.payments-container {
    max-width: 560px;
    margin: 0 auto;
    padding: 4px;
}

/* 页面头部 */
.page-header {
    margin-bottom: 24px;
}
.page-header h2 {
    display: flex;
    align-items: center;
    font-size: 24px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 6px;
}
.text-muted {
    display: flex;
    align-items: center;
    color: var(--text-muted);
    font-size: 13px;
}

/* 提示消息 */
.alert {
    display: flex;
    align-items: center;
    padding: 14px 20px;
    border-radius: var(--radius-md);
    margin-bottom: 24px;
    font-size: 14px;
}
.alert-success {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid #a7f3d0;
}

/* 表单卡片 */
.form-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 32px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}

/* 表单组 */
.form-group {
    margin-bottom: 22px;
}
.form-group label {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    font-weight: 500;
    font-size: 14px;
    color: var(--text-primary);
}
.form-group input[type="text"],
.form-group input[type="url"] {
    width: 100%;
    padding: 12px 16px;
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 14px;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: all 0.2s;
}
.form-group input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
.form-group small {
    display: block;
    margin-top: 6px;
    font-size: 12px;
    color: var(--text-muted);
}

/* 复选框组 */
.checkbox-group {
    display: flex;
    gap: 24px;
    margin-top: 8px;
}
.checkbox-label {
    display: flex;
    align-items: center;
    cursor: pointer;
}
.checkbox-label input[type="checkbox"] {
    width: 18px;
    height: 18px;
    margin-right: 8px;
    cursor: pointer;
    accent-color: var(--primary-color);
}
.checkbox-text {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: var(--text-primary);
}

/* 信息提示区 */
.info-note {
    background: var(--bg-secondary);
    padding: 20px;
    border-radius: var(--radius-md);
    margin: 24px 0;
    border: 1px solid var(--border-color);
}
.info-note-header {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
}
.info-note-header h4 {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}
.callback-item {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    flex-wrap: wrap;
    gap: 8px;
}
.callback-label {
    display: flex;
    align-items: center;
    font-size: 13px;
    color: var(--text-secondary);
    min-width: 120px;
}
.callback-item code {
    background: var(--bg-primary);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    font-size: 12px;
    font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
    color: var(--primary-color);
    border: 1px solid var(--border-color);
    word-break: break-all;
}
.copy-url-btn {
    background: none;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 6px 10px;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.copy-url-btn:hover {
    background: var(--bg-primary);
    border-color: var(--primary-color);
}
.info-note .text-muted {
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px dashed var(--border-color);
}

/* 按钮 */
.form-actions {
    margin-top: 28px;
}
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 12px 28px;
    border-radius: var(--radius-md);
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid transparent;
}
.btn-primary {
    background: var(--primary-color);
    color: white;
    box-shadow: 0 2px 4px rgba(102, 126, 234, 0.2);
    width: 100%;
}
.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}

/* 响应式 */
@media (max-width: 768px) {
    .form-card {
        padding: 20px;
    }
    .callback-item {
        flex-direction: column;
        align-items: flex-start;
    }
    .callback-label {
        min-width: auto;
    }
    .callback-item code {
        width: 100%;
    }
}
</style>

<script>
function copyToClipboard(text) {
    navigator.clipboard?.writeText(text).then(() => {
        window.showToast && window.showToast('回调地址已复制', 'success');
    }).catch(() => {
        const ta = document.createElement('textarea');
        ta.value = text;
        document.body.appendChild(ta);
        ta.select();
        document.execCommand('copy');
        document.body.removeChild(ta);
        window.showToast && window.showToast('回调地址已复制', 'success');
    });
}
</script>