<?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=ruang&ac=input" class='btn btn-sm btn-success'><i class='fas fa-plus-circle'></i>Ruang</a>
                  
                    </div>
					<h5 class='card-title'><i class="fas fa-th"></i> Data Ruang</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 Ruang</th>
                                    <th>Panjang</th>  
                                     <th>Lebar</th> 	
									 <th>Luas</th>
                                       <th>Kapasitas</th>							  
									<th>Action</th>
                                </tr>
                            </thead>
							
							<tbody>
							
                                <?php
								$no = 0;
								 
								 $query = mysqli_query($koneksi, "select * from sapras WHERE jenis='ruang'");								  
                            while ($ruang = mysqli_fetch_array($query)) {
							
                                $no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
									 <td style="color:blue"><?= $ruang['kelompok'] ?></td>
									 <td><?= $ruang['noreg'] ?></td>
									 <td><?= $ruang['nama'] ?></td>
							        <td><?= $ruang['panjang'] ?> M</td>
									<td><?= $ruang['lebar'] ?> M</td>
									<td><?= number_format($ruang['luas']) ?> M<sup>2</sup></td>					
									<td><?= $ruang['kapasitas'] ?> </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=ruang&ac=foto&id=<?= $ruang['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>
                                        <a href="?pg=ruang&ac=edit&id=<?= $ruang['id_sapras'] ?>" 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="<?= $ruang['id_sapras'] ?>" 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 ruang</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($ruang['foto']<>''): ?>
																	<img src="../<?= $ruang['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($ruang['foto']<>''): ?>
																	<img src="../<?= $ruang['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($ruang['foto']<>''): ?>
																	<img src="../<?= $ruang['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/crud.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 sapras_reg WHERE kode='P'";
$hasil = mysqli_query($koneksi,$query);
$data  = mysqli_fetch_array($hasil);
$no= $data['maxKode'];
$noUrut= $no + 1;

$kode =  sprintf("%02s", $noUrut);
if($setting['jenjang']=='SD'){
$nomorbaru = $tahun."-SD.SAP-R".$kode;
}elseif($setting['jenjang']=='SMP'){
$nomorbaru = $tahun."-SMP.SAP-R".$kode;
}elseif($setting['jenjang']=='SMA'){
$nomorbaru = $tahun."-SMA.SAP-R".$kode;
}elseif($setting['jenjang']=='SMK'){
$nomorbaru = $tahun."-SMK.SAP-R".$kode;
}elseif($setting['jenjang']=='PAKET-A'){
$nomorbaru = $tahun."-PKBM-A.SAP-R".$kode;
}elseif($setting['jenjang']=='PAKET-B'){
$nomorbaru = $tahun."-PKBM-B.SAP-R".$kode;
}elseif($setting['jenjang']=='PAKET-C'){
$nomorbaru = $tahun."-PKBM-C.SAP-R".$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 Ruang</h5>
					</div>
                      <div class="card-body">
                                <form id="formjadwal" >
                                         <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Jenis</label>
							                <div class="col-sm-9">
                                             <select name='kelompok'  class='form-select' required='true'>
                                                        <option value=''></option>
                                                        <?php
                                                        $levelQ = mysqli_query($koneksi, "SELECT * FROM m_ruang ORDER BY id DESC");
                                                        while ($r = mysqli_fetch_array($levelQ)) {
                                                            echo "<option value='$r[nama_ruang]'>$r[nama_ruang]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                        </div>
                                        </div>
										 
											 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Nama Ruang</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">Lantai</label>
							                <div class="col-sm-9">
                                             <select name='lantai'  class='form-select' required='true'>
                                                 <option value=''></option>
                                                   <option value='1'>Lantai 1</option>
												    <option value='2'>Lantai 2</option>
													
                                                    </select>
                                        </div>
                                        </div>
											 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Panjang {Meter)</label>
								   <div class="col-sm-9">
						            <input type="number"  name="panjang" class="form-control" required>
						         </div>
						             </div>
										  
                                       <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Lebar {Meter)</label>
								   <div class="col-sm-9">
						            <input type="number"  name="lebar" class="form-control" required>
						         </div>
						             </div>
									  <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kapasitas</label>
								   <div class="col-sm-9">
						            <input type="number"  name="kapasitas" 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();
        $.ajax({
            type: 'POST',
             url: 'sandik_sapras/crud.php?pg=ruang',
            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=ruang');
                    }, 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 $ruang=fetch($koneksi,'sapras',['id_sapras'=>$_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 Prasarana ruang</h5>
					</div>
                      <div class="card-body">
                                <form id="formjadwal" >
								<input type="hidden" name="ids" value="<?= $ruang['id_sapras'] ?>" >
                                         <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Nama</label>
							                <div class="col-sm-9">
                                             <input type="text"  name="nama" value="<?= $ruang['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="<?= $ruang['noreg'] ?>" class="form-control" readonly>
						         </div>
						             </div>
									  <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Lantai</label>
							                <div class="col-sm-9">
                                             <select name='lantai'  class='form-select' required='true'>
                                                 <option value="<?= $ruang['lantai'] ?>"><?= $ruang['lantai'] ?></option>
                                                   <option value='1'>Lantai 1</option>
												    <option value='2'>Lantai 2</option>
													
                                                    </select>
                                        </div>
                                        </div>
											 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Panjang {Meter)</label>
								   <div class="col-sm-9">
						            <input type="number"  name="panjang" value="<?= $ruang['panjang'] ?>" class="form-control" required>
						         </div>
						             </div>
										  
                                       <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Lebar {Meter)</label>
								   <div class="col-sm-9">
						            <input type="number"  name="lebar" value="<?= $ruang['lebar'] ?>" class="form-control" required>
						         </div>
						             </div>
									 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kapasitas</label>
								   <div class="col-sm-9">
						            <input type="number"  name="kapasitas" value="<?= $ruang['kapasitas'] ?>" 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();
        $.ajax({
            type: 'POST',
             url: 'sandik_sapras/crud.php?pg=editruang',
            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=ruang');
                    }, 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 $ruang=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>Prasarana ruang</h5>
					</div>
                      <div class="card-body">
                             
                                          <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Nama</label>
							                <div class="col-sm-9">
                                             <input type="text"  name="nama" value="<?= $ruang['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="<?= $ruang['noreg'] ?>" class="form-control" readonly>
						         </div>
						             </div>
									  <div class="row mb-2">
											<label  class="col-md-3 col-form-label bold">Lantai</label>
							                <div class="col-sm-9">
                                             <select name='lantai'  class='form-select' required='true'>
                                                 <option value="<?= $ruang['lantai'] ?>"><?= $ruang['lantai'] ?></option>
                                                   <option value='1'>Lantai 1</option>
												    <option value='2'>Lantai 2</option>
													
                                                    </select>
                                        </div>
                                        </div>
											 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Panjang {Meter)</label>
								   <div class="col-sm-9">
						            <input type="number"  name="panjang" value="<?= $ruang['panjang'] ?>" class="form-control" required>
						         </div>
						             </div>
										  
                                       <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Lebar {Meter)</label>
								   <div class="col-sm-9">
						            <input type="number"  name="lebar" value="<?= $ruang['lebar'] ?>" class="form-control" required>
						         </div>
						             </div>
									 <div class="row mb-2">
										<label  class="col-md-3 col-form-label bold">Kapasitas</label>
								   <div class="col-sm-9">
						            <input type="number"  name="kapasitas" value="<?= $ruang['kapasitas'] ?>" class="form-control" required>
						         </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 Prasarana ruang</h5>
					</div>
                      <div class="card-body">
                                <form id="formjadwal" >
								<input type="hidden" name="ids" value="<?= $ruang['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/crud.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=ruang');
                }, 2000);

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

	<?php } ?>