<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
   <div class='row'>
   <div class='col-md-12'>
      <div class="card">
             <div class="card-header">
                    <div class='right '>
					  <a href="?pg=buatjadwal" class='btn btn-sm btn-success'><i class='fas fa-plus-circle'></i>Jadwal</a>
                  
                    </div>
					<h5 class='card-title'><i class="fas fa-graduation-cap"></i> Jadwal Mapel</h5>
                </div>
						 
                <div class='card-body'>
                    <div class='table-responsive'>
                        <table style="font-size: 12px" id='example1' class='table'>
                            <thead>
                                <tr>
                                    <th width='3px'>No</th>
									<th width='5%'>Tingkat</th>
									  <th width='5%'>Kelas</th>
                                    <th>Nama Mapel</th>  
                                     <th>Guru Mapel</th> 	
									 <th width='10%'>Hari</th>
                                      <th width='5%'>Dari</th>
                                        <th width='5%'>Sampai</th>									  
									<th>Action</th>
                                </tr>
                            </thead>
							
							<tbody>
							
                                <?php
								$no = 0;
								 if ($user['level'] == 'admin') :
								 $query = mysqli_query($koneksi, "select * from jadwal_mapel
								 JOIN users ON users.id_user=jadwal_mapel.guru");
								  elseif ($user['level'] == 'guru') :
								   $query = mysqli_query($koneksi, "select * from jadwal_mapel
								 JOIN users ON users.id_user=jadwal_mapel.guru WHERE  jadwal_mapel.guru='$_SESSION[id_user]' ");
								   endif;
                            while ($mapel = mysqli_fetch_array($query)) {
							$hari=fetch($koneksi,'m_hari',['inggris' =>$mapel['hari']]);
                                $no++;
                            ?>
							 <tr>
                                    <td class="hidden-xs"><?= $no; ?></td>
									 <td><?= $mapel['tingkat'] ?></td>
									 <td><?= $mapel['kelas'] ?></td>
							        <td><?= $mapel['mapel'] ?></td>
									<td><?= $mapel['nama'] ?></td>
									<td><?= $hari['hari'] ?></td>					
									<td><?= $mapel['dari'] ?></td>
									<td><?= $mapel['sampai'] ?></td>
                                   <td style="text-align:center">
                                        <a href="?pg=editjadwal&id=<?= $mapel['id_jadwal'] ?>" class='btn btn-sm btn-outline-primary'><i class='fas fa-edit'></i></a>
										 <button data-id="<?= $mapel['id_jadwal'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-times"></i></button>
                                   </td>
								  
                                     
							       </tr>
                            <?php } ?>
                        </tbody>
                        </table>
                    </div>
                    </div>
					</div>
                     
	 <script>
	  $("#level").change(function() {
        var level = $(this).val();
        console.log(level);
        $.ajax({
            type: "POST", 
            url: "sandik_ptm/crud_jadwal.php?pg=ambil_kelas", 
            data: "level=" + level, 
            success: function(response) { 
                $("#kelas").html(response);
            }
        });
    });
	
	$("#kode").change(function() {
        var kode = $(this).val();
        console.log(kode);
        $.ajax({
            type: "POST", 
            url: "sandik_ptm/crud_jadwal.php?pg=ambil_mapel", 
            data: "kode=" + kode, 
            success: function(response) { 
                $("#mapel").html(response);
            }
        });
    });
</script>

	<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_ptm/crud_jadwal.php?pg=hapus_jadwal',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                   iziToast.info(
            {
                title: 'Sukses!',
                message: 'Jadwal 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>