<?php
// Admin Engine: Manual Bankers & Combo Slips Management
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/../config/config.php';

if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header("Location: login.php");
    exit();
}

$success_msg = '';
$error_msg = '';

// 1. CREATE: Handle Manual Tip Insertion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_banker'])) {
    $match_title = trim($_POST['match_title'] ?? '');
    $match_date  = trim($_POST['match_date'] ?? '');
    $match_time  = trim($_POST['match_time'] ?? '');
    $team_a      = trim($_POST['team_a'] ?? '');
    $team_b      = trim($_POST['team_b'] ?? '');
    $game_option = trim($_POST['game_option'] ?? '');
    $access_tier = $_POST['access_tier'] ?? 'FREE';
    $tip_type    = $_POST['tip_type'] ?? 'BANKER';
    $confidence  = intval($_POST['confidence'] ?? 75);
    $confidence  = max(0, min(100, $confidence));

    if (!empty($match_title) && !empty($match_date) && !empty($match_time) && !empty($team_a) && !empty($team_b) && !empty($game_option)) {
        $stmt = $pdo->prepare("INSERT INTO admin_manual_tips (tip_type, match_title, match_date, match_time, team_a, team_b, game_option, outcome, access_tier, confidence) VALUES (?, ?, ?, ?, ?, ?, ?, 'PENDING', ?, ?)");
        if ($stmt->execute([$tip_type, $match_title, $match_date, $match_time, $team_a, $team_b, $game_option, $access_tier, $confidence])) {
            $success_msg = "Tip successfully scheduled and deployed to the database matrix.";
        } else {
            $error_msg = "Failed to schedule tip.";
        }
    } else {
        $error_msg = "All fields including Match Title are required.";
    }
}

// 2. UPDATE: Edit existing tip
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_tip'])) {
    $tip_id     = intval($_POST['tip_id'] ?? 0);
    $match_title = trim($_POST['match_title'] ?? '');
    $match_date  = trim($_POST['match_date'] ?? '');
    $match_time  = trim($_POST['match_time'] ?? '');
    $team_a      = trim($_POST['team_a'] ?? '');
    $team_b      = trim($_POST['team_b'] ?? '');
    $game_option = trim($_POST['game_option'] ?? '');
    $access_tier = $_POST['access_tier'] ?? 'FREE';
    $tip_type    = $_POST['tip_type'] ?? 'BANKER';
    $confidence  = intval($_POST['confidence'] ?? 75);
    $confidence  = max(0, min(100, $confidence));

    if ($tip_id > 0 && !empty($match_title) && !empty($match_date) && !empty($match_time) && !empty($team_a) && !empty($team_b) && !empty($game_option)) {
        $update = $pdo->prepare("UPDATE admin_manual_tips SET match_title=?, match_date=?, match_time=?, team_a=?, team_b=?, game_option=?, access_tier=?, tip_type=?, confidence=? WHERE id=?");
        if ($update->execute([$match_title, $match_date, $match_time, $team_a, $team_b, $game_option, $access_tier, $tip_type, $confidence, $tip_id])) {
            $success_msg = "Tip successfully updated.";
        } else {
            $error_msg = "Failed to update tip.";
        }
    } else {
        $error_msg = "All fields are required to edit a tip.";
    }
}

// 3. ACTION: Toggle Outcome / Status
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_outcome'])) {
    $tip_id     = intval($_POST['tip_id'] ?? 0);
    $new_status = $_POST['new_status'] ?? 'PENDING';
    
    if ($tip_id > 0 && in_array($new_status, ['PENDING', 'WON', 'LOST'])) {
        $update = $pdo->prepare("UPDATE admin_manual_tips SET outcome = ? WHERE id = ?");
        $update->execute([$new_status, $tip_id]);
        $success_msg = "Match outcome officially updated to " . $new_status . ".";
    }
}

// 4. DELETE: Remove Manual Tip
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_tip'])) {
    $tip_id = intval($_POST['delete_id'] ?? 0);
    if ($tip_id > 0) {
        $delete = $pdo->prepare("DELETE FROM admin_manual_tips WHERE id = ?");
        if ($delete->execute([$tip_id])) {
            $success_msg = "Manual tip permanently removed from schedule.";
        }
    }
}

