<?php
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
include "../../assets/phpqrcode/qrlib.php";
(isset($_GET['pg'])) ? $pg = $_GET['pg'] : $pg = '';

if ($pg == 'kategori') {
  $where = [
			'kode' => $_POST['kode']				 
            ];
    
	$data = [
			'kode' => $_POST['kode'],
			'kategori'=>$_POST['kategori']
            ];	
	 $cek = rowcount($koneksi, 'buku_kategori', $where);
            if ($cek == 0) {
            $exec = insert($koneksi, 'buku_kategori', $data);
            echo "OK";
        }
}
if ($pg == 'rak') {
  $where = [
			'nama_rak' => $_POST['rak'],
           'nomor_box' => $_POST['nomor']			
            ];
    
	$data = [
			'nama_rak' => $_POST['rak'],
			'nomor_box'=>$_POST['nomor']
			  
            ];	
	 $cek = rowcount($koneksi, 'buku_rak', $where);
            if ($cek == 0) {
            $exec = insert($koneksi, 'buku_rak', $data);
            echo "OK";
        }
}


if ($pg == 'box') {
    $idrak = $_POST['rak'];
    $sql = mysqli_query($koneksi, "SELECT * FROM buku_rak WHERE nama_rak='" . $idrak . "'");
    echo "<option value=''>Pilih Nomor Box</option>";
    while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[id_rak]'>$data[nomor_box]</option>";
    }
}
if ($pg == 'hapuskategori') {
    $id = $_POST['id'];
    delete($koneksi, 'buku_kategori', ['id_kategori' => $id]);
	 delete($koneksi, 'buku', ['idk' => $id]);
}

if ($pg == 'hapusrak') {
    $id = $_POST['id'];
    delete($koneksi, 'buku_rak', ['id_rak' => $id]);
	 delete($koneksi, 'buku', ['idrak' => $id]);
}
if ($pg == 'hapuspinjam') {
    $id = $_POST['id'];
    delete($koneksi, 'buku_pinjam', ['id_pinjam' => $id]);

}
if ($pg == 'hapusbuku') {
    $id = $_POST['id'];
	$query = "SELECT * FROM buku WHERE id_buku='".$id."'";
		$sql = mysqli_query($koneksi, $query); 
		$data = mysqli_fetch_array($sql);
    delete($koneksi, 'buku', ['id_buku' => $id]);

	unlink("../../temp/buku/".$data['judul'].'.png');
}
if ($pg == 'kembali') {
	$data =[
	'tgl_kembali'=>date('Y-m-d'),
	'status'=>0
	];
    $id = $_POST['id'];
    update($koneksi, 'buku_pinjam',$data, ['id_pinjam' => $id]);

}
if ($pg == 'tambah') {
	$tempdir = "../../temp/buku/"; 
if (!file_exists($tempdir)) 
    mkdir($tempdir);
 $where = [
			'idk' => $_POST['idk'],
			'idrak'=>$_POST['nomor'],
			  'judul'=>$_POST['judul']		
            ];
    
	$data = [
			'idk' => $_POST['idk'],
			'idrak'=>$_POST['nomor'],
			  'judul'=>$_POST['judul'],
			  'pengarang'=>$_POST['pengarang'],
			  'penerbit'=>$_POST['penerbit'],
			  'jumlah'=>$_POST['jumlah'],
			  'status'=>$_POST['status']
            ];	
	 $cek = rowcount($koneksi, 'buku', $where);
            if ($cek == 0) {
            $exec = insert($koneksi, 'buku', $data);
            echo "OK";
			
$codeContents = $_POST['judul'];
QRcode::png($codeContents, $tempdir . $_POST['judul'] . '.png', QR_ECLEVEL_M, 4);
        }	
		

}
if ($pg == 'ambiljudul') {
    $idk = $_POST['idk'];
    $sql = mysqli_query($koneksi, "SELECT * FROM buku WHERE idk='" . $idk . "'");
    echo "<option value=''>Pilih Judul</option>";
    while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[id_buku]'>$data[judul]</option>";
    }
}
if ($pg == 'ambilsiswa') {
    $kelas = $_POST['kelas'];
    $sql = mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_kelas='" . $kelas . "'");
    echo "<option value=''>Pilih Siswa</option>";
    while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[id_siswa]'>$data[nama]</option>";
    }
}
if ($pg == 'pinjaman') {
	$tanggal= date('Y-m-d');
 $where = [
			'idsiswa' => $_POST['siswa'],
			'idbuku'=>$_POST['judul'],
			  'tgl_pinjam'=>$tanggal		
            ];
    
	$data = [
			'kelas' => $_POST['kelas'],
			'idsiswa' => $_POST['siswa'],
			'idbuku'=>$_POST['judul'],
			  'tgl_pinjam'=>$tanggal,
			  'jumlah'=>$_POST['jumlah']
            ];	
	 $cek = rowcount($koneksi, 'buku_pinjam', $where);
            if ($cek == 0) {
            $exec = insert($koneksi, 'buku_pinjam', $data);
            echo "OK";
        }	
}