<?php
require_once "auth.php";
require_once '../db/config.php';
$pdo = db();

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
    $id = $_POST['id'];
    $control = $_POST['control']; // 'none', 'win', 'loss'
    $stmt = $pdo->prepare("UPDATE option_orders SET control = ? WHERE id = ? AND status = 'pending'");
    $stmt->execute([$control, $id]);
    header("Location: options_orders.php");
    exit;
}

$orders = $pdo->query("SELECT o.*, u.username, u.win_loss_control as user_control FROM option_orders o JOIN users u ON o.user_id = u.id ORDER BY o.id DESC")->fetchAll();
$unread_msgs = $pdo->query("SELECT COUNT(*) FROM messages WHERE sender = 'user' AND is_read = 0")->fetchColumn();
$pending_orders = $pdo->query("SELECT COUNT(*) FROM fiat_orders WHERE status IN ('matching', 'submitting')")->fetchColumn();
$pending_kyc = $pdo->query("SELECT COUNT(*) FROM users WHERE kyc_status = 1")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>秒合约订单 - NovaEx 管理后台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        :root { --primary: #f0b90b; --bg: #ffffff; --text: #1e2329; --border: #eaecef; }
        body { background: #f4f6f9; color: var(--text); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; margin: 0; }
        .admin-layout { display: flex; min-height: 100vh; }
        .sidebar { width: 250px; background: #ffffff; border-right: 1px solid var(--border); padding: 1.5rem; }
        .main-content { flex: 1; padding: 2rem; background: #ffffff; }
        .menu-item { padding: 12px 15px; color: #474d57; text-decoration: none; display: flex; align-items: center; gap: 12px; border-radius: 8px; margin-bottom: 8px; transition: 0.2s; }
        .menu-item:hover, .menu-item.active { background: #f5f5f5; color: var(--primary); font-weight: bold; }
        .badge { background: #f6465d; color: white; border-radius: 10px; padding: 2px 8px; font-size: 0.7rem; margin-left: auto; }
        .card { background: white; border-radius: 12px; border: 1px solid var(--border); padding: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.05); }
        .table { width: 100%; border-collapse: collapse; margin-top: 1.5rem; font-size: 0.85rem; }
        .table th, .table td { padding: 12px; text-align: left; border-bottom: 1px solid var(--border); }
        .table th { background: #f9fafb; color: #707a8a; font-weight: 600; text-transform: uppercase; }
        .btn-sm { padding: 5px 10px; font-size: 0.75rem; border-radius: 4px; border: 1px solid transparent; cursor: pointer; }
        .control-active { border: 2px solid #000 !important; font-weight: bold; }
    </style>
</head>
<body>
<div class="admin-layout">
    <div class="sidebar">
        <h2 style="color: var(--primary); margin-bottom: 2rem;">NovaEx Admin</h2>
        <a href="index.php" class="menu-item"><i class="fas fa-home"></i> 仪表盘</a>
        <a href="users.php" class="menu-item"><i class="fas fa-users"></i> 用户管理</a>
        <a href="agents.php" class="menu-item"><i class="fas fa-user-shield"></i> 代理管理</a>
        <a href="kyc.php" class="menu-item"><i class="fas fa-id-card"></i> KYC 审核
            <?php if($pending_kyc > 0): ?><span class="badge"><?php echo $pending_kyc; ?></span><?php endif; ?>
        </a>
        <a href="chat.php" class="menu-item"><i class="fas fa-comments"></i> 客服管理
            <?php if($unread_msgs > 0 || $pending_orders > 0): ?><span class="badge"><?php echo ($unread_msgs + $pending_orders); ?></span><?php endif; ?>
        </a>
        <a href="options_orders.php" class="menu-item active"><i class="fas fa-clock"></i> 秒合约</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="withdrawals.php" class="menu-item"><i class="fas fa-hand-holding-usd"></i> 提现记录</a>
        <a href="settings.php" class="menu-item"><i class="fas fa-cog"></i> 系统设置</a>
        <a href="logout.php" class="menu-item"><i class="fas fa-sign-out-alt"></i> 退出登录</a>
    </div>

    <div class="main-content">
        <h1>秒合约订单管理</h1>
        <div class="card" style="margin-top: 20px;">
            <table class="table">
                <thead>
                    <tr>
                        <th>ID / 用户</th>
                        <th>交易对</th>
                        <th>方向</th>
                        <th>金额</th>
                        <th>周期</th>
                        <th>开仓价</th>
                        <th>状态</th>
                        <th>当前控制</th>
                        <th>时间</th>
                        <th>输赢干预</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($orders as $o): ?>
                    <tr>
                        <td>
                            <div style="font-weight: bold;"><?php echo $o['id']; ?></div>
                            <div style="font-size: 0.75rem; color: #707a8a;"><?php echo htmlspecialchars($o['username']); ?></div>
                        </td>
                        <td><?php echo $o['symbol']; ?></td>
                        <td><span style="color: <?php echo $o['direction'] == 'up' ? '#00c087' : '#f6465d'; ?>; font-weight: bold;"><?php echo $o['direction'] == 'up' ? '买涨 ↑' : '买跌 ↓'; ?></span></td>
                        <td style="font-weight: bold;"><?php echo number_format($o['amount'], 2); ?></td>
                        <td><?php echo $o['duration']; ?>s</td>
                        <td><?php echo number_format($o['opening_price'], 2); ?></td>
                        <td>
                            <span style="background: #eef2f7; padding: 3px 8px; border-radius: 4px; font-size: 0.75rem;">
                                <?php echo $o['status'] == 'pending' ? '进行中' : '已结算'; ?>
                            </span>
                        </td>
                        <td>
                            <?php 
                            $ctrl = $o['control'];
                            if ($o['status'] == 'pending') {
                                if ($ctrl != 'none') {
                                    echo "<span style='color: ".($ctrl == 'win' ? '#00c087' : '#f6465d')."; font-weight: bold;'>单控: ".($ctrl == 'win' ? '赢' : '亏')."</span>";
                                } else {
                                    $uc = $o['user_control'];
                                    echo "<span style='color: #848e9c;'>".($uc == 'none' ? '默认' : ($uc == 'win' ? '全控赢' : '全控亏'))."</span>";
                                }
                            } else {
                                echo "<span style='color: ".($o['result'] == 'win' ? '#00c087' : '#f6465d')."; font-weight: bold;'>".($o['result'] == 'win' ? '盈利' : '亏损')."</span>";
                            }
                            ?>
                        </td>
                        <td style="font-size: 0.75rem; color: #848e9c;"><?php echo $o['created_at']; ?></td>
                        <td>
                            <?php if($o['status'] == 'pending'): ?>
                                <form method="POST" style="display: flex; gap: 5px;">
                                    <input type="hidden" name="id" value="<?php echo $o['id']; ?>">
                                    <input type="hidden" name="action" value="control">
                                    <button type="submit" name="control" value="win" class="btn-sm <?php echo $ctrl == 'win' ? 'control-active' : ''; ?>" style="background: #00c087; color: white;">控赢</button>
                                    <button type="submit" name="control" value="loss" class="btn-sm <?php echo $ctrl == 'loss' ? 'control-active' : ''; ?>" style="background: #f6465d; color: white;">控亏</button>
                                    <button type="submit" name="control" value="none" class="btn-sm <?php echo $ctrl == 'none' ? 'control-active' : ''; ?>" style="background: #eee; color: #333;">取消</button>
                                </form>
                            <?php else: ?>
                                <span style="color: #ccc;">已结单</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>