<?php
session_start();
include 'template/header.php'; 
require_once 'db/koneksi.php';

$username = isset($_SESSION['username']) ? $_SESSION['username'] : '';
$tgl = isset($_GET['tgl']) ? mysqli_real_escape_string($koneksi, $_GET['tgl']) : '';
$psn = isset($_GET['psn']) ? mysqli_real_escape_string($koneksi, $_GET['psn']) : '';

// Ambil semua angka yang dibeli
$query = mysqli_query($koneksi, "SELECT * FROM tb_taruhan_togel WHERE username = '$username' AND tanggal = '$tgl' AND pasaran = '$psn'");

// Query khusus untuk cek kemenangan
$query_menang = mysqli_query($koneksi, "SELECT * FROM tb_taruhan_togel WHERE username = '$username' AND tanggal = '$tgl' AND pasaran = '$psn' AND status = 'Menang'");
?>
<div style="margin-top: 50px; color: #fff; padding: 10px; font-family: sans-serif; position: relative; top: 20px;">
    <div style="width: 100%; margin: 0 auto;">

        <?php if (mysqli_num_rows($query_menang) > 0): ?>
        <div style="background: #1a1a1a; border: 1px solid #2ecc71; border-radius: 8px; margin-bottom: 20px; overflow: hidden; position: relative; top: 15px;">
            <div style="background: #2ecc71; color: #000; padding: 5px 10px; font-weight: bold; font-size: 12px; text-align: center;">DAFTAR KEMENANGAN</div>
            <div style="overflow-x: auto; width: 100%;">
            <table width="100%" style="text-align: center; border-collapse: collapse; font-size: 11px;">
                <tr style="color: #fff; background: #262626;">
                    <th style="padding: 8px;">Tanggal</th>
                    <th style="padding: 8px;">Nomor</th>
                    <th style="padding: 8px;">Pasaran</th>
                    <th style="padding: 8px;">Mode</th>
                    <th style="padding: 8px;">Bet</th>
                    <th style="padding: 8px;">Hadiah</th>
                </tr>
                <?php 
                mysqli_data_seek($query_menang, 0); 
                while($m = mysqli_fetch_assoc($query_menang)): 
                    $nominal_menang = (int)$m['nominal'];
                    $angka = trim($m['nomor']);
                    
                    // Definisikan variabel yang dibutuhkan untuk perhitungan
                    $mode_menang = strtoupper($m['mode_taruhan']);
                    $diskon_menang = trim($m['diskon']);
                    $game_menang = strtoupper($m['game']);
                    
                    // Logika Deteksi Digit
                    if (strpos($game_menang, '4D') !== false || $m['game'] == '4D') { $digit_m = 4; }
                    elseif (strpos($game_menang, '3D') !== false) { $digit_m = 3; }
                    else { $digit_m = 2; }

                    // Logika Format Tanda X (Nomor Tampil)
                    $nomor_tampil = $angka;
                    if (strpos($game_menang, '4D') !== false) {
                        $nomor_tampil = $angka; 
                    } elseif (strpos($game_menang, '3D') !== false) {
                        $nomor_tampil = (strpos($game_menang, 'DEPAN') !== false) ? $angka . 'x' : 'x' . $angka;
                    } elseif (strpos($game_menang, '2D') !== false) {
                        if (strpos($game_menang, 'DEPAN') !== false) { $nomor_tampil = $angka . 'xx'; }
                        elseif (strpos($game_menang, 'TENGAH') !== false) { $nomor_tampil = 'x' . $angka . 'x'; }
                        else { $nomor_tampil = 'xx' . $angka; }
                    }
                    
                    // Logika Hitung Hadiah
                    $h_per_1000 = 0;
                    $diskon_bersih = str_replace(['%', ' '], '', $diskon_menang);

                    if (strpos($mode_menang, 'FULL') !== false || $diskon_bersih == '0' || $diskon_bersih == '') {
                        if ($digit_m == 4) { $h_per_1000 = 10000000; } 
                        elseif ($digit_m == 3) { $h_per_1000 = 1000000; } 
                        elseif ($digit_m == 2) { $h_per_1000 = 100000; }
                    } else {
                        if ($digit_m == 4) { $h_per_1000 = 3400000; } 
                        elseif ($digit_m == 3) { $h_per_1000 = 400000; } 
                        elseif ($digit_m == 2) { $h_per_1000 = 70000; }
                    }
                    
                    $total_hadiah = ($nominal_menang / 1000) * $h_per_1000; 
                    $grand_total += $total_hadiah; // 2. Akumulasi total
                ?>
                <tr style="border-top: 1px solid #333;">
                    <td style="padding: 8px;"><?php echo $m['tanggal']; ?></td>
                    <td style="padding: 8px; font-weight: bold; color: #FEDB37;"><?php echo $nomor_tampil; ?></td>
                    <td style="padding: 8px;"><?php echo strtoupper($m['pasaran']); ?></td>
                    <td style="padding: 8px;"><?php echo strtoupper($m['mode_taruhan']); ?></td>
                    <td style="padding: 8px;">Rp<?php echo number_format($nominal_menang); ?></td>
                    <td style="padding: 8px; color: #FEDB37;">Rp<?php echo number_format($total_hadiah); ?></td>
                </tr>
                <?php endwhile; ?> 
                <tr style="background: #262626; color: #fff; font-weight: bold;">
                    <td colspan="5" style="padding: 10px; text-align: right;">TOTAL HADIAH :</td>
                    <td colspan="2" style="padding: 10px; color: #FEDB37; text-align: left;">Rp<?php echo number_format($grand_total); ?></td>
                </tr>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div> 