<?php if(empty($connection) AND !isset($_COOKIE['siswa'])){
    header('location:./404/');
}else{
$data_jam = getJam($connection, $hari_ini, 'Siswa');

function getLokasiData($connection, $lokasi) {
    $lokasi_id_safe = $connection->real_escape_string($lokasi);
    $query = "SELECT * FROM lokasi WHERE lokasi_id='$lokasi_id_safe'";
    $result = $connection->query($query);
    if ($result && $result->num_rows > 0) {
        $data_lokasi = $result->fetch_assoc();
        $lokasi = strip_tags(epm_encode($data_lokasi['lokasi_id']));
        $latitude = strip_tags($data_lokasi['lokasi_latitude']??'-');
        $longitude = strip_tags($data_lokasi['lokasi_longitude']??'-');
        $radius = strip_tags($data_lokasi['lokasi_radius']??'-');
        $status = strip_tags($data_lokasi['lokasi_status']??'-');
        return [
            'lokasi' => $lokasi,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'radius' => $radius,
            'latlong' => $latitude . ', ' . $longitude,
            'status' => $status
        ];
    } else {
        return null;
    }
}

$data_lokasi = getLokasiData($connection, $data_user['lokasi']);
if (!$data_lokasi) {?>
<script type="text/javascript">
swal({
        text: "Lokasi tidak temukan, silahkan lengkapi Profil Anda!",
        icon: "warning",
        buttons: {
            cancel: true,
            confirm: true,
        },
        value: "yes",
    })

    .then((value) => {
        if (value) {
            setTimeout("location.href = './profile';");
        }
    });
</script>
<?php }

if($data_lokasi['status'] == 'Y'){
echo'<span class="radius_required d-none">Y</span>
<span class="validLat d-none">'.($data_lokasi['latitude']??'0').'</span>
<span class="validLon d-none">'.($data_lokasi['longitude']??'0').'</span>
<span class="maxRadius d-none">'.intval($data_lokasi['radius']).'</span>';
}else{
echo'<span class="radius_required d-none">N</span>';
}

echo'
<main class="flex-shrink-0 main">
    <div class="container mb-4">
            <div class="card">
                <div class="card-body text-center">';
                    if($row_site['tipe_absen_siswa'] =='qrcode'){
                        echo'
                        <div class="webcame text-center">
                            <div id="reader"></div>
                            <div id="map-overlay" class="mt-3"></div>
                            <div class="card mb-3 mt-1 bg-primary text-white">
                                <div class="card-body pt-2">
                                    <div class="row align-items-center">';
                                    if ($data_jam) {
                                        echo'
                                        <div class="col-4 align-self-center">
                                            <small><i class="far fa-clock"></i></small>
                                            <p >'.($data_jam['hari']??'-').'</p>
                                        </div>

                                        <div class="col-4 align-self-center">
                                            <small>Jam Masuk</small>
                                            <p>'.($data_jam['jam_masuk']??'-').'</p>
                                        </div>

                                        <div class="col-4 align-self-center">
                                            <small>Jam Pulang</small>
                                            <p>'.($data_jam['jam_pulang']??'-').'</p>
                                        </div>';
                                    }
                                        echo'
                                    </div>
                                </div>
                            </div>
                        </div>';

                    }else if($row_site['tipe_absen_siswa'] =='selfie'){
                        /** Absen dengan foto  */
                    echo'
                    <div class="webcam-app mb-3">
                        <div class="body-webcame">
                            <div class="datetime-overlay date">'.tgl_ind($date).'</div>
                            <div class="datetime-overlay time"><span class="clock">'.$time.'</span></div>
                            <video id="webcam" autoplay playsinline width="640" height="480" class="s-widodo.com"></video>
                            <canvas id="canvas" class="s-widodo.com"></canvas>
                            <div id="map-overlay"></div>
                        </div>

                        <div class="card mb-2 mt-1 bg-primary text-white">
                            <div class="card-body pt-2">
                                <div class="row align-items-center">';
                                if ($data_jam) {
                                    echo'
                                    <div class="col-4 align-self-center">
                                        <small><i class="far fa-clock"></i></small>
                                        <p>'.($data_jam['hari']??'-').'</p>
                                    </div>

                                    <div class="col-4 align-self-center">
                                        <small>Jam Masuk</small>
                                        <p>'.($data_jam['jam_masuk']??'-').'</p>
                                    </div>

                                    <div class="col-4 align-self-center">
                                        <small>Jam Pulang</small>
                                        <p>'.($data_jam['jam_pulang']??'-').'</p>
                                    </div>';
                                }
                                    echo'
                                </div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col">
                                <button type="button" class="btn btn-default btn-block rounded btn-take-photo"><i class="fas fa-fingerprint"></i> Masuk</button>
                            </div>

                            <div class="col">
                                <button type="button" class="btn btn-danger btn-block rounded" disabled><i class="fas fa-fingerprint"></i> Pulang</button>
                            </div>
                        </div>

                    </div>';

                    }else{
                        echo'
                        <div class="webcame-recognition mb-3">
                            <div class="video-recognition canvas-box">
                                <div class="datetime-overlay date">'.tgl_ind($date).'</div>
                                <div class="datetime-overlay time"><span class="clock">'.$time.'</span></div>
                                <div id="map-overlay"></div>
                                <video id="video" class="vidDisplay" autoplay muted></video>
                                <!--<div class="overlay-recognition">
                                    <div class="result-label" id="status">Loading..</div>
                                    <div class="scan-line"></div>
                                </div>-->
                            </div>

                            <div class="card mb-2 mt-1 bg-primary text-white">
                                <div class="card-body pt-2">
                                    <div class="row align-items-center">';
                                    if ($data_jam) {
                                        echo'
                                        <div class="col-4 align-self-center">
                                            <small><i class="far fa-clock"></i></small>
                                            <p>'.($data_jam['hari']??'-').'</p>
                                        </div>

                                        <div class="col-4 align-self-center">
                                            <small>Jam Masuk</small>
                                            <p>'.($data_jam['jam_masuk']??'-').'</p>
                                        </div>

                                        <div class="col-4 align-self-center">
                                            <small>Jam Pulang</small>
                                            <p>'.($data_jam['jam_pulang']??'-').'</p>
                                        </div>';
                                    }
                                        echo'
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col">
                                    <button type="button" class="btn btn-default btn-block rounded" id="btnAbsen" disabled><i class="fas fa-fingerprint"></i> Masuk</button>
                                </div>

                                <div class="col">
                                    <button type="button" class="btn btn-danger btn-block rounded" disabled><i class="fas fa-fingerprint"></i> Pulang</button>
                                </div>
                            </div>

                        </div>';
                    }
                echo'
                </div>
            </div>
        </div>
    
</main>';
}?>