<?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 DESKRIPSI KI-4</h5>
										 <a href="erapor" class="btn btn-secondary kanan" data-bs-toggle="tooltip" data-bs-placement="top" title="Kembali"><i class="material-icons">arrow_back</i></a>						
                                    </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>TK</th>													
                                                   
                                                    <th>MATA PELAJARAN</th>
													 <th>GURU PENGAMPU</th>
													  <th>KD</th>
													 <th>DES</th>
													  <th></th>
                                                </tr>
                                            </thead>
                                            <tbody>
											<?php
											$no=0;
											if($user['level']=='admin'):
											$query = mysqli_query($koneksi, "SELECT * FROM jadwal_rapor WHERE kurikulum='K-2013'"); 
											else:
											$query = mysqli_query($koneksi, "SELECT * FROM jadwal_rapor WHERE kurikulum='K-2013' AND idguru='$user[id_user]'"); 	
											endif;
											  while ($data = mysqli_fetch_array($query)) :
											 $mapel = fetch($koneksi,'mata_pelajaran',['kode'=>$data['kodemap']]);
											  $peg = fetch($koneksi,'users',['id_user'=>$data['idguru']]);
											 $jdes = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM deskrip WHERE kodemap='$data[kodemap]' AND tingkat='$data[tingkat]' AND idguru='$data[idguru]' AND ket='KI4'"));
											$no++;
											   ?>
                                                <tr>
                                                    <td><?= $no; ?></td>
                                                     <td><?= $data['tingkat'] ?></td>
                                                      
													   <td><?= $mapel['nama_mapel'] ?></td>
													  <td><?= $peg['nama'] ?></td>
													   <td>
													   <h5><span class="badge badge-primary"><?= $data['jumkd'] ?></span></h5>
													   </td>
													   <td>
													   <h5><span class="badge badge-dark"><?= $jdes ?></span></h5>
													   </td>
													  <td>
											<a href="?pg=deskrip4&ac=view&id=<?= $data['id'] ?>" class="btn btn-sm btn-success" data-bs-toggle="tooltip" data-bs-placement="top" title="Lihat Data Deskripsi"><i class="material-icons">search</i> </a>
											<?php if($data['jumkd']==$jdes): ?>
											<a href="#" class="btn btn-sm btn-secondary"  disabled><i class="material-icons">lock</i> </a>
											<?php else: ?>
											<a href="?pg=deskrip4&idj=<?= $data['id'] ?>" class="btn btn-sm btn-primary" data-bs-toggle="tooltip" data-bs-placement="top" title="Tambah Deskripsi"><i class="material-icons">add</i> </a>
											<?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">
                                  
                                    </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>
											<?php $idj = $_GET['idj']; ?>
											<?php 
											$desQ = fetch($koneksi,'jadwal_rapor',['id'=>$idj]); 
											 $jdes = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM deskrip WHERE kodemap='$desQ[kodemap]' AND tingkat='$desQ[tingkat]' AND idguru='$desQ[idguru]' AND ket='KI4'"));
											?>
									<div class="widget-payment-request-info m-t-md">
									 <div class="d-grid gap-2">
									<button class="btn btn-primary" type="button" disabled>
										<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
										 INPUT DESKRIPSI KI-4 (Pengetahuan}
									</button>
											</div>
											<?php if($desQ['jumkd']<>0): ?>
									<form id='formdeskrip3' class="row g-1">                         
                               <div class="col-md-12">
								<label class="form-label bold">Tingkat</label>
								<select name='level' id='level' class='form-select' required='true' style="width: 100%">
								    <option value="<?= $desQ['tingkat'] ?>"><?= $desQ['tingkat'] ?></option>									
									 </select>
							     </div>	
							             
								
							<div class="col-md-12">
								<label class="form-label bold">Mata Pelajaran</label>
								<select name='mapel' id='mapel' class='form-select' required='true' style="width: 100%">
								  <option value="<?= $desQ['kodemap'] ?>"><?= $desQ['kodemap'] ?></option>	
									 </select>
							</div>
							 <div class="col-md-12">
								<label class="form-label bold">Deskripsi (maximal 200 karakter)</label>
							   <textarea name="deskrip" class="form-control" rows="5" required="true" maxlength="200" ></textarea>							   
							   </div>
							   <div id="count">
								<span id="current_count">0</span>
								<span id="maximum_count">/ 200</span>
							</div>
							<script type="text/javascript">
							$('textarea').keyup(function() {    
								var characterCount = $(this).val().length,
									current_count = $('#current_count'),
									maximum_count = $('#maximum_count'),
									count = $('#count');    
									current_count.text(characterCount);        
							});
							</script>
								 <div class="col-md-12">
								<label class="form-label bold">Guru Pengampu</label>
								<select name="guru" class='form-select' required='true' style="width: 100%" >
										 <?php
													
										$guruku = mysqli_query($koneksi, "SELECT * FROM users where  id_user='$desQ[idguru]'");
										while ($guru = mysqli_fetch_array($guruku)) {										
										echo "<option value='$guru[id_user]'>$guru[nama]</option>";
													}
													?>
												</select>
							          </div><p>
									  
								           <div class="d-grid gap-2">
										   <?php if($desQ['jumkd']==$jdes): ?>
										    <button type="submit" class="btn btn-light flex-grow-1 m-l-xxs" disabled>Simpan</button>
										   <?php else : ?>
                                         <button type="submit" class="btn btn-primary flex-grow-1 m-l-xxs" id="blockui-2">Simpan</button>
                                           <?php endif; ?>
										   </div>
										</form>
										<?php else: ?>
										<div class="widget-payment-request-info m-t-md">
										 <div class="d-grid gap-2">
										 
										<button class="btn btn-dark" type="button" disabled>Pastikan Jumlah KD Mapel Sudah di isi</button>
										 </div>
										  </div>
										<?php endif; ?>
										</div>
									 </div>
					            </div>
								</div>
							</div>
						</div>
					</div>
				</div>
							<script>
						$('#formdeskrip3').submit(function(e) {
								e.preventDefault();
								var data = new FormData(this);
								$.ajax({
									type: 'POST',
									 url: 'cbtserver_rapor/tdeskrip.php?pg=deskrip4',
									enctype: 'multipart/form-data',
									data: data,
									cache: false,
									contentType: false,
									processData: false,
									beforeSend: function() {},
									success: function(data) {
									   
										setTimeout(function() {
											window.location.reload();
										}, 1500);
									}
								})
								return false;
							});
							</script>
							
							
             <?php elseif ($ac == 'view'): ?>
			 <?php
			 $id = $_GET['id'];
			 $jadwal = fetch($koneksi,'jadwal_rapor',['id'=>$id]);
			 ?>
	                  <div class="row">
                          <div class="col-md-12">
                                <div class="card">
                                    <div class="card-header">
                                        <h5>DESKRIPSI KI-4 <span class="badge badge-secondary"><?= $jadwal['kodemap'] ?></span></h5>
										<div class="pull-right">
										 <a href="?pg=deskrip4" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="top" title="Kembali"><i class="material-icons">arrow_back</i></a>						
										</div>
                                    </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 width="10%">TINGKAT</th>													
                                                    <th width="10%">KODE</th>
                                                    <th>DESKRIPSI</th>									
													 <th width="10%">ACTION</th>
                                                </tr>
                                            </thead>
                                            <tbody>
											<?php
											$no=0;
											$query = mysqli_query($koneksi, "SELECT * FROM deskrip WHERE kodemap='$jadwal[kodemap]' AND tingkat='$jadwal[tingkat]' AND idguru='$jadwal[idguru]' AND ket='KI4'"); 	
											  while ($data = mysqli_fetch_array($query)) :
											
											$no++;
											   ?>
                                                <tr>
                                                    <td><?= $no; ?></td>
                                                     <td><?= $data['tingkat'] ?></td>
                                                      <td><?= $data['kodemap'] ?></td>
													   <td><?= $data['deskripsi'] ?></td>
													  <td>
													  <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#example<?= $no; ?>"><i class="material-icons">edit</i></button>
													  <button data-id="<?= $data['id'] ?>"  class="hapus btn btn-sm btn-danger" data-bs-toggle="tooltip" data-bs-placement="top" title="Hapus"><i class="material-icons">delete</i></button>
													  </td>
													   <div class="modal fade" id="example<?= $no; ?>" tabindex="-1" aria-labelledby="exampleModalLiveLabel" aria-hidden="true">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title" id="exampleModalLiveLabel">EDIT DESKRIPSI</h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                            </div>
                                                            <div class="modal-body">
															<form id='formdes<?= $no; ?>' class="row g-1">
																<input type="hidden" name="id" value="<?= $data['id'] ?>">															
                                                               <div class="col-md-12">
																<label class="form-label bold">Deskripsi (maximal 200 karakter)</label>
															   <textarea name="deskrip" class="form-control" rows="5" required="true" maxlength="200"><?= $data['deskripsi'] ?></textarea>							   
															   </div>
															   <div id="count">
																<span id="current_count">0</span>
																<span id="maximum_count">/ 200</span>
															</div>
															<script type="text/javascript">
															$('textarea').keyup(function() {    
																var characterCount = $(this).val().length,
																	current_count = $('#current_count'),
																	maximum_count = $('#maximum_count'),
																	count = $('#count');    
																	current_count.text(characterCount);        
															});
															</script>
                                                            </div>
                                                            <div class="modal-footer">
                                                               
                                                                <button type="submit"  type="button" class="btn btn-primary">Simpan</button>
                                                            </div>
															</form>
                                                        </div>
                                                    </div>
                                                </div>
												 <script>
											  $('#formdes<?= $no; ?>').submit(function(e) {
												e.preventDefault();
												var data = new FormData(this);
												$.ajax({
													type: 'POST',
													 url: 'cbtserver_rapor/tdeskrip.php?pg=edit_deskrip4',
													enctype: 'multipart/form-data',
													data: data,
													cache: false,
													contentType: false,
													processData: false,
													beforeSend: function() {},
													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.reload();
														}, 1500);
													}
												})
												return false;
											});
											</script>
												
                                                </tr>
												<?php endwhile; ?>
                                                </table>
												 </div>
											  </div>
										   </div>
										</div>
									</div>
								</div>	
								
									<script>
									$('#datatable1').on('click', '.hapus', function() {
									var id = $(this).data('id');
									console.log(id);
									swal({
											  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_rapor/tdeskrip.php?pg=hapus4',
												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>    	
					  <?php endif ?>
					  
					  
					  
					  	  
					  
					  
					