<?php 

// ============================
// KONEKSI DATABASE (FIX)
// ============================
$koneksi = mysqli_connect('localhost','root','','absenKaryawan');

if (!$koneksi) {
    die("Koneksi database gagal : " . mysqli_connect_error());
}

/* =====================================================
   ================= ADMIN SECTION =====================
===================================================== */

function panggil_karyawan()
{
	global $koneksi;
	$id  = $_SESSION['idabsen2'];
	return mysqli_query($koneksi, "SELECT * FROM tb_karyawan WHERE id='$id'");
}

function select_admin()
{
	global $koneksi;
	return mysqli_query($koneksi, "SELECT * FROM tb_admin ORDER BY id DESC");
}

function select_admin_2()
{
	global $koneksi;
	$select = mysqli_query($koneksi, "SELECT count(id) AS jadmin FROM tb_admin");
	$r = mysqli_fetch_array($select);
	echo $r['jadmin'];
}


/* =====================================================
   ================= ABSEN SECTION =====================
===================================================== */

function simpan_absen()
{
	global $koneksi;

	date_default_timezone_set("Asia/Jakarta");

	$nip   = $_POST['nip'] ?? '';
	$nama  = $_POST['nama'] ?? '';
	$shift = $_POST['shift'] ?? '';
	$keterangan = $_POST['keterangan'] ?? 'Hadir';
	$alasan = $_POST['alasan'] ?? '';

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

	if ($nip == '' || $nama == '' || $shift == '') {
		echo "<script>alert('Data tidak lengkap!');window.history.back();</script>";
		return;
	}

	// Cek sudah absen
	$cek = mysqli_query($koneksi,
		"SELECT * FROM tb_absen 
		 WHERE nip='$nip' AND tanggal='$tanggal'"
	);

	if (mysqli_num_rows($cek) > 0) {
		echo "<script>alert('Anda sudah absen hari ini!');window.location='index.php?m=awal';</script>";
		return;
	}

	// ====================
	// LOGIKA SHIFT & TELAT
	// ====================

	if ($keterangan == "Hadir") {

		if ($shift == "Pagi") {
			$batas = 730;
		} elseif ($shift == "Middle") {
			$batas = 900;
		} elseif ($shift == "Siang") {
			$batas = 1500;
		} elseif ($shift == "Malam") {
			$batas = 2200;
		} else {
			$batas = 730;
		}

		if ($jam2 > $batas) {
			$status = "Telat";
		} else {
			$status = "Hadir";
		}

	} else {
		$status = $keterangan; // Izin / Sakit
	}

	// ====================
	// UPLOAD FOTO
	// ====================

	$foto = '';

	if (!empty($_FILES['foto']['name'])) {

		$allowed_ext = array('png','jpg','jpeg');
		$x = explode(".", $_FILES['foto']['name']);
		$ext = strtolower(end($x));

		if (in_array($ext, $allowed_ext)) {

			$namaFile = time().'-'.$_FILES['foto']['name'];
			$tmp = $_FILES['foto']['tmp_name'];

			if (!is_dir("../admin/img/karyawan")) {
				mkdir("../admin/img/karyawan", 0777, true);
			}

			move_uploaded_file($tmp, "../admin/img/karyawan/".$namaFile);
			$foto = $namaFile;
		}
	}

	// ====================
	// INSERT DATA
	// ====================

	$insert = mysqli_query($koneksi, "
		INSERT INTO tb_absen SET
		nip='$nip',
		nama='$nama',
		tanggal='$tanggal',
		bulan='$bulan',
		tahun='$tahun',
		shift='$shift',
		keterangan='$keterangan',
		kehadiran='$status',
		alasan='$alasan',
		foto='$foto',
		jam='$jam',
		jam2='$jam2'
	");

	if ($insert) {
		echo "<script>
				alert('Absensi berhasil!');
				window.location='index.php?m=awal';
			  </script>";
	} else {
		echo "<script>alert('Gagal menyimpan absensi!');</script>";
	}
}


/* =====================================================
   ================= URL FUNCTION ======================
===================================================== */

function url()
{
	return "http://localhost/absen/assets/";
}

?>
