<?php
    require_once 'data.php';

    $draw = "SELECT * FROM justicewonder WHERE DATE(jam) = '$todaydate' AND trashed = 0 ORDER BY id DESC LIMIT 1";
    $qdraw = mysqli_query($data, $draw);
    $data_draw = array();
    
    if(mysqli_num_rows($qdraw) > 0) {
            $fdraw = mysqli_fetch_assoc($qdraw);
            $p1 = $fdraw['prize1'];
            $p2 = $fdraw['prize2'];
            $p3 = $fdraw['prize3'];
            $p4 = $fdraw['speciala1'];
            $p5 = $fdraw['speciala2'];
            $p6 = $fdraw['speciala3'];
            $p7 = $fdraw['speciala4'];
            $p8 = $fdraw['speciala5'];
            $p9 = $fdraw['consola1'];
            $p10 = $fdraw['consola2'];
            $p11 = $fdraw['consola3'];
            $p12 = $fdraw['consola4'];
            $p13 = $fdraw['consola5'];
             
             $drawtime = $fdraw['mulaitgl'];
 
             $stime = "SELECT * FROM workingtime";
             $qtime = mysqli_query($data, $stime);
             $ftime = mysqli_fetch_assoc($qtime);

             $jam1 = date("Y-m-d ". $ftime['bola_1']);
             $jam2 = date("Y-m-d ". $ftime['bola_2']);
             $jam3 = date("Y-m-d ". $ftime['bola_3']);
             $jam4 = date("Y-m-d ". $ftime['bola_4']);
             $jam5 = date("Y-m-d ". $ftime['bola_5']);
             $jam6 = date("Y-m-d ". $ftime['bola_6']);
             $jam7 = date("Y-m-d ". $ftime['bola_7']);
             $jam8 = date("Y-m-d ". $ftime['bola_8']);
             $jam9 = date("Y-m-d ". $ftime['bola_9']);
             $jam10 = date("Y-m-d ". $ftime['bola_10']);
             $jam11 = date("Y-m-d ". $ftime['bola_11']);
             $jam12 = date("Y-m-d ". $ftime['bola_12']);
             $jam13 = date("Y-m-d ". $ftime['bola_13']);

             $ctime = date("Y-m-d H:i");

                if($ctime >= $jam1) {
                    $select_update = "SELECT * FROM justicewonder WHERE is_result = 0";
                    $querry = mysqli_query($data, $select_update);

                    if(mysqli_num_rows($querry) > 0) {
                        $update_isresult = "UPDATE justicewonder SET is_result = 1";
                        mysqli_query($data, $update_isresult);
                    }
                }

                $data_draw[] = array(
                    'ticket_no' => $fdraw['id'],
                    'day' => $fdraw['hari'],
                    'start_date' => $fdraw['mulaitgl'],
                    'prize' => intval($fdraw['top_prize']),
                    'prize_1' => (($ctime >= $jam1) ? intval($p1) : '????' ),
                    'prize_2' => (($ctime >= $jam2) ? intval($p2) : '????' ),
                    'prize_3' => (($ctime >= $jam3) ? intval($p3) : '????' ),
                    'prize_4' => (($ctime >= $jam4) ? intval($p4) : '????' ),
                    'prize_5' => (($ctime >= $jam5) ? intval($p5) : '????' ),
                    'prize_6' => (($ctime >= $jam6) ? intval($p6) : '????' ),
                    'prize_7' => (($ctime >= $jam7) ? intval($p7) : '????' ),
                    'prize_8' => (($ctime >= $jam8) ? intval($p8) : '????' ),
                    'prize_9' => (($ctime >= $jam9) ? intval($p9) : '????' ),
                    'prize_10' => (($ctime >= $jam10) ? intval($p10) : '????' ),
                    'prize_11' => (($ctime >= $jam11) ? intval($p11) : '????' ),
                    'prize_12' => (($ctime >= $jam12) ? intval($p12) : '????' ),
                    'prize_13' => (($ctime >= $jam13) ? intval($p13) : '????' ),
                );

             
             } else {

                // Previous Draw
                $draw1 = "SELECT * FROM justicewonder WHERE DATE(jam) < '$todaydate' AND trashed = 0 ORDER BY id DESC LIMIT 1";
                $qdraw1 = mysqli_query($data, $draw1);
                
                if(mysqli_num_rows($qdraw1) > 0) {
                    $fdraw1 = mysqli_fetch_assoc($qdraw1);
                    $pr1 = $fdraw1['prize1'];
                    $pr2 = $fdraw1['prize2'];
                    $pr3 = $fdraw1['prize3'];
                    $pr4 = $fdraw1['speciala1'];
                    $pr5 = $fdraw1['speciala2'];
                    $pr6 = $fdraw1['speciala3'];
                    $pr7 = $fdraw1['speciala4'];
                    $pr8 = $fdraw1['speciala5'];
                    $pr9 = $fdraw1['consola1'];
                    $pr10 = $fdraw1['consola2'];
                    $pr11 = $fdraw1['consola3'];
                    $pr12 = $fdraw1['consola4'];
                    $pr13 = $fdraw1['consola5'];

                    $data_draw[] = array(
                        'ticket_no' => $fdraw1['id'],
                        'day' => $fdraw1['hari'],
                        'start_date' => $fdraw1['mulaitgl'],
                        'prize' => intval($fdraw1['top_prize']),
                        'prize_1' => intval($pr1),
                        'prize_2' => intval($pr2),
                        'prize_3' => intval($pr3),
                        'prize_4' => intval($pr4),
                        'prize_5' => intval($pr5),
                        'prize_6' => intval($pr6),
                        'prize_7' => intval($pr7),
                        'prize_8' => intval($pr8),
                        'prize_9' => intval($pr9),
                        'prize_10' => intval($pr10),
                        'prize_11' => intval($pr11),
                        'prize_12' => intval($pr12),
                        'prize_13' => intval($pr13),
                     );
                }
            }

     header('Content-Type: application/json');
     header('Content-Type: application/json');
    header("Access-Control-Allow-Origin: *"); // Replace with your Next.js app's origin
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
    header("Access-Control-Allow-Methods: GET");
     echo json_encode($data_draw)

?>