<?php
require_once __DIR__ . '/layout.php';
$db = db();

if (!hasPermission('view_orders')) {
    echo "权限不足";
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] === 'set_control') {
        $id = (int)$_POST['order_id'];
        $status = (int)$_POST['control_status'];
        $db->prepare("UPDATE contract_orders SET control_status = ? WHERE id = ?")->execute([$status, $id]);
        header("Location: contract.php?msg=updated");
        exit;
    }
}

$title = '永续合约管理';
ob_start();
$user_id = isset($_GET['user_id']) ? (int)$_GET['user_id'] : null;
$sql = "SELECT o.*, u.username, u.uid FROM contract_orders o JOIN users u ON o.user_id = u.id";
$params = [];
if ($admin['is_agent']) {
    $sql .= " WHERE u.agent_id = ?";
    $params[] = $admin['id'];
}
if ($user_id) {
    $sql .= (strpos($sql, 'WHERE') === false ? " WHERE" : " AND") . " o.user_id = ?";
    $params[] = $user_id;
}
$sql .= " ORDER BY o.created_at DESC";
$stmt = $db->prepare($sql);
$stmt->execute($params);
$orders = $stmt->fetchAll();
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div class="d-flex align-items-center gap-3">
        <a href="<?= $user_id ? 'users.php' : 'index.php' ?>" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left"></i> 返回</a>
        <h4 class="mb-0">永续合约订单 <?= $user_id ? "(用户ID: $user_id)" : "" ?></h4>
    </div>
</div>
<div class="table-container">
    <table class="table table-hover">
        <thead>
            <tr class="small text-muted">
                <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><?= $o['id'] ?></td>
                <td><?= htmlspecialchars($o['username']) ?><br><code class="small"><?= $o['uid'] ?></code></td>
                <td>
                    <strong><?= $o['symbol'] ?></strong><br>
                    <span class="badge <?= $o['direction'] === 'long' ? 'bg-success' : 'bg-danger' ?>"><?= $o['direction'] === 'long' ? '做多' : '做空' ?></span>
                </td>
                <td><?= $o['leverage'] ?>x<br><?= number_format($o['amount'], 2) ?> USDT</td>
                <td><?= $o['entry_price'] ?><br><?= $o['close_price'] ?? '-' ?></td>
                <td class="<?= $o['profit'] >= 0 ? 'text-success' : 'text-danger' ?>"><?= number_format($o['profit'], 2) ?></td>
                <td><?= $o['status'] ?></td>
                <td>
                    <?php if ($o['control_status'] == 1): ?><span class="badge bg-success">控赢</span>
                    <?php elseif ($o['control_status'] == 2): ?><span class="badge bg-danger">控输</span>
                    <?php else: ?><span class="badge bg-secondary">正常</span><?php endif; ?>
                </td>
                <td>
                    <form method="POST" class="btn-group btn-group-sm">
                        <input type="hidden" name="order_id" value="<?= $o['id'] ?>">
                        <input type="hidden" name="action" value="set_control">
                        <button name="control_status" value="1" class="btn btn-outline-success">控赢</button>
                        <button name="control_status" value="2" class="btn btn-outline-danger">控输</button>
                        <button name="control_status" value="0" class="btn btn-outline-secondary">正常</button>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php
$content = ob_get_clean();
renderAdminPage($content, $title);
?>
