<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
$link = 'template/importdatamapel.xlsx';
$total = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM mata_pelajaran"));
?>
<div class='row'>
   
    <div class='col-md-8'>
       <div class="card">
      <div class="card-header">	
			   
                <h5 class='card-title'>Mata Pelajaran</h5>
            </div>
			
            <div class='card-body'>
                <div class='table-responsive'>
                    <table id='datatable1' class='table table-hover'>
                        <thead>
                            <tr>
                                <th width='5px'>#</th>
                                <th>Kode Mapel</th>
                                <th>Mata Pelajaran</th>
								<th width="10%" class="text-center">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $mapelQ = mysqli_query($koneksi, "SELECT * FROM mata_pelajaran ORDER BY nama_mapel ASC"); ?>
                            <?php while ($mapel = mysqli_fetch_array($mapelQ)) : ?>
                                <?php $no++; ?>
                                <tr>
                                    <td><?= $no ?></td>
                                    <td><?= $mapel['kode_mapel'] ?></td>
                                    <td><?= $mapel['nama_mapel'] ?></td>
									 <td class="text-center">
									 <button data-id="<?= $mapel['kode_mapel'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
									 </td>
                                </tr>
                            <?php endwhile ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
     <div class="modal fade" id="tambahmapel" tabindex="-1" aria-labelledby="tambahmapel" aria-hidden="true">
				   <div class="modal-dialog">
					   <div class="modal-content">
						  <div class="modal-header">
							<h5 class="modal-title" id="tambahjadwal">Tambah Mapel</h5>
							   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
									</div>
									  <div class="modal-body">
                    <form id="formmapel" >
                        <div class='form-group-sm'>
                            <label class="bold">Kode Mapel</label>
                            <input type='text' name='kodemapel' class='form-control' required='true' />
                        </div>
                        <div class='form-group-sm'>
                            <label class="bold">Nama Pelajaran</label>
                            <input type='text' name='namamapel' class='form-control' required='true' />
                        </div>
                        <p>
                            <div class='right'>
                                <button type='submit'  class='btn btn-outline-primary'><i class='fa fa-check'></i> Simpan</button>
                          
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
	                   <div class="col-md-4">
					    <div class="card">
                      <div class="card-header">
	                   <div>
                    <button class='btn btn-sm btn-outline-default' data-bs-toggle='modal' data-bs-target='#tambahmapel'><i class='fa fa-plus-circle'></i> Mapel</button>
                     </div> <p>
							 <div class="row">
                                <div class="card widget widget-stats">
                                    <div class="card-body">
									
                                        <div class="widget-stats-container d-flex">
                                            <div class="widget-stats-icon widget-stats-icon-custom">
                                                <i class="fa fa-book fa-2x"></i>
                                            </div>
                                            <div class="widget-stats-content flex-fill">
                                                <span class="widget-stats-title">MATA PELAJARAN</span>
                                                <span class="widget-stats-amount"><?= $total ?></span>
                                                <span class="widget-stats-info"></span>
                                            </div>
                                            
                                        </div>
                                    </div>
                                </div>
                            </div>
<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_master/crud_master.php?pg=hapusmapel',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                 iziToast.info(
            {
                title: 'Sukses!',
                message: 'Mapel 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>
 $('#formmapel').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_master/crud_master.php?pg=tambahmapel',
            data: $(this).serialize(),
            beforeSend: function() {
                $('form button').on("click", function(e) {
                    e.preventDefault();
                });
            },
            success: function(data) {
                console.log(data);
                if (data == 'OK') {
                     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);
                } else {
                     iziToast.info(
            {
                title: 'Gagal!',
                message: 'Kode Mapel sudah ada',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'
                    });
					setTimeout(function() {
                        window.location.reload();
                    }, 2000);
                }

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