<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>

 <div class='row'>
 <div class='col-md-8'>
   <div class="card">
             <div class="card-header">		
                  <h5 class='card-title'><i class="fas fa-graduation-cap"></i> Ekstrakurikuler</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='5px'>
                                    #
                                </th>
                                <th>Ekstrakurikuler</th>
								<th width="30%">Guru Pembimbing</th>
                                <th width="5%">Action</th>
                                
                            </tr>
						
                        </thead>
                        <tbody>
						
                            <?php
							
							$tg=date('Y-m-d');
                            $query = mysqli_query($koneksi, "select * from m_eskul");
                            $no = 0; ?>
                           <?php while ($jadwal = mysqli_fetch_array($query)) {
								$guru=fetch($koneksi,'users',['id_user' =>$jadwal['idguru']]);
                                $no++;                             
                            ?>							
                                <tr>
                                    <td><?= $no; ?></td>
                                    <td><?= $jadwal['ekstra'] ?></td>
									 <td><?= $guru['nama'] ?></td>
                                   
                                    <td>
									 <button data-id="<?= $jadwal['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>
		 
           <div class='col-md-4'>
            <div class="card">
             <div class="card-header">	
                  <h5 class='card-title'><i class="fas fa-graduation-cap"></i> Input Eskul</h5>
               </div>
			
                       <div class='card-body'>             
					 <form id="formtujuan" action=''>
                  
									  <div class="col-md-12">
                                                   <label class="bold">Nama Eskul</label>
                                                <input type="text" name="eskul" class="form-control" required="true" >   
                                                </div>
											<div class="col-md-12">
                                                    <label class="bold">Guru Pembimbing</label>
                                                   <select id='guru' name='guru' class='form-select' style='width:100%' required>                                                  
													 <?php
													 
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM users WHERE level<>'admin' ");
													
                                                        while ($ekstra = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$ekstra[id_user]'>$ekstra[nama]</option>";
                                                        }
                                                        ?>
													
                                                    </select>
                                                </div>
												<p>
						<div class="right">
					   <button type='submit' name='submit' class='btn  btn-outline-success'><i class='fa fa-check-circle' ></i> Simpan</button>
                                     
					 </div>
                                    </form>
                               
                        </div>
                    </div>
                </div>
            </div>			
<script>
	 $('#formtujuan').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_rapor/crud_mapel.php?pg=tambah_eskul',
            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: 'AGAL!',
                message: 'Data 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;
    });
	
	 $('#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_rapor/crud_mapel.php?pg=hapus_eskul',
                    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>
	