<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Rebate - 91CLUB</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        @font-face { font-family: 'Bahnschrift'; src: url('https://91appt.com/assets/woff2/bahnschrift-610d57ac.woff2') format('woff2'); font-display: swap; }
        :root { --bg-body: #F7F8FA; --primary: #F3595B; --primary-grad: linear-gradient(135deg, #FF7B7B 0%, #F3595B 100%); }
        * { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent;}
        .bahnschrift { font-family: 'Bahnschrift', sans-serif !important; }
        body { background-color: var(--bg-body); color: #333; max-width: 480px; margin: 0 auto; padding-top: 50px; min-height: 100vh; }

        /* Header */
        .header { position: fixed; top: 0; left: 50%; transform: translateX(-50%); width: 100%; max-width: 480px; height: 50px; background: #fff; display: flex; justify-content: center; align-items: center; z-index: 1000; box-shadow: 0 1px 5px rgba(0,0,0,0.02);}
        .back-btn { position: absolute; left: 15px; font-size: 20px; color: #666; cursor: pointer; text-decoration: none;}
        .title { font-size: 16px; font-weight: bold; }

        /* Category Tabs */
        .cat-tabs { display: flex; overflow-x: auto; background: #fff; padding: 15px; gap: 10px; box-shadow: 0 2px 5px rgba(0,0,0,0.02); margin-bottom: 15px;}
        .cat-tabs::-webkit-scrollbar { display: none; }
        .tab-item { min-width: 70px; display: flex; flex-direction: column; align-items: center; gap: 5px; padding: 10px 5px; border-radius: 8px; color: #999; font-size: 11px; font-weight: bold; cursor: pointer; transition: 0.3s;}
        .tab-item.active { background: linear-gradient(180deg, #ff8b7b, #f3595b); color: #fff; box-shadow: 0 4px 10px rgba(243, 89, 91, 0.3);}
        .tab-item i { font-size: 18px; }

        /* Main Rebate Card */
        .rebate-card { background: #fff; border-radius: 12px; margin: 0 15px 20px; padding: 20px 15px; box-shadow: 0 2px 10px rgba(0,0,0,0.03);}
        .r-title { font-size: 15px; font-weight: 800; color: #333; margin-bottom: 10px;}
        .rt-badge { display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--primary); color: var(--primary); font-size: 10px; font-weight: bold; padding: 2px 8px; border-radius: 12px; margin-bottom: 15px;}
        .rt-badge i { font-size: 10px; }
        
        .r-amount-wrap { display: flex; align-items: center; gap: 8px; margin-bottom: 15px;}
        .r-amount-wrap img { width: 24px; height: 24px; }
        .r-amount { font-size: 28px; font-weight: bold; color: var(--primary); }
        
        .vip-upgrade-box { background: #f7f8fa; border-radius: 8px; padding: 10px; font-size: 11px; color: #888; font-weight: bold; margin-bottom: 15px;}
        
        .r-stats { display: flex; justify-content: space-between; margin-bottom: 20px;}
        .r-stat-item { flex: 1; }
        .r-stat-lbl { font-size: 11px; color: #999; margin-bottom: 4px;}
        .r-stat-val { font-size: 16px; font-weight: bold; color: #333; }

        .r-notice { font-size: 10px; color: #aaa; margin-bottom: 15px; text-align: center;}

        /* One Click Button */
        .btn-claim { width: 100%; border: none; padding: 14px; border-radius: 30px; font-size: 14px; font-weight: bold; transition: 0.3s; cursor: pointer;}
        .btn-disabled { background: #e0e0e0; color: #999; box-shadow: none; cursor: not-allowed;}
        .btn-active { background: linear-gradient(90deg, #FF7B7B, #F3595B); color: white; box-shadow: 0 4px 15px rgba(243, 89, 91, 0.4);}
        .btn-active:active { transform: scale(0.97); }

        /* History Section */
        .history-sec { padding: 0 15px; }
        .h-title { font-size: 14px; font-weight: 800; color: #333; margin-bottom: 15px; border-left: 3px solid var(--primary); padding-left: 8px;}
        .btn-history { width: 100%; background: #fff; border: 1.5px solid var(--primary); color: var(--primary); padding: 12px; border-radius: 25px; font-size: 14px; font-weight: bold; cursor: pointer; text-align: center; text-decoration: none; display: block;}
        .btn-history:active { background: #fff5f5; }

        .toast { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: rgba(0,0,0,0.8); color: white; padding: 12px 24px; border-radius: 8px; font-size: 14px; z-index: 2000; display: none; text-align: center; font-weight: bold;}
    </style>
</head>
<body>

    <div class="header">
        <a href="activity.php" class="back-btn">❮</a>
        <div class="title">Rebate</div>
    </div>

    <div class="cat-tabs">
        <div class="tab-item active">
            <i class="fas fa-th-large"></i> All
        </div>
        <div class="tab-item">
            <i class="fas fa-gem"></i> Lottery
        </div>
        <div class="tab-item">
            <i class="fas fa-dice"></i> Casino
        </div>
        <div class="tab-item">
            <i class="fas fa-chess-knight"></i> Rummy
        </div>
        <div class="tab-item">
            <i class="fas fa-fish"></i> Fishing
        </div>
    </div>

    <div class="rebate-card">
        <div class="r-title">All-Total betting rebate</div>
        <div class="rt-badge"><i class="fas fa-check-circle"></i> Real-time count</div>
        
        <div class="r-amount-wrap">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 2L2 9L12 22L22 9L12 2Z" fill="#F3595B" opacity="0.2"/>
                <path d="M12 2L6 9L12 22L18 9L12 2Z" fill="#F3595B"/>
                <path d="M12 2L8.5 9L12 22L15.5 9L12 2Z" fill="#FF7B7B"/>
            </svg>
            <div class="r-amount bahnschrift" id="availableRebate">0.00</div>
        </div>

        <div class="vip-upgrade-box">Upgrade VIP level to increase rebate rate</div>

        <div class="r-stats">
            <div class="r-stat-item">
                <div class="r-stat-lbl">Today rebate</div>
                <div class="r-stat-val bahnschrift" id="todayRebate">0</div>
            </div>
            <div class="r-stat-item">
                <div class="r-stat-lbl">Total rebate</div>
                <div class="r-stat-val bahnschrift" id="totalRebate">0</div>
            </div>
        </div>

        <div class="r-notice">Automatic code washing at 01:00:00 every morning</div>

        <button id="claimBtn" class="btn-claim btn-disabled" disabled onclick="claimRebate()">One-Click Rebate</button>
    </div>

    <div class="history-sec">
        <div class="h-title">Rebate history</div>
        <a href="rebate_history.php" class="btn-history">All history</a>
    </div>

    <div id="toast" class="toast"></div>

    <script>
        function showToast(msg) {
            const toast = document.getElementById('toast');
            toast.innerText = msg;
            toast.style.display = 'block';
            setTimeout(() => { toast.style.display = 'none'; }, 2000);
        }

        // Fetch Rebate Data from Backend API
        function loadRebateData() {
            fetch('rebit.php')
                .then(res => res.json())
                .then(data => {
                    if (data.success) {
                        document.getElementById('availableRebate').innerText = data.available;
                        document.getElementById('todayRebate').innerText = data.today;
                        document.getElementById('totalRebate').innerText = data.total;

                        const btn = document.getElementById('claimBtn');
                        if (parseFloat(data.available) > 0) {
                            btn.classList.remove('btn-disabled');
                            btn.classList.add('btn-active');
                            btn.disabled = false;
                        } else {
                            btn.classList.add('btn-disabled');
                            btn.classList.remove('btn-active');
                            btn.disabled = true;
                        }
                    }
                }).catch(e => console.error(e));
        }

        // Handle Claim
        function claimRebate() {
            let formData = new FormData();
            formData.append('action', 'claim');

            fetch('rebit.php', { method: 'POST', body: formData })
            .then(res => res.json())
            .then(data => {
                showToast(data.msg);
                if (data.success) {
                    loadRebateData(); // Refresh UI dynamically
                }
            })
            .catch(err => showToast("Network Error!"));
        }

        // Initialize
        loadRebateData();
    </script>
</body>
</html>