<?php
require_once 'koneksi/koneksi.php';
require_once 'fungsi/function_crud.php';
require_once 'fungsi/function_redirect.php';

date_default_timezone_set('Asia/Jakarta');

$time_now = date("H:i:s");
$date_now = date('Y-m-d');

// Cek waktu draw dan redirect
$q_talaga = mysqli_query($koneksi, "SELECT draw_time, draw_end FROM atur_talaga WHERE id_situs = '1'");
$data_talaga = mysqli_fetch_assoc($q_talaga);

if (
    isset($data_talaga['draw_time'], $data_talaga['draw_end']) &&
    $time_now >= $data_talaga['draw_time'] &&
    $time_now <= $data_talaga['draw_end']
) {
    header("Location: draw.php");
    exit();
}

// Ambil 1 data draw terbaru dari result_lengkap berdasarkan tanggal terbaru
$jadwal_draw = mysqli_query(
    $koneksi,
    "SELECT * FROM result_lengkap 
        WHERE tanggal <= '$date_now' 
        ORDER BY tanggal DESC 
        LIMIT 1"
);

$data_draw = mysqli_fetch_assoc($jadwal_draw);

// Siapkan data untuk ditampilkan di frontend
$tanggal = '-';
if (isset($data_draw['tanggal'])) {
    $tanggalObj = new DateTime($data_draw['tanggal']);
    $tanggal = $tanggalObj->format('l, M d Y'); // contoh output: Wednesday, Feb 21 2024
}

$winner_prize = isset($data_draw['winner_prize']) ? str_split($data_draw['winner_prize']) : [];
$prize2 = isset($data_draw['prize2']) ? $data_draw['prize2'] : '';
$prize3 = isset($data_draw['prize3']) ? $data_draw['prize3'] : '';
$spesial1 = isset($data_draw['spesial1']) ? $data_draw['spesial1'] : '';
$spesial2 = isset($data_draw['spesial2']) ? $data_draw['spesial2'] : '';

