<?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: 20px; font-family: sans-serif;">
    
    <div style="margin-bottom: 20px; text-align: center;">
        <a href="riwayat_menang.php?tgl=<?php echo $tgl; ?>&psn=<?php echo $psn; ?>" 
           style="background: #2ecc71; color: #000; padding: 10px 20px; text-decoration: none; border-radius: 5px; font-weight: bold; display: inline-block;">
           Lihat Daftar Kemenangan Saya
        </a>
    </div>

    <div style="width: 100%; margin: 20px auto 20px auto; overflow: hidden;">
    <div style="font-family: Teko-SemiBold; text-align: center; font-size: 24px; 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%); padding: 5px 0;">
        RINCIAN BETTING
        <hr style="background: linear-gradient(#000 0 0) padding-box, linear-gradient(to left, #FEDB37, #8A6E2F) border-box; border-top: 2px solid transparent; margin: 0 auto; width: 70px;">
    </div>
        <p style="text-align: center; font-size: 11px; color: #ccc; margin-top: 0; margin-bottom: 20px;">
            Pasaran: <span style="color:#FEDB37; font-weight:bold;"><?php echo strtoupper($psn); ?></span> | 
            Waktu: <span style="color:#fff;"><?php echo $tgl; ?></span>
        </p>

        <div style="background: #1a1a1a; border-radius: 8px; overflow: hidden; border: 1px solid #333; width: 100%;">
            <table width="100%" style="text-align: center; border-collapse: collapse; font-size: 11px; table-layout: fixed;">
                
                <thead>
                    <tr style="background: #262626; color: #fff; font-weight: bold; border-bottom: 2px solid #333;">
                        <th style="padding: 12px 3px; width: 15%;">Nomor</th>
                        <th style="padding: 12px 3px; width: 15%;">Game</th>
                        <th style="padding: 12px 3px; width: 14%; padding-left: 10px;">Bet</th>
                        <th style="padding: 12px 3px; width: 13%;">Mode</th>
                        <th style="padding: 12px 3px; width: 13%; padding-left: 10px;">Bayar</th>
                        <th style="padding: 12px 3px; width: 14%; color: #FEDB37; padding-left: 10px;">Hadiah</th>
                        <th style="padding: 12px 2px; width: 20%; color: #fff; padding-left: 20px;">Status</th>
                    </tr>
                </thead>
                
                <tbody>
                <?php 
if ($query && mysqli_num_rows($query) > 0):
    while($d = mysqli_fetch_assoc($query)): 
        $nominal_bet  = (int)$d['nominal'];
        $mode_taruhan = strtoupper($d['mode_taruhan']);
        $diskon_text  = trim($d['diskon']); 
        $nomor        = trim($d['nomor']);      
        $jenis_game   = strtoupper($d['game']); 

        if (strpos($jenis_game, '4D') !== false || $d['game'] == '4D') {
            $digit = 4;
        } elseif (strpos($jenis_game, '3D') !== false) {
            $digit = 3;
        } else {
            $digit = 2;
        }

        $nomor_tampilan = $nomor; 
        if ($digit == 4) {
            $nomor_tampilan = str_pad($nomor, 4, "0", STR_PAD_LEFT); 
        } elseif ($digit == 3) {
            $nomor_bersih = str_pad($nomor, 3, "0", STR_PAD_LEFT);
            if (strpos($jenis_game, 'DEPAN') !== false) {
                $nomor_tampilan = $nomor_bersih . 'x';  
            } else {
                $nomor_tampilan = 'x' . $nomor_bersih;  
            }
        } elseif ($digit == 2) {
            $nomor_bersih = str_pad($nomor, 2, "0", STR_PAD_LEFT);
            if (strpos($jenis_game, 'DEPAN') !== false) {
                $nomor_tampilan = $nomor_bersih . 'xx'; 
            } elseif (strpos($jenis_game, 'TENGAH') !== false) {
                $nomor_tampilan = 'x' . $nomor_bersih . 'x'; 
            } else {
                $nomor_tampilan = 'xx' . $nomor_bersih; 
            }
        }

        $hadiah_per_1000 = 0;
        if (strpos($mode_taruhan, 'FULL') !== false || $diskon_text == '0%' || $diskon_text == '0' || $diskon_text == '') {
            if ($digit == 4) { $hadiah_per_1000 = 10000000; } 
            elseif ($digit == 3) { $hadiah_per_1000 = 1000000; } 
            elseif ($digit == 2) { $hadiah_per_1000 = 100000; }
        } else {
            if ($digit == 4) { $hadiah_per_1000 = 3400000; } 
            elseif ($digit == 3) { $hadiah_per_1000 = 400000; } 
            elseif ($digit == 2) { $hadiah_per_1000 = 70000; }
        }
        
        $total_hadiah_pola = ($nominal_bet / 1000) * $hadiah_per_1000;

        $status_raw = isset($d['status']) ? trim($d['status']) : '';
        if (strcasecmp($status_raw, 'Menang') == 0) {
            $status_html = '<span style="color: #2ecc71; font-weight: bold;">MENANG</span>';
        } elseif (strcasecmp($status_raw, 'Kalah') == 0) {
            $status_html = '<span style="color: #e74c3c; font-weight: bold;">KALAH</span>';
        } else {
            $status_html = '<span style="color: #f39c12; font-weight: bold;">JALAN</span>';
        }
?>
                    <tr style="background: #1f1f1f;"> 
                        <td style="color: #FEDB37; font-weight: bold; font-size: 12px; padding: 15px 2px; letter-spacing: 0.5px;">
                            <?php echo $nomor_tampilan; ?>
                        </td>
                        
                        <td style="padding: 15px 2px;">
                            <span style="font-size: 9px; background: #111; padding: 2px 4px; border-radius: 4px; color:#bbb; border: 1px solid #333; display: inline-block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
                                <?php echo !empty($d['game']) ? $d['game'] : $digit.'D'; ?>
                            </span>
                        </td>
                        
                        <td style="padding: 10px 2px;">Rp<?php echo number_format($nominal_bet); ?></td>
                        <td style="padding: 10px 2px; color: #e74c3c;"><?php echo (!empty($diskon_text) && $diskon_text != '0%') ? $diskon_text : '-'; ?></td>
                        <td style="padding: 10px 2px; color: #2ecc71;">Rp<?php echo number_format($d['total_bayar']); ?></td>
                        
                        <td style="padding: 10px 2px; color: #FEDB37; font-weight: bold;">Rp<?php echo number_format($total_hadiah_pola); ?></td> 
                        
                        <td style="padding: 10px 2px; padding-left: 20px;"><?php echo $status_html; ?></td>
            
                    </tr>
                    <?php 
                        endwhile; 
                    else:
                    ?>
                    <tr>
                        <td colspan="7" style="padding: 25px; color: #aaa;">Data rincian tidak ditemukan.</td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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