<?php
/**
 * =====================================================
 * SIMPAS v1.0
 * Modul : Jadwal Absensi
 * File   : simpan.php
 * =====================================================
 */

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

// Ambil data dari form
$hari              = mysqli_real_escape_string($conn, $_POST['hari']);
$jam_masuk         = mysqli_real_escape_string($conn, $_POST['jam_masuk']);
$batas_terlambat   = mysqli_real_escape_string($conn, $_POST['batas_terlambat']);
$jam_pulang        = mysqli_real_escape_string($conn, $_POST['jam_pulang']);
$status            = mysqli_real_escape_string($conn, $_POST['status']);

/*
|--------------------------------------------------------------------------
| Cek Hari Sudah Ada?
|--------------------------------------------------------------------------
*/

$cek = mysqli_query($conn,"
SELECT *
FROM jadwal_absensi
WHERE hari='$hari'
");

if(mysqli_num_rows($cek) > 0){

    echo "
    <script>
        alert('Jadwal hari $hari sudah ada.');
        window.history.back();
    </script>
    ";

    exit;

}

/*
|--------------------------------------------------------------------------
| Simpan Data
|--------------------------------------------------------------------------
*/

$simpan = mysqli_query($conn,"
INSERT INTO jadwal_absensi
(
    hari,
    jam_masuk,
    batas_terlambat,
    jam_pulang,
    status
)
VALUES
(
    '$hari',
    '$jam_masuk',
    '$batas_terlambat',
    '$jam_pulang',
    '$status'
)
");

if($simpan){

    echo "
    <script>
        alert('Jadwal berhasil disimpan.');
        window.location='index.php';
    </script>
    ";

}else{

    echo "
    <script>
        alert('Gagal menyimpan jadwal.');
        window.history.back();
    </script>
    ";

}
?>