<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once __DIR__ . '/auth.php';

// Check if update pool request
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_pool') {
    $new_balance = floatval($_POST['pool_balance']);
    $pdo->prepare("UPDATE chicken_pool SET balance = ? WHERE id = 1")->execute([$new_balance]);
    header("Location: Chicken.php?msg=Pool+Updated");
    exit;
}

// Ensure pool exists
$pdo->exec("INSERT IGNORE INTO chicken_pool (id, balance) VALUES (1, 0.00)");

// Get pool balance
$pool_stmt = $pdo->query("SELECT balance FROM chicken_pool WHERE id=1 LIMIT 1");
$pool_balance = $pool_stmt ? (float)$pool_stmt->fetchColumn() : 0.00;

// Get history
$history_stmt = $pdo->query("
    SELECT b.id, b.user_id, b.round_token, b.amount, b.multiplier, b.winnings, b.is_won, b.is_settled, b.created_at, b.settled_at
    FROM chicken_bets b
    ORDER BY b.id DESC 
    LIMIT 100
");
$history = $history_stmt ? $history_stmt->fetchAll(PDO::FETCH_ASSOC) : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Chicken Control — PakClub Admin</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{background:#0b0e18;color:#e8eaf0;font-family:'Segoe UI',sans-serif;display:flex;min-height:100vh}
.main{flex:1;overflow-x:hidden}
.topbar{background:#111827;border-bottom:1px solid rgba(255,255,255,.07);padding:16px 28px;display:flex;align-items:center;justify-content:space-between}
.topbar h2{font-size:20px;font-weight:700;color:#fff}
.content{padding:24px 28px}
.card{background:#1e2130;border:1px solid rgba(255,255,255,.07);border-radius:14px;overflow:hidden;overflow-x:auto;margin-bottom:24px}
.card-header{padding:16px 20px;border-bottom:1px solid rgba(255,255,255,.07);background:#181b28;display:flex;justify-content:space-between;align-items:center}
.card-title{font-size:16px;font-weight:600;color:#fff}
.card-body{padding:20px}

.form-group{margin-bottom:15px}
.form-group label{display:block;margin-bottom:6px;font-size:13px;color:#9ca3af;font-weight:500}
.form-control{width:100%;background:#0f1117;border:1px solid rgba(255,255,255,.1);border-radius:8px;padding:10px 14px;color:#fff;font-size:14px;outline:none;font-family:inherit}
.form-control:focus{border-color:#f26d0f}

.btn{padding:10px 18px;border-radius:10px;border:none;cursor:pointer;font-size:14px;font-weight:600;display:inline-flex;align-items:center;gap:7px;transition:opacity .2s}
.btn:hover{opacity:.85}
.btn-orange{background:#f26d0f;color:#fff}

table{width:100%;border-collapse:collapse;font-size:13px;min-width:600px;}
th{padding:11px 16px;color:#4b5563;font-size:11px;text-transform:uppercase;letter-spacing:.7px;border-bottom:1px solid rgba(255,255,255,.06);text-align:left;font-weight:600}
td{padding:12px 16px;border-bottom:1px solid rgba(255,255,255,.04);color:#c9ccd6;vertical-align:middle}
tr:last-child td{border-bottom:none}
tr:hover td{background:rgba(255,255,255,.02)}

.badge{padding:4px 8px;border-radius:4px;font-size:11px;font-weight:700;text-transform:uppercase}
.bg-danger{background:rgba(239,68,68,.15);color:#ef4444}
.bg-warning{background:rgba(245,158,11,.15);color:#f59e0b}
.bg-success{background:rgba(34,197,94,.15);color:#22c55e}
.bg-primary{background:rgba(59,130,246,.15);color:#3b82f6}

.alert{padding:15px;background:rgba(34,197,94,.15);color:#22c55e;border-radius:10px;margin-bottom:20px;border:1px solid rgba(34,197,94,.3)}
</style>
</head>
<body>
<?php include 'sidebar.php'; ?>
<div class="main">
    <div class="topbar">
        <h2><i class="fa-solid fa-drumstick-bite" style="color:#f26d0f;margin-right:10px"></i>Chicken Game Control Panel</h2>
    </div>
    
    <div class="content">
        <?php if (isset($_GET['msg'])): ?>
            <div class="alert"><i class="fa-solid fa-check-circle"></i> <?php echo htmlspecialchars($_GET['msg']); ?></div>
        <?php endif; ?>

        <div class="card" style="max-width: 600px;">
            <div class="card-header">
                <div class="card-title">Dynamic Profit Pool</div>
            </div>
            <div class="card-body">
                <p style="margin-bottom:15px;font-size:15px">Current Chicken Pool Balance: <strong style="color:#22c55e"><?php echo number_format($pool_balance, 2); ?> PKR</strong></p>
                <form method="POST">
                    <input type="hidden" name="action" value="update_pool">
                    <div class="form-group">
                        <label>Set Pool Balance Manually</label>
                        <input type="number" step="0.01" class="form-control" name="pool_balance" value="<?php echo $pool_balance; ?>">
                    </div>
                    <button type="submit" class="btn btn-orange"><i class="fa-solid fa-floppy-disk"></i> Update Pool</button>
                </form>
                <hr style="border:none;border-top:1px solid rgba(255,255,255,.07);margin:20px 0">
                <div style="font-size:12px;color:#9ca3af">70% of placed bets are added to this pool. Players can only win up to what's available in this pool. In addition, there is a 15% chance to force a loss even if the pool can afford the win, guaranteeing long-term profit retention.</div>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <div class="card-title">Recent Bets</div>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>Bet ID</th>
                        <th>User ID</th>
                        <th>Amount</th>
                        <th>Status</th>
                        <th>Result</th>
                        <th>Winnings</th>
                        <th>Placed At</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($history as $bet): ?>
                    <tr>
                        <td>#<?php echo $bet['id']; ?></td>
                        <td><?php echo htmlspecialchars((string)$bet['user_id']); ?></td>
                        <td style="font-weight:600"><?php echo number_format((float)$bet['amount'], 2); ?> PKR</td>
                        <td><?php echo $bet['is_settled'] ? '<span class="badge bg-success">Settled</span>' : '<span class="badge bg-warning">Playing</span>'; ?></td>
                        <td>
                            <?php if($bet['is_settled']): ?>
                                <?php if($bet['is_won']): ?>
                                    <span class="badge bg-success">Won (<?php echo number_format((float)$bet['multiplier'], 2); ?>x)</span>
                                <?php else: ?>
                                    <span class="badge bg-danger">Lost</span>
                                <?php endif; ?>
                            <?php else: ?>
                                <span style="color:#6b7280">-</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if($bet['is_won']): ?>
                                <span style="color:#4ade80">+<?php echo number_format((float)$bet['winnings'], 2); ?> PKR</span>
                            <?php else: ?>
                                <span style="color:#9ca3af">0.00 PKR</span>
                            <?php endif; ?>
                        </td>
                        <td style="font-size:12px;color:#9ca3af"><?php echo $bet['created_at']; ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(empty($history)): ?>
                    <tr><td colspan="7" style="text-align:center;padding:30px;color:#9ca3af">No bets placed yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

    </div>
</div>
</body>
</html>