<?php
include '../admin/koneksi.php'; // Pastikan terhubung ke database

if (isset($_POST['nama_pasaran'])) {
    $nama_pasaran = $_POST['nama_pasaran'];

    // Ambil data dari tabel result_pasaran berdasarkan pilihan user
    $query = "SELECT result_pasaran, tanggal_result FROM result_pasaran WHERE nama_pasaran = ? ORDER BY tanggal_result DESC";
    $stmt = $koneksi->prepare($query);
    $stmt->bind_param("s", $nama_pasaran);
    $stmt->execute();
    $result = $stmt->get_result();

    // Inisialisasi array untuk menyimpan data per minggu
    $data_paito = [];
    $hari_mapping = ["Monday" => "Senin", "Tuesday" => "Selasa", "Wednesday" => "Rabu", "Thursday" => "Kamis", "Friday" => "Jumat", "Saturday" => "Sabtu", "Sunday" => "Minggu"];

    while ($row = $result->fetch_assoc()) {
        $hari = date("l", strtotime($row['tanggal_result']));
        $hari_id = $hari_mapping[$hari];
        $angka = str_split($row['result_pasaran']);

        if (count($angka) == 4) {
            $d_value = array_sum(array_slice($angka, -2));
            if ($d_value >= 10) {
                $d_value = array_sum(str_split($d_value));
            }

            $data_paito[] = ["hari" => $hari_id, "angka" => $angka, "d" => $d_value];
        }
    }

    // Jika tidak ada data, tampilkan pesan
    if (empty($data_paito)) {
        echo "<p class='text-center text-warning'>Tidak ada data untuk pasaran ini!</p>";
        exit;
    }

    $minggu_paito = array_chunk($data_paito, 7);
    ?>

    <h3 class="text-center title-paito text-uppercase fw-bold text-warning">
        Paito Togel Warna <?php echo htmlspecialchars(strtoupper($nama_pasaran)); ?>
    </h3>

    <table id="table-paito" class="table-paito w-100 d-block d-md-table">
        <thead>
            <tr class="head text-center">
                <?php foreach ($hari_mapping as $hari): ?>
                    <th colspan="4"><?php echo $hari; ?></th>
                    <th>D</th>
                <?php endforeach; ?>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($minggu_paito as $minggu): ?>
                <tr>
                    <?php foreach ($hari_mapping as $hari): ?>
                        <?php
                        $data_hari = array_filter($minggu, function ($entry) use ($hari) {
                            return $entry['hari'] === $hari;
                        });
                        $data_hari = reset($data_hari);
                        ?>
                        <?php if ($data_hari): ?>
                            <td class="paito-num p-num"><?php echo $data_hari['angka'][0]; ?></td>
                            <td class="paito-num p-num"><?php echo $data_hari['angka'][1]; ?></td>
                            <td class="paito-num p-num"><?php echo $data_hari['angka'][2]; ?></td>
                            <td class="paito-num p-num"><?php echo $data_hari['angka'][3]; ?></td>
                            <td class="paito-num addi-paito"><?php echo $data_hari['d']; ?></td>
                        <?php else: ?>
                            <td class="paito-num p-num"></td>
                            <td class="paito-num p-num"></td>
                            <td class="paito-num p-num"></td>
                            <td class="paito-num p-num"></td>
                            <td class="paito-num addi-paito"></td>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php
}
?>