<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
<?php if ($ac == '') { ?>
   <div class='row'>
   <div class='col-md-12'>
      <div class="card">
             <div class="card-header">
                    <div class='right '>
					  <a href="?pg=kondisi&ac=input" class='btn btn-sm btn-success'><i class='fas fa-plus-circle'></i>Kondisi</a>
                  
                    </div>
					<h5 class='card-title'><i class="fas fa-th"></i> Kondisi Bangunan Sekolah</h5>
                </div>
						 
                <div class='card-body'>
                    <div class='table-responsive'>
                        <table  id='datatable1' class='table table-hover'>
                            <thead>
                                <tr>
                                    <th width='3px'>No</th>
									<th>Nama Ruang</th>
									  <th>Nilai Kerusakan</th>
                                    <th>Keterangan</th>  
                                     <th>Usulan Bantuan</th> 	
										<th>Action</th>		  
                                </tr>
                            </thead>
							
							<tbody>
							
                                <?php
								$no = 0;
								 
								 $query = mysqli_query($koneksi, "select * from sapras WHERE jenis='ruang'");								  
                            while ($kondisi = mysqli_fetch_array($query)) {
							$kerusakan = $kondisi['atap']+$kondisi['plafon']+$kondisi['dinding']+$kondisi['kusen']+$kondisi['lante']+$kondisi['pondasi']+$kondisi['utilitas'];
                              $rusak = ($kerusakan/7);
							  
							    if($rusak<=30){
								{$ket="Rusak ringan";}
								}elseif($rusak>=30 && $rusak<=45){
								{$ket="Rusak sedang";}
								}elseif($rusak>=45 && $rusak<=65){
								{$ket="Rusak berat";}
								}elseif($rusak>=65){
								{$ket="Rusak total";}
								}	
								$no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
									 <td style="color:blue"><?= $kondisi['nama'] ?></td>
									 <td><?= number_format($rusak) ?> %</td>
							        <td><?= $ket ?> </td>
									<td>
									<?php if($rusak >=45){ ?>
									<b>RKB <?= $kondisi['nama'] ?></b>
									<?php }else{ ?>
									<b>Rehab <?= $kondisi['nama'] ?></b>
									<?php } ?>
									</td>
									
                                   <td style="text-align:center">
								   <button type="button" class="btn btn-sm btn-outline-info" data-bs-toggle="modal" data-bs-target="#modal<?= $no ?>"><i class='fas fa-eye'></i></button>
								    <a href="?pg=kondisi&ac=foto&id=<?= $kondisi['id_sapras'] ?>" class='btn btn-sm btn-outline-success' data-bs-toggle="tooltip" data-bs-placement="top" title="Upload Foto"><i class='fas fa-plus-circle'></i></a>
                                        
                                   </td>
								   <div class="modal fade" id="modal<?= $no ?>" tabindex="-1" aria-labelledby="modal<?= $no ?>" aria-hidden="true">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title" id="modal<?= $no ?>">Foto kondisi</h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                            </div>
                                                            <div class="modal-body">
                                                              <div id="carouselExampleSlidesOnly" class="carousel slide" data-bs-ride="carousel">
															<div class="carousel-inner">
																<div class="carousel-item active">
																<?php if($kondisi['foto']<>''): ?>
																	<img src="../<?= $kondisi['foto'] ?>" class="d-block w-100" alt="...">
																<?php else: ?>
																<img src="../foto/sapras/avatar.png" class="d-block w-100" alt="...">
																<?php endif ?>
																</div>
																<div class="carousel-item">
																<?php if($kondisi['foto']<>''): ?>
																	<img src="../<?= $kondisi['foto'] ?>" class="d-block w-100" alt="...">
																<?php else: ?>
																<img src="../foto/sapras/avatar.png" class="d-block w-100" alt="...">
																<?php endif ?>
																</div>
																<div class="carousel-item">
																<?php if($kondisi['foto']<>''): ?>
																	<img src="../<?= $kondisi['foto'] ?>" class="d-block w-100" alt="...">
																<?php else: ?>
																<img src="../foto/sapras/avatar.png" class="d-block w-100" alt="...">
																<?php endif ?>
																</div>
															</div>
														</div>
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                                                
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                     
							       </tr>
                            <?php } ?>
                        </tbody>
                        </table>
                    </div>
                    </div>
					</div>
                     
	
	
	<?php } elseif ($ac == 'input') { ?>
	
	 <div class="row">
     <div class="col-md-12">
		<div class="card">
             <div class="card-header">									
                  <h5 class="card-title"><i class="fas fa-th"></i> Tambah kondisi</h5>
					</div>
                      <div class="card-body">
                                <form id="formjadwal" >
                                         <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Pilih Ruang</label>
							                <div class="col-sm-9">
                                          <select name='ids'  class='form-select' required='true'>
                                                        <option value=''></option>
                                                  <?php
                                                        $pras = mysqli_query($koneksi, "SELECT * FROM sapras WHERE jenis='ruang' GROUP BY nama");
                                                        while ($s = mysqli_fetch_array($pras)) {
                                                            echo "<option value='$s[id_sapras]'>$s[nama]</option>";
                                                        }
                                                        ?>	
                                                    </select>
                                        </div>
                                        </div>
										 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kerusakan Atap (%)</label>
								   <div class="col-sm-2">
						            <input type="number"  name="atap"  class="form-control" >
						         </div>					            
										<label  class="col-md-2 col-form-label bold">Keterangan</label>
								   <div class="col-sm-5">
						            <input type="text"  name="ket_atap" class="form-control">
						           </div>
								     </div>
									  <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kerusakan Plafon (%)</label>
								   <div class="col-sm-2">
						            <input type="number"  name="plafon"  class="form-control" >
						         </div>					            
										<label  class="col-md-2 col-form-label bold">Keterangan</label>
								   <div class="col-sm-5">
						            <input type="text"  name="ket_plafon" class="form-control">
						           </div>
								   </div>
								    <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kerusakan Dinding (%)</label>
								   <div class="col-sm-2">
						            <input type="number"  name="dinding"  class="form-control" >
						         </div>					            
										<label  class="col-md-2 col-form-label bold">Keterangan</label>
								   <div class="col-sm-5">
						            <input type="text"  name="ket_dinding" class="form-control">
						           </div>
								   </div>
								   <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kerusakan Pintu & Jendela (%)</label>
								   <div class="col-sm-2">
						            <input type="number"  name="kusen"  class="form-control" >
						         </div>					            
										<label  class="col-md-2 col-form-label bold">Keterangan</label>
								   <div class="col-sm-5">
						            <input type="text"  name="ket_kusen" class="form-control">
						           </div>
								   </div>
								     
								    <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kerusakan Lantai (%)</label>
								   <div class="col-sm-2">
						            <input type="number"  name="lantai"  class="form-control" >
						         </div>					            
										<label  class="col-md-2 col-form-label bold">Keterangan</label>
								   <div class="col-sm-5">
						            <input type="text"  name="ket_lantai" class="form-control">
						           </div>
								   </div>
								    <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kerusakan Pondasi (%)</label>
								   <div class="col-sm-2">
						            <input type="number"  name="pondasi"  class="form-control" >
						         </div>					            
										<label  class="col-md-2 col-form-label bold">Keterangan</label>
								   <div class="col-sm-5">
						            <input type="text"  name="ket_pondasi" class="form-control">
						           </div>
								   </div>
								    <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kerusakan Utilitas (%)</label>
								   <div class="col-sm-2">
						            <input type="number"  name="utilitas"  class="form-control" >
						         </div>					            
										<label  class="col-md-2 col-form-label bold">Keterangan</label>
								   <div class="col-sm-5">
						            <input type="text"  name="ket_utilitas" class="form-control">
						           </div>
								   </div>
											<p>	  
											
								   <div class='right'>
									  <button type='submit'  class='btn btn-outline-primary'><i class='fa fa-check-circle'></i>Simpan</button>
										
											 </div>
								 
                                </form>
                            </div>
                        </div>
                    </div>
				
 <script>
  $('#formjadwal').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
             url: 'sandik_sapras/crudsarana.php?pg=kondisi',
            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: 'Data berhasil disimpan',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'	
                    });
                    setTimeout(function() {
                        window.location.replace('?pg=kondisi');
                    }, 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>

	<?php } elseif ($ac == 'foto') { ?>
	 <?php $kondisi=fetch($koneksi,'sapras',['id_sapras'=>$_GET['id']]); ?>	
	 <div class="row">
     <div class="col-md-7">
		<div class="card">
             <div class="card-header">									
                  <h5 class="card-title"><i class="fas fa-th"></i> Foto <?= $kondisi['nama'] ?></h5>
					</div>
                      <div class="card-body">
                             
                            <div id="carouselExampleSlidesOnly" class="carousel slide" data-bs-ride="carousel">
								<div class="carousel-inner">
									<div class="carousel-item active">
										<?php if($kondisi['foto']<>''): ?>
																	<img src="../<?= $kondisi['foto'] ?>" class="d-block w-100" alt="...">
																<?php else: ?>
																<img src="../foto/sapras/avatar.png" class="d-block w-100" alt="...">
																<?php endif ?>
																</div>
																<div class="carousel-item">
																<?php if($kondisi['foto']<>''): ?>
																	<img src="../<?= $kondisi['foto'] ?>" class="d-block w-100" alt="...">
																<?php else: ?>
																<img src="../foto/sapras/avatar.png" class="d-block w-100" alt="...">
																<?php endif ?>
																</div>
																<div class="carousel-item">
																<?php if($kondisi['foto']<>''): ?>
																	<img src="../<?= $kondisi['foto'] ?>" class="d-block w-100" alt="...">
																<?php else: ?>
																<img src="../foto/sapras/avatar.png" class="d-block w-100" alt="...">
																<?php endif ?>
																</div>
															</div>
								 </div>
                            </div>
                        </div>
                    </div>
				 <div class="col-md-5">
		<div class="card">
             <div class="card-header">									
                  <h5 class="card-title"><i class="fas fa-image"></i> Foto kondisi</h5>
					</div>
                      <div class="card-body">
                                <form id="formjadwal" >
								<input type="hidden" name="ids" value="<?= $kondisi['id_sapras'] ?>" >
                                         <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Upload Foto</label>
							                <div class="col-sm-9">
                                             <input type="file"  name="gambar"  class="form-control" required>
                                        </div>
                                        </div>
										 
											
											<p>	  
										  <div class='right'>
									  <button type='submit'  class='btn btn-outline-primary'><i class='fa fa-check-circle'></i>Simpan</button>
										
											 </div>	
								  
                                </form>
                            </div>
                        </div>
                    </div>
 <script>
	$('#formjadwal').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
            url: 'sandik_sapras/crudkondisi.php?pg=foto',
            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.reload();
                }, 2000);

            }
        });
        return false;
    });
</script>

	<?php } ?>