<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
   <div class='row'>
        <div class='col-md-5'>
            <div class="card">
             <div class="card-header">	
               <h5 class="card-title"><i class="fas fa-th"></i> Master Prasarana Ruang</h5>
			   </div>    
			 
                <div class='card-body'>
                   <form id="formkategori" action="" >
                  
                                <div class='form-group'>
                                    <label class="bold">Jenis Prasarana Ruang</label>
									<div class="input-group">
                                    <input type="text" class="form-control" name="pras" autocomplete="off" required>
                                <span class="input-group-btn">
						   <button type='submit' name='submit' class='btn btn-success'><i class='fa fa-check-circle' ></i>Simpan</button>
                          </span>
                    </div>
					  </div>
					
                            
                </form>
            </div>
        </div>
    </div>
 
 <script>
	 $('#formkategori').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_sapras/crud.php?pg=mruang',
            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.reload();
					}, 2000);
					} else {
					 iziToast.info(
					{
					title: 'GAGAL!',
					message: 'Data sudah ada',
					titleColor: '#FFFF00',
					messageColor: '#fff',
					backgroundColor: 'rgba(0, 0, 0, 0.5)',
					 progressBarColor: '#FFFF00',
					 position: 'topRight'							  
						});
						setTimeout(function() {
							window.location.reload();
							}, 2000);
								}

							}
						});
						return false;
					}); 
  </script>
        <div class='col-md-7'>
		  <div class="card">
             <div class="card-header">	
               <h5 class="card-title">Master Prasarana 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 Prasarana Ruang</th>  
									<th width="5%"></th>

										</tr>
									</thead>							
							<tbody>							
                                <?php
								$no = 0;
								
								 $query = mysqli_query($koneksi, "select * from m_ruang");
                            while ($tjn = mysqli_fetch_array($query)) {
							
                                $no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
							        <td><?= $tjn['nama_ruang'] ?></td>
									
									<td>
									 <button data-id="<?= $tjn['id'] ?>" class="hapus btn-sm btn btn-default"><i class="fas fa-trash"></i></button>
									</td>
									</tr>
                            <?php } ?>
                        </tbody>
                        </table>
            </div>
        </div>
    </div>
	<script>
	$('#example1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Are you sure?',
            text: 'Akan menghapus Semua 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=hapusmpras',
                    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>