<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
<?php
$total = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jenis WHERE status='aktif'"));
?>
<div class="row">
   <div class="col-md-8">
	<div class="card">
      <div class="card-header">	
       
         <h5 class="card-title">Data Jenis Ujian</h5>
			</div>
                <div class="card-body">
				 <div class='table-responsive'>
                    <table id='datatable1' class='table table-hover'>
                        <thead>
                            <tr>
                            <th>Kode Ujian</th>
                            <th>Nama Ujian</th>
                            <th>Status</th>
							<th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $adminQ = mysqli_query($koneksi, "SELECT * FROM jenis ORDER BY id_jenis ASC"); ?>
                        <?php while ($adm = mysqli_fetch_array($adminQ)) : ?>
                            <?php $no++; ?>
                            <tr>
                                <td><?= $adm['id_jenis'] ?></td>
                                <td><?= $adm['nama'] ?></td>
                                <td><?= $adm['status'] ?></td>
								<td class="text-center">
								<button type="button" class="btn btn-outline-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modal-edit<?= $no ?>">
														<i class="fas fa-edit"></i></button>
									 <button data-id="<?= $adm['id_jenis'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
								
								</td>
								<div class="modal fade" id="modal-edit<?= $no ?>" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
								   <div class="modal-dialog">
									   <div class="modal-content">
										  <div class="modal-header">
											<h5 class="modal-title" id="tambahjadwal">Edit Jenis Ujian</h5>
											   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
													</div>
													  <div class="modal-body">
														<form id="form-edit<?= $no ?>" class="form-horizontal">
														
														<div class="form-group">
														<label class="bold">Kode Ujian</label>
														<div class="col-sm-6">
															<input type='text' name='idujian' value="<?= $adm['id_jenis'] ?>" class='form-control' readonly />
														</div>
													</div>
													
													<div class="form-group">
														<label class="bold">Nama Ujian</label>
														<div class="col-sm-12">
															 <input type='text' name='nama' value="<?= $adm['nama'] ?>" class='form-control' required='true' />
														</div>
													</div>	
													
													<div class="form-group">
														<label class="bold">Status</label>
														<div class="col-sm-12">
															<select class="form-control" name="status" required>
														<option value="<?= $adm['status'] ?>"><?= $adm['status'] ?></option>
														<option value="tidak">Tidak</option>
														<option value="aktif">Aktif</option>
													   </select>
														</div>
													</div>	
													<label class="col-sm-12 control-label"></label><br>
													<button type='submit'  class='btn right btn-outline-primary'><i class='fa fa-check'></i><span> Simpan</span></button>
												</div>
											 </form>	
													</div>
													</div>
													</div>										

														</tr>
							                 <script>
											$('#form-edit<?= $no ?>').submit(function(e) {
												e.preventDefault();
												$.ajax({
													type: 'POST',
													url: 'sandik_ujian/crud.php?pg=ubahujian',
													data: $(this).serialize(),
													success: function(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();
															}, 1000);
															$('#modal-edit<?= $no ?>').modal('hide');
														}
														else {
															iziToast.info({
																title: 'GAGAL!',
																message: 'Data gagal disimpan',
																titleColor: '#FFFF00',
																messageColor: '#fff',
																backgroundColor: 'rgba(0, 0, 0, 0.5)',
																progressBarColor: '#FFFF00',
																position: 'topRight'
															});
															setTimeout(function() {
																window.location.reload();
															}, 1000);
														}

													}
												});
												return false;
											});
											</script>
											
											<?php endwhile; ?>
											</tbody>
										</table>
									</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='#tambahujian'><i class='fa fa-plus-circle'></i> Ujian</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-wifi fa-2x"></i>
                                            </div>
                                            <div class="widget-stats-content flex-fill">
                                                <span class="widget-stats-title">JENIS UJIAN AKTIF</span>
                                                <span class="widget-stats-amount"><?= $total ?></span>
                                                <span class="widget-stats-info"></span>
                                            </div>
                                            
                                        </div>
                                    </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_ujian/crud.php?pg=hapusujian',
										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>
		 <div class="modal fade" id="tambahujian" tabindex="-1" aria-labelledby="tambahujian" aria-hidden="true">
				   <div class="modal-dialog">
					   <div class="modal-content">
						  <div class="modal-header">
							<h5 class="modal-title" id="tambahjadwal">Tambah Jenis Ujian</h5>
							   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
									</div>
									  <div class="modal-body">
                        <form id="formujian" >             
						<div class="form-group">
							<label class="bold">Kode Ujian</label>
							<div class="col-sm-6">
								<input type='text' name='idujian' class='form-control' required='true' />
							</div>
						</div>
						
						<div class="form-group">
							<label class="bold">Nama Ujian</label>
							<div class="col-sm-12">
								 <input type='text' name='nama' class='form-control' required='true' />
							</div>
						</div>						
						<label class="col-sm-12 control-label"></label><br>
						<button type='submit' name='submit' class='btn  right btn-outline-primary'><i class='fa fa-check'></i><span> Simpan</span></button>
					</div>
				</form>
                </div>
            </div>
        </div>
    </div>
    <script>
 $('#formujian').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_ujian/crud.php?pg=tambahujian',
            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>