<?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=sarana&ac=input" class='btn btn-sm btn-success'><i class='fas fa-plus-circle'></i>Sarana</a>
                  
                    </div>
					<h5 class='card-title'><i class="fas fa-th"></i> Sarana 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>Jenis</th>
									  <th>No Registrasi</th>
                                    <th>Nama Sarana</th>  
                                     <th>Keperluan</th> 	
									 <th>Kondisi</th>
                                      							  
									<th>Action</th>
                                </tr>
                            </thead>
							
							<tbody>
							
                                <?php
								$no = 0;
								 
								 $query = mysqli_query($koneksi, "select * from sarana");								  
                            while ($sarana = mysqli_fetch_array($query)) {
							
                                $no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
									 <td style="color:blue"><?= $sarana['jenis'] ?></td>
									 <td><?= $sarana['noreg'] ?></td>
							        <td><?= $sarana['nama'] ?> </td>
									<td><?= $sarana['keperluan'] ?> </td>
									<td><?= $sarana['kondisi'] ?></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=sarana&ac=foto&id=<?= $sarana['id_sarana'] ?>" 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>
                                        <a href="?pg=sarana&ac=edit&id=<?= $sarana['id_sarana'] ?>" class='btn btn-sm btn-outline-primary' data-bs-toggle="tooltip" data-bs-placement="top" title="Edit"><i class='fas fa-edit'></i></a>
										 <button data-id="<?= $sarana['id_sarana'] ?>" class="hapus btn-sm btn btn-outline-danger" data-bs-toggle="tooltip" data-bs-placement="top" title="Hapus"><i class="fas fa-times"></i></button>
                                   </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 sarana</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($sarana['foto']<>''): ?>
																	<img src="../<?= $sarana['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($sarana['foto']<>''): ?>
																	<img src="../<?= $sarana['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($sarana['foto']<>''): ?>
																	<img src="../<?= $sarana['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>
                     
	
	<script>
	
	 $('#datatable1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Are you sure?',
            text: 'Akan menghapus data ini!',
			type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'iya, hapus'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_sapras/crudsarana.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>
	<?php } elseif ($ac == 'input') { ?>
	<?php

$tahun = date ('Y');
$query = "SELECT max(id) as maxKode FROM sarana";
$hasil = mysqli_query($koneksi,$query);
$data  = mysqli_fetch_array($hasil);
$no= $data['maxKode'];
$noUrut= $no + 1;
$kode =  sprintf("%02s", $noUrut);
$kodeQ = "SAP-S".$kode;
if($setting['jenjang']=='SD'){
$nomorbaru = $tahun."-SD.SAP-S".$kode;
}elseif($setting['jenjang']=='SMP'){
$nomorbaru = $tahun."-SMP.SAP-S".$kode;
}elseif($setting['jenjang']=='SMA'){
$nomorbaru = $tahun."-SMA.SAP-S".$kode;
}elseif($setting['jenjang']=='SMK'){
$nomorbaru = $tahun."-SMK.SAP-S".$kode;
}elseif($setting['jenjang']=='PAKET-A'){
$nomorbaru = $tahun."-PKBM-A.SAP-S".$kode;
}elseif($setting['jenjang']=='PAKET-B'){
$nomorbaru = $tahun."-PKBM-B.SAP-S".$kode;
}elseif($setting['jenjang']=='PAKET-C'){
$nomorbaru = $tahun."-PKBM-C.SAP-S".$kode;
}
$bulane=fetch($koneksi,'bulan',['bln'=>$blnQ]);
?>
	 <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 Sarana</h5>
					</div>
                      <div class="card-body">
                                <form id="formjadwal" >
                                         <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Jenis Sarana</label>
							                <div class="col-sm-9">
                                          <select name='jenis'  class='form-select' required='true'>
                                                        <option value=''></option>
                                                        <?php
                                                        $levelQ = mysqli_query($koneksi, "SELECT * FROM m_sarana");
                                                        while ($r = mysqli_fetch_array($levelQ)) {
                                                            echo "<option value='$r[nama_sarana]'>$r[nama_sarana]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                        </div>
                                        </div>
										 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kode</label>
								   <div class="col-sm-9">
						            <input type="text"  name="kode" value="<?= $kodeQ ?>" class="form-control" readonly>
						         </div>
						             </div>
											 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Nama Sarana</label>
								   <div class="col-sm-9">
						            <input type="text"  name="nama" class="form-control" required>
						         </div>
						             </div>
									 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">No Registrasi</label>
								   <div class="col-sm-9">
						            <input type="text"  name="noreg" value="<?= $nomorbaru ?>" class="form-control" readonly>
						         </div>
						             </div>
									 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Untuk Keperluan</label>
								   <div class="col-sm-9">
						            <input type="text"  name="keperluan" class="form-control" required>
						         </div>
						             </div>
									   <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Kondisi</label>
							                <div class="col-sm-9">
                                          <select name='kondisi'  class='form-select' required='true'>
                                               <option value=''></option>
                                                  <option value='Baik'> Baik</option>
                                                  <option value='Rusak'> Rusak</option>												  
                                                    </select>
                                        </div>
                                        </div>
										 <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Lokasi Sarana</label>
							                <div class="col-sm-9">
                                          <select name='lokasi'  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[nama]'>$s[nama]</option>";
                                                        }
                                                        ?>										  
                                                    </select>
                                        </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=sarana',
            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=sarana');
                    }, 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 == 'edit') { ?>
	 <?php $sarana=fetch($koneksi,'sarana',['id_sarana'=>$_GET['id']]); ?>	
	 <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> Edit Sarana</h5>
					</div>
                      <div class="card-body">
                                <form id="formjadwal" >
								<input type="hidden" name="ids" value="<?= $sarana['id_sarana'] ?>" >
                                         <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Jenis Sarana</label>
							                <div class="col-sm-9">
                                          <select name='jenis'  class='form-select' required='true'>
                                                        <option value="<?= $sarana['jenis'] ?>"><?= $sarana['jenis'] ?></option>
                                                        <?php
                                                        $levelQ = mysqli_query($koneksi, "SELECT * FROM m_sarana");
                                                        while ($r = mysqli_fetch_array($levelQ)) {
                                                            echo "<option value='$r[nama_sarana]'>$r[nama_sarana]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                        </div>
                                        </div>
										 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kode</label>
								   <div class="col-sm-9">
						            <input type="text"  name="kode" value="<?= $sarana['kode'] ?>" class="form-control" readonly>
						         </div>
						             </div>
											 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Nama Sarana</label>
								   <div class="col-sm-9">
						            <input type="text"  name="nama" value="<?= $sarana['nama'] ?>" class="form-control" required>
						         </div>
						             </div>
									 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">No Registrasi</label>
								   <div class="col-sm-9">
						            <input type="text"  name="noreg" value="<?= $sarana['noreg'] ?>" class="form-control" readonly>
						         </div>
						             </div>
									 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Untuk Keperluan</label>
								   <div class="col-sm-9">
						            <input type="text"  name="keperluan" value="<?= $sarana['keperluan'] ?>" class="form-control" required>
						         </div>
						             </div>
									   <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Kondisi</label>
							                <div class="col-sm-9">
                                          <select name='kondisi'  class='form-select' required='true'>
                                               <option value="<?= $sarana['kondisi'] ?>"><?= $sarana['kondisi'] ?></option>
                                                  <option value='Baik'> Baik</option>
                                                  <option value='Rusak'> Rusak</option>												  
                                                    </select>
                                        </div>
                                        </div>
										 <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Lokasi Sarana</label>
							                <div class="col-sm-9">
                                          <select name='lokasi'  class='form-select' required='true'>
                                               <option value="<?= $sarana['lokasi'] ?>"><?= $sarana['lokasi'] ?></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[nama]'>$s[nama]</option>";
                                                        }
                                                        ?>										  
                                                    </select>
                                        </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=editsarana',
            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=sarana');
                    }, 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 $sarana=fetch($koneksi,'sarana',['id_sarana'=>$_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> Sarana</h5>
					</div>
                      <div class="card-body">
                             
                                         <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Jenis Sarana</label>
							                <div class="col-sm-9">
                                          <select name='jenis'  class='form-select' required='true'>
                                                        <option value="<?= $sarana['jenis'] ?>"><?= $sarana['jenis'] ?></option>
                                                        <?php
                                                        $levelQ = mysqli_query($koneksi, "SELECT * FROM m_sarana");
                                                        while ($r = mysqli_fetch_array($levelQ)) {
                                                            echo "<option value='$r[nama_sarana]'>$r[nama_sarana]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                        </div>
                                        </div>
										 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kode</label>
								   <div class="col-sm-9">
						            <input type="text"  name="kode" value="<?= $sarana['kode'] ?>" class="form-control" readonly>
						         </div>
						             </div>
											 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Nama Sarana</label>
								   <div class="col-sm-9">
						            <input type="text"  name="nama" value="<?= $sarana['nama'] ?>" class="form-control" required>
						         </div>
						             </div>
									 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">No Registrasi</label>
								   <div class="col-sm-9">
						            <input type="text"  name="noreg" value="<?= $sarana['noreg'] ?>" class="form-control" readonly>
						         </div>
						             </div>
									 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Untuk Keperluan</label>
								   <div class="col-sm-9">
						            <input type="text"  name="keperluan" value="<?= $sarana['keperluan'] ?>" class="form-control" required>
						         </div>
						             </div>
									   <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Kondisi</label>
							                <div class="col-sm-9">
                                          <select name='kondisi'  class='form-select' required='true'>
                                               <option value="<?= $sarana['kondisi'] ?>"><?= $sarana['kondisi'] ?></option>
                                                  <option value='Baik'> Baik</option>
                                                  <option value='Rusak'> Rusak</option>												  
                                                    </select>
                                        </div>
                                        </div>
										 <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Lokasi Sarana</label>
							                <div class="col-sm-9">
                                          <select name='lokasi'  class='form-select' required='true'>
                                               <option value="<?= $sarana['lokasi'] ?>"><?= $sarana['lokasi'] ?></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[nama]'>$s[nama]</option>";
                                                        }
                                                        ?>										  
                                                    </select>
                                        </div>
                                        </div>
											<p>	  
								
                            </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 Sarana</h5>
					</div>
                      <div class="card-body">
                                <form id="formjadwal" >
								<input type="hidden" name="ids" value="<?= $sarana['id_sarana'] ?>" >
                                         <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/crudsarana.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.replace('?pg=sarana');
                }, 2000);

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

	<?php } ?>