// Ambil data konfigurasi situs
$qry_atur = mysqli_query($koneksi, "SELECT id, nama_pasaran, title, meta_desc, logo, favicon, custom_script FROM atur_web WHERE id = '1'");
if ($qry_atur && mysqli_num_rows($qry_atur) > 0) {
    $data_situs = mysqli_fetch_assoc($qry_atur);
    $id = $data_situs["id"];
    $nama_pasaran = $data_situs["nama_pasaran"];
    $title = $data_situs["title"];
    $meta_desc = $data_situs["meta_desc"];
    $logo = $data_situs["logo"];
    $favicon = $data_situs["favicon"];
    $custom_script = $data_situs["custom_script"];
} else {
    $id = 1;
    $nama_pasaran = $title = $meta_desc = $logo = $favicon = $custom_script = "";
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo $nama_pasaran; ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="allow-search" content="Yes">
    <meta name="ROBOTS" content="All">
    <link rel="icon" type="image/png" href="<?php echo $favicon; ?>?v=124208" />
    <link rel='shortcut icon' type='image/vnd.microsoft.icon' href='<?php echo $favicon; ?>?v=124208' />
    <link rel="icon" href='<?php echo $favicon; ?>' type="image/x-icon" />
    <link rel="shortcut icon" href='<?php echo $favicon; ?>?v=124208' type="image/x-icon" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link rel="stylesheet"
        href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic,700,700italic">
    <link rel="stylesheet" href="templates/www/default/css/reset.css" />
    <link rel="stylesheet" href="templates/www/default/css/960.css" />
    <link rel="stylesheet" href="templates/www/default/css/style.css" />
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.2/themes/blitzer/jquery-ui.css">
    <script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    <script type="text/javascript">
        function display_c() {
            var refresh = 1000; // Refresh rate in milli seconds
            mytime = setTimeout('display_ct()', refresh)
        }
        function display_ct() {
            var strcount
            var x = new Date()
            x = + x.getHours() + ":" + x.getMinutes() + ":" + x.getSeconds();
            document.getElementById('ct').innerHTML = x;

            tt = display_c();
        }
    </script>
</head>

<body onload=display_ct();>
    <div class="container_12">
        <div class="grid_12 logo">
            <div class="left-logo">
                <a href="index.php"><img src="<?php echo $logo; ?>" alt="<?php echo $nama_pasaran; ?>" width="300"
                        height="99" border="0"></a>
            </div>
            <div class="right-logo">
                <div class="nav-menu">
                    <ul>
                        <li class="red"><a class="selected" href="index.php">HOME</a></li>
                        <li class="maroon"><a href="about.php">ABOUT US</a></li>

                        <li class="maroon"><a href="result.php">RESULT</a></li>
                        <li class="red"><a href="contact.php">CONTACT</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="grid_9">
            <div class="slider">
                <div id="wrapper">
                    <div id="images">
                        <div class="carousel">
                            <img src="templates/www/default/images/slide1.jpg" alt="cod" width="700" height="200" />
                            <img src="templates/www/default/images/slide2.jpg" alt="cod" width="700" height="200" />
                        </div>
                    </div>
                    <div id="timer"></div>
                </div>
            </div>
        </div>
        <div class="grid_6">
            <div class="welcome-page">
                <h2 class="header underline"><?php echo $nama_pasaran; ?></h2>
                <p><?php echo $nama_pasaran; ?> is one of the first and highest paying European lottery lucky draw in
                    Italy, headquarter
                    in Rome.</p>
                <p><?php echo $nama_pasaran; ?> is subsidiaries of Italy & European Lottery Consortium, a legal lottery
                    organization
                    oversee by the Italian government.</p>
                <p><?php echo $nama_pasaran; ?> is proud to bring the heritage of the people of Italy, playing with
                    confident to support
                    bravest gladiator ever to fight in Colloseum, now in form of lottery.</p>
                <div class="welcome-page-inner">
                    <h2>Grand Prize</h2>
                    <p>Daily lottery lucky draw with high chance of winning have been our most successful products, we
                        have achieve the highest average winning chance within Italy.</p>
                    <p>Our yearly grand prize lottery in November have consistently brought more than <b>€250,000</b>
                        prize pool. This events is also supported by local and media coverage across Italy.</p>
                </div>
                <div class="welcome-page-inner">
                    <h2>The European Lotteries</h2>
                    <p><?php echo $nama_pasaran; ?> is supporting "The European Lotteries Gambling Responsibly"
                        initiatives, promoting a
                        responsible and smart investment in lottery. Also promoting a welfare program funding to local
                        community for Italian, such as education and health relief efforts.</p>
                    <small style="color:orange;"><i>*<?php echo $nama_pasaran; ?> is gold partner of The European
                            Lotteries.</i></small>
                </div>
            </div>
        </div>
        <div class="grid_3">
            <div class="main-left">
                <h2 class="header underline"><?php echo $nama_pasaran; ?> SCHEDULE</h2>
                <h2>LUCKY DRAW`S SCHEDULE</h2>
                <p><span class="redone">MONDAY - SUNDAY</span>&nbsp;</p>
                <?php
                date_default_timezone_set('Asia/Jakarta');
                include 'koneksi.php';

                // Ambil waktu draw dari atur_talaga
                $query = "SELECT draw_time, draw_end FROM atur_talaga LIMIT 1";
                $result = mysqli_query($koneksi, $query);
                $data = mysqli_fetch_assoc($result);

                // Konversi dari Asia/Jakarta ke Europe/Dublin
                function convertTimeToDublin($time_str)
                {
                    $wib = new DateTime($time_str, new DateTimeZone('Asia/Jakarta'));
                    $wib->setTimezone(new DateTimeZone('Europe/Dublin'));
                    return $wib->format('h.i A'); // format: 06.00 AM
                }

                $drawStart = convertTimeToDublin($data['draw_time']);
                $drawEnd = convertTimeToDublin($data['draw_end']);
                ?>

                <p><?= $drawStart ?> - <?= $drawEnd ?><br />
                    ( UTC/GMT +1 )<br />
                    Dublin - Ireland
                </p>

            </div>
        </div>
    </div>
    <footer id="footer">
        <div class="container_12">
            <div class="grid_12">
                <p>Copyright © <?php echo $nama_pasaran; ?> 2005 - 2016. All Rights Reserved.</p>
            </div>
        </div>
    </footer>
    <script src="templates/www/default/js/jquery.carouFredSel-6.2.0-packed.js" type="text/javascript"></script>
    <script type="text/javascript">
        var livedrawWindow = null;
        function livedraw(mypage, myname, w, h, pos, infocus) {

            if (pos == 'random') { LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100; TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100; }
            else { LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100; TopPosition = (screen.height) ? (screen.height - h) / 2 : 100; }
            settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no'; livedrawWindow = window.open('', myname, settings);
            if (infocus == 'front') { livedrawWindow.focus(); livedrawWindow.location = mypage; }
            if (infocus == 'back') { livedrawWindow.blur(); livedrawWindow.location = mypage; livedrawWindow.blur(); }

        }
    </script>
    <script type="text/javascript">
        $(function () {
            var $imgs = $('#images .carousel'),
                $capt = $('#captions .carousel'),
                $timr = $('#timer');

            $imgs.carouFredSel({
                circular: false,
                scroll: {
                    easing: 'quadratic',
                    duration: 2,
                    timeoutDuration: 3000,
                    onBefore: function (data) {
                        $capt.trigger('slideTo', ['.' + data.items.visible.first().attr('alt')]);
                        $timr.stop().animate({
                            opacity: 0
                        }, data.scroll.duration);
                    },
                    onAfter: function () {
                        $timr.stop().animate({
                            opacity: 1
                        }, 150);
                    }
                },
                auto: {
                    progress: '#timer'
                },
                pagination: {
                    container: '.pager',
                    anchorBuilder: function (i) {
                        return '<a class="p' + i + '" href="#"></a>';
                    }
                }
            });
            $capt.carouFredSel({
                circular: false,
                auto: false,
                scroll: {
                    easing: 'quadratic',
                    duration: 2
                }
            });
        });
    </script>
</body>

</html>