 <?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
$jfile = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM file_pendukung"));
?>

 <div class='row'>
        <div class='col-md-8'>
            <div class="card">
              <div class="card-header">	
					 <h5 class='card-title'> File Pendukung Soal </h5>
                </div>
				
			
                <div class='card-body'>
                    <div class='table-responsive'>
                        <table  id='datatable1' class='table  table-hover'>
                            <thead>
                            <tr>
                              
                                    <th width='3px'>No</th>
									<th>Kode_Mapel</th>
                                    <th>File</th>
                                    <th>Nama File</th>
                                    <th class="text-center">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $query = mysqli_query($koneksi, "select * from file_pendukung");
                            $no = 0;
                            while ($filex = mysqli_fetch_array($query)) {
							$mapel = fetch($koneksi, 'banksoal', ['id_bank' => $filex['id_bank']]);
                                $no++;
                              
                            ?>
                                <tr>
                                    <td><?= $no; ?></td>
                                    <td><?= $mapel['kode'] ?></td>
									 <td><?= "<img src='../files/" . $filex['nama_file'] . "' width='50'>" ?></td>
									 <td><?= $filex['nama_file'] ?></td>
									<td class="text-center">
									 <button data-id="<?= $filex['id_file'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
									 </td>
								  
									  </tr>
                            <?php }
                            ?>


                        </tbody>
                    </table>
                </div>
            </div>
        </div>
		</div>
		<div class='col-md-4'>
           <div class="card">
              <div class="card-header">	
					 <h5 class='card-title'> File Pendukung Soal </h5>
                </div>
                <div class='card-body'>
				<?php if($jfile<>0){ ?>
					 <form id="formzip" action=''>
					 <center>
					
					 <b style="color:red"> Data ini akan digunakan untuk sinkron kirim dan tarik soal bergambar</b>
					 </center>
					<div class="d-grid gap-2">
                    <button name="submit3" type="submit" class="btn btn-outline-success"> Buat File Zip</button>  
                       </div>
                     </form>	
					 
				<?php }else{ ?>
                  <h5 class="card-title text-center" style="color:red;"><i class="fas fa-image fa-fw"></i> Tidak Ada File Pendukung</h5>
                <?php } ?>	
                                 <br>
                       <form id="formgambar" action=''>								 
                    			
					<div class="d-grid gap-2">
                  <button name="submit" type="submit" class="btn btn-outline-danger"> Hapus Semua Gambar</button>  	
                   </div>

					</form>
					<p>
						<div class="d-grid gap-2">
						 <button id="btnrestore" class='btn btn-outline-primary btn-block' data-placement="top" data-toggle="tooltip" data-original-title="Restore">Restore File</button>					
						</div>
						</div>
					  
    </div>
</div>
 <script>
 
    $('#formzip').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '<?= $homeurl ?>/zip.php',
            data: $(this).serialize(),
            success: function(data) {
                console.log(data);
              if (data == 'OK') {
                 iziToast.info({
			title: 'Sukses!',
			message: 'File 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: 'Sukses!',
			message: 'File 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>
<script>
 $('#datatable1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        Swal.fire({
				  title: 'Are you sure?',
				  text: "You won't be able to revert this!",
				  type: 'warning',
				  showCancelButton: true,
				  confirmButtonColor: '#3085d6',
				  cancelButtonColor: '#d33',
				  confirmButtonText: 'Yes, delete it!'	
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_file/hapus_file.php',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                 iziToast.info({
			title: 'Sukses!',
			message: 'File 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>
<script>
 
    $('#formgambar').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
            url: 'server/hapusgambar.php',
            enctype: 'multipart/form-data',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
             iziToast.info({
			title: 'Sukses!',
			message: 'File 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>
<script>
   $("#btnrestore").click(function() {
        var id = $(this).data('id');
        swal({
            title: 'Konfirmasi ',
            text: 'Akan mengembalikan File Pendukung ??',
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Ya, Restore!'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: '<?= $homeurl ?>/restorefile.php',
                    data: "id=" + id,
                    type: "POST",
                    success: function(respon) {
                        iziToast.info(
            {
                title: 'Sukses!',
                message: 'Files berhasil di restore',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'		  
                });
                        setTimeout(function() {
                            window.location.reload();
                        }, 2000);
                    }
                });
            }
            return false;
        })

    });
</script>