<?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-image"></i> Setting Background</h5></div>    
			  
			
				 <div class='card-body'>
                   <form id="formtujuan" action="" >
                    <div class='modal-body'>
                        <div class="row">
                            
                                
                                <div class='form-group-sm'>
                                    <label class="bold">Tingkat</label>
									<div class="input-group">
                                     <select id='level' name='level' class='form-select' required='true'>
                                      <option value=''>Pilih Tingkat</option>
                                       <?php $quem = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY level"); ?>
                                                <?php while ($m = mysqli_fetch_array($quem)) : ?>
                                                    <option value="<?= $m['level'] ?>"><?= $m['level'] ?></option>
                                                <?php endwhile ?>
                                                    </select>    
                                <span class="input-group-btn">
						   <button type='submit' name='submit' class='btn btn-outline-primary' style="margin-right:18px"><i class='fa fa-check-circle' ></i><span> Simpan</span></button>
                          </span>
                    </div>
					  </div>
					<div class="form-group-sm">
                                    <label class="bold">File Background</label>
                                    <input type="file" class="form-control" name="file" autocomplete="off" required>

                                </div>                
                         </form>
					</div>
            </div>
        </div>
    </div>
</div>

<script>
	 $('#formtujuan').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
            url: 'sandik_karpel/crud.php?pg=karpel',
            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>
        <div class='col-md-7'>
		 <div class="card">
             <div class="card-header">
                    <h5 class='card-title'>Data Background Karpel</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>Tingkat</th>
                                    <th>Background</th>  
									<th width="5%"></th>

										</tr>
									</thead>							
							<tbody>							
                                <?php
								$no = 0;
								
								 $query = mysqli_query($koneksi, "select * from karpel");
                            while ($tjn = mysqli_fetch_array($query)) {
							
                                $no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
							        <td><?= $tjn['level'] ?></td>
									<td ><img src="../dist/img/karpel/<?= $tjn['gambar'] ?>" width="100px"></td>
									
									<td>
									 <button data-id="<?= $tjn['id'] ?>" 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_karpel/crud.php?pg=hapus',
                    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>