<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
$jsiswa = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM siswa"));
$jsisL = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM siswa WHERE jk='L'"));
$jsisP = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM siswa WHERE jk='P'"));

?>           
			<?php if ($ac == '') : ?>
			       <div class="row">
                          <div class="col-md-8">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">Data Siswa</h5>
										<div class="pull-right">
										<button id="confirm"  class="hapus btn btn-danger" data-bs-toggle="tooltip" data-bs-placement="top" title="Reset Data Siswa"><i class="material-icons">delete</i></button>
                                   <a href="." class="btn btn-light" data-bs-toggle="tooltip" data-bs-placement="top" title="Kembali">BACK</a>
								   </div>
									</div>
                                    <div class="card-body">
									<br>
									<div class="card-box table-responsive">
                                        <table id="datatable1" class="table table-bordered table-hover" style="width:100%">
                                            <thead>
                                                <tr>
                                                    <th>No</th>
                                                    <th>N I S</th>
                                                    <th>NAMA SISWA</th>
                                                    <th>ROMBEL</th>
                                                    <th>JK</th>
                                                  
                                                </tr>
                                            </thead>
                                            <tbody>
											<?php
											$no=0;
											$query = mysqli_query($koneksi, "SELECT * FROM siswa"); 
											  while ($data = mysqli_fetch_array($query)) :
											$no++;
											   ?>
                                                <tr>
                                                    <td><?= $no; ?></td>
                                                    <td><?= $data['nis'] ?></td>
                                                     <td><a href="?pg=siswa&ac=info&id=<?= $data['id_siswa'] ?>" class="link" style="text-decoration:none"><?= $data['nama'] ?></a></td>
                                                    <td><?= $data['kelas'] ?></td>
                                                    <td><?= $data['jk'] ?></td>
                                                 
                                                </tr>
												<?php endwhile; ?>
                                                </table>
												<br>
												 </div>
											</div>
										</div>
									</div>
							
		          <div class="col-md-4">
                       <div class="card" id="blockui-card-1">
                                <div class="card widget widget-payment-request">
                                    <div class="card-header">
                                        <h5 class="card-title">Upload</h5>
										<a href="cbtserver_master/M_SISWA.xlsx" class="btn btn-link pull-right"><i class="material-icons">download</i>Download Format</a>   
                                    </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">UPLOAD SISWA</span>
                                                    <span class="widget-payment-request-author-about"><?= $setting['sekolah'] ?></span>
                                                </div>
                                            </div>
									<div class="widget-payment-request-info m-t-md">
									<form id='formsiswa' >	
                                      <label>Pilih File Excel</label>
									  <div class="input-group mb-3">
                                       <input type='file' name='file' class='form-control' required='true' />
									    <span class="input-group-text">
											<button type="submit" id="blockui-1" class="btn btn-primary"><i class="material-icons">upload</i></button>
										</span>
                                        </div>	
										</form>
									 </div>
					            </div>
								</div>
							</div>
						</div>
						
