<?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">									
                  <h5 class="card-title">Tambah Jadwal Mapel</h5>
					</div>
                      <div class="card-body">
                                <form id="formjadwal" >
                                         <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Tingkat</label>
							                <div class="col-sm-3">
                                             <select name='level' id="level" class='form-select' required='true'>
                                                        <option value=''></option>
                                                        <?php
                                                        $levelQ = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY level");
                                                        while ($level = mysqli_fetch_array($levelQ)) {
                                                            echo "<option value='$level[level]'>$level[level]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                        </div>
										<label  class="col-md-1 col-form-label bold">Kelas</label>
							                <div class="col-sm-3">
                                            <select name='kelas' id="kelas" class='form-select' required='true'>
                                                        
                                                    </select>
                                       </div>
                                        </div>
										 
											 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kode Mapel</label>
								   <div class="col-sm-3">
						            <select name='kode' id="kode" class='form-select' required='true'>
                                                        <option value=''></option>
                                                        <?php
                                                        $mapelQ = mysqli_query($koneksi, "SELECT * FROM mata_pelajaran");
                                                        while ($mapel = mysqli_fetch_array($mapelQ)) {
                                                            echo "<option value='$mapel[kode_mapel]'>$mapel[kode_mapel]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                       </div>
                                <label  class="col-md-1 col-form-label bold">Mapel</label>
							      <div class="col-sm-5">
						             <select name='mapel' id="mapel" class='form-select' required='true'>
                                                       
                                                    </select>
						         </div>
						             </div>
											 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Hari</label>
								   <div class="col-sm-9">
                                            <select name='hari' class='form-select' style='width:100%' required>
                                                                <option value='' >--Pilih Hari--</option>
                                                                <?php
														$queryh = mysqli_query($koneksi, "select * from m_hari");
														while ($hari = mysqli_fetch_array($queryh)) {
																					?>
															<option value="<?= $hari['inggris'] ?>"><?= $hari['hari'] ?></option>
														<?php } ?>
															</select>
                                        </div>
										 </div>
										  <div class="row mb-2">
									 <label  class="col-md-3 col-form-label bold">Jam Pelajaran Ke - Sampai</label>
							      <div class="col-sm-9">
                                     <select name='ke[]' class='form-select select2' style='width:100%' multiple required='true'>
                                                                        <option value=''></option>
                                                                        <?php
														for($i=1; $i<=15;$i++){
																					?>
															<option value="<?= $i ?>"><?= $i ?></option>
														<?php } ?>
                                                                       
                                                                    </select>
                                        </div>
									 </div>
									
                                        <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Dari Jam</label> 
						                  <div class="col-md-3">
                                                <input type="text"  name="dari" class="form-control" required>
                                                </div>
												 <label  class="col-md-1 col-form-label bold">Sampai</label>
						                             <div class="col-sm-3">
                                                   <input type="text"  name="sampai" class="form-control" required>
                                                </div>
												 </div>
												  <div class="row mb-2">
										             <label  class="col-md-3 col-form-label bold">Guru Mapel</label> 
						                             <div class="col-sm-9">
                                                    <select name='guru' class='form-select' required='true'>
                                                        <option value=''></option>
                                                        <?php
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM users WHERE level='guru'");
                                                        while ($ekstra = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$ekstra[id_user]'>$ekstra[nama]</option>";
                                                        }
                                                        ?>
													 
                                                    </select>
                                                </div>												 
												  </div>
									
									<div class='modal-footer'>
								   <div class='box-tools pull-right btn-group'>
									  <button type='submit'  class='btn btn-outline-primary'><i class='fa fa-check-circle'></i>Simpan</button>
										 
											</div>
											 </div>
								 
                                </form>
                            </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>
  $('#formjadwal').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
             url: 'sandik_ptm/crud_jadwal.php?pg=tambah',
            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: 'Jadwal berhasil disimpan',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'	
                    });
                    setTimeout(function() {
                        window.location.replace('?pg=jadwalmapel');
                    }, 2000);
                } else {
                iziToast.info(
            {
                title: 'Gagal!',
                message: 'Jadwal sudah ada',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'	
                    });
                }

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