<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
   <div class='row'>
        <div class='col-md-5'>
           <div class="card">
             <div class="card-header">	
               <h5 class="card-title"><i class="fas fa-th"></i> Managemen Buku</h5>
			   </div>    
			  
			 
                <div class='card-body'>
                   <form id="formkategori" action="" >
                   
                                <div class='form-group'>
                                    <label class="bold">Kategori Buku</label>
									<div class="input-group">
                                    <select name='idk' id="idk" class='form-select' required='true'>
									<option value=''>Pilih Kategori</option>
                                            <?php
                                            $lQ = mysqli_query($koneksi, "SELECT * FROM buku_kategori");
                                            while ($bk = mysqli_fetch_array($lQ)) {
                                                echo "<option value='$bk[id_kategori]'>$bk[kategori]</option>";
                                            }
                                            ?>
                                        </select>
                                <span class="input-group-btn">
						   <button type='submit' name='submit' class='btn  btn-success'><i class='fa fa-check-circle' ></i>Simpan</button>
                          </span>
                    </div>
					  </div>
					<div class="form-group">
                      <label class="bold">Judul Buku</label>
                       <select name='judul' id="judul" class='form-select' required='true'>
					
                           </select>  
                     </div>
					 <div class="form-group">
                      <label class="bold">Kelas</label>
                       <select name='kelas' id="kelas" class='form-select' required='true'>
					<option value=''>Pilih Kelas</option>
                                            <?php
                                            $siswa = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY id_kelas");
                                            while ($kls = mysqli_fetch_array($siswa)) {
                                                echo "<option value='$kls[id_kelas]'>$kls[id_kelas]</option>";
                                            }
                                            ?>
                                        </select>
                           </select>  
                     </div>
                      <div class="form-group">
                      <label class="bold">Pilih Siswa</label>
                       <select name='siswa' id="siswa" class='form-select' required='true'>
					
                           </select>  
                     </div>
                     <div class="form-group">
                      <label class="bold">Jumlah Pinjam</label>
                      <input type="number" class="form-control" name="jumlah" value="1" autocomplete="off" required>
                     </div>		
                  	<div class="form-group">
                      <label class="bold">Tanggal Pinjam</label>
                      <input type="text" class="form-control" name="tgl" value="<?= date('d-m-Y') ?>" autocomplete="off" readonly>
                     </div>						 
                </form>
            </div>
        </div>
    </div>

 <script>
	 $('#formkategori').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_perpus/crud.php?pg=pinjaman',
            data: $(this).serialize(),
            beforeSend: function() {
                $('form button').on("click", function(e) {
                    e.preventDefault();
                });
            },
            success: function(data) {
                console.log(data);
                if (data == 'OK') {
                    iziToast.info(
					{
					title: 'Sukses!',
					message: 'Data berhasil disimpan',
					titleColor: '#FFFF00',
					messageColor: '#fff',
					backgroundColor: 'rgba(0, 0, 0, 0.5)',
				    progressBarColor: '#FFFF00',
					 position: 'topRight'				  
						});
					setTimeout(function() {
					window.location.reload();
					}, 2000);
					} else {
					 iziToast.info(
					{
					title: 'GAGAL!',
					message: 'Data sudah ada',
					titleColor: '#FFFF00',
					messageColor: '#fff',
					backgroundColor: 'rgba(0, 0, 0, 0.5)',
					 progressBarColor: '#FFFF00',
					 position: 'topRight'							  
						});
						setTimeout(function() {
							window.location.reload();
							}, 2000);
								}

							}
						});
						return false;
					}); 
		$("#idk").change(function() {
        var idk = $(this).val();
        console.log(idk);
        $.ajax({
            type: "POST",
            url: "sandik_perpus/crud.php?pg=ambiljudul", 
            data: "idk=" + idk, 
            success: function(response) { 
                $("#judul").html(response);
            }
        });
    });
	$("#kelas").change(function() {
        var kelas = $(this).val();
        console.log(kelas);
        $.ajax({
            type: "POST",
            url: "sandik_perpus/crud.php?pg=ambilsiswa", 
            data: "kelas=" + kelas, 
            success: function(response) { 
                $("#siswa").html(response);
            }
        });
    });
  </script>
        <div class='col-md-7'>
		 <div class="card">
             <div class="card-header">	
               <h5 class="card-title">Data Peminjam Buku</h5>
                   
				</div>
          <div class='card-body'>
                    <div class='table-responsive'>
                        <table style="font-size: 11px" id='example1' class='table table-hover'>
                            <thead >
                                <tr>
                                    <th width='3px'>No</th>
									  <th>Tgl Pinjam</th>
                                    <th>Judul Buku</th> 
                                      <th>Nama Siswa</th> 
                                      <th>Kelas</th> 										  
                                       <th width="5%">Jml</th> 									   
									<th width="5%"></th>

										</tr>
									</thead>							
							<tbody>							
                                <?php
								$no = 0;
								
								 $query = mysqli_query($koneksi, "select * from buku_pinjam WHERE status='1' ORDER BY id_pinjam DESC");
                            while ($tjn = mysqli_fetch_array($query)) {
							$buku = fetch($koneksi,'buku',['id_buku'=>$tjn['idbuku']]);
							$siswa = fetch($koneksi,'siswa',['id_siswa'=>$tjn['idsiswa']]);
							
                                $no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
							        <td><?= $tjn['tgl_pinjam'] ?></td>
									<td ><?= $buku['judul'] ?></td>
									<td><?= $siswa['nama'] ?></td>
									<td><?= $tjn['kelas'] ?></td>
									<td><?= $tjn['jumlah'] ?></td>
									<td>
									 <button data-id="<?= $tjn['id_pinjam'] ?>" class="hapus btn-sm btn btn-default"><i class="fas fa-trash"></i></button>
									</td>
									</tr>
                            <?php } ?>
                        </tbody>
                        </table>
            </div>
        </div>
    </div>
	<script>
	$('#example1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Are you sure?',
            text: 'Akan menghapus data ini!',
			type:'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'iya, hapus'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_perpus/crud.php?pg=hapuspinjam',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                       iziToast.info(
									{
						title: 'Sukses!',
						message: 'Data berhasil dihapus',
						titleColor: '#FFFF00',
						messageColor: '#fff',
						backgroundColor: 'rgba(0, 0, 0, 0.5)',
						 progressBarColor: '#FFFF00',
						 position: 'topRight'					  
						});
                        setTimeout(function() {
                            window.location.reload();
                        }, 2000);
                    }
                });
            }
            return false;
        })
 });
	</script>