<?php
require_once '../data.php';
require_once '../config.php';

if (isset($_GET['lang'])) {
    $lang = mysqli_real_escape_string($data, $_GET['lang']);
    $draw = "SELECT * FROM justicewonder WHERE DATE(jam) = '$todaydate' AND trashed = 0 ORDER BY jam DESC LIMIT 1";
    $qdraw = mysqli_query($data, $draw);

    if (mysqli_num_rows($qdraw) > 0) {
        $fdraw = mysqli_fetch_assoc($qdraw);
        $p1 = $fdraw['prize1'];
        $p2 = $fdraw['prize2'];
        $p3 = $fdraw['prize3'];

        $drawtime = $fdraw['mulaitgl'];
        $regex = '/(?<!\ )[0-9]/';

        $num1 = preg_replace($regex, '<li>\\0</li> ' . '', $p1);
        $num2 = preg_replace($regex, '<li>\\0</li> ' . '', $p2);
        $num3 = preg_replace($regex, '<li>\\0</li> ' . '', $p3);

        $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']);

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

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


        echo liveDraw($lang, $fdraw['id'], $drawtime, number_format($fdraw['top_prize']), (($ctime >= $jam1) ? $num1 : '<li>?</li> <li>?</li> <li>?</li> <li>?</li>'), (($ctime >= $jam2) ? $num2 : '<li>?</li> <li>?</li> <li>?</li> <li>?</li>'), (($ctime >= $jam3) ? $num3 : '<li>?</li> <li>?</li> <li>?</li> <li>?</li>'));
    } else {

        // Previous Draw
        $draw1 = "SELECT * FROM justicewonder WHERE DATE(jam) < '$todaydate' AND trashed = '0' ORDER BY jam 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'];

            $drawt = $fdraw1['mulaitgl'];
            $regex1 = '/(?<!\ )[0-9]/';

            $n1 = preg_replace($regex1, '<li>\\0</li> ' . '', $pr1);
            $n2 = preg_replace($regex1, '<li>\\0</li> ' . '', $pr2);
            $n3 = preg_replace($regex1, '<li>\\0</li> ' . '', $pr3);

            echo liveDraw($lang, $fdraw1['id'], $drawt, number_format($fdraw1['top_prize']), $n1, $n2, $n3);
        }
    }
}
