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

?>           
	<?php if ($ac == '') : ?>
                   <div class="row">
                          <div class="col-md-8">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">INPUT DATA TIDAK HADIR</h5>
										
                                    </div>
                                    <div class="card-body">
									
									<div class="card-box table-responsive">
                                        <table id="datatable1" class="table table-bordered table-hover" style="width:100%;font-size:12px">
                                            <thead>
                                                <tr>
                                                    <th width="5%">NO</th>                                               
                                                   
                                                    <th>NAMA LENGKAP</th>
													 <th>ROMBEL</th>
													 <th>STATUS</th>
													 <th></th>
                                                </tr>
                                            </thead>
                                            <tbody>
											<?php
											$no=0;
											if (isset($_POST['kelas'])) {
												$kelas = $_POST['kelas'];
											$query = mysqli_query($koneksi,"SELECT * FROM siswa WHERE kelas='$kelas' AND NOT EXISTS(SELECT * FROM absensi WHERE siswa.id_siswa=absensi.idsiswa AND absensi.tanggal='$tanggal')");
											}
											  while ($data = mysqli_fetch_array($query)) :
											  
											$no++;
											   ?>
                                                <tr>
                                                    <td><?= $no; ?></td>
                                                    
                                                     <td><?= $data['nama'] ?></td>
													 <td><?= $data['kelas'] ?></td>
													  <td>
													 <h5><span class="badge badge-secondary">Belum Absen</span></h5>
													  
													  </td>
													  <td>
											<a href="?pg=insis&ac=siswa&ids=<?= $data['id_siswa'] ?>"  class="btn btn-sm btn-primary" data-bs-toggle="tooltip" data-bs-placement="top" title="Input Absen"><i class="material-icons">draw</i> </a>
											</td>
                                                </tr>
												<?php endwhile; ?>
                                                </table>
												 </div>
											</div>
										</div>
									</div>
									
					       <div class="col-md-4">
                     
                                <div class="card widget widget-payment-request">
                                    <div class="card-header">
                                       
										<a href="?pg=insis" class="btn btn-success"><i class="material-icons">crisis_alert</i>Siswa</a>
										<?php if($user['level']=='admin'): ?>
										<a href="?pg=insis&ac=pegawai" class="btn btn-primary"><i class="material-icons">crisis_alert</i>Pegawai</a>
                                    <?php endif; ?>
									</div>
                                    <div class="card-body">
									 <form id="formabsen" method="POST" action="" enctype="multipart/form-data">
                                        <div class="widget-payment-request-container">
                                            <div class="widget-payment-request-author">
                                                <div class="avatar m-r-sm">
                                                    <img src="../images/user.png" alt="">
                                                </div>
                                                <div class="widget-payment-request-author-info">
                                                    <span class="widget-payment-request-author-name">Data Siswa</span>
                                                    <span class="widget-payment-request-author-about"><?= $setting['sekolah'] ?></span>
                                               
											   </div>
                                            </div>
											<div class="widget-payment-request-info m-t-md">
											<div class="d-grid gap-2">
											<button class="btn btn-dark" type="button" disabled>
											<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
											Loading... Data harus diinput sebelum Jam Deteksi Alpha
										</button>
										 </div><p>
                                             <div class="widget-payment-request-info-item">												
                                                    <span class="widget-payment-request-info-title d-block">													
													<label class="form-label">ROMBEL</label>                               
										  <select name="kelas"  class="form-select" style="width: 100%;" required >
									  <option value=''></option>
										 <?php $kls = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY kelas"); ?>
										   <?php while ($Q = mysqli_fetch_array($kls)) : ?>
											 <option value="<?= $Q['kelas'] ?>"><?= $Q['kelas'] ?></option>
												<?php endwhile ?>
												   </select>  
													</span>                                                  
                                                </div>                                               
                                            </div>
									<div class="widget-payment-request-info m-t-md">							
										<div class="d-grid gap-2">										
										  <button type="submit"  class="btn btn-primary flex-grow-1 m-l-xxs">PILIH</button>
										 </div>
										</form>
									 </div>
					            </div>
								</div>
							</div>
						</div>
					
					
      <?php elseif ($ac == 'siswa') : ?>
	  <?php 
		$ids = $_GET['ids'];
		$siswa = fetch($koneksi,'siswa',['id_siswa'=>$ids]);
		?>
					<div class="row">
                          <div class="col-md-8">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">INPUT ABSENSI SISWA</h5>
										
                                    </div>
                                    <div class="card-body">
									<div class="d-grid gap-2">
										<button class="btn btn-secondary" type="button" disabled>
											<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
										<?= $siswa['nama'] ?>
										</button>
										 </div>
											</div>
										</div>
									</div>
									
					       <div class="col-md-4">
                     
                                <div class="card widget widget-payment-request">
                                    <div class="card-header">
                                        <h5 class="card-title">INPUT ABSENSI</h5>
										
                                    </div>
                                    <div class="card-body">
									<form id='formabsensi' >	
                                        <div class="widget-payment-request-container">
                                            <div class="widget-payment-request-author">
                                                <div class="avatar m-r-sm">
                                                    <img src="../images/user.png" alt="">
                                                </div>
                                                <div class="widget-payment-request-author-info">
                                                    <span class="widget-payment-request-author-name"><?= $siswa['nama'] ?></span>
                                                    <span class="widget-payment-request-author-about"><?= $setting['sekolah'] ?></span>
                                               
											   </div>
                                            </div>
											
									<div class="widget-payment-request-info m-t-md">
									
									<label>Pilih Keterangan</label>
									   <div class="input-group mb-1">
									   <select name="ket"  class="form-select" style="width: 100%;" required >
									   <option value=''>Piliah Alasan</option>
											  <option value='S'>Sakit</option>
											  <option value='I'>Izin</option>
											  </select>
                                        </div>
									 <label>N I S</label>
									  <div class="input-group mb-1">
                                       <input type='text' name='nis' class='form-control' value="<?= $siswa['nis'] ?>" readonly />
									    <input type='hidden' name='id' class='form-control' value="<?= $siswa['id_siswa'] ?>"   />
									   
                                        </div>	
										 <label>Nama Lengkap</label>
									  <div class="input-group mb-1">
                                       <input type='text' name='nama' class='form-control' value="<?= $siswa['nama'] ?>" readonly />
                                        </div>
										<label>Rombel</label>
									  <div class="input-group mb-1">
                                       <input type='text' name='kelas' class='form-control' value="<?= $siswa['kelas'] ?>" readonly />
                                        </div>
										
										
										<p>
										<div class="d-grid gap-2">
                                         <button type="submit" class="btn btn-primary flex-grow-1 m-l-xxs" id="blockui-2">Simpan</button>
                                            </div>
										</form>
									 </div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>	
			</div>		
				
					   <script>
						  $('#formabsensi').submit(function(e) {
								e.preventDefault();
								$.ajax({
									type: 'POST',
									url: 'cbtserver_absen/absen/tabsen.php?pg=siswa',
									data: $(this).serialize(),
									beforeSend: function() {
										$('form button').on("click", function(e) {
											e.preventDefault();
										});
									},
									success: function(data) {
										console.log(data);
										if (data == 'OK') {
											
											setTimeout(function() {
												window.location.replace('?pg=insis');
											}, 2000);
										} else {
										   iziToast.info(
									{
										title: 'Gagal!',
										message: 'Alasan Siswa Belum dipilih',
										titleColor: '#FFFF00',
										messageColor: '#fff',
										backgroundColor: 'rgba(0, 0, 0, 0.5)',
										 progressBarColor: '#FFFF00',
										  position: 'topRight'
											});
											setTimeout(function() {
												window.location.reload();
											}, 2000);
										}

									}
								});
								return false;
							});
						   
						</script>
					
		      <?php elseif ($ac == 'pegawai') : ?>			
					<div class="row">
                          <div class="col-md-8">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="bold">INPUT ABSENSI <span class=" badge badge-secondary"><?= date('d M Y') ?></span></h5>
										
                                    </div>
                                    <div class="card-body">
									
									<div class="card-box table-responsive">
                                        <table id="datatable1" class="table table-bordered table-hover" style="width:100%;font-size:12px">
                                            <thead>
                                                <tr>
                                                    <th width="5%">NO</th>                                               
                                                    <th>N I P</th>
                                                    <th>NAMA GURU</th>
													 <th>JABATAN</th>
													  <th>KET</th>
													 <th></th>
                                                </tr>
                                            </thead>
                                            <tbody>
											<?php
											$no=0;
											$query = mysqli_query($koneksi, "SELECT * FROM users WHERE  level<>'admin'"); 
											  while ($data = mysqli_fetch_array($query)) :
											$absen = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM absensi  WHERE idpeg='$data[id_user]' AND tanggal='$tanggal' AND ket<>'H'"));
											$guru = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM  absensi  WHERE idpeg='$data[id_user]' AND tanggal='$tanggal'"));
											$no++;
											   ?>
                                                <tr>
                                                    <td><?= $no; ?></td>
                                                    <td><?= $data['nip'] ?></td>
                                                     <td><?= $data['nama'] ?></td>
													  <td><?= $data['level'] ?></td>
													    <td><?= $absen['ket'] ?></td>
													  <td>
											<?php if($guru==0): ?>
											  <a href="?pg=insis&ac=pegawai&ids=<?= $data['id_user'] ?>"> <button class='btn btn-sm btn-success' data-bs-toggle="tooltip" data-bs-placement="top" title="Input Absen"><i class="material-icons">edit</i></button></a>
											<?php else: ?>
											<button class="btn btn-sm btn-secondary" disabled><i class="material-icons">lock</i></button>
											<?php endif; ?>
											</td>
                                                </tr>
												<?php endwhile; ?>
                                                </table>
												 </div>
											</div>
										</div>
									</div>
								
					       <div class="col-md-4">
                     
                                <div class="card widget widget-payment-request">
                                    <div class="card-header">
                                        <h5 class="card-title">INPUT ABSEN</h5>
										
                                    </div>
                                    <div class="card-body">
                                        <div class="widget-payment-request-container">
                                            <div class="widget-payment-request-author">
                                                <div class="avatar m-r-sm">
                                                    <img src="../images/user.png" alt="">
                                                </div>
                                                <div class="widget-payment-request-author-info">
                                                    <span class="widget-payment-request-author-name"><?= strtoupper($user['nama']) ?></span>
                                                    <span class="widget-payment-request-author-about"><?= $setting['sekolah'] ?></span>
                                               
											   </div>
                                            </div>
											<div class="d-grid gap-2">
										<button class="btn btn-dark" type="button" disabled>
											<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
											Loading...
										</button>
										 </div>
											<?php 
											$ids = $_GET['ids'];
											$user = fetch($koneksi,'users',['id_user'=>$ids]);
											?>
									<div class="widget-payment-request-info m-t-md">
									<form id='formabpeg' >	
									 <div class="input-group mb-1">
                                       <label>ID Peg</label>			  
                                        <select name="idpeg"  class="form-select" style="width: 100%;" required >
											  <option value="<?= $ids ?>"><?= $ids ?></option>
											  
											  </select>
											   </div>
										 <label>Nama Lengkap</label>
									  <div class="input-group mb-1">
                                       <input type='text' name='nama' class='form-control' value="<?= $user['nama'] ?>" required="true" />
                                        </div>
										<label>Jabatan</label>
									  <div class="input-group mb-1">
                                       <input type='text' name='kelas' class='form-control' value="<?= $user['level'] ?>" readonly />
                                        </div>
										<div class="input-group mb-1">
										<label>Alasan Tidak Hadir</label>			  
                                        <select name="ket"  class="form-select" style="width: 100%;" required >
									   <option value=''>Piliah Alasan</option>
											  <option value='S'>Sakit</option>
											  <option value='I'>Izin</option>
											   <option value='A'>Alpha</option>
											  </select>
                                         </div>
										<p>
										<div class="d-grid gap-2">
                                         <button type="submit" class="btn btn-primary flex-grow-1 m-l-xxs" id="blockui-2">Simpan</button>
                                            </div>
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>	
			</div>	
	</div>		
					
					<script>
						  $('#formabpeg').submit(function(e) {
								e.preventDefault();
								$.ajax({
									type: 'POST',
									url: 'cbtserver_absen/absen/tabsen.php?pg=pegawai',
									data: $(this).serialize(),
									beforeSend: function() {
										$('form button').on("click", function(e) {
											e.preventDefault();
										});
									},
									success: function(data) {
										console.log(data);
										if (data == 'OK') {
											
											setTimeout(function() {
												window.location.reload();
											}, 2000);
										} else {
										   iziToast.info(
									{
										title: 'Gagal!',
										message: 'Alasan Siswa Belum dipilih',
										titleColor: '#FFFF00',
										messageColor: '#fff',
										backgroundColor: 'rgba(0, 0, 0, 0.5)',
										 progressBarColor: '#FFFF00',
										  position: 'topRight'
											});
											setTimeout(function() {
												window.location.reload();
											}, 2000);
										}

									}
								});
								return false;
							});
						   
						</script>
					
					  <?php endif ?>
					