<?php
include "../koneksi.php";

// Karena hanya kota 'talaga', kita tetapkan langsung
$tabel_result = "result_talaga";
$tabel_atur = "atur_talaga";
$draw_type = "TALAGA";

// Ambil draw_time dan draw_end dari atur_talaga
$query_atur = "SELECT draw_time, draw_end FROM $tabel_atur LIMIT 1";
$result_atur = mysqli_query($koneksi, $query_atur);
$row_atur = mysqli_fetch_assoc($result_atur);

$draw_time = $row_atur['draw_time'] ?? '09:00:00';
$draw_end = $row_atur['draw_end'] ?? '09:30:00';

// Ambil tanggal terakhir dari result_talaga
$query_last_date = "SELECT MAX(tanggal) AS last_date FROM $tabel_result";
$result_last_date = mysqli_query($koneksi, $query_last_date);
$row_last_date = mysqli_fetch_assoc($result_last_date);
$lastDate = $row_last_date['last_date'] ?? "2024-01-01";

// Proses tanggal dari hari setelah lastDate sampai kemarin
$lastDate = new DateTime($lastDate);
$lastDate->modify('+1 day'); // mulai dari hari setelah last result
$today = new DateTime();
$today->modify('-1 day'); // sampai H-1 dari hari ini

// Loop membuat data result per hari
while ($lastDate <= $today) {
    $tanggal = $lastDate->format('Y-m-d');
    $winner_prize = rand(1000, 9999);
    $prize2 = rand(1000, 9999);
    $prize3 = rand(1000, 9999);
    $draw_id = str_pad(rand(0, 999999), 6, '0', STR_PAD_LEFT);

    // Generate 6 spesial dan 6 consolation
    $spesial = [];
    $consolation = [];
    for ($i = 1; $i <= 6; $i++) {
        $spesial[$i] = rand(1000, 9999);
        $consolation[$i] = rand(1000, 9999);
    }

    // Insert ke result_talaga
    $query_result = "INSERT INTO $tabel_result (
        draw_type, tanggal, winner_prize, prize2, draw_id, prize3,
        spesial1, spesial2, spesial3, spesial4, spesial5, spesial6,
        consolation1, consolation2, consolation3, consolation4, consolation5, consolation6
    ) VALUES (
        '$draw_type', '$tanggal', '$winner_prize', '$prize2', '$draw_id', '$prize3',
        '{$spesial[1]}', '{$spesial[2]}', '{$spesial[3]}', '{$spesial[4]}', '{$spesial[5]}', '{$spesial[6]}',
        '{$consolation[1]}', '{$consolation[2]}', '{$consolation[3]}', '{$consolation[4]}', '{$consolation[5]}', '{$consolation[6]}'
    )";
    mysqli_query($koneksi, $query_result);

    // Insert juga ke result_lengkap
    $query_lengkap = "INSERT INTO result_lengkap (
        tanggal, draw_time, draw_end, draw_type, winner_prize, prize2, draw_id, prize3,
        spesial1, spesial2, spesial3, spesial4, spesial5, spesial6,
        consolation1, consolation2, consolation3, consolation4, consolation5, consolation6
    ) VALUES (
        '$tanggal', '$draw_time', '$draw_end', '$draw_type', '$winner_prize', '$prize2', '$draw_id', '$prize3',
        '{$spesial[1]}', '{$spesial[2]}', '{$spesial[3]}', '{$spesial[4]}', '{$spesial[5]}', '{$spesial[6]}',
        '{$consolation[1]}', '{$consolation[2]}', '{$consolation[3]}', '{$consolation[4]}', '{$consolation[5]}', '{$consolation[6]}'
    )";
    mysqli_query($koneksi, $query_lengkap);

    $lastDate->modify('+1 day');
}

// Tutup koneksi dan redirect
mysqli_close($koneksi);
header("Location: result.php?alert=berhasil");
exit();
?>