<?php
require("../config/koneksi.php");
require("../config/function.php");
require("../config/crud.php");
if (isset($_POST['Get_Fingerid'])) {
		$data = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM datafinger WHERE sts='0'"));
	$output = 
    [
        "Detail" =>
        [
            
            "Data User" => $data['nama'],
            "Data ID" => $data['idjari'],
			 "Data Serial" => $data['serialnumber']
        ]
    ];
    $json = json_encode($output);
    echo $json;
	
	mysqli_query($koneksi,"UPDATE datafinger SET sts='1' WHERE idjari='$data[idjari]'");
}
if (isset($_POST['Hapus_id'])) {
	$data = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM temp_finger ORDER BY id DESC LIMIT 1"));
	$output = 
    [
        "Detail" =>
        [
            
            "Data User" => $data['nama'],
            "Data ID" => $data['idjari'],
			 "Data Serial" => $data['serial']
        ]
    ];
    $json = json_encode($output);
    echo $json;
	 delete($koneksi, 'temp_finger', ['idjari' => $data['idjari']]);
	
}

if (isset($_POST['uid'])) {
	$idj = $_POST['uid'];
	
	
$status = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM status"));	
$query = mysqli_query($koneksi, "select * from datafinger where idjari='$idj'");
$cek = mysqli_num_rows($query);
$data = mysqli_fetch_array($query);
$nama = $data['nama'];
$tanggal = date('Y-m-d');
$jam = date('H:i:s');
$bulan = date('m');
$tahun    = date('Y');
if ($cek ==0) {
	echo "TIDAK TERDAFTAR";
	}else{
     echo $nama;	
        $peg = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM users WHERE idjari='$data[idjari]'"));
		$siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM siswa WHERE idjari='$data[idjari]'"));
		$kelas = $siswa['kelas'];
        $cari_absen = mysqli_query($koneksi, "select * from absensi where nokartu='$idj' and tanggal='$tanggal'");
		$jumlah_absen = mysqli_num_rows($cari_absen);
		if($status['mode']=='1' AND $jumlah_absen==0):
			if($data['level']=='pegawai'){
				
			mysqli_query($koneksi, "insert into absensi(nokartu,tanggal,idguru, masuk, ket, bulan,tahun,level)values('$idj','$tanggal', '$peg[id_user]', '$jam','H', '$bulan','$tahun','$data[level]')");		
			}else{
				 $koneksi->query("INSERT INTO  absensi(nokartu,tanggal,idsiswa,kelas,masuk,ket,bulan,tahun,level)values('$idj','$tanggal', '$siswa[id_siswa]', '$kelas', '$jam','H', '$bulan','$tahun','$data[level]')");			
				
			}
		endif;
		if($status['mode']=='2'):
			mysqli_query($koneksi, "update absensi set pulang='$jam' where nokartu='$idj' and tanggal='$tanggal'");
			endif;
	}
  }
?>