<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
<?php
$tahun=date('Y');
$wln=date('m');
$hari = date('D');
$wulan=fetch($koneksi,'bulan',['bln'=>$wln]);

?>

<?php if ($ac == '') { ?>
 <div class='row'>
 <div class='col-md-12'>
 <div class="card">
             <div class="card-header">									                
                    <div class='right '>
					<a href="?pg=agenda&ac=tambah" class="btn btn-sm btn-outline-primary"><i class="fa fa-plus-circle"></i> Agenda</a>
                   <button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#myModal"><i class="fa fa-print"></i> Cetak</button>
                   
                    </div>
					<h5 class="card-title"><i class="fas fa-laptop"></i> Agenda Guru</h5>
                </div>
				
            <div class='card-body'>
                <div class="table-responsive">
                     <table style="font-size: 12px" id='example1' class='table'>
                        <thead>
						
                            <tr>
                                <th width='5px'>
                                    #
                                </th>
                                <th>Mapel</th>
								<th width="5%">Kelas</th>
                                <th>Tanggal</th>
								 <th> Materi</th>
                                 <th> Indikator</th>
								   <th width='10%'>Pencapaian</th>
	                            <th width='5%' >Kehadiran Siswa</th>
                                 <th width='15%'>Action</th>
                            </tr>
						
                        </thead>
                        <tbody>
						
                            <?php
                            $query = mysqli_query($koneksi, "select * from agenda 
							JOIN jadwal_mapel ON jadwal_mapel.id_jadwal=agenda.idjadwal WHERE jadwal_mapel.guru='$_SESSION[id_user]' AND agenda.bulan='$wln' AND agenda.tahun='$tahun' ORDER BY agenda.id DESC");
                            $no = 0; ?>
                           <?php while ($agenda = mysqli_fetch_array($query)) {
							 $lingkup = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM lingkup WHERE lm='$agenda[lm]'"));
							 $tujuan = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM tujuan WHERE tp='$agenda[tp]'"));
								$tanggal=date('d-m-Y',strtotime($agenda['tanggal']));
								$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_kelas='$agenda[kelas]' "));
                                $hadir = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM absen_mapel WHERE idjadwal='$agenda[id_jadwal]' AND tanggal='$agenda[tanggal]' AND ket='H'"));
                                $bagi=100/$jumlah;
							    $prosen=$bagi * $hadir;
							   $no++;
                              
                            ?>
							
                                <tr>
                                    <td><?= $no; ?></td>
                                    <td><?= $agenda['mapel'] ?></td>
									 <td><?= $agenda['kelas'] ?></td>
                                    <td><?= $tanggal ?></td>
									<?php if($agenda['kurikulum']=='K-13'){ ?>
									<td><?= $agenda['materi'] ?></td>
									<td><?= $agenda['indikator'] ?></td>
									<?php }else{ ?>
									<td><?= $lingkup['materi'] ?></td>
									<td><?= $tujuan['tujuan'] ?></td>
									<?php } ?>
									<td>
									<?php if($prosen>=50){ ?>
									 <b style="color:blue;">Tercapai</b>
									 <?php } ?>
									  <?php if($prosen<50){ ?>
									 <b style="color:red;">Tidak Tercapai</b>
									 
									 <?php } ?>
									</td>
									<td><?= round($prosen,2) ?> %</td>
                                    <td>
                                      
                                        <button data-id="<?= $agenda['id'] ?>" class="hapus btn-sm btn btn-outline-danger" data-bs-placement="top" data-bs-toggle="tooltip" title="Hapus"><i class="fas fa-times-circle"></i></button>
                                       
                                        <?php if($prosen<=50){ ?>
										 <a href="?pg=jurnal&id=<?= $agenda['id'] ?>" class="btn-sm btn btn-outline-primary" data-bs-placement="top" data-bs-toggle="tooltip" title="Buat Jurnal"><i class="fas fa-plus-circle"></i></a>
										<?php }else{ ?>
										<button type="button" class="btn btn-sm btn-default" disabled>
                                          <i class="fas fa-lock" ></i>
                                        </button>
										<?php } ?>
										</td>
											 
																</tr>
																<?php }
																?>
															</tbody>
														</table>
														

													</div>
												</div>
											</div>
										</div>
									</div>
						
				<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
				    <div class="modal-dialog">
				   <div class="modal-content">
					  <div class="modal-header">
						<h5 class="modal-title" id="tambahjadwal">Cetak Agenda</h5>
						   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
								</div>
                      <div class="modal-body">
					<div class='form-group-sm'>
                        <div class='row'>
                            <div class='col-md-6'>
                               <label>Mapel</label>
                                 <?php
                            $sql = mysqli_query($koneksi, "SELECT * FROM jadwal_mapel WHERE guru='$user[id_user]'");
                            ?>
                            <?php while ($jadwal = mysqli_fetch_array($sql)) : ?>
                                <div class='radio'>
                                    <label><input type='radio' name='idj' value="<?= $jadwal['id_jadwal'] ?>" onclick="printagenda('<?= $jadwal[0] ?>')" /> <?= $jadwal['kode'] ?> <?= $jadwal['kelas'] ?></label>
                                </div>
                            <?php endwhile ?>
                                                </div>
                                                
                                     </div>
					  </div>
					  <div class="modal-footer">
					  <button class='btn  btn-outline-primary' onclick="frames['frameresult'].print()"><i class='fa fa-print'></i> Print</button>  
					  </div>
					 
					</div>
				  </div>
				</div>
 <iframe id='loadframe' name='frameresult' src="sandik_agenda/cetakagenda.php" style='display:none'></iframe>
					 
					 
<?php } elseif ($ac == 'tambah') { ?>				 
	 <div class='row'>
     <div class='col-md-12'>
         <div class="card">
             <div class="card-header">									                
					<h5 class="card-title"><i class="fas fa-laptop"></i> Agenda Guru</h5>
                </div>
			
							  <div class="card-body">
                                    <form id='formagenda' action=''>
                                       <div class='row'>                                     
                                      <div class='col-md-6'>
                                      <label class="bold">Mapel</label>
                                         <select name='jadwal' id="jadwal" class='form-select' style='width:100%' required>
                                                <option value=''>Pilih Mata Pelajaran</option>
												
                                                <?php $que = mysqli_query($koneksi, "SELECT * FROM jadwal_mapel WHERE guru='$_SESSION[id_user]'  "); ?>
			
											   <?php while ($mapel = mysqli_fetch_array($que)) : ?>

                                                    <option value="<?= $mapel['id_jadwal'] ?>"><?= $mapel['mapel'] ?> <?= $mapel['kelas'] ?></option>

                                                <?php endwhile ?>
                                            </select>
                                                </div>
                                                <div class='col-md-6'>
                                                     <label class="bold">Kelas</label>
                                         <select name='kelas' id="kelas" class='form-select' style='width:100%' required>
                                          
                                            </select>
                                                </div>
                                          
                                                <div class='col-md-6'>
                                            <label class="bold">Tanggal</label>
                                            <input type="text" class="form-control datepicker" name="tgl" id="tgl" value="<?= date('Y-m-d'); ?>" autocomplete="off" required>
                                        </div>
                                       
                                                <div class='col-md-6'>
                                                   <label class="bold">Kurikulum</label>
                                         <select name='kurikulum' id="kurikulum" class='form-select' style='width:100%' required>
                                          
                                            </select>
                                                </div>
												
                                                <div class='col-md-12'id="kd" style="display: none;">
                                                     <label class="bold">No KI/KD</label>
                                                      <input type="text" name="kd"  class="form-control" autocomplete='off' >
                                                </div>
                                                <div class='col-md-12'id="lingkup" style="display: none;">
                                                     <label class="bold">Lingkup Materi</label>
                                                   <select name='lingkup'  class='form-select' style='width:100%' >
                                                <option value=''>Pilih LM</option>
												 <?php $q = mysqli_query($koneksi, "SELECT * FROM lingkup JOIN jadwal_mapel ON jadwal_mapel.id_jadwal=lingkup.idjadwal WHERE jadwal_mapel.guru='$user[id_user]'"); ?>
                                                <?php while ($l = mysqli_fetch_array($q)) : ?>

                                                    <option value="<?= $l['lm'] ?>">(<?= $l['lm'] ?>) <?= $l['materi'] ?></option>

                                                <?php endwhile ?>
                                            </select>
                                                </div>
                                        
                                                <div class='col-md-12'id="tujuan" style="display: none;">
                                                     <label class="bold">Lingkup Materi</label>
                                                   <select name='tujuan'  class='form-select' style='width:100%' >
                                                <option value=''>Pilih TP</option>
												 <?php $qt = mysqli_query($koneksi, "SELECT * FROM tujuan JOIN jadwal_mapel ON jadwal_mapel.id_jadwal=tujuan.idjadwal WHERE jadwal_mapel.guru='$user[id_user]'"); ?>
                                                <?php while ($t = mysqli_fetch_array($qt)) : ?>

                                                    <option value="<?= $t['tp'] ?>">(<?= $t['tp'] ?>) <?= $t['tujuan'] ?></option>

                                                <?php endwhile ?>
                                            </select>
                                                </div>
                                        
										 <div class='col-md-12' id="materi" style="display: none;">
                                                   <label class="bold">Materi</label>
                                                  <textarea id='editor2' name='materi' class='form-control' rows='2' cols='80' style='width:100%;' ></textarea>
                                                </div>
											
										 <div class='col-md-12' id="indik" style="display: none;">
                                                      <label class="bold">Indikator</label>
                                              <textarea  name='indikator' class='form-control' rows='2' cols='80' style='width:100%;' ></textarea>
                                                </div>
                                           
										 <div class='col-md-12'>
                                         <label class="bold">Guru Pengampu</label>
                                         <select name='guru' class='form-select' style='width:100%' required>                                               
                                            <option value="<?= $user['id_user'] ?>"><?= $user['nama'] ?></option>  
                                            </select>
                                                </div>
												 </div>
												 <p>
                                        <div class='right '>	
										<button type="submit" class="btn btn-primary"><i class='fa fa-check-circle'></i> Simpan</button>
									  </div>
									  
                                    </form>
									
                                </div>
                            </div>
                        </div>
                      </div>				
				</div>	
				 
					 
					 
					 
           <?php } ?>

                       		
							
               <script>
				  $("#jadwal").change(function() {
					var jadwal = $(this).val();
					console.log(jadwal);
					$.ajax({
						type: "POST", 
						url: "sandik_agenda/crud_agenda.php?pg=ambil_kelas", 
						data: "jadwal=" + jadwal, 
						success: function(response) { 
							$("#kelas").html(response);
						}
					});
				});		
				 
               $("#kelas").change(function() {
					var kelas = $(this).val();
					console.log(kelas);
					$.ajax({
						type: "POST", 
						url: "sandik_agenda/crud_agenda.php?pg=ambil_kurikulum", 
						data: "kelas=" + kelas, 
						success: function(response) { 
							$("#kurikulum").html(response);
						}
					});
				});						
			</script>
					<script type="text/javascript">
    $(function () {
        $("#kurikulum").change(function () {
            if ($(this).val() == "K-13") {
                $("#kd").show();
                $("#materi").show();
				$("#indik").show();
				 $("#lingkup").hide();
				  $("#tujuan").hide();
            } else {
                $("#kd").hide();
                $("#materi").hide();
				$("#indik").hide();
				 $("#lingkup").show();
				 $("#tujuan").show();
            }
        });
    });
</script>						
	<script>
 $('#formagenda').submit(function(e) {
      e.preventDefault();
        var data = new FormData(this);
    $.ajax(
    {
        type: 'POST',
        url: 'sandik_agenda/crud_agenda.php?pg=tambah',
        enctype: 'multipart/form-data',
        data: data,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data){   	
            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.replace('?pg=agenda');
            }, 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!',
            showCancelButton: true,
			type:'warning',
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'iya, hapus'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_agenda/crud_agenda.php?pg=hapus',
                    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>        
 						
<script>
$(function(){
    $("#datepicker").datepicker({
        dateFormat: "D,Y-m-d"
    });
});
function printagenda(idj) {
			$('#loadframe').attr('src', 'sandik_agenda/cetakagenda.php?idjadwal=' + idj);
		}
</script>
