<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>


<div class='row'>
        <div class='col-md-7'>
           <div class="card">
             <div class="card-header">
                  <h5 class='box-title'><i class="fas fa-tools"></i> Master <small>Sub Kategori Pelanggaran</small></h5>
				  </div>    
			 
						<div class="card-body">
                   <div class='table-responsive'>
                        <table  id='datatable' class='table  table-hover'>
                            <thead>
                            <tr>                             
                             <th width='5%'>No</th>								                                 
                              <th>Kategori Pelanggaran</th>	
                              <th>Sub Kategori Pelanggaran</th>								  
						       <th width='25%'>Action</th>
                            </tr>							
                        </thead>
                        <tbody>
                            <?php
                            $query = mysqli_query($koneksi, "select * from bk_sub ORDER BY id DESC");
                            $no = 0;
                            while ($bk = mysqli_fetch_array($query)) {
                            $kat=fetch($koneksi,'bk_kategori',['id'=>$bk['id_kat']]);								
                                $no++;                             
                                ?>
                                <tr>
                                    <td><?= $no; ?></td>
									 <td><b><?= $kat['kategori'] ?></b></td>
                                    <td><?= $bk['sub_kategori'] ?></td>									                                    
								   <td>		
									<a href="?pg=subkategori&ac=edit&id=<?= $bk['id'] ?>" class="btn btn-sm btn-outline-success"><i class="fas fa-edit"></i></a>
									 <button data-id="<?= $bk['id'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
									</td>
									</tr>
									<?php } ?>
                        </tbody>
                    </table>
				
            </div>
        </div>
    </div>
</div>
<script>
	 $('#datatable').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_bk/crud_bk.php?pg=hapus_sub',
                    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>	
	<?php if ($ac == ''): ?>
  <div class='col-md-5'>
           <div class="card">
             <div class="card-header">
			  <h5 class='card-title'>Sub Kategori Pelanggaran</h5>
			  </div>
			  <div class='card-body'>    
				  <form  id="forminput" class="form-horizontal" enctype='multipart/form-data'>   
                   		                       						
						     <div class="form-group">
							<label class="bold">Kategori</label>
							<select  class='form-select' name="kategori" required >
							 <option value=""></option>
							   <?php $qtl = mysqli_query($koneksi, "SELECT * FROM bk_kategori"); ?>
                                   <?php while ($bk = mysqli_fetch_array($qtl)) : ?>
                                      <option value="<?= $bk['id'] ?>"><?= $bk['kategori'] ?></option>
                                                <?php endwhile ?>
                                                    </select>   
                                            </div>
						          
							<div class="form-group">
							<label class="bold">Sub Kategori</label>
							   <input type='text' name='sub'  class='form-control' autocomplete="off" required='true' />
                                            </div>
						                 <p>
									  <div class='right'>
				   		 <button type='submit' name="submit"  class='btn btn-sm btn-success'><i class='fa fa-check-circle' ></i>Simpan</button>		
							</div>
							</form>
							 </div>
						</div>
					</div>
				</div>
			</div>
			<script>
		$('#forminput').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
             url: "sandik_bk/crud_bk.php?pg=tambah_sub", 
            enctype: 'multipart/form-data',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
              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);

            }
        });
        return false;
    });
</script>
<?php elseif($ac == 'edit'): ?>	
<?php
			$id = $_GET['id'];
            $bks = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM bk_sub WHERE id='$id'"));
			 $kate =fetch($koneksi,'bk_kategori',['id'=>$bks['id_kat']]);
            
			?>
 <div class='col-md-5'>
            <div class="card">
             <div class="card-header">
                  <h5 class='box-title'>Sub Kategori Pelanggaran</h5>
				  </div>
				<div class='card-body'>   
				<form id="formedit" class="form-horizontal" enctype='multipart/form-data'>   
                   		          <input type="hidden" name="id" value="<?= $id ?>" >              						
						     <div class="form-group">
							<label class="bold">Kategori</label>
							<select  class='form-control' name="kategori" required >
							 <option value="<?= $kate['id'] ?>"><?= $kate['kategori'] ?></option>
							   <?php $qtl = mysqli_query($koneksi, "SELECT * FROM bk_kategori WHERE id='$kate[id]'"); ?>
                                   <?php while ($kateg = mysqli_fetch_array($qtl)) : ?>
                                      <option value="<?= $kateg['id'] ?>"><?= $kateg['kategori'] ?></option>
                                                <?php endwhile ?>
                                                    </select>   
                                            </div>
						            
							<div class="form-group">
							<label class="bold">Sub Kategori</label>
							   <input type='text' name='sub' value="<?= $bks['sub_kategori'] ?>" class='form-control' autocomplete="off" required='true' />
                                            </div>
											<p>
						           <div class='right'>
				   		 <button type='submit' name="submit"  class='btn btn-sm btn-success pull-right'><i class='fa fa-check' ></i><span> Simpan</span></button>		
							</div>
							</form>
							 </div>
						</div>
					</div>
				</div>
			</div>			
			<script>
		$('#formedit').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
             url: "sandik_bk/crud_bk.php?pg=edit_sub", 
            enctype: 'multipart/form-data',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
              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.replace('?pg=subkategori');
                }, 2000);

            }
        });
        return false;
    });
</script>	
			
<?php endif; ?>