<?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' 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>
                      <input type="text" class="form-control" name="judul" autocomplete="off" required>
                     </div>
                      <div class="form-group">
                      <label class="bold">Pengarang</label>
                      <input type="text" class="form-control" name="pengarang" autocomplete="off" required>
					    </div>
					  <div class="form-group">
                      <label class="bold">Penerbit</label>
                      <input type="text" class="form-control" name="penerbit" autocomplete="off" required>
                     </div>
                      <div class='form-group'>
                         <label class="bold">Rak Buku</label>
                           <select name='rak' id="rak" class='form-select' required='true'>
						<option value=''>Pilih Rak</option>
                              <?php
                           $rakQ = mysqli_query($koneksi, "SELECT * FROM buku_rak GROUP BY nama_rak");
                             while ($rak = mysqli_fetch_array($rakQ)) {
                                 echo "<option value='$rak[nama_rak]'>$rak[nama_rak]</option>";
                                            }
                                     ?>
                           </select>   
                    </div>		

                    <div class='form-group'>
                         <label class="bold">Nomor Box</label>
                           <select name='nomor' id="nomor" class='form-select' required='true'>
					
                           </select>   
                    </div>	
                     <div class="form-group">
                      <label class="bold">Jumlah Buku</label>
                      <input type="number" class="form-control" name="jumlah" autocomplete="off" required>
                     </div>		
                  <div class='form-group'>
                         <label class="bold">Status Buku</label>
                           <select name='status' class='form-select' required='true'>
						<option value='1'>ADA</option>
                          <option value='2'>DIPINJAM</option>
                           </select>   
                    </div>							 
                </form>
            </div>
        </div>
    </div>

 <script>
	 $('#formkategori').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_perpus/crud.php?pg=tambah',
            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;
					}); 
					 $("#rak").change(function() {
        var rak = $(this).val();
        console.log(rak);
        $.ajax({
            type: "POST",
            url: "sandik_perpus/crud.php?pg=box", 
            data: "rak=" + rak, 
            success: function(response) { 
                $("#nomor").html(response);
            }
        });
    });
  </script>
        <div class='col-md-7'>
		 <div class="card">
             <div class="card-header">	
               <h5 class="card-title">Data 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>Rak Buku</th>
                                    <th width="5%">No Box</th> 
                                      <th>Judul Buku</th> 	
                                       <th width="5%">Jml</th> 
                                      <th width="5%">Pinjam</th> 
                                      <th width="5%">Sisa</th> 										  
									<th width="5%"></th>

										</tr>
									</thead>							
							<tbody>							
                                <?php
								$no = 0;
								
								 $query = mysqli_query($koneksi, "select * from buku ORDER BY id_buku DESC");
                            while ($tjn = mysqli_fetch_array($query)) {
							$rak = fetch($koneksi,'buku_rak',['id_rak'=>$tjn['idrak']]);
                             $klr = mysqli_fetch_array(mysqli_query($koneksi, "SELECT SUM(jumlah) AS total FROM buku_pinjam  WHERE idbuku='$tjn[id_buku]'"));  
							   $no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
							        <td><?= $rak['nama_rak'] ?></td>
									<td ><?= $rak['nomor_box'] ?></td>
									<td><?= $tjn['judul'] ?></td>
									<td><?= $tjn['jumlah'] ?></td>
									<td><?= $klr['total'] ?></td>
									<td><?= $tjn['jumlah']-$klr['total'] ?></td>
									<td>
									 <button data-id="<?= $tjn['id_buku'] ?>" 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=hapusbuku',
                    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>