  <?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
					   <div class="row">
                            <div class="col-md-8">
                                <div class="card">
                                    <div class="card-header">									
                                      <h5 class="card-title">Data Users - Guru</h5>
									
                                    </div>
                                    <div class="card-body">
									<div class="table-responsive">
                                               
                                    <table id="datatable1" class="table table-hover">
                                            <thead>
                                                <tr>                                                  
                                                    <th >Nama</th>
                                                    <th>User</th>
                                                    <th>Pass</th>
                                                      <th>Walas</th>													
													  <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
											 <?php $guru = mysqli_query($koneksi, "SELECT * FROM users where level<>'admin' ORDER BY nama ASC"); ?>
											<?php while ($p = mysqli_fetch_array($guru)) : ?>
												<?php $no++; ?>
                                                <tr style="height:10px">
                                                    <td><?= $p['nama'] ?></td>
                                                    <td><?= $p['username'] ?></td>
                                                    <td><?= $p['password'] ?></td>
                                                    <td><?= $p['jabatan'] ?></td>
                                                    <td>
													 <a href="?pg=guru&ac=edit&id=<?= $p['id_user'] ?>"> <button class='btn btn-sm btn-outline-primary'><i class='fa fa-edit'></i></button></a>
													<button data-id="<?= $p['id_user'] ?>"  class="hapus btn btn-sm btn-outline-danger"><i class='fa fa-trash'></i></button>
													
													</td>
                                                </tr>
                                               <?php endwhile ?>
                                               
                                            </tbody>
                                        </table>
                                  </div>
                               </div>
                             </div>	
						</div>
			<script>
        $('#datatable1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        Swal.fire({
				  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: 'sandik_admin/crud_admin.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 if($ac == ''): ?>							  
                            <div class="col-md-4">
                                <div class="card" >
                                    <div class="card-header">
                                        <h5 class="card-title">Tambah User - Guru</h5>
                                         </div>
                                  
                                            <div class="card-body">
                                               <form id="formguru">
                                                    <div class="col-md-12">
                                                        <label class="bold">N I P</label>
                                                        <input type="text" name="nip" class="form-control" required="true">
                                                    </div>
                                                    <div class="col-md-12">
                                                        <label class="bold">Nama Lengkap</label>
                                                        <input type="text" name="nama" class="form-control" required="true">
                                                    </div>
                                                    <div class="col-12">
                                                        <label class="bold">Username</label>
                                                        <input type="text" name="username" class="form-control"  required="true">
                                                    </div>
                                                    <div class="col-12">
                                                        <label class="bold">No WA</label>
                                                        <input type="number" name="nowa" class="form-control" required="true" >
                                                    </div>
                                                    <div class="col-md-12">
                                                        <label class="bold">Wali Kelas</label>
                                                        <select class="form-select" name="walas" >
														<option value="">Bukan Walas</option>
														<?php $q = mysqli_query($koneksi, "select * from kelas WHERE id_kelas<>'' ");
														while ($data = mysqli_fetch_array($q)) { ?>
															<option value="<?= $data['id_kelas'] ?>"><?= $data['id_kelas'] ?></option>
														<?php } ?>
													</select>
                                                    </div>
													 <div class="col-12">
                                                        <label class="bold">Password</label>
                                                        <input type="password" name="password" class="form-control"  required="true">
                                                    </div>
													<br>
													<div class="d-grid gap-2">
													
                                                   <button type="submit" class="btn btn-outline-primary">Simpan</button>
													
                                                    </div>
                                            </div>
                                          </form >
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
	<script>
    $('#formguru').submit(function(e){
    e.preventDefault();
    var data = new FormData(this);
    $.ajax(
    {
        type: 'POST',
        url: 'sandik_admin/crud_admin.php?pg=tambah_guru',
        enctype: 'multipart/form-data',
        data: data,
        cache: false,
        contentType: false,
        processData: false,
        success: function(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.warning(
            {
                title: 'Gagal!',
                message: 'Username sudah digunakan',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: '#8b0000',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'
            });
            setTimeout(function(){
                window.location.reload();
            }, 2000);	
           }			
        }
    });
    return false;
});
</script>	
                        <?php elseif($ac == 'edit'): ?>	
                        <?php
						$id = $_GET['id'];
						$guru = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM users WHERE id_user='$id'"));
                          ?>			
                            <div class="col-md-4">
                                <div class="card" >
                                    <div class="card-header">
                                        <h5 class="card-title">Edit User - Guru</h5>
                                         </div>
                                  
                                            <div class="card-body">
                                               <form id="formedit">
                                                    <div class="col-md-12">
                                                        <label class="bold">N I P</label>
                                                        <input type="text" name="nip" value="<?= $guru['nip'] ?>" class="form-control" required="true">
														<input type="hidden" name="id" value="<?= $guru['id_user'] ?>" class="form-control" required="true">
                                                    </div>
                                                    <div class="col-md-12">
                                                        <label class="bold">Nama Lengkap</label>
                                                        <input type="text" name="nama" value="<?= $guru['nama'] ?>" class="form-control" required="true">
                                                    </div>
                                                    <div class="col-12">
                                                        <label class="bold">Username</label>
                                                        <input type="text" name="username" class="form-control" value="<?= $guru['username'] ?>" readonly>
                                                    </div>
                                                    <div class="col-12">
                                                        <label class="bold">No WA</label>
                                                        <input type="number" name="nowa" class="form-control" value="<?= $guru['nowa'] ?>" required="true" >
                                                    </div>
                                                    <div class="col-md-12">
                                                        <label class="bold">Wali Kelas</label>
                                                        <select class="form-select" name="walas" >
														<option value="<?= $guru['jabatan'] ?>"><?= $guru['jabatan'] ?></option>
														<option value="">Bukan Walas</option>
														<?php $q = mysqli_query($koneksi, "select * from kelas WHERE id_kelas<>'' ");
														while ($data = mysqli_fetch_array($q)) { ?>
															<option value="<?= $data['id_kelas'] ?>"><?= $data['id_kelas'] ?></option>
														<?php } ?>
													</select>
                                                    </div>
													 <div class="col-12">
                                                        <label class="bold">Password</label>
                                                        <input type="text" name="password" value="<?= $guru['password'] ?>" class="form-control"  required="true">
                                                    </div>
													<br>
													<div class="d-grid gap-2">
													
                                                   <button class="btn btn-outline-primary">Simpan</button>
													
                                                    </div>
                                            </div>
                                          </form >
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
	<script>
    $('#formedit').submit(function(e){
    e.preventDefault();
    var data = new FormData(this);
    $.ajax(
    {
        type: 'POST',
        url: 'sandik_admin/crud_admin.php?pg=edit_guru',
        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=guru');
            }, 2000);
		  
        }
    });
    return false;
});
</script>	
				<?php endif; ?>