<script>
    $('#formsiswa').submit(function(e){
		e.preventDefault();
		var data = new FormData(this);
		$.ajax(
		{
			type: 'POST',
             url: 'cbtserver_master/import_siswa.php',
            data: data,
			cache: false,
			contentType: false,
			processData: false,
			success: function(data){   		
			setTimeout(function()
				{
				window.location.reload();
						}, 1000);
									  
						}
					});
				return false;
			});
		</script>	
		 <div class="card widget widget-list">
                                    
                                    <div class="card-body">
                                       
                                        <ul class="widget-list-content list-unstyled">
                                            <li class="widget-list-item widget-list-item-blue">
                                                <span class="widget-list-item-icon"><i class="material-icons-outlined">face</i></span>
                                                <span class="widget-list-item-description">
                                                    <a href="#" class="widget-list-item-description-title">
                                                      <?= $jsisL ?> Laki-laki 
                                                    </a>
                                                    <span class="widget-list-item-description-progress">
                                                        <div class="progress">
                                                            <div class="progress-bar bg-success" role="progressbar" style="width: <?= round(($jsisL/$jsiswa)*100) ?>%;" aria-valuenow="<?= round(($jsisL/$jsiswa)*100) ?>" aria-valuemin="0" aria-valuemax="100"></div>
                                                        </div>
                                                    </span>
                                                </span>
                                            </li>
                                            <li class="widget-list-item widget-list-item-yellow">
                                                <span class="widget-list-item-icon"><i class="material-icons-outlined">verified_user</i></span>
                                                <span class="widget-list-item-description">
                                                    <a href="#" class="widget-list-item-description-title">
                                                       <?= $jsisP ?> Perempuan
                                                    </a>
                                                    <span class="widget-list-item-description-progress">
                                                        <div class="progress">
                                                            <div class="progress-bar bg-warning" role="progressbar" style="width: <?= round(($jsisP/$jsiswa)*100) ?>%;" aria-valuenow="<?= round(($jsisP/$jsiswa)*100) ?>" aria-valuemin="0" aria-valuemax="100"></div>
                                                        </div>
                                                    </span>
                                                </span>
                                            </li>
                                           
                                        </ul>
                                    </div>
                                </div>
								
                                <div class="card widget widget-payment-request">
                                    <div class="card-header">
                                        <h5 class="card-title">Upload Foto</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">UPLOAD FOTO</span>
                                                    <span class="widget-payment-request-author-about"><?= $setting['sekolah'] ?></span>
                                                </div>
                                            </div>
									<div class="widget-payment-request-info m-t-md">
									<form id='formfoto' >	
                                      <label>Pilih File Zip</label>
									  <div class="input-group mb-3">
                                       <input type='file' name='file' class='form-control' required='true' />
									    <span class="input-group-text">
											<button type="submit" id="blockui-3" class="btn btn-success"><i class="material-icons">upload</i></button>
										</span>
                                        </div>	
										</form>
									 </div>
					           
								</div>
							</div>
						</div>
					</div>		
                  </div>	
             </div>		
   <script>
    $('#formfoto').submit(function(e){
		e.preventDefault();
		var data = new FormData(this);
		$.ajax(
		{
			type: 'POST',
            url: 'cbtserver_master/tfoto.php',
			enctype: 'multipart/form-data',
            data: data,
			cache: false,
			contentType: false,
			processData: false,
			success: function(data){   		
			setTimeout(function()
				{
				window.location.reload();
						}, 2000);
									  
						}
					});
				return false;
			});
		</script>				 
		<?php elseif($ac == 'info'): ?>
		<?php 
		$id = $_GET['id']; 
		$siswa = fetch($koneksi,'siswa', ['id_siswa'=>$id]);
		if($siswa['jk']=='L'){$kelamin='Laki-laki';}if($siswa['jk']=='P'){$kelamin='Perempuan';}
		?>
							<div class="row">
								<div class="col-md-6">
		
                                <div class="card widget widget-payment-request">
                                    <div class="card-header">
                                        <h5 class="card-title">HAPUS DATA</small></h5>
										
                                    </div>
                                    <div class="card-body">
                                        <div class="widget-payment-request-container">
                                            <div class="widget-payment-request-author">
                                                <div class="avatar m-r-sm">
                                                  <?php if($siswa['foto']<>''): ?>
												<img src="<?= $homeurl ?>/images/fotosiswa/<?= $siswa['foto'] ?>" alt="">
												<?php else: ?>
                                                    <img src="../images/user.png" alt="">
												<?php endif; ?>
                                                </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">KELAS : <?= $siswa['kelas'] ?></span>
                                                </div>
                                            </div>
		                             </div>
									  <div class="widget-payment-request-author-info">
									  <br>
                                        <div class="d-grid gap-2" id="data1">									
										<button data-id="<?= $id ?>"  class="hapus btn  btn-danger" data-bs-toggle="tooltip" data-bs-placement="top" title="Hapus Data Siswa"> HAPUS <?= $siswa['nama'] ?></button>
									<a href="?pg=siswa" class="btn  btn-dark" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Kembali"> KEMBALI KE HALAMAN</a>
									</div>                 
									  </div>
									    </div>
									     </div>
										 </div>
										 
						<div class="col-md-6">
		                    <div class="card" id="blockui-card-1">
                                <div class="card widget widget-payment-request">
                                    <div class="card-header">
                                        <h5 class="card-title">EDIT DATA</h5>
										
                                    </div>
                                    <div class="card-body">
                                        <div class="widget-payment-request-container">
                                            <div class="widget-payment-request-author">
                                                <div class="avatar m-r-sm">
												<?php if($siswa['foto']<>''): ?>
												<img src="<?= $homeurl ?>/images/fotosiswa/<?= $siswa['foto'] ?>" alt="">
												<?php else: ?>
                                                    <img src="../images/user.png" alt="">
												<?php endif; ?>
                                                </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">KELAS : <?= $siswa['kelas'] ?></span>
                                                </div>
                                            </div>
		                             </div>
									 <form id='formedit' class="row g-1" >	
									 <input type="hidden" name="ids" value="<?= $id ?>">
                                      <div class="col-md-12">
									    <label class="form-label bold">NAMA LENGKAP</label>
									     <input type="text" class="form-control" name="nama" value="<?= $siswa['nama'] ?>" required>
											</div>	
											<div class="col-md-12">
									    <label class="form-label bold">NOMOR WA <small style="color:blue;font-weight:bold">Jika Ada</small></label>
									     <input type="number" class="form-control" name="nowa" value="<?= $siswa['nowa'] ?>" >
											</div>
										<div class="col-md-6">
									    <label class="form-label bold">N I S</label>
									     <input type="text" class="form-control" name="nis" value="<?= $siswa['nis'] ?>" required>
											</div>
											<div class="col-md-3">
									    <label class="form-label bold">TINGKAT</label>
										 <select class="form-select" name="level" required>
										 <option value="<?= $siswa['level'] ?>" selected ><?= $siswa['level'] ?></option>
									    <option value=''>--Pilih Tingkat--</option>
										<?php
										$lev = mysqli_query($koneksi, "SELECT * FROM level");
										while ($level = mysqli_fetch_array($lev)) {
										echo "<option value='$level[level]'>$level[level]</option>";
										}
										?>
									 </select>
											</div>
											<div class="col-md-3">
									    <label class="form-label bold">ROMBEL</label>
									       <select class="form-select" name="kelas" required>
										    <option value="<?= $siswa['kelas'] ?>" selected ><?= $siswa['kelas'] ?></option>
										   <option value=''>--Pilih Rombel--</option>
										<?php
										$kls = mysqli_query($koneksi, "SELECT * FROM kelas");
										while ($rombel = mysqli_fetch_array($kls)) {
										echo "<option value='$rombel[kelas]'>$rombel[kelas]</option>";
										}
										?>
									 </select>
											</div>
											<div class="col-md-6">
									    <label class="form-label bold">JENIS KELAMIN</label>
									     <select class="form-select" name="jk" required>
										    <option value="<?= $siswa['jk'] ?>" selected ><?= $kelamin ?></option>
										   <option value=''>--Pilih JK--</option>
										<option value='L'>Laki-laki</option>
										<option value='P'>Perempuan</option>
									 </select>
											</div>
											<div class="col-md-6">
									    <label class="form-label bold">AGAMA</label>
									     <input type="text" class="form-control" name="agama" value="<?= $siswa['agama'] ?>" required>
											</div>
											<div class="col-md-12">
									    <label class="form-label bold">FOTO <small style="color:blue;font-weight:bold">Jika Ada</small></label>
									     <input type="file" class="form-control" name="file" >
											</div>
											<div class="d-grid gap-2" id="data1">									
										<button type="submit" class="btn btn-success" id="blockui-1" data-bs-toggle="tooltip" data-bs-placement="top" title="Edit Data Siswa"> SIMPAN <?= $siswa['nama'] ?></button>
									</div>  
										</form>
									 
									 
									  </div>
									</div>
							    </div>
							</div>
						
	<script>
      $('#formedit').submit(function(e){
		e.preventDefault();
		  var data = new FormData(this);
		  $.ajax(
		   {
			type: 'POST',
            url: 'cbtserver_master/tsiswa.php?pg=edit',
			enctype: 'multipart/form-data',
            data: data,
			cache: false,
			contentType: false,
			processData: false,
			success: function(data){   		
			setTimeout(function()
				{
				window.location.reload();
						}, 2000);
									  
						}
					});
				return false;
			});
		</script>	
                 <?php endif; ?>	
                                     <script>
									$('#data1').on('click', '.hapus', function() {
									var id = $(this).data('id');
									console.log(id);
									Swal.fire({
											  title: 'Yakin hapus data?',
											  text: "You won't be able to revert this!",
											  type: 'warning',
											  showCancelButton: true,
											  confirmButtonColor: '#3085d6',
											  cancelButtonColor: '#d33',
											  confirmButtonText: 'Ya, Hapus!',
											  cancelButtonText: "Batal"				  
									}).then((result) => {
										if (result.value) {
											$.ajax({
											   url: 'cbtserver_master/tsiswa.php?pg=hapus',
												method: "POST",
												data: 'id=' + id,
												success: function(data) {
													 Swal.fire(
													 
													  'Deleted!',
													  'Your file has been deleted.',
													  'success'
													) 
													setTimeout(function() {
														window.location.replace('?pg=siswa');
													}, 1000);
												}
											});
										}
										return false;
									})

								});

							</script>		
		          <script>
				$("#confirm").click(function(){
		    	Swal.fire({
				  title: 'Reset Data Siswa',
				  text: "Data Siswa akan dikosongkan !",
				  type: 'warning',
				  showCancelButton: true,
				  confirmButtonColor: '#3085d6',
				  cancelButtonColor: '#d33',
				  confirmButtonText: 'Yes, Reset!'
				}).then((result) => {
				  if (result.value) {
					$.ajax({
					url: 'cbtserver_master/tsiswa.php?pg=kosong',
					success: function(data) {
						 Swal.fire(
				      'Deleted!',
				      'Your file has been deleted.',
				      'success'
				    )
				   setTimeout(function() {
					window.location.reload();
					}, 1000);
						}
					});
					}
					return false;
						})

						});
					</script>	
	