<?php
    function ambil_data_kemaren($koneksi, $db_waktu, $db_tanggal){
        $qry_hadiah = mysqli_query(
            $koneksi,
            "SELECT * FROM $db_waktu WHERE id_situs ='1'"
        );
        $dt_bank = mysqli_fetch_array($qry_hadiah);
        $draw_time = $dt_bank["draw_time"];
        $draw_end = $dt_bank["draw_end"];
        $jam_target_awal = $draw_time;
        $jam_target_akhir = $draw_end;
        $time_now = date("H:i");
        $date_now = date('Y-m-d');
        $date = new DateTime($date_now);
        $date->modify('-1 day');
        $previous_date = $date->format('Y-m-d');
        $jadwal_draw = mysqli_query(
            $koneksi,
            "SELECT * FROM $db_tanggal
            WHERE DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') = '$previous_date'
            ORDER BY id DESC LIMIT 1"
        );

        if(!empty($jadwal_draw)){
            return mysqli_fetch_all($jadwal_draw, MYSQLI_ASSOC);
        }else{
            return null;
        }
    }

    function ambil_data_hari_ini($koneksi, $db_waktu, $db_tanggal, $tgl){
        $qry_hadiah = mysqli_query(
            $koneksi,
            "SELECT * FROM $db_waktu WHERE id_situs ='1'"
        );
        $dt_bank = mysqli_fetch_array($qry_hadiah);
        $draw_time = $dt_bank["draw_time"];
        $draw_end = $dt_bank["draw_end"];
        $jam_target_awal = $draw_time;
        $jam_target_akhir = $draw_end;
        $time_now = date("H:i");
        $date_now = $tgl;
        $jadwal_draw = mysqli_query(
            $koneksi,
            "SELECT * FROM $db_tanggal
            WHERE DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') = '$date_now'
            ORDER BY id DESC LIMIT 1"
        );

        if(!empty($jadwal_draw)){
            return mysqli_fetch_all($jadwal_draw, MYSQLI_ASSOC);
        }else{
            return null;
        }
    }


    function pagination_ambil_data_hari($koneksi, $db_waktu, $id_db_waktu, $db_tanggal, $sql){
        $qry_hadiah = mysqli_query(
            $koneksi,
            "SELECT * FROM $db_waktu WHERE id_situs ='$id_db_waktu'"
        );
        $dt_bank = mysqli_fetch_array($qry_hadiah);
        $draw_time = $dt_bank["draw_time"];
        $draw_end = $dt_bank["draw_end"];
        $jam_target_awal = $draw_time;
        $jam_target_akhir = $draw_end;
        $time_now = date("H:i");
        $date_now = date("Y-m-d");
        $jadwal_draw = mysqli_query(
            $koneksi,
            "SELECT * FROM $db_tanggal
            WHERE DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') <= '$date_now'
            ORDER BY DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') DESC $sql"
        );

        if(empty($jadwal_draw) || ($time_now < $jam_target_akhir)){
            $jadwal_draw = mysqli_query(
               $koneksi,
               "SELECT * FROM $db_tanggal
               WHERE DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') < '$date_now'
               ORDER BY DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') DESC $sql"
            );
            $jadwal_draw = mysqli_fetch_all($jadwal_draw, MYSQLI_ASSOC);
            return $jadwal_draw;
        }else{
            if(!empty($jadwal_draw)){
                return mysqli_fetch_all($jadwal_draw, MYSQLI_ASSOC);
            }else{
                return null;
            }
        }
    }


    function ambil_data_hari($koneksi, $db_waktu, $id_db_waktu, $db_tanggal, $sql){
        $qry_hadiah = mysqli_query(
            $koneksi,
            "SELECT * FROM $db_waktu WHERE id_situs ='$id_db_waktu'"
        );
        $dt_bank = mysqli_fetch_array($qry_hadiah);
        $draw_time = $dt_bank["draw_time"];
        $draw_end = $dt_bank["draw_end"];
        $jam_target_awal = $draw_time;
        $jam_target_akhir = $draw_end;
        $time_now = date("H:i");
        $date_now = date("Y-m-d");
        $jadwal_draw = mysqli_query(
            $koneksi,
            "SELECT * FROM $db_tanggal
            WHERE DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') = '$date_now'
            ORDER BY DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') DESC $sql"
        );

        if(empty($jadwal_draw) || ($time_now < $jam_target_akhir)){
            $jadwal_draw = mysqli_query(
               $koneksi,
               "SELECT * FROM $db_tanggal
               WHERE DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') < '$date_now'
               ORDER BY DATE_FORMAT(STR_TO_DATE(tanggal, '%a, %d %b %Y'), '%Y-%m-%d') DESC $sql"
            );
            $jadwal_draw = mysqli_fetch_all($jadwal_draw, MYSQLI_ASSOC);
            return $jadwal_draw;
        }else{
            if(!empty($jadwal_draw)){
                return mysqli_fetch_all($jadwal_draw, MYSQLI_ASSOC);
            }else{
                return null;
            }
        }
    }

?>