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

if (isset($_POST['action']) && isset($_POST['user_id'])) {
    $uid = $_POST['user_id'];
    $status = $_POST['action'] == 'approve' ? 2 : 0;
    $pdo->prepare("UPDATE users SET kyc_status = ? WHERE id = ?")->execute([$status, $uid]);
}

$pending_kyc_list = $pdo->query("SELECT * FROM users WHERE kyc_status = 1 ORDER BY id DESC")->fetchAll();
$approved_kyc_list = $pdo->query("SELECT * FROM users WHERE kyc_status = 2 ORDER BY 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_count = count($pending_kyc_list);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>KYC 审核 - 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; }
        
        .kyc-card { background: white; border: 1px solid var(--border); padding: 25px; border-radius: 12px; margin-bottom: 25px; box-shadow: 0 2px 8px rgba(0,0,0,0.05); }
        .kyc-img { width: 180px; height: 110px; object-fit: cover; border-radius: 8px; cursor: pointer; border: 1px solid var(--border); transition: 0.2s; }
        .kyc-img:hover { transform: scale(1.05); box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
        .btn { padding: 10px 20px; border-radius: 6px; border: none; cursor: pointer; color: white; font-weight: bold; font-size: 0.9rem; }
        
        .tabs { display: flex; gap: 20px; border-bottom: 1px solid var(--border); margin-bottom: 30px; }
        .tab { padding: 12px 20px; cursor: pointer; font-weight: 600; color: #707a8a; position: relative; }
        .tab.active { color: var(--primary); }
        .tab.active::after { content: ''; position: absolute; bottom: -1px; left: 0; right: 0; height: 3px; background: var(--primary); border-radius: 3px 3px 0 0; }
        
        #detail-modal { position: fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.8); display:none; align-items:center; justify-content:center; z-index:9999; }
        .modal-content { background:white; padding:30px; border-radius:12px; max-width:600px; width:90%; }
    </style>
</head>
<body>
<div id="detail-modal">
    <div class="modal-content">
        <h2 id="modal-title">用户认证详情</h2>
        <div id="modal-body" style="display:flex; gap:20px; flex-wrap:wrap; margin-top:20px;"></div>
        <button onclick="document.getElementById('detail-modal').style.display='none'" class="btn" style="background:var(--text); width:100%; margin-top:20px;">关闭</button>
    </div>
</div>

<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 active"><i class="fas fa-id-card"></i> KYC 审核
            <?php if($pending_kyc_count > 0): ?><span class="badge"><?php echo $pending_kyc_count; ?></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"><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>实名认证管理 (KYC)</h1>
        
        <div class="tabs">
            <div class="tab active" onclick="switchTab('pending')">待审核 (<?php echo count($pending_kyc_list); ?>)</div>
            <div class="tab" onclick="switchTab('approved')">已通过 (<?php echo count($approved_kyc_list); ?>)</div>
        </div>

        <div id="pending-section">
            <?php if(empty($pending_kyc_list)): ?>
                <div style="text-align: center; padding: 100px; color: #707a8a; background: #f9fafb; border-radius: 12px; border: 1px dashed var(--border);">
                    <i class="fas fa-check-circle" style="font-size: 3rem; margin-bottom: 15px; opacity: 0.3;"></i>
                    <p>目前没有待处理的认证申请</p>
                </div>
            <?php endif; ?>

            <?php foreach($pending_kyc_list as $k): ?>
            <div class="kyc-card">
                <div style="display: flex; justify-content: space-between; align-items: flex-start;">
                    <div>
                        <div style="display: flex; align-items: center; gap: 10px; margin-bottom: 5px;">
                            <span style="font-size: 1.2rem; font-weight: 800;"><?php echo htmlspecialchars($k['kyc_name']); ?></span>
                            <span style="background: #fff9e6; color: #f0b90b; padding: 2px 8px; border-radius: 4px; font-size: 0.75rem; font-weight: bold;">待审核</span>
                        </div>
                        <p style="color: #474d57; font-size: 1rem; margin-bottom: 8px;">证件号码: <span style="font-family: monospace; font-weight: bold;"><?php echo htmlspecialchars($k['kyc_id_number']); ?></span></p>
                        <p style="color: #707a8a; font-size: 0.85rem;">关联账号: <?php echo htmlspecialchars($k['username']); ?> (UID: <?php echo $k['uid']; ?>)</p>
                    </div>
                    <div style="display: flex; gap: 10px;">
                        <form method="POST">
                            <input type="hidden" name="user_id" value="<?php echo $k['id']; ?>">
                            <input type="hidden" name="action" value="approve">
                            <button type="submit" class="btn" style="background: #00c087;"><i class="fas fa-check"></i> 通过审核</button>
                        </form>
                        <form method="POST">
                            <input type="hidden" name="user_id" value="<?php echo $k['id']; ?>">
                            <input type="hidden" name="action" value="reject">
                            <button type="submit" class="btn" style="background: #f6465d;"><i class="fas fa-times"></i> 驳回</button>
                        </form>
                    </div>
                </div>
                <div style="display: flex; gap: 20px; margin-top: 25px; flex-wrap: wrap;">
                    <div style="text-align: center;">
                        <img src="../<?php echo $k['kyc_id_front']; ?>" class="kyc-img" onclick="window.open(this.src)">
                        <div style="font-size: 0.75rem; color: #707a8a; margin-top: 8px;">正面照</div>
                    </div>
                    <div style="text-align: center;">
                        <img src="../<?php echo $k['kyc_id_back']; ?>" class="kyc-img" onclick="window.open(this.src)">
                        <div style="font-size: 0.75rem; color: #707a8a; margin-top: 8px;">反面照</div>
                    </div>
                    <div style="text-align: center;">
                        <img src="../<?php echo $k['kyc_id_handheld']; ?>" class="kyc-img" onclick="window.open(this.src)">
                        <div style="font-size: 0.75rem; color: #707a8a; margin-top: 8px;">手持照</div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>

        <div id="approved-section" style="display: none;">
            <?php if(empty($approved_kyc_list)): ?>
                <p style="color: #707A8A; text-align: center; padding: 50px;">暂无已通过认证的用户。</p>
            <?php endif; ?>

            <div style="background: white; border-radius: 12px; border: 1px solid var(--border); overflow: hidden;">
                <table style="width: 100%; border-collapse: collapse;">
                    <thead style="background: #f9fafb;">
                        <tr>
                            <th style="padding: 15px; text-align: left; color: #707a8a; font-size: 0.8rem;">姓名</th>
                            <th style="padding: 15px; text-align: left; color: #707a8a; font-size: 0.8rem;">证件号码</th>
                            <th style="padding: 15px; text-align: left; color: #707a8a; font-size: 0.8rem;">关联账号</th>
                            <th style="padding: 15px; text-align: left; color: #707a8a; font-size: 0.8rem;">认证时间</th>
                            <th style="padding: 15px; text-align: right; color: #707a8a; font-size: 0.8rem;">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($approved_kyc_list as $k): ?>
                        <tr style="border-top: 1px solid var(--border);">
                            <td style="padding: 15px; font-weight: bold;"><?php echo htmlspecialchars($k['kyc_name']); ?></td>
                            <td style="padding: 15px; font-family: monospace;"><?php echo htmlspecialchars($k['kyc_id_number']); ?></td>
                            <td style="padding: 15px;"><?php echo htmlspecialchars($k['username']); ?> (<?php echo $k['uid']; ?>)</td>
                            <td style="padding: 15px; font-size: 0.85rem; color: #707a8a;"><?php echo $k['created_at']; ?></td>
                            <td style="padding: 15px; text-align: right; display: flex; gap: 10px; justify-content: flex-end;">
                                <button onclick="showDetails('<?php echo htmlspecialchars($k['kyc_name']); ?>', '<?php echo $k['kyc_id_front']; ?>', '<?php echo $k['kyc_id_back']; ?>', '<?php echo $k['kyc_id_handheld']; ?>')" style="background: none; border: none; color: var(--primary); cursor: pointer; font-size: 0.85rem; font-weight: bold;">查看照片</button>
                                <form method="POST" onsubmit="return confirm('确定要取消该用户的认证状态吗？')">
                                    <input type="hidden" name="user_id" value="<?php echo $k['id']; ?>">
                                    <input type="hidden" name="action" value="reject">
                                    <button type="submit" style="background: none; border: none; color: #f6465d; cursor: pointer; font-size: 0.85rem; font-weight: bold;">撤销认证</button>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
    function switchTab(tab) {
        const tabs = document.querySelectorAll('.tab');
        const pending = document.getElementById('pending-section');
        const approved = document.getElementById('approved-section');
        
        tabs.forEach(t => t.classList.remove('active'));
        if (tab === 'pending') {
            tabs[0].classList.add('active');
            pending.style.display = 'block';
            approved.style.display = 'none';
        } else {
            tabs[1].classList.add('active');
            pending.style.display = 'none';
            approved.style.display = 'block';
        }
    }
    
    function showDetails(name, front, back, handheld) {
        document.getElementById('modal-title').innerText = name + ' 的证件照片';
        document.getElementById('modal-body').innerHTML = `
            <div style="text-align:center;"><img src="../${front}" style="width:150px; height:100px; object-fit:cover; border-radius:8px; border:1px solid #eee;"><div style="font-size:12px; margin-top:5px;">正面</div></div>
            <div style="text-align:center;"><img src="../${back}" style="width:150px; height:100px; object-fit:cover; border-radius:8px; border:1px solid #eee;"><div style="font-size:12px; margin-top:5px;">反面</div></div>
            <div style="text-align:center;"><img src="../${handheld}" style="width:150px; height:100px; object-fit:cover; border-radius:8px; border:1px solid #eee;"><div style="font-size:12px; margin-top:5px;">手持</div></div>
        `;
        document.getElementById('detail-modal').style.display = 'flex';
    }
</script>
</body>
</html>