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

 <div class="row">
       <div class="col-md-6">
			 <div class="card widget widget-files">
                                    <div class="card-header">
                                        <h5 class="card-title"> KATEGORI PELANGGARAN</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="widget-files-continer" id="hapus">
                                            <ul class="widget-files-list list-unstyled">
											 <?php
												$query = mysqli_query($koneksi, "select * from bk_kategori ORDER BY id DESC");
												$no = 0;
												while ($bk = mysqli_fetch_array($query)) {							
													$no++;                             
													?>
											
                                                <li class="widget-files-list-item" >
                                                    <span class="avatar m-r-sm">
                                                       <img src="../images/user.png" alt="">
                                                    </span>
                                                    <span class="widget-files-list-item-content">
                                                        <h5 class="bold"><?= $bk['kategori'] ?></h5>
                                                        <span class="widget-files-list-item-size"></span>
                                                    </span>
                                                    <a href="?pg=kategori&ac=edit&id=<?= $bk['id'] ?>" class="btn btn-sm btn-primary kanan"><i class="material-icons">edit</i> </a>&nbsp;
                                                      <button  data-id="<?= $bk['id'] ?>" class="hapus btn btn-sm btn-danger kanan"><i class="material-icons">delete</i> </button>
                                                      
                                                </li>
                                               <?php } ?>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>
			  
   
<script>
	 $('#hapus').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: 'cbtserver_bk/crud_bk.php?pg=hapus_kat',
                    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-6'>
           <div class="card">
             <div class="card-header">
                <h5 class='card-title'>
				  TAMBAH KATEGORI
				  </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>						
							   <input type='text' name='kategori'  class='form-control' autocomplete="off" required='true' />
                              </div>
							  <p>
						          <div class='kanan'>
								   <button type='submit' name="submit" id="blockui-3" class='btn btn-primary'>Simpan</button>							
							 </div>
							</form>
							 </div>
						</div>
					</div>
				</div>
			
			<script>
		$('#forminput').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
             url: "cbtserver_bk/crud_bk.php?pg=tambah_kategori", 
            enctype: 'multipart/form-data',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
             
                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_kategori WHERE id='$id'"));
			?>
	<div class='col-md-6'>
           <div class="card">
             <div class="card-header">
                  <h5 class='card-title'>
				 EDIT  KATEGORI
				  </h5>
				  </div>
				  <div class='card-body'>
				 <form  id="formedit"  enctype='multipart/form-data'>  
                          <input type="hidden" name="id" value="<?= $id ?>" >                						
						     <div class="form-group">
							<label class="bold">Kategori</label>
							   <input type='text' name='kategori' value="<?= $bks['kategori'] ?>" class='form-control' autocomplete="off" required='true' />
                                            </div><p>
						           <div class='kanan'>
				   		 <button type='submit' name="submit" id="blockui-3" class='btn btn-primary'>Simpan</button>		
							</div>
							</form>
							 </div>
						</div>
					</div>
				</div>
			</div>	
<script>
		$('#formedit').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
             url: "cbtserver_bk/crud_bk.php?pg=edit_kategori", 
            enctype: 'multipart/form-data',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
             
                setTimeout(function() {
                    window.location.replace('?pg=kategori');
                }, 2000);

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