<?php
require_once __DIR__ . '/../includes/lang.php';
require_once __DIR__ . '/../db/config.php';

// Simple admin check (in real app, use roles)
if (!isset($_SESSION['user_id'])) {
    header('Location: /auth/login.php');
    exit;
}

$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email_verify = isset($_POST['email_verification_enabled']) ? '1' : '0';
    
    $settings_to_update = [
        'email_verification_enabled' => $email_verify,
        'smtp_host' => $_POST['smtp_host'] ?? '',
        'smtp_port' => $_POST['smtp_port'] ?? '587',
        'smtp_user' => $_POST['smtp_user'] ?? '',
        'smtp_pass' => $_POST['smtp_pass'] ?? '',
        'smtp_secure' => $_POST['smtp_secure'] ?? 'tls',
        'mail_from_email' => $_POST['mail_from_email'] ?? '',
        'mail_from_name' => $_POST['mail_from_name'] ?? 'Byro Exchange',
        'ios_download_url' => $_POST['ios_download_url'] ?? '#',
        'android_download_url' => $_POST['android_download_url'] ?? '#',
        'apk_download_url' => $_POST['apk_download_url'] ?? '/downloads/byro.apk'
    ];

    foreach ($settings_to_update as $key => $value) {
        $stmt = db()->prepare("INSERT INTO system_settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
        $stmt->execute([$key, $value]);
    }
    
    $message = '设置已更新';
}

$email_verify_enabled = getSetting('email_verification_enabled', '0') === '1';
$smtp_host = getSetting('smtp_host', '');
$smtp_port = getSetting('smtp_port', '587');
$smtp_user = getSetting('smtp_user', '');
$smtp_pass = getSetting('smtp_pass', '');
$smtp_secure = getSetting('smtp_secure', 'tls');
$mail_from_email = getSetting('mail_from_email', '');
$mail_from_name = getSetting('mail_from_name', 'Byro Exchange');

function getSetting($key, $default = null) {
    $stmt = db()->prepare("SELECT setting_value FROM system_settings WHERE setting_key = ?");
    $stmt->execute([$key]);
    $row = $stmt->fetch();
    return $row ? $row['setting_value'] : $default;
}

ob_start();
?>

<div class="row">
    <div class="col-md-8 mx-auto">
        <div class="card shadow-sm border-0">
            <div class="card-header bg-white py-3">
                <h5 class="mb-0 fw-bold"><i class="bi bi-gear-fill me-2 text-primary"></i> 注册与邮件设置</h5>
            </div>
            <div class="card-body p-4">
                <?php if ($message): ?>
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        <i class="bi bi-check-circle-fill me-2"></i><?= $message ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                <?php endif; ?>

                <form method="POST">
                    <div class="mb-4">
                        <label class="form-label fw-bold">注册验证</label>
                        <div class="form-check form-switch p-3 bg-light rounded-3 border">
                            <input class="form-check-input ms-0 me-3" type="checkbox" name="email_verification_enabled" id="email_verify" <?= $email_verify_enabled ? 'checked' : '' ?>>
                            <label class="form-check-label" for="email_verify">开启邮箱/手机验证码 (注册时必须填写)</label>
                        </div>
                        <div class="form-text text-muted mt-2 small">开启后，用户在注册页面必须通过验证码验证。测试环境默认验证码：123456</div>
                    </div>

                    <hr class="my-4">

                    <h6 class="fw-bold mb-3"><i class="bi bi-envelope-at me-2 text-primary"></i> SMTP 邮件服务设置</h6>
                    
                    <div class="row g-3">
                        <div class="col-md-9">
                            <label class="form-label small text-muted">SMTP 主机</label>
                            <input type="text" name="smtp_host" class="form-control" value="<?= htmlspecialchars($smtp_host) ?>" placeholder="例如: smtp.gmail.com">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label small text-muted">端口</label>
                            <input type="text" name="smtp_port" class="form-control" value="<?= htmlspecialchars($smtp_port) ?>" placeholder="587">
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label small text-muted">账号 / 邮箱</label>
                            <input type="text" name="smtp_user" class="form-control" value="<?= htmlspecialchars($smtp_user) ?>" placeholder="your-email@example.com">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small text-muted">授权码 / 密码</label>
                            <input type="password" name="smtp_pass" class="form-control" value="<?= htmlspecialchars($smtp_pass) ?>" placeholder="********">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label small text-muted">安全协议</label>
                            <select name="smtp_secure" class="form-select">
                                <option value="tls" <?= $smtp_secure === 'tls' ? 'selected' : '' ?>>TLS (推荐)</option>
                                <option value="ssl" <?= $smtp_secure === 'ssl' ? 'selected' : '' ?>>SSL</option>
                                <option value="none" <?= $smtp_secure === 'none' ? 'selected' : '' ?>>无</option>
                            </select>
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label small text-muted">发件人姓名</label>
                            <input type="text" name="mail_from_name" class="form-control" value="<?= htmlspecialchars($mail_from_name) ?>" placeholder="Byro Exchange">
                        </div>

                        <div class="col-md-12">
                            <label class="form-label small text-muted">发件人邮箱</label>
                            <input type="email" name="mail_from_email" class="form-control" value="<?= htmlspecialchars($mail_from_email) ?>" placeholder="no-reply@yourdomain.com">
                        </div>
                    </div>

                    <hr class="my-4">

                    <h6 class="fw-bold mb-3"><i class="bi bi-phone me-2 text-primary"></i> APP 下载设置</h6>
                    
                    <div class="row g-3">
                        <div class="col-md-12">
                            <label class="form-label small text-muted">iOS 下载链接 (App Store)</label>
                            <input type="text" name="ios_download_url" class="form-control" value="<?= htmlspecialchars(getSetting('ios_download_url', '#')) ?>" placeholder="https://apps.apple.com/app/...">
                        </div>
                        <div class="col-md-12">
                            <label class="form-label small text-muted">Android 下载链接 (Google Play)</label>
                            <input type="text" name="android_download_url" class="form-control" value="<?= htmlspecialchars(getSetting('android_download_url', '#')) ?>" placeholder="https://play.google.com/store/apps/...">
                        </div>
                        <div class="col-md-12">
                            <label class="form-label small text-muted">Android APK 直接下载链接</label>
                            <div class="input-group">
                                <input type="text" name="apk_download_url" class="form-control" value="<?= htmlspecialchars(getSetting('apk_download_url', '/downloads/byro.apk')) ?>" placeholder="/downloads/byro.apk">
                                <span class="input-group-text bg-light"><i class="bi bi-info-circle" title="建议将 APK 文件上传到 /downloads/ 目录下并在此填入路径"></i></span>
                            </div>
                        </div>
                    </div>

                    <div class="mt-5 text-end">
                        <button type="submit" class="btn btn-primary px-5 py-2 fw-bold rounded-pill shadow-sm">
                            保存所有设置
                        </button>
                    </div>
                </form>
            </div>
        </div>
        
        <div class="mt-4 p-4 bg-info bg-opacity-10 border border-info border-opacity-20 rounded-4">
            <h6 class="fw-bold text-info mb-2"><i class="bi bi-info-circle-fill me-2"></i> 关于短信验证</h6>
            <p class="small text-muted mb-0">
                目前系统通过邮件服务发送验证码。手机号注册将同样通过绑定的邮件或系统预设通道发送。如需接入特定短信网关，请联系技术支持对接 API。
            </p>
        </div>
    </div>
</div>

<?php 
$content = ob_get_clean();
require_once __DIR__ . '/layout.php';
renderAdminPage($content, '注册与邮件设置');
?>
