<?php

date_default_timezone_set("Asia/Jakarta");

require_once "../../config/koneksi.php";
require_once "../../config/session.php";

if($_SERVER['REQUEST_METHOD']!="POST"){
    exit("Request tidak valid");
}

if(empty($_POST['qr'])){
    exit("QR Code kosong");
}

$qr=trim($_POST['qr']);

$data=explode("|",$qr);

if(count($data)!=2){
    exit("Format QR salah");
}

$nis=mysqli_real_escape_string($conn,$data[1]);

// =======================
// DATA SISWA
// =======================

$q=mysqli_query($conn,"
SELECT
s.*,
k.nama_kelas
FROM siswa s
LEFT JOIN kelas k
ON s.id_kelas=k.id_kelas
WHERE s.nis='$nis'
LIMIT 1
");

if(mysqli_num_rows($q)==0){

?>

<div class="alert alert-danger text-center">

<i class="fas fa-times-circle fa-4x"></i>

<h3 class="mt-3">

DATA SISWA TIDAK DITEMUKAN

</h3>

</div>

<?php
exit;

}

$siswa=mysqli_fetch_assoc($q);

$id_siswa=$siswa['id_siswa'];

$tanggal=date("Y-m-d");
$jam=date("H:i:s");

$hari=array(

"Sunday"=>"Minggu",
"Monday"=>"Senin",
"Tuesday"=>"Selasa",
"Wednesday"=>"Rabu",
"Thursday"=>"Kamis",
"Friday"=>"Jumat",
"Saturday"=>"Sabtu"

);

$hari_ini=$hari[date("l")];

// =======================
// JADWAL
// =======================

$j=mysqli_query($conn,"
SELECT *
FROM jadwal_absensi
WHERE hari='$hari_ini'
AND aktif='Y'
LIMIT 1
");

if(mysqli_num_rows($j)==0){

?>

<div class="alert alert-warning text-center">

<i class="fas fa-calendar-times fa-4x"></i>

<h3 class="mt-3">

BELUM ADA JADWAL ABSENSI

</h3>

</div>

<?php
exit;

}

$jadwal=mysqli_fetch_assoc($j);

// =======================
// BELUM WAKTU MASUK
// =======================

if(strtotime($jam)<strtotime($jadwal['jam_masuk_mulai'])){

?>

<div class="alert alert-warning text-center">

<i class="fas fa-clock fa-4x"></i>

<h3 class="mt-3">

ABSENSI BELUM DIBUKA

</h3>

<h4>

Silakan kembali pukul

<?= $jadwal['jam_masuk_mulai']; ?>

</h4>

</div>

<?php
exit;

}

// =======================
// STATUS
// =======================

$status="Hadir";

if(strtotime($jam)>strtotime($jadwal['jam_masuk_selesai'])){

$status="Terlambat";

}

// =======================
// CEK ABSENSI HARI INI
// =======================

$cek=mysqli_query($conn,"
SELECT *
FROM absensi
WHERE id_siswa='$id_siswa'
AND tanggal='$tanggal'
LIMIT 1
");
/* ==========================================================
   BELUM PERNAH ABSEN
========================================================== */

if(mysqli_num_rows($cek)==0){

    // Jika jam masuk sudah ditutup
    if(strtotime($jam) > strtotime($jadwal['jam_pulang_mulai'])){

        ?>

        <div class="alert alert-danger text-center shadow">

            <i class="fas fa-times-circle fa-4x"></i>

            <h2 class="mt-3">

                ABSENSI MASUK SUDAH DITUTUP

            </h2>

        </div>

        <?php
        exit;

    }

    // Simpan absensi masuk

    $simpan=mysqli_query($conn,"
    INSERT INTO absensi
    (
        id_siswa,
        tanggal,
        jam_masuk,
        status,
        created_at
    )
    VALUES
    (
        '$id_siswa',
        '$tanggal',
        '$jam',
        '$status',
        NOW()
    )
    ");

    if(!$simpan){

        die(mysqli_error($conn));

    }

    // ==========================
    // TAMPILAN
    // ==========================

    if($status=="Hadir"){

        $warna="success";
        $icon="fas fa-check-circle";
        $judul="SELAMAT DATANG";
        $badge="badge-success";
        $statusText="HADIR TEPAT WAKTU";
        $audio="../../assets/audio/success.mp3";

    }else{

        $warna="warning";
        $icon="fas fa-clock";
        $judul="ANDA TERLAMBAT";
        $badge="badge-warning";
        $statusText="TERLAMBAT";
        $audio="../../assets/audio/warning.mp3";

    }

    ?>

    <div class="alert alert-<?= $warna ?> text-center shadow-lg">

        <i class="<?= $icon ?>"
           style="font-size:90px"></i>

        <h1 class="mt-3 font-weight-bold">

            <?= strtoupper(htmlspecialchars($siswa['nama_siswa'])) ?>

        </h1>

        <h4>

            <?= htmlspecialchars($siswa['nama_kelas']) ?>

        </h4>

        <hr>

        <table class="table table-bordered">

            <tr>

                <th width="170">

                    Jam Masuk

                </th>

                <td>

                    <?= $jam ?>

                </td>

            </tr>

            <tr>

                <th>

                    Status

                </th>

                <td>

                    <span class="badge <?= $badge ?> p-2">

                        <?= $statusText ?>

                    </span>

                </td>

            </tr>

        </table>

    </div>

    <script>

    try{

        new Audio("<?= $audio ?>").play();

    }catch(e){}

    setTimeout(function(){

        $("#hasil").html("");

        if(typeof loadCamera==="function"){

            loadCamera();

        }

    },3000);

    </script>

    <?php

    exit;

}


$absen=mysqli_fetch_assoc($cek);
/* ==========================================================
   SUDAH ABSEN MASUK
========================================================== */

if(empty($absen['jam_pulang'])){

    /* ==========================================
       BELUM MASUK JAM PULANG
    ========================================== */

    if(strtotime($jam) < strtotime($jadwal['jam_pulang_mulai'])){

        ?>

        <div class="alert alert-info text-center shadow-lg">

            <i class="fas fa-info-circle fa-4x"></i>

            <h2 class="mt-3">

                SUDAH ABSEN MASUK

            </h2>

            <h3>

                <?= strtoupper(htmlspecialchars($siswa['nama_siswa'])) ?>

            </h3>

            <hr>

            <p>

                Absensi pulang belum dibuka.

            </p>

            <h4>

                Dibuka pukul

                <strong>

                    <?= $jadwal['jam_pulang_mulai']; ?>

                </strong>

            </h4>

        </div>

        <script>

        setTimeout(function(){

            $("#hasil").html("");

            if(typeof loadCamera==="function"){

                loadCamera();

            }

        },3000);

        </script>

        <?php

        exit;

    }

    /* ==========================================
       SIMPAN JAM PULANG
    ========================================== */

    $update=mysqli_query($conn,"
    UPDATE absensi
    SET jam_pulang='$jam'
    WHERE id_absensi='".$absen['id_absensi']."'
    ");

    if(!$update){

        die(mysqli_error($conn));

    }

    ?>

    <div class="alert alert-primary text-center shadow-lg">

        <i class="fas fa-home fa-4x"></i>

        <h1 class="mt-3">

            <?= strtoupper(htmlspecialchars($siswa['nama_siswa'])) ?>

        </h1>

        <h2>

            SELAMAT PULANG

        </h2>

        <hr>

        <table class="table table-bordered">

            <tr>

                <th width="170">

                    Jam Pulang

                </th>

                <td>

                    <?= $jam ?>

                </td>

            </tr>

        </table>

    </div>

    <script>

    try{

        new Audio("../../assets/audio/bye.mp3").play();

    }catch(e){}

    setTimeout(function(){

        $("#hasil").html("");

        if(typeof loadCamera==="function"){

            loadCamera();

        }

    },3000);

    </script>

    <?php

    exit;

}

/* ==========================================================
   ABSENSI HARI INI SUDAH LENGKAP
========================================================== */
?>

<div class="alert alert-success text-center shadow-lg">

    <i class="fas fa-check-double fa-4x"></i>

    <h1 class="mt-3">

        <?= strtoupper(htmlspecialchars($siswa['nama_siswa'])) ?>

    </h1>

    <h3>

        ABSENSI HARI INI SUDAH LENGKAP

    </h3>

    <hr>

    <table class="table table-bordered">

        <tr>

            <th width="180">

                Kelas

            </th>

            <td>

                <?= htmlspecialchars($siswa['nama_kelas']) ?>

            </td>

        </tr>

        <tr>

            <th>

                Jam Masuk

            </th>

            <td>

                <?= htmlspecialchars($absen['jam_masuk']) ?>

            </td>

        </tr>

        <tr>

            <th>

                Jam Pulang

            </th>

            <td>

                <?= htmlspecialchars($absen['jam_pulang']) ?>

            </td>

        </tr>

        <tr>

            <th>

                Status

            </th>

            <td>

                <span class="badge badge-success p-2">

                    <?= strtoupper(htmlspecialchars($absen['status'])) ?>

                </span>

            </td>

        </tr>

    </table>

</div>

<script>

setTimeout(function(){

    $("#hasil").html("");

    if(typeof loadCamera==="function"){

        loadCamera();

    }

},3000);

</script>

<?php
exit;