// DATA FETCHING & DATE FILTERING
$active_tab = $_GET['tab'] ?? 'today';

$query = "SELECT * FROM admin_manual_tips";
if ($active_tab === 'today') {
    $query .= " WHERE match_date = CURDATE()";
} elseif ($active_tab === 'yesterday') {
    $query .= " WHERE match_date = DATE_SUB(CURDATE(), INTERVAL 1 DAY)";
} elseif ($active_tab === 'scheduled') {
    $query .= " WHERE match_date > CURDATE()";
} else {
    $query .= " WHERE 1=1"; 
}
$query .= " ORDER BY match_date ASC, match_time ASC LIMIT 100";

$stmt = $pdo->query($query);
$tips = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_tips = count($tips);

// Fetch single tip for editing (if edit modal is triggered)
$edit_tip = null;
if (isset($_GET['edit']) && ctype_digit($_GET['edit'])) {
    $edit_id = intval($_GET['edit']);
    $stmt = $pdo->prepare("SELECT * FROM admin_manual_tips WHERE id = ?");
    $stmt->execute([$edit_id]);
    $edit_tip = $stmt->fetch(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin - Bankers & Combos Control</title>
    <link rel="stylesheet" href="<?php echo SITE_URL; ?>/assets/css/output.css?v=<?php echo time(); ?>">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Inter', sans-serif; }
        /* Modal overlay */
        .modal-overlay {
            display: none;
            position: fixed;
            inset: 0;
            background: rgba(0,0,0,0.7);
            backdrop-filter: blur(4px);
            z-index: 1000;
            align-items: center;
            justify-content: center;
            padding: 1rem;
        }
        .modal-overlay.active {
            display: flex;
        }
        .modal-box {
            background: #1e293b;
            border-radius: 1.5rem;
            max-width: 600px;
            width: 100%;
            max-height: 90vh;
            overflow-y: auto;
            padding: 2rem;
            box-shadow: 0 20px 60px rgba(0,0,0,0.8);
            border: 1px solid #334155;
        }
        @media (max-width: 640px) {
            .modal-box { padding: 1.5rem; }
        }
        .modal-box .close-btn {
            float: right;
            background: none;
            border: none;
            color: #94a3b8;
            font-size: 1.5rem;
            cursor: pointer;
        }
        .modal-box .close-btn:hover { color: #fff; }
        .dark .modal-box { background: #0f172a; }
    </style>
    <script>
        if (localStorage.getItem('fpp_theme') === 'dark') {
            document.documentElement.classList.add('dark');
        } else {
            document.documentElement.classList.remove('dark');
        }
    </script>
</head>
<body class="bg-slate-50 text-slate-900 dark:bg-slate-950 dark:text-gray-200 antialiased min-h-screen flex flex-col md:flex-row transition-colors duration-300">

    <?php include __DIR__ . '/includes/admin-sidebar.php'; ?>

    <main class="flex-1 p-4 sm:p-6 md:p-10 overflow-x-hidden flex flex-col gap-6">
        
        <!-- Edit Modal -->
        <div class="modal-overlay <?php echo $edit_tip ? 'active' : ''; ?>" id="editModal">
            <div class="modal-box">
                <button class="close-btn" onclick="closeEditModal()">&times;</button>
                <h3 class="text-xl font-bold text-white mb-4">✏️ Edit Match</h3>
                <form action="bankers-control.php?tab=<?php echo $active_tab; ?>" method="POST" class="space-y-4">
                    <input type="hidden" name="tip_id" value="<?php echo $edit_tip['id'] ?? ''; ?>">
                    
                    <div>
                        <label class="block text-xs text-slate-400 mb-1 tracking-wider uppercase font-semibold">Match Title / League</label>
                        <input type="text" name="match_title" value="<?php echo htmlspecialchars($edit_tip['match_title'] ?? ''); ?>" required class="w-full bg-slate-800 border border-slate-700 rounded-lg p-3 text-white focus:outline-none focus:border-cyan-500 text-sm">
                    </div>
                    <div class="grid grid-cols-2 gap-3">
                        <div>
                            <label class="block text-xs text-slate-400 mb-1 tracking-wider uppercase font-semibold">Match Date</label>
                            <input type="date" name="match_date" value="<?php echo htmlspecialchars($edit_tip['match_date'] ?? ''); ?>" required class="w-full bg-slate-800 border border-slate-700 rounded-lg p-3 text-white focus:outline-none focus:border-cyan-500 text-sm">
                        </div>
                        <div>
                            <label class="block text-xs text-slate-400 mb-1 tracking-wider uppercase font-semibold">Kick-off Time</label>
                            <input type="time" name="match_time" value="<?php echo htmlspecialchars($edit_tip['match_time'] ?? ''); ?>" required class="w-full bg-slate-800 border border-slate-700 rounded-lg p-3 text-white focus:outline-none focus:border-cyan-500 text-sm">
                        </div>
                    </div>
                    <div class="grid grid-cols-2 gap-3">
                        <div>
                            <label class="block text-xs text-slate-400 mb-1 tracking-wider uppercase font-semibold">Home Team</label>
                            <input type="text" name="team_a" value="<?php echo htmlspecialchars($edit_tip['team_a'] ?? ''); ?>" required class="w-full bg-slate-800 border border-slate-700 rounded-lg p-3 text-white focus:outline-none focus:border-cyan-500 text-sm">
                        </div>
                        <div>
                            <label class="block text-xs text-slate-400 mb-1 tracking-wider uppercase font-semibold">Away Team</label>
                            <input type="text" name="team_b" value="<?php echo htmlspecialchars($edit_tip['team_b'] ?? ''); ?>" required class="w-full bg-slate-800 border border-slate-700 rounded-lg p-3 text-white focus:outline-none focus:border-cyan-500 text-sm">
                        </div>
                    </div>
                    <div>
                        <label class="block text-xs text-slate-400 mb-1 tracking-wider uppercase font-semibold">Betting Option</label>
                        <input type="text" name="game_option" value="<?php echo htmlspecialchars($edit_tip['game_option'] ?? ''); ?>" required class="w-full bg-slate-800 border border-slate-700 rounded-lg p-3 text-white focus:outline-none focus:border-cyan-500 text-sm">
                    </div>
                    <div>
                        <label class="block text-xs text-slate-400 mb-1 tracking-wider uppercase font-semibold">Confidence (0-100)</label>
                        <div class="flex items-center gap-3">
                            <input type="number" name="confidence" min="0" max="100" value="<?php echo htmlspecialchars($edit_tip['confidence'] ?? 75); ?>" class="w-full bg-slate-800 border border-slate-700 rounded-lg p-3 text-white focus:outline-none focus:border-cyan-500 text-sm">
                            <span class="text-xs text-slate-400 font-mono" id="editConfidenceDisplay"><?php echo htmlspecialchars($edit_tip['confidence'] ?? 75); ?>%</span>
                        </div>
                        <div class="mt-1 w-full bg-slate-700 rounded-full h-1.5">
                            <div id="editConfidenceBar" class="bg-cyan-500 h-1.5 rounded-full" style="width:<?php echo htmlspecialchars($edit_tip['confidence'] ?? 75); ?>%"></div>
                        </div>
                    </div>
                    <div class="grid grid-cols-2 gap-3">
                        <div>
                            <label class="block text-xs text-slate-400 mb-1 tracking-wider uppercase font-semibold">Category</label>
                            <select name="tip_type" class="w-full bg-slate-800 border border-slate-700 rounded-lg p-3 text-white focus:outline-none focus:border-cyan-500 text-sm">
                                <option value="BANKER" <?php echo ($edit_tip['tip_type'] ?? '') === 'BANKER' ? 'selected' : ''; ?>>Banker</option>
                                <option value="SLIP" <?php echo ($edit_tip['tip_type'] ?? '') === 'SLIP' ? 'selected' : ''; ?>>Slip of the Day</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-xs text-slate-400 mb-1 tracking-wider uppercase font-semibold">Access Tier</label>
                            <select name="access_tier" class="w-full bg-slate-800 border border-slate-700 rounded-lg p-3 text-white focus:outline-none focus:border-cyan-500 text-sm">
                                <option value="FREE" <?php echo ($edit_tip['access_tier'] ?? '') === 'FREE' ? 'selected' : ''; ?>>👤 FREE</option>
                                <option value="VIP" <?php echo ($edit_tip['access_tier'] ?? '') === 'VIP' ? 'selected' : ''; ?>>👑 VIP</option>
                            </select>
                        </div>
                    </div>
                    <div class="flex gap-3 pt-2">
                        <button type="submit" name="edit_tip" class="flex-1 bg-cyan-600 hover:bg-cyan-500 text-black font-bold py-3 rounded-xl transition shadow-lg shadow-cyan-900/30 text-sm tracking-wider uppercase">Save Changes</button>
                        <button type="button" onclick="closeEditModal()" class="flex-1 bg-slate-700 hover:bg-slate-600 text-white font-bold py-3 rounded-xl transition text-sm tracking-wider uppercase">Cancel</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
            <div>
                <h2 class="text-2xl sm:text-3xl font-extrabold text-slate-900 dark:text-white tracking-wide">Banker Tips Control</h2>
                <p class="text-xs text-slate-500 dark:text-gray-400 mt-1">Schedule 7-day drip-feed tips and manage ticket outcomes.</p>
            </div>
            
            <div class="flex flex-wrap bg-slate-200 dark:bg-slate-900 rounded-xl p-1 shadow-inner">
                <a href="?tab=yesterday" class="px-3 sm:px-4 py-2 text-xs font-bold rounded-lg transition <?php echo $active_tab === 'yesterday' ? 'bg-white dark:bg-slate-800 text-cyan-600 dark:text-cyan-400 shadow' : 'text-slate-500 hover:text-slate-700 dark:hover:text-white'; ?>">Yesterday</a>
                <a href="?tab=today" class="px-3 sm:px-4 py-2 text-xs font-bold rounded-lg transition <?php echo $active_tab === 'today' ? 'bg-white dark:bg-slate-800 text-cyan-600 dark:text-cyan-400 shadow' : 'text-slate-500 hover:text-slate-700 dark:hover:text-white'; ?>">Today</a>
                <a href="?tab=scheduled" class="px-3 sm:px-4 py-2 text-xs font-bold rounded-lg transition <?php echo $active_tab === 'scheduled' ? 'bg-white dark:bg-slate-800 text-emerald-600 dark:text-emerald-400 shadow' : 'text-slate-500 hover:text-slate-700 dark:hover:text-white'; ?>">Scheduled</a>
                <a href="?tab=history" class="px-3 sm:px-4 py-2 text-xs font-bold rounded-lg transition <?php echo $active_tab === 'history' ? 'bg-white dark:bg-slate-800 text-cyan-600 dark:text-cyan-400 shadow' : 'text-slate-500 hover:text-slate-700 dark:hover:text-white'; ?>">History</a>
            </div>
        </div>

        <?php if(!empty($success_msg)): ?>
            <div class="bg-emerald-50 border-emerald-200 text-emerald-600 dark:bg-emerald-500/10 dark:border-emerald-500/30 dark:text-emerald-500 border p-4 rounded-xl text-sm font-semibold shadow-sm"><?php echo $success_msg; ?></div>
        <?php endif; ?>
        <?php if(!empty($error_msg)): ?>
            <div class="bg-rose-50 border-rose-200 text-rose-600 dark:bg-rose-500/10 dark:border-rose-500/30 dark:text-rose-500 border p-4 rounded-xl text-sm font-semibold shadow-sm"><?php echo $error_msg; ?></div>
        <?php endif; ?>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            
            <!-- Form Card (1/3) -->
            <div class="bg-white border border-slate-200 dark:bg-slate-900 dark:border-slate-800 rounded-2xl p-4 sm:p-6 h-fit shadow-sm transition-colors duration-300">
                <h3 class="font-bold text-lg text-slate-900 dark:text-white border-b border-slate-200 dark:border-slate-800 pb-3 mb-6">📅 Schedule New Match</h3>

                <form action="bankers-control.php?tab=<?php echo $active_tab; ?>" method="POST" class="space-y-4">
                    
                    <div>
                        <label class="block text-xs text-slate-500 dark:text-gray-400 mb-1 tracking-wider uppercase font-semibold">Match Title / League</label>
                        <input type="text" name="match_title" placeholder="e.g. English Premier League - Matchday 4" required class="w-full bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-800 rounded-lg p-3 text-slate-900 dark:text-white focus:outline-none focus:border-cyan-500 text-sm transition-colors">
                    </div>

                    <div class="grid grid-cols-2 gap-2">
                        <div>
                            <label class="block text-xs text-slate-500 dark:text-gray-400 mb-1 tracking-wider uppercase font-semibold">Match Date</label>
                            <input type="date" name="match_date" required class="w-full bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-800 rounded-lg p-3 text-slate-900 dark:text-white focus:outline-none focus:border-cyan-500 text-sm transition-colors">
                        </div>
                        <div>
                            <label class="block text-xs text-slate-500 dark:text-gray-400 mb-1 tracking-wider uppercase font-semibold">Kick-off Time</label>
                            <input type="time" name="match_time" required class="w-full bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-800 rounded-lg p-3 text-slate-900 dark:text-white focus:outline-none focus:border-cyan-500 text-sm transition-colors">
                        </div>
                    </div>

                    <div class="grid grid-cols-2 gap-2">
                        <div>
                            <label class="block text-xs text-slate-500 dark:text-gray-400 mb-1 tracking-wider uppercase font-semibold">Home Team</label>
                            <input type="text" name="team_a" placeholder="Arsenal" required class="w-full bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-800 rounded-lg p-3 text-slate-900 dark:text-white focus:outline-none focus:border-cyan-500 text-sm transition-colors">
                        </div>
                        <div>
                            <label class="block text-xs text-slate-500 dark:text-gray-400 mb-1 tracking-wider uppercase font-semibold">Away Team</label>
                            <input type="text" name="team_b" placeholder="Chelsea" required class="w-full bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-800 rounded-lg p-3 text-slate-900 dark:text-white focus:outline-none focus:border-cyan-500 text-sm transition-colors">
                        </div>
                    </div>

                    <div>
                        <label class="block text-xs text-slate-500 dark:text-gray-400 mb-1 tracking-wider uppercase font-semibold">Betting Option</label>
                        <input type="text" name="game_option" placeholder="Over 2.5 Goals or Home Win" required class="w-full bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-800 rounded-lg p-3 text-slate-900 dark:text-white focus:outline-none focus:border-cyan-500 text-sm transition-colors">
                    </div>

                    <!-- Confidence Input -->
                    <div>
                        <label class="block text-xs text-slate-500 dark:text-gray-400 mb-1 tracking-wider uppercase font-semibold">AI Confidence Level (0-100)</label>
                        <div class="flex items-center gap-3">
                            <input type="number" name="confidence" min="0" max="100" value="75" 
                                   class="w-full bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-800 rounded-lg p-3 text-slate-900 dark:text-white focus:outline-none focus:border-cyan-500 text-sm transition-colors" id="newConfidence">
                            <span class="text-xs text-slate-400 dark:text-slate-500 font-mono" id="newConfidenceDisplay">75%</span>
                        </div>
                        <div class="mt-1 w-full bg-slate-200 dark:bg-slate-700 rounded-full h-1.5">
                            <div id="newConfidenceBar" class="bg-cyan-500 h-1.5 rounded-full" style="width:75%"></div>
                        </div>
                    </div>

                    <div class="grid grid-cols-2 gap-2">
                        <div>
                            <label class="block text-xs text-slate-500 dark:text-gray-400 mb-1 tracking-wider uppercase font-semibold">Category</label>
                            <select name="tip_type" class="w-full bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-800 rounded-lg p-3 text-slate-900 dark:text-white focus:outline-none focus:border-cyan-500 text-sm transition-colors">
                                <option value="BANKER">Banker</option>
                                <option value="SLIP">Slip of the Day</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-xs text-slate-500 dark:text-gray-400 mb-1 tracking-wider uppercase font-semibold">Access Tier</label>
                            <select name="access_tier" class="w-full bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-800 rounded-lg p-3 text-slate-900 dark:text-white focus:outline-none focus:border-cyan-500 text-sm transition-colors">
                                <option value="FREE">👤 FREE</option>
                                <option value="VIP">👑 VIP</option>
                            </select>
                        </div>
                    </div>

                    <button type="submit" name="add_banker" class="w-full bg-cyan-600 hover:bg-cyan-500 text-black font-bold py-3.5 rounded-xl transition mt-4 shadow-lg shadow-cyan-900/30 text-sm tracking-wider uppercase">Add to Schedule</button>
                </form>
            </div>

            <!-- Table Card (2/3) -->
            <div class="lg:col-span-2 bg-white border border-slate-200 dark:bg-slate-900 dark:border-slate-800 rounded-2xl p-4 sm:p-6 h-fit shadow-sm transition-colors duration-300">
                <h3 class="font-bold text-lg mb-6 text-slate-900 dark:text-white border-b border-slate-200 dark:border-slate-800 pb-3 capitalize">
                    <?php echo htmlspecialchars($active_tab); ?> Queue (<?php echo $total_tips; ?>)
                </h3>
                
                <div class="overflow-x-auto -mx-4 sm:-mx-6 px-4 sm:px-6">
                    <table class="w-full text-left text-sm border-collapse min-w-[800px]">
                        <thead>
                            <tr class="text-slate-500 dark:text-gray-500 border-b border-slate-200 dark:border-slate-800 text-xs tracking-wider">
                                <th class="py-3 px-2 font-semibold">Match Info</th>
                                <th class="py-3 px-2 font-semibold">Pick / Tier</th>
                                <th class="py-3 px-2 font-semibold text-center">Confidence</th>
                                <th class="py-3 px-2 font-semibold text-center">Set Outcome</th>
                                <th class="py-3 px-2 font-semibold text-center" colspan="2">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-100 dark:divide-slate-800/60 text-slate-700 dark:text-gray-300 text-xs">
                            <?php foreach($tips as $tip): 
                                $tier_label = ($tip['access_tier'] ?? 'FREE') === 'VIP' ? '👑 VIP' : '👤 FREE';
                                $type_label = ($tip['tip_type'] ?? 'BANKER') === 'SLIP' ? 'Slip' : 'Banker';
                                $conf = intval($tip['confidence'] ?? 75);
                                $conf = max(0, min(100, $conf));
                                $conf_color = $conf >= 80 ? 'text-emerald-500' : ($conf >= 50 ? 'text-amber-500' : 'text-rose-500');
                            ?>
                            <tr class="hover:bg-slate-50 dark:hover:bg-slate-800/50 transition">
                                <td class="py-3 px-2">
                                    <div class="text-[9px] text-amber-500 dark:text-amber-400 font-bold tracking-widest uppercase mb-1">
                                        <?php echo htmlspecialchars($tip['match_title'] ?? 'N/A'); ?>
                                    </div>
                                    <div class="text-[9px] text-cyan-600 dark:text-cyan-400 font-mono mb-0.5 font-bold">
                                        <?php echo date("d M Y", strtotime($tip['match_date'])); ?> @ <?php echo date("H:i", strtotime($tip['match_time'])); ?>
                                    </div>
                                    <div class="font-bold text-slate-900 dark:text-white">
                                        <?php echo htmlspecialchars($tip['team_a']); ?> vs <?php echo htmlspecialchars($tip['team_b']); ?>
                                    </div>
                                </td>
                                
                                <td class="py-3 px-2">
                                    <div class="font-extrabold text-slate-900 dark:text-white text-sm mb-1">
                                        <?php echo htmlspecialchars($tip['game_option']); ?>
                                    </div>
                                    <span class="bg-slate-100 text-slate-600 dark:bg-slate-800 dark:text-slate-400 text-[9px] px-2 py-0.5 rounded font-bold tracking-wider uppercase border border-slate-200 dark:border-slate-700">
                                        <?php echo $type_label; ?> | <?php echo $tier_label; ?>
                                    </span>
                                </td>

                                <td class="py-3 px-2 text-center">
                                    <div class="flex flex-col items-center">
                                        <span class="text-xs font-black <?php echo $conf_color; ?>"><?php echo $conf; ?>%</span>
                                        <div class="w-full max-w-[60px] h-1 bg-slate-200 dark:bg-slate-700 rounded-full overflow-hidden">
                                            <div class="h-full rounded-full bg-<?php echo $conf >= 80 ? 'emerald-500' : ($conf >= 50 ? 'amber-500' : 'rose-500'); ?>" style="width:<?php echo $conf; ?>%"></div>
                                        </div>
                                    </div>
                                </td>
                                
                                <td class="py-3 px-2 text-center">
                                    <form action="bankers-control.php?tab=<?php echo $active_tab; ?>" method="POST" class="flex items-center justify-center space-x-1">
                                        <input type="hidden" name="tip_id" value="<?php echo $tip['id']; ?>">
                                        <select name="new_status" class="bg-slate-50 border border-slate-200 dark:bg-slate-950 dark:border-slate-700 rounded p-1 text-[10px] font-bold uppercase text-slate-700 dark:text-gray-300 focus:outline-none">
                                            <option value="PENDING" <?php if($tip['outcome'] == 'PENDING') echo 'selected'; ?>>Pending</option>
                                            <option value="WON" <?php if($tip['outcome'] == 'WON') echo 'selected'; ?>>✅ Won</option>
                                            <option value="LOST" <?php if($tip['outcome'] == 'LOST') echo 'selected'; ?>>❌ Lost</option>
                                        </select>
                                        <button type="submit" name="update_outcome" class="bg-blue-600 hover:bg-blue-500 text-white text-[9px] px-2 py-1.5 rounded font-bold uppercase tracking-wider transition">Save</button>
                                    </form>
                                </td>

                                <td class="py-3 px-2 text-center">
                                    <a href="?tab=<?php echo $active_tab; ?>&edit=<?php echo $tip['id']; ?>" class="bg-amber-50 text-amber-600 border border-amber-200 hover:bg-amber-100 dark:bg-amber-500/10 dark:text-amber-400 dark:border-amber-500/30 px-2 py-1.5 rounded text-[9px] font-bold uppercase tracking-wider transition inline-block">✏️ Edit</a>
                                </td>
                                <td class="py-3 px-2 text-right">
                                    <form action="bankers-control.php?tab=<?php echo $active_tab; ?>" method="POST">
                                        <input type="hidden" name="delete_id" value="<?php echo $tip['id']; ?>">
                                        <button type="submit" name="delete_tip" onclick="return confirm('Purge this match record completely?');" class="bg-rose-50 text-rose-600 border border-rose-200 hover:bg-rose-100 dark:bg-rose-500/10 dark:text-rose-400 dark:border-rose-500/30 w-7 h-7 flex items-center justify-center rounded transition">🗑️</button>
                                    </form>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            <?php if(empty($tips)): ?>
                                <tr><td colspan="6" class="py-8 text-center text-slate-400 font-bold uppercase tracking-widest text-xs">Queue is empty.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>

    <script>
        // Close modal
        function closeEditModal() {
            const modal = document.getElementById('editModal');
            modal.classList.remove('active');
            // Remove edit parameter from URL without reload
            if (window.history.pushState) {
                const url = new URL(window.location.href);
                url.searchParams.delete('edit');
                window.history.pushState({}, '', url);
            }
        }

        // Confidence slider sync for new tip form
        const newConfInput = document.getElementById('newConfidence');
        if (newConfInput) {
            newConfInput.addEventListener('input', function() {
                let val = parseInt(this.value) || 0;
                val = Math.min(100, Math.max(0, val));
                document.getElementById('newConfidenceDisplay').textContent = val + '%';
                const bar = document.getElementById('newConfidenceBar');
                bar.style.width = val + '%';
                if (val >= 80) bar.style.background = '#10b981';
                else if (val >= 50) bar.style.background = '#f59e0b';
                else bar.style.background = '#ef4444';
            });
        }

        // Confidence sync for edit modal (if open)
        const editConfInput = document.querySelector('input[name="confidence"]');
        if (editConfInput) {
            editConfInput.addEventListener('input', function() {
                let val = parseInt(this.value) || 0;
                val = Math.min(100, Math.max(0, val));
                const display = document.getElementById('editConfidenceDisplay');
                const bar = document.getElementById('editConfidenceBar');
                if (display) display.textContent = val + '%';
                if (bar) {
                    bar.style.width = val + '%';
                    if (val >= 80) bar.style.background = '#10b981';
                    else if (val >= 50) bar.style.background = '#f59e0b';
                    else bar.style.background = '#ef4444';
                }
            });
        }

        // Close modal on background click
        document.getElementById('editModal').addEventListener('click', function(e) {
            if (e.target === this) closeEditModal();
        });
    </script>
</body>
</html>