<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
   <div class='row'>
   <div class='col-md-12'>
             <div class='panel panel-default'>
               <div class="panel-heading" style="height:45px">
                    <div class='box-tools pull-right '>
					 
                    </div>
					<h4 class='box-title'><i class="fas fa-graduation-cap"></i> Jadwal Mapel</h4>
                </div>
				<div class='box-header with-border '>
			   <div class='box-tools pull-right '>
			   <table>
			   <tr>
			   <td>
			   <div class="form-group-sm">
			  <select name='jenjang' class='form-control' >
              <option value=""><?= $setting['sekolah'] ?></option>
               </select>
			   </div>
				</td>
			   <td>
			   <div class="form-group-sm">
			  <select name='jenjang' class='form-control' >
              <option value=""><?= $setting['jenjang'] ?></option>
               </select>
			   </div>
				</td>
				<td>
				<div class="form-group-sm">
			  <select name='jenjang' class='form-control'>
               <option value=""><?= $setting['kota'] ?></option>
               </select>
			   </div>
				</td>
				<td>
				<div class="form-group-sm">
			  <select name='jenjang' class='form-control' >
               <option value=""><?= $setting['kecamatan'] ?></option>
               </select>
			   </div>
				</td>
				</tr>					
			</table>
			</div>
			 <h4 class='box-title'></h4>
			  </div>
			  <div class="box-body">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true" >Menu E Rapor</a></li>
                    </ul>
					<div class="tab-content">
                    <div class="tab-pane active" id="tab_1" >
				<div class="panel panel-default">
				 <div class='box-body'> 
        <div class='col-md-12'>
            <div class='box box-solid '>
                <div class='box-header with-border '> 
                    <h4 class='box-title'>Jadwal Mengajar</h4>
                    <div class='box-tools pull-right'>
					
				 <a data-toggle='modal' data-backdrop="static" data-target='#tambahwalas' class='btn btn-sm btn-success'><i class='glyphicon glyphicon-plus'></i> <span class='hidden-xs'>Tambah</span></a>
                  
                </div>
				</div>
					
                <div class='box-body'>
                    <div class='table-responsive'>
                        <table style="font-size: 11px" 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='5%'>Urut</th>
                                     								  
									<th width='10%'>Action</th>
                                </tr>
                            </thead>
							
							<tbody>
							
                                <?php
								$no = 0;
								 if ($pengawas['level'] == 'admin') :
								 $query = mysqli_query($koneksi, "select * from jadwal_proto
								 JOIN pengawas ON pengawas.id_pengawas=jadwal_proto.idguru");
								  elseif ($pengawas['level'] == 'guru') :
								   $query = mysqli_query($koneksi, "select * from jadwal_proto
								 JOIN pengawas ON pengawas.id_pengawas=jadwal_proto.idguru WHERE  jadwal_proto.idguru='$_SESSION[id_pengawas]' ");
								   endif;
                            while ($mapel = mysqli_fetch_array($query)) {
							
                                $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><?= $mapel['nourut'] ?></td>					
									
                                   <td style="text-align:center">
                                       <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#modaledit<?= $no ?>">
                                                    <i class="fas fa-edit    "></i>
                                                </button>
										 <button data-id="<?= $mapel['id_jadwal'] ?>" class="hapus btn-xs btn btn-danger"><i class="fas fa-times"></i></button>
                                   </td>
								   <div class="modal fade" id="modaledit<?= $no ?>" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
                                        <div class="modal-dialog modal-md" role="document">
                                            <div class="modal-content">
                                                   <div class='modal-header'>
												      <button class='close' data-dismiss='modal'><span aria-hidden='true'><i class='glyphicon glyphicon-remove'></i></span></button>
													<h4 class='modal-title'>Jadwal Mapel</h4>                               
											</div>
											
                                                <form id="formedit<?= $no ?>">
												<input type="hidden" name="idj" value="<?= $mapel['id_jadwal'] ?>" >
                                       <div class='modal-body'>
									<div class="col-md-6">
                                        <div class='form-group-sm'>
                                                   <label>Tingkat/Level</label>
                                                    <select name='kelas' class='form-control' disabled='true'>
                                                        <option value='<?= $mapel[kelas] ?>' ><?= $mapel['kelas'] ?></option>
                                                        <?php
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY id_kelas");
                                                        while ($kelas = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$kelas[id_kelas]'>$kelas[id_kelas]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
												</div>
												<div class="col-md-6">
                                          <div class='form-group-sm'>
                                                   <label>Kode Mapel</label>
                                                    <select name='kode' class='form-control' disabled='true'>
                                                        <option value='<?= $mapel['kode'] ?>'><?= $mapel['kode'] ?></option>
                                                        <?php
                                                        $mapelQ = mysqli_query($koneksi, "SELECT * FROM mata_pelajaran");
                                                        while ($mapelx = mysqli_fetch_array($mapelQ)) {
                                                            echo "<option value='$mapelx[kode_mapel]'>$mapelx[kode_mapel]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
												</div>
												<div class="col-md-12">
                                          <div class='form-group-sm'>
                                                   <label>Nama Mapel</label>
                                                    <select name='mapel' class='form-control' disabled='true'>
                                                        <option value='<?= $mapel['mapel'] ?>'><?= $mapel['mapel'] ?></option>
                                                        <?php
                                                        $mapelX = mysqli_query($koneksi, "SELECT * FROM mata_pelajaran");
                                                        while ($mapelM = mysqli_fetch_array($mapelX)) {
                                                            echo "<option value='$mapelM[nama_mapel]'>$mapelM[nama_mapel]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
												</div>
												
												 <div class="col-md-12">
                                                             <div class="form-group-sm">
                                                        <label>No Urut Mapel dalam Rapor</label>
															<select name='urut' class='form-control' style='width:100%' required>
                                                                <option value='<?= $mapel[nourut] ?>' ><?= $mapel['nourut'] ?></option>
                                                                <?php
														for($i=1; $i<=25;$i++){
																					?>
															<option value="<?= $i ?>"><?= $i ?></option>
														<?php } ?>
															</select>
                                                        </div>
                                                        </div>
														
														
												<div class="col-md-12">
											   <div class='form-group-sm'>
                                                   <label>Guru Mapel</label>
												  <?php $user=fetch($koneksi,'pengawas',['id_pengawas'=>$mapel['idguru']]); ?>
                                                    <select name='guru' class='form-control' >
                                                        <option value="<?= $mapel[idguru] ?>"><?= $user['nama'] ?></option>
                                                         <?php if ($pengawas['level'] == 'admin'){ ?>
                                                       
                                                        <?php
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM pengawas WHERE level='guru'");
                                                        while ($ekstra = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$ekstra[id_pengawas]'>$ekstra[nama]</option>";
                                                        }
                                                        ?>
													 <?php }else{ ?>
													 <?php
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM pengawas WHERE id_pengawas='$_SESSION[id_pengawas]' ");
                                                        while ($ekstra = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$ekstra[id_pengawas]'>$ekstra[nama]</option>";
                                                        }
                                                        ?>
													 <?php } ?>
                                                    </select>
                                                </div>
												</div>
												
											     <div class='modal-footer' >
                                            <div class='box-tools pull-right' >
											<br>
                                                <button type='submit' class='btn btn-sm btn-success'><i class='fa fa-check'></i> Simpan</button>
                                               
                                            </div>
                                        </div>
										
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                    <script>
                        $("#formedit<?= $no ?>").submit(function(e) {
                            e.preventDefault();
                            $.ajax({
                                type: 'POST',
                                url: 'sandik_ikm/crud_jadwal.php?pg=editjadwal',
                                data: $(this).serialize(),
                                success: function(data) {
                                    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.reload();
                                    }, 2000);
                                }
                            });
                            return false;
                        });
                    </script>
   			
							       </tr>
                            <?php } ?>
                        </tbody>
                        </table>
                    </div>
                    </div>
					</div>
                      <div class='modal fade' id='tambahwalas' style='display: none;'>
                        <div class='modal-dialog'>
                            <div class='modal-content'>
                                <div class='modal-header'>
                                    <button class='close' data-dismiss='modal'><span aria-hidden='true'><i class='glyphicon glyphicon-remove'></i></span></button>
                                    <h4 class='modal-title'></h4>
                                </div>
                                
                                     <form id="formjadwal" action='' >
									 <div class='modal-body'>
									 <div class='row'>
									 <div class="col-md-12">
                                          <div class='form-group-sm'>
                                                   <label>Fase</label>
                                                    <select name='fase' id="fase" class='form-control' required='true'>
                                                      <option value=''></option>
                                                        <?php
                                                        $fQ = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY fase");
                                                        while ($f = mysqli_fetch_array($fQ)) {
                                                            echo "<option value='$f[fase]'>$f[fase]</option>";
                                                        }
                                                        ?>   
                                                    </select>
                                                </div>
												</div>
									<div class="col-md-6">
                                        <div class='form-group-sm'>
                                                   <label>Tingkat/Level</label>
                                                    <select name='level' id="level" class='form-control' 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>
												</div>
												
												<div class="col-md-6">
                                          <div class='form-group-sm'>
                                                   <label>Kelas</label>
                                                    <select name='kelas' id="kelas" class='form-control' required='true'>
                                                        
                                                    </select>
                                                </div>
												</div>
												<div class="col-md-6">
                                          <div class='form-group-sm'>
                                                   <label>Kode Mapel</label>
                                                    <select name='kode' id="kode" class='form-control' 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>
												</div>
												<div class="col-md-6">
                                          <div class='form-group-sm'>
                                                   <label>Nama Mapel</label>
                                                    <select name='mapel' id="mapel" class='form-control' required='true'>
                                                       
                                                    </select>
                                                </div>
												</div>
												
												
                                                       <div class="col-md-12">
                                                             <div class="form-group-sm">
                                                        <label>No Urut Mapel dalam Rapor</label>
															<select name='urut' class='form-control' style='width:100%' required>
                                                                <option value='' >--Pilih No Urut--</option>
                                                                <?php
														for($i=1; $i<=25;$i++){
																					?>
															<option value="<?= $i ?>"><?= $i ?></option>
														<?php } ?>
															</select>
                                                        </div>
                                                        </div>
														
														
												<div class="col-md-12">
											   <div class='form-group-sm'>
                                                   <label>Guru Mapel</label>
                                                    <select name='guru' class='form-control' required='true'>
													 <?php if ($pengawas['level'] == 'admin'){ ?>
                                                        <option value=''></option>
                                                        <?php
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM pengawas WHERE level='guru'");
                                                        while ($ekstra = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$ekstra[id_pengawas]'>$ekstra[nama]</option>";
                                                        }
                                                        ?>
													 <?php }else{ ?>
													 <?php
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM pengawas WHERE id_pengawas='$_SESSION[id_pengawas]' ");
                                                        while ($ekstra = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$ekstra[id_pengawas]'>$ekstra[nama]</option>";
                                                        }
                                                        ?>
													 <?php } ?>
                                                    </select>
                                                </div>
												</div>
											</div>
												</div>
                                                <div class='modal-footer' >
                                            <div class='box-tools pull-right btn-group' >
                                                <button type='submit'  class='btn btn-sm btn-success'><i class='fa fa-check'></i> Simpan</button>
                                               
                                            </div>
                                        </div>
										
                                    </form>
                               
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
	 <script>
	  $("#level").change(function() {
        var level = $(this).val();
        console.log(level);
        $.ajax({
            type: "POST", 
            url: "sandik_ikm/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_ikm/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_ikm/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.reload();
                    }, 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>
	<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_ikm/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>