<?php 
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

include 'template/header.php';

// ==========================================================
// PERBAIKAN UTAMA: Ambil username dari session login member
// ==========================================================
$user_id        = $_SESSION['id'] ?? '';
$username_login = $_SESSION['username'] ?? ''; // <--- Ini yang krusial dan sebelumnya hilang!

// Menangkap filter dari form pencarian
$type         = $_GET['type'] ?? '';
$providers_id = $_GET['providers_id'] ?? '';
$from_date    = $_GET['from_date'] ?? date('Y-m-d');
$end_date     = $_GET['end_date'] ?? date('Y-m-d');
$from_hour    = $_GET['from_hour'] ?? '00:00';
$end_hour     = $_GET['end_hour'] ?? '23:59';

$datetime_start = $from_date . ' ' . $from_hour . ':00';
$datetime_end   = $end_date . ' ' . $end_hour . ':59';

$promo_name     = "Tidak Ada Promo Aktif";
$target_turnover = 0;

// BACA DATA DARI tb_deposit: Ambil promo deposit terakhir yang aktif/sukses
if(!empty($username_login) && isset($koneksi)) {
    // DISESUAIKAN: status 'Sukses' (Sesuai dengan file konfirmasi admin Anda)
    $query_depo = mysqli_query($koneksi, "SELECT * FROM `tb_deposit` 
                   WHERE `username` = '" . mysqli_real_escape_string($koneksi, $username_login) . "' 
                   AND `status` = 'Sukses' 
                   AND `bonus` > 0 
                   ORDER BY `id` DESC LIMIT 1");

    if (mysqli_num_rows($query_depo) > 0) {
        $data_depo = mysqli_fetch_assoc($query_depo);
        $promo_name = "BONUS NEW MEMBER 50+50 (TO X 15)";
        
        // ==========================================================
        // PERBAIKAN PERKALIAN TO: Diubah menjadi 15 sesuai target promo
        // ==========================================================
        $perkalianto = 15; 
        $target_turnover = ($data_depo['jumlah'] + $data_depo['bonus']) * $perkalianto;
        
        // OTOMATISASI KUNCI WAKTU: Tanggal & jam pencarian dikunci sejak deposit sukses masuk
        $datetime_start = $data_depo['created_at']; 
        $from_date      = date('Y-m-d', strtotime($datetime_start));
        $from_hour      = date('H:i', strtotime($datetime_start));
    }
}

// BACA DATA DARI tb_play: Hitung total transaksi bermain member
$total_turnover = 0;
$total_win      = 0;

if(!empty($username_login) && isset($koneksi)) {
    $sql_bet = "SELECT 
                    SUM(CAST(JSON_EXTRACT(tc, '$.bet') AS DECIMAL(15,2))) as total_to, 
                    SUM(CAST(JSON_EXTRACT(tc, '$.win') AS DECIMAL(15,2))) as total_win 
                FROM `tb_play` 
                WHERE `user` = '" . mysqli_real_escape_string($koneksi, $username_login) . "' 
                AND `gamestart` BETWEEN '$datetime_start' AND '$datetime_end'";

    if(!empty($type)) { $sql_bet .= " AND `type` = '$type'"; }
    if(!empty($providers_id)) { $sql_bet .= " AND `provider` = '$providers_id'"; }

    $query_bet = mysqli_query($koneksi, $sql_bet);
    if ($query_bet) {
        $res_bet        = mysqli_fetch_assoc($query_bet);
        $total_turnover = $res_bet['total_to'] ?? 0;
        $total_win      = $res_bet['total_win'] ?? 0;
    }
}

// Hitung sisa target turnover
$sisa_to = $target_turnover - $total_turnover;
if ($sisa_to < 0) { $sisa_to = 0; }

$p_query = isset($_GET['p']) ? $_GET['p'] : null;
?>


<div style="margin-top: 50px;">
    <div style="padding: 20px 0; background-color:var(--secondary-color);">
        <div
            style="width: 90%; margin: 0 auto 10px auto;  color: transparent !important; -webkit-background-clip: text; background-image: radial-gradient(ellipse farthest-corner at right bottom, #FEDB37 0%, #FDB931 8%, #9f7928 30%, #8A6E2F 40%, transparent 80%), radial-gradient(ellipse farthest-corner at left top, #e99c05 0%, #f4ae00 8%, #f8bf33 25%, #f8ce19 62.5%, #ffdc02 100%); font-family: Teko-SemiBold; text-align: center; font-size: 24px;">
            PENGECEKAN TURN OVER
        </div>

        <div style="width: 90%; margin: 0 auto 15px auto; padding: 15px; background-color: #212121; border: 1px solid #444; border-radius: 6px; color: #fff; font-size: 13px; line-height: 1.6;">
            <div style="font-weight: bold; color: #FEDB37; border-bottom: 1px solid #444; padding-bottom: 5px; margin-bottom: 8px; text-transform: uppercase;">
                Status Bonus Deposit Anda
            </div>
            <table width="100%" style="font-size: 12px; color: #e0e0e0;">
                <tr>
                    <td width="40%">Promo Teraktif</td>
                    <td width="5%">:</td>
                    <td style="font-weight: bold; color: #fff;"><?php echo htmlspecialchars($promo_name); ?></td>
                </tr>
                <tr>
                    <td>Target Turnover</td>
                    <td>:</td>
                    <td style="color: #64b5f6;">Rp <?php echo number_format($target_turnover, 0, ',', '.'); ?></td>
                </tr>
                <tr>
                    <td>Turnover Tercapai</td>
                    <td>:</td>
                    <td style="color: #ffb74d;">Rp <?php echo number_format($total_turnover, 0, ',', '.'); ?></td>
                </tr>
                <tr>
                    <td>Status Kelayakan WD</td>
                    <td>:</td>
                    <td>
                        <?php if($target_turnover > 0): ?>
                            <?php if($sisa_to > 0): ?>
                                <span style="background-color: #d32f2f; color: #fff; padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: bold;">
                                    Terkunci (Sisa TO: Rp <?php echo number_format($sisa_to, 0, ',', '.'); ?>)
                                </span>
                            <?php else: ?>
                                <span style="background-color: #388e3c; color: #fff; padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: bold;">
                                    ✓ TO Terpenuhi (Bisa WD)
                                </span>
                            <?php endif; ?>
                        <?php else: ?>
                            <span style="color: #aaa; font-style: italic;">Tidak terikat target promo</span>
                        <?php endif; ?>
                    </td>
                </tr>
            </table>
        </div>

        <form method="get" action="turn-over.php" class="filter_submit"
            style="width: 90%; margin: 0 auto 20px auto; padding: 10px; background-color: var(--secondary-color); border-radius: 6px;">
            <input type="hidden" name="view" value="mobile">
            
            <div style="display: flex;">
                <div style="width: 50%; margin-right: 8px;">
                    <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">Tipe</div>
                    <div class="select_box" style="margin-bottom: 12px; position: relative;">
                        <select name="type" class="form-control">
                            <option value="" <?php echo $type == '' ? 'selected' : ''; ?>>Semua</option>
                            <option value="SLOT" <?php echo $type == 'SLOT' ? 'selected' : ''; ?>>Slots</option>
                            <option value="LIVE_CASINO" <?php echo $type == 'LIVE_CASINO' ? 'selected' : ''; ?>>Live Casino</option>
                            <option value="FISHING" <?php echo $type == 'FISHING' ? 'selected' : ''; ?>>Tembak Ikan</option>
                            <option value="SPORT_BOOK" <?php echo $type == 'SPORT_BOOK' ? 'selected' : ''; ?>>Sports</option>
                            <option value="OTHER" <?php echo $type == 'OTHER' ? 'selected' : ''; ?>>Virtual</option>
                            <option value="COCK_FIGHTING" <?php echo $type == 'COCK_FIGHTING' ? 'selected' : ''; ?>>Sabung Ayam</option>
                            <option value="LOTTERY" <?php echo $type == 'LOTTERY' ? 'selected' : ''; ?>>Togel</option>
                        </select>
                    </div>
                </div>

                <div style="width: 50%;">
                    <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">Provider</div>
                    <div class="select_box" style="margin-bottom: 12px; position: relative;">
                        <select name="providers_id" class="form-control">
                            <option value="" <?php echo $providers_id == '' ? 'selected' : ''; ?>>Semua</option>
                            <option value="1006" <?php echo $providers_id == '1006' ? 'selected' : ''; ?>>Pragmatic</option>
                            <option value="1007" <?php echo $providers_id == '1007' ? 'selected' : ''; ?>>Pg Soft</option>
                            <option value="3" <?php echo $providers_id == '3' ? 'selected' : ''; ?>>Joker</option>
                            <option value="4" <?php echo $providers_id == '4' ? 'selected' : ''; ?>>Habanero</option>
                            <option value="5" <?php echo $providers_id == '5' ? 'selected' : ''; ?>>Spade Gaming</option>
                        </select>
                    </div>
                </div>
            </div>

            <div style="display: flex;">
                <div style="width: 50%; margin-right: 8px;">
                    <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">Dari Tanggal</div>
                    <div style="margin-bottom: 12px;">
                        <input type="date" name="from_date" class="from_date form-control" value="<?php echo htmlspecialchars($from_date); ?>">
                    </div>
                </div>
                <div style="width: 50%;">
                    <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">Sampai Tanggal</div>
                    <div style="margin-bottom: 12px;">
                        <input type="date" name="end_date" class="end_date form-control" value="<?php echo htmlspecialchars($end_date); ?>">
                    </div>
                </div>
            </div>

            <div style="display: flex;">
                <div style="width: 50%; margin-right: 8px;">
                    <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">Dari Jam</div>
                    <div style="margin-bottom: 12px;">
                        <input type="time" name="from_hour" class="from_hour form-control" value="<?php echo htmlspecialchars($from_hour); ?>" style="line-height: normal;">
                    </div>
                </div>
                <div style="width: 50%;">
                    <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">Sampai Jam</div>
                    <div style="margin-bottom: 12px;">
                        <input type="time" name="end_hour" class="end_hour form-control" value="<?php echo htmlspecialchars($end_hour); ?>" style="line-height: normal;">
                    </div>
                </div>
            </div>

            <div style="width: 100px; margin: auto;">
                <button type="submit" class="glowing2 btn btn-primary"
                    style="background-image: radial-gradient(ellipse farthest-corner at right bottom, #FEDB37 0%, #FDB931 8%, #9f7928 30%, #8A6E2F 40%, transparent 80%), radial-gradient(ellipse farthest-corner at left top, #e99c05 0%, #f4ae00 8%, #f8bf33 25%, #f8ce19 62.5%, #ffdc02 100%); color: #fff; border: 0; width: 100%; font-weight: bold; border-radius: 25px; padding: 10px 0;">
                    Cari
                </button>
            </div>
        </form>

        <div style="width: 90%; margin: 0 auto 20px auto; display: block; overflow: auto;">
            <table width="100%" style="font-size: 11px;">
                <thead>
                    <tr style="background-color: #494848; color: #fff; font-weight: bold; text-align: center;">
                        <td style="padding: 12px 5px;">Periode Pencarian</td>
                        <td style="padding: 12px 5px;">Total Turn Over</td>
                        <td style="padding: 12px 5px;">Total Win</td>
                    </tr>
                </thead>
                <tbody>
                    <?php if($total_turnover > 0): ?>
                    <tr style="background-color: var(--secondary-color); color: #fff; text-align: center;">
                        <td style="padding: 12px 5px;"><?php echo htmlspecialchars($from_date . ' ' . $from_hour) . ' s/d ' . htmlspecialchars($end_date . ' ' . $end_hour); ?></td>
                        <td style="padding: 12px 5px; color: #FEDB37; font-weight: bold;">Rp <?php echo number_format($total_turnover, 0, ',', '.'); ?></td>
                        <td style="padding: 12px 5px; color: #4fc3f7;">Rp <?php echo number_format($total_win, 0, ',', '.'); ?></td>
                    </tr>
                    <?php else: ?>
                    <tr style="background-color: var(--secondary-color); color: #fff;">
                        <td colspan="3" style="padding: 24px; text-align: center; font-size: 11px; color: #C4C4C4;">
                            Tidak ada data turnover ditemukan pada periode ini.
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'template/footer.php'; ?>