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

$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();

$orders = $pdo->query("SELECT o.*, u.username FROM option_orders o JOIN users u ON o.user_id = u.id ORDER BY o.created_at DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>秒合约详情 - NovaEx 管理后台</title>
    <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: #FFFFFF; border-right: 1px solid #EAECEF; padding: 1rem; }
        .main-content { flex: 1; padding: 2rem; background: #FFFFFF; color: #1E2329; }
        .menu-item { padding: 12px; color: #474D57; text-decoration: none; display: flex; align-items: center; gap: 10px; border-radius: 4px; margin-bottom: 5px; }
        .menu-item:hover, .menu-item.active { background: #F5F5F5; color: #F0B90B; }
        .badge { background: #F6465D; color: white; border-radius: 10px; padding: 2px 8px; font-size: 0.7rem; margin-left: auto; }
        .table { width: 100%; border-collapse: collapse; margin-top: 1rem; }
        .table th, .table td { padding: 12px; text-align: left; border-bottom: 1px solid #EAECEF; font-size: 0.85rem; color: #1E2329; }
    </style>
</head>
<body style="background: white;">
<div class="admin-layout">
    <div class="sidebar">
        <h3 style="color: #1E2329; margin-bottom: 2rem;">NovaEx 管理员</h3>
        <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="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="settings.php" class="menu-item"><i class="fas fa-cog"></i> 系统设置</a>
    </div>
    <div class="main-content">
        <h2>秒合约交易记录</h2>
        <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>
                    <th>状态</th>
                    <th>下单时间</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($orders as $o): ?>
                <tr>
                    <td><?php echo $o['id']; ?></td>
                    <td><?php echo htmlspecialchars($o['username']); ?></td>
                    <td><?php echo $o['symbol']; ?></td>
                    <td style="color: <?php echo $o['direction'] == 'up' ? '#00c087' : '#f6465d'; ?>">
                        <?php echo $o['direction'] == 'up' ? '买涨 ↑' : '买跌 ↓'; ?>
                    </td>
                    <td><b><?php echo number_format($o['amount'], 2); ?></b></td>
                    <td><?php echo $o['duration']; ?>s</td>
                    <td><?php echo $o['profit_rate'] * 100; ?>%</td>
                    <td><?php echo number_format($o['opening_price'], 4); ?></td>
                    <td><?php echo $o['closing_price'] ? number_format($o['closing_price'], 4) : '--'; ?></td>
                    <td style="color: <?php echo $o['result'] == 'win' ? '#00c087' : ($o['result'] == 'loss' ? '#f6465d' : 'inherit'); ?>">
                        <?php echo $o['result'] == 'none' ? '--' : number_format($o['profit'], 2); ?>
                    </td>
                    <td>
                        <?php echo $o['status'] == 'pending' ? '<span style="color: #f0b90b;">进行中</span>' : '<span style="color: #848e9c;">已完成</span>'; ?>
                    </td>
                    <td><?php echo $o['created_at']; ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>