<?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>Pelanggaran</small></h5>
				  </div>    
			
				<div class="card-body">
                   <div class='table-responsive'>
                        <table style="font-size: 12px" id='example1' class='table  table-hover'>
                            <thead>
                            <tr>                             
                             <th width='5%'>No</th>								                                 
                              <th>Kategori</th>	
                              <th>Sub Kategori</th>	
                               <th>Pelanggaran</th>
                               <th width='5%'>Poin</th>								   
						       <th width='12%'>Action</th>
                            </tr>							
                        </thead>
                        <tbody>
                            <?php
                            $query = mysqli_query($koneksi, "select * from bk_pelanggaran ORDER BY id DESC");
                            $no = 0;
                            while ($bk = mysqli_fetch_array($query)) {
                            $sub=fetch($koneksi,'bk_sub',['id'=>$bk['idsub']]);		
                             $kat=fetch($koneksi,'bk_kategori',['id'=>$sub['id_kat']]);									
                                $no++;                             
                                ?>
                                <tr>
                                    <td><?= $no; ?></td>
									 <td><b><?= $kat['kategori'] ?></b></td>
                                    <td><?= $sub['sub_kategori'] ?></td>	
									<td><?= $bk['pelanggaran'] ?></td>
									<td><b><?= $bk['poin'] ?></b></td>
								   <td>		
								    <a href="?pg=pelanggaran&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>
	 $('#example1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Are you sure?',
            text: 'Akan menghapus data ini!',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'iya, hapus'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                   url: 'sandik_bk/crud_bk.php?pg=hapus_pelanggaran',
                    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='box-title'>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" id="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>
									  <select  class='form-select' name="sub" id="sub" required >
									  
									   </select>   
                                            </div>
						            
							<div class="form-group">
							<label class="bold">Pelanggaran</label>
								<textarea name="pelanggaran" rows="5" class="form-control" required /></textarea>
							</div>
						          
							<div class="form-group">
							<label class="bold">Poin</label>
								<input type="number" name="poin" maxlength="3" class="form-control" required />
						            </div>
									<p>
									<div class='right'>
				   		 <button type='submit'  class='btn btn-sm btn-success'><i class='fa fa-check' ></i>Simpan</button>		
							</div>
							</form>
							 </div>
						</div>
					</div>
				</div>
			</div>
			<script>
              $("#kategori").change(function() {
            var kategori = $(this).val();
            console.log(kategori);
            $.ajax({
                type: "POST",
                url: "sandik_bk/crud_bk.php?pg=ambil_sub", 
                data: "kategori=" + kategori, 
                success: function(response) { 
                    $("#sub").html(response);
                    console.log(response);
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        });
		</script>
		<script>
		$('#forminput').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
             url: "sandik_bk/crud_bk.php?pg=simpanpel", 
            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=fetch($koneksi,'bk_pelanggaran',['id'=>$id]);
?>
 <div class='col-md-5'>
            <div class="card">
             <div class="card-header">
			  <h5 class='card-title'>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">Pelanggaran</label>
								<textarea name="pelanggaran" rows="5" class="form-control" required /><?= $bks['pelanggaran'] ?></textarea>
							</div>
						            
							<div class="form-group">
							<label class="bold">Poin</label>
								<input type="number" name="poin" value="<?= $bks['poin'] ?>"  maxlength="3" class="form-control" required />
							</div>
							<p>
						   <div class='right'>
				   		 <button type='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=editpel", 
            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=pelanggaran');
                }, 2000);

            }
        });
        return false;
    });
</script>



<?php endif; ?>