<?php
require_once __DIR__ . '/../db/config.php';
session_start();

if (!isset($_SESSION['admin_logged_in'])) {
    header('Location: ../login.php');
    exit;
}

$message = '';
$db = db();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_settings'])) {
    foreach ($_POST['settings'] as $key => $value) {
        $stmt = $db->prepare("UPDATE settings SET key_value = ? WHERE key_name = ?");
        $stmt->execute([$value, $key]);
    }
    $message = '设置已成功更新';
}

$settings_raw = $db->query("SELECT * FROM settings ORDER BY id ASC")->fetchAll();
$settings = [];
foreach ($settings_raw as $s) {
    $settings[$s['key_name']] = $s;
}

// Group settings for better UI
$groups = [
    '基础信息' => ['site_name', 'site_logo', 'site_description', 'footer_text', 'notice', 'tg_channel'],
    '外观样式 (PC端)' => ['primary_color', 'accent_color'],
    '外观样式 (手机端)' => ['mobile_primary_color', 'mobile_accent_color'],
    '支付与联系' => ['usdt_address', 'qr_code_custom', 'tg_link'],
    'API 与 通知' => ['tg_bot_token', 'tg_chat_id']
];
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>系统全域设置 - 管理后台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <style>
        body { background-color: #f0f2f5; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
        .sidebar { min-height: 100vh; background: #1a1d20; color: white; }
        .sidebar a { color: rgba(255,255,255,0.7); text-decoration: none; padding: 12px 25px; display: block; transition: all 0.3s; border-left: 4px solid transparent; }
        .sidebar a:hover { background: rgba(255,255,255,0.05); color: white; border-left-color: #0d6efd; }
        .sidebar a.active { background: rgba(13, 110, 253, 0.1); color: #0d6efd; border-left-color: #0d6efd; font-weight: bold; }
        .card { border: none; border-radius: 12px; margin-bottom: 2rem; }
        .group-title { border-bottom: 2px solid #0d6efd; padding-bottom: 8px; margin-bottom: 20px; color: #1a1d20; font-weight: 800; display: inline-block; }
        .form-label { color: #4b5563; font-size: 0.9rem; margin-bottom: 8px; }
        .preview-logo { max-height: 50px; background: #eee; padding: 5px; border-radius: 5px; }
        .color-input-wrapper { display: flex; align-items: center; gap: 10px; }
        .color-preview { width: 30px; height: 30px; border-radius: 5px; border: 1px solid #ddd; }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-2 p-0 sidebar">
            <?php include 'sidebar.php'; ?>
        </div>
        <div class="col-md-10 p-4 p-lg-5">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2 class="fw-bold"><i class="bi bi-sliders2-vertical me-2 text-primary"></i> 全域系统配置</h2>
                <div class="text-muted">您可以从这里全面掌控前端展示效果</div>
            </div>
            
            <?php if ($message): ?>
                <div class="alert alert-success alert-dismissible fade show shadow-sm border-0" role="alert">
                    <i class="bi bi-check-circle-fill me-2"></i> <?php echo $message; ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            <?php endif; ?>

            <form method="POST">
                <input type="hidden" name="update_settings" value="1">
                
                <?php foreach ($groups as $group_name => $keys): ?>
                <div class="card shadow-sm">
                    <div class="card-body p-4">
                        <h5 class="group-title"><?php echo $group_name; ?></h5>
                        <div class="row">
                            <?php foreach ($keys as $key): ?>
                                <?php if (!isset($settings[$key])) continue; 
                                    $s = $settings[$key];
                                ?>
                                <div class="col-md-6 mb-4">
                                    <label class="form-label fw-bold d-flex justify-content-between">
                                        <span><?php echo $s['description']; ?></span>
                                        <code class="text-muted small"><?php echo $key; ?></code>
                                    </label>
                                    
                                    <?php if ($key === 'notice' || $key === 'site_description'): ?>
                                        <textarea name="settings[<?php echo $key; ?>]" class="form-control border-light-subtle shadow-sm" rows="3"><?php echo htmlspecialchars($s['key_value']); ?></textarea>
                                    <?php elseif (strpos($key, 'color') !== false): ?>
                                        <div class="color-input-wrapper">
                                            <input type="color" class="form-control-color border-0" value="<?php echo htmlspecialchars($s['key_value']); ?>" oninput="this.nextElementSibling.value = this.value">
                                            <input type="text" name="settings[<?php echo $key; ?>]" class="form-control border-light-subtle shadow-sm" value="<?php echo htmlspecialchars($s['key_value']); ?>">
                                        </div>
                                    <?php elseif ($key === 'site_logo'): ?>
                                        <div class="d-flex align-items-center gap-3">
                                            <input type="text" name="settings[<?php echo $key; ?>]" class="form-control border-light-subtle shadow-sm" value="<?php echo htmlspecialchars($s['key_value']); ?>" oninput="this.nextElementSibling.src = this.value">
                                            <img src="<?php echo htmlspecialchars($s['key_value']); ?>" class="preview-logo" onerror="this.src='../assets/pasted-20260208-082111-302e08e2.png'">
                                        </div>
                                    <?php else: ?>
                                        <input type="text" name="settings[<?php echo $key; ?>]" class="form-control border-light-subtle shadow-sm" value="<?php echo htmlspecialchars($s['key_value']); ?>">
                                    <?php endif; ?>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>

                <div class="sticky-bottom bg-white p-3 shadow rounded-4 text-center mb-5 border border-primary border-opacity-10">
                    <button type="submit" class="btn btn-primary btn-lg px-5 shadow fw-bold">
                        <i class="bi bi-cloud-arrow-up-fill me-2"></i> 保存并生效所有修改
                    </button>
                    <p class="text-muted small mt-2 mb-0">所有更改将实时反映在 PC 端和手机端前端页面上。</p>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>