<?php
require("../config/koneksi.php");
require("../config/function.php");
require("../config/apk.php");
(isset($_GET['pg'])) ? $pg = $_GET['pg'] : $pg = '';
if ($pg == 'login') {
$username = $_POST['username'];
$password = $_POST['password'];

$query = mysqli_query($koneksi, "SELECT * FROM users WHERE username='$username' AND password='$password' ");
$cek = mysqli_num_rows($query);
if ($cek <> 0) {
	$user = mysqli_fetch_array($query);
   echo $user['nama'];
   }
   else{
	echo "0";
}
	
}
if ($pg == 'live') {
	$tanggal= date('Y-m-d');
	$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM absen WHERE tanggal='$tanggal'"));
	if($jumlah >= 1):
	$siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM absen ORDER BY id DESC LIMIT 1"));
	echo $siswa['nama']."#";
	echo $siswa['tanggal']."#";
	echo $siswa['masuk']."#";
	echo $siswa['istirahat']."#";
	echo $siswa['kembali']."#";
	echo $siswa['pulang']."#";
	echo $siswa['foto'];
	else:
	echo "0";
	endif;
}
if ($pg == 'dtsiswa') {
$query = "SELECT * FROM siswa WHERE sts='0' ORDER BY id_siswa ASC LIMIT 1";
$data = mysqli_query($koneksi, $query) or die(mysqli_error($koneksi));
$ResultData = mysqli_num_rows($data);

if ($ResultData > 0) {
$getdata = array();
 while($row =mysqli_fetch_assoc($data))
 {
 $getdata[] = $row;
 }
 echo json_encode($getdata);
} else {
echo "0";
}

}
if($pg == 'absensi'){
	$cari = $_POST['nama'];
$query = "SELECT * FROM siswa where nama like '%".$cari."%' AND idjari<>''";
$data = mysqli_query($koneksi, $query) or die(mysqli_error($koneksi));
$ResultData = mysqli_num_rows($data);

if ($ResultData > 0) {
$getdata = array();
 while($row =mysqli_fetch_assoc($data))
 {
 $getdata[] = $row;
 }
 echo json_encode($getdata);
} else {
echo "0";
}
}
if($pg == 'absen'){
	$tanggal = date('Y-m-d');
	$blth = date('m');
	$tahun = date('Y');
	$jam = date('H:i:s');
	$idj = $_POST['idjari'];
	$ket = $_POST['ket'];
	$siswa = mysqli_fetch_array(mysqli_query($koneksi, "select * from siswa where idjari='$idj'"));	
	$idsiswa = $siswa['id_siswa'];
	$sql = mysqli_query($koneksi, "select * from status");
	$dataQ = mysqli_fetch_array($sql);
	$mode_absen = $dataQ['mode'];
	
			$cari_absen = mysqli_query($koneksi, "select * from absensi where idsiswa='$idsiswa' and tanggal='$tanggal'");			
			$jumlah_absen = mysqli_num_rows($cari_absen);
			if($jumlah_absen == 0){
			mysqli_query($koneksi, "insert into absensi(idsiswa, tanggal, masuk, pulang, ket,bulan,tahun)values('$idsiswa', '$tanggal', '$jam', '$jam', '$ket', '$blth','$tahun')");
			}
			
	 		
}	
?>