<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
<?php if ($ac == '') : ?>
    <div class='row'>
	<div class='col-md-8'>
             <div class="card">
			 <div class="card-header">	
				<h5 class='card-title'><i class="fas fa-users-cog"></i> Kandidat</h5>
                </div>
				
                <div class='card-body'>
                    <div class='table-responsive'>
                        <table style="font-size: 11px" id='datatable1' class='table table-hover'>
                            <thead>
                                <tr>
                                    <th width='3%'>#</th>
                                    <th width='30%'>Kandidat</th>
                                    <th width='5%'>No Urut </th>
                                    <th>Visi</th>
                                    <th>Misi</th>
                                     <th width='10%'> Photo</th>
                                     <th width='5%'>Hapus</th>
                                       
                                </tr>
                            </thead>
							 <tbody>
                            <?php
							$no=0;
							$query = mysqli_query($koneksi, "select * FROM kandidat");
							 while ($kandidat = mysqli_fetch_array($query)) {
							$siswa=fetch($koneksi,'siswa',['id_siswa'=>$kandidat['idsiswa']]);
							 $no++;
                            ?>
                                <tr>
                                    <td><?= $no; ?></td>
                                    <td><?= $siswa['nama'] ?></td>
                                    <td><?= $kandidat['nomor'] ?></td>
								<td><?= $kandidat['visi'] ?></td>
                                    <td><?= $kandidat['misi'] ?></td>
							<td><center><img src='../<?= $kandidat['gambar'] ?>' width="40px"></center></td>
							<td>
							 <button data-id="<?= $kandidat['id'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
                           </td>
						   <?php } ?>
                            


                        </tbody>
                        </table>
                    
                </div>
            </div>
        </div>
    </div>
					
                  <div class='col-md-4'>
             <div class="card">
			 <div class="card-header">	
				<h5 class='card-title'><i class="fas fa-users-cog"></i> Kandidat</h5>
                </div>
                                <div class='card-body'>
                                   <form id="formkandidat" action="" enctype="multipart/form-data">
                                        <div class='form-group'>
                                            <label class="bold">Tingkat</label>
                                          <select id='level' name='level' class='form-select' required='true'>
                                      <option value=''>Pilih Tingkat</option>
                                       <?php $quem = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY level"); ?>
                                                <?php while ($m = mysqli_fetch_array($quem)) : ?>
                                                    <option value="<?= $m['level'] ?>"><?= $m['level'] ?></option>
                                                <?php endwhile ?>
                                                    </select>         
                                        </div>
										 <div class='form-group'>
                                            <label class="bold">Kelas</label>
										<select name='kelas' id="kelas" class='form-select'  required='true'>
                            
                                                </select>
										<div class='form-group'>
                                            <label class="bold">Kandidat</label>
                                           <select name='kandidat' id="kandidat" class='form-select' required='true'>
                                                        
                                                    </select>
                                        </div>
                                        <div class='form-group'>                                       
                                                <label class="bold">No Urut Kandidat</label>
                                                    <select name='nomor' class='form-select' required='true'>
                                                        <option value=''>Pilih No Urut</option>
                                                       <option value='1'>1</option>
													   <option value='2'>2</option>
													    <option value='3'>3</option>
													    <option value='4'>4</option>
													    <option value='5'>5</option>
													    <option value='6'>6</option>
														 <option value='7'>7</option>
														  <option value='8'>8</option>
														   <option value='9'>9</option>
														    <option value='10'>10</option>
                                                    </select>
                                                </div>
                                              
                                                <div class='form-group'>
                                                    <label class="bold">Photo</label>
                                                    <input type='file' name='gambar' class='form-control'  />
                                                </div>
                                          
                                                <div class='form-group'>
                                                    <label class="bold">Visi</label>
                                                    <textarea name='visi' class='form-control' rows="3" autocomplete="off" required /></textarea>
                                                </div>
                                           
                                        <div class='form-group'>
                                                    <label class="bold">Misi</label>
                                                    <textarea name='misi' class='form-control' rows="3" autocomplete="off" required /></textarea>
                                               </div>
											   <p>
                                        <div class='right'>
                                          
                                                <button type='submit' name='submit' class='btn btn-sm btn-success'><i class='fa fa-check-circle'></i>Simpan</button>

                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                   
	              <script>
				  $("#level").change(function() {
					var level = $(this).val();
					console.log(level);
					$.ajax({
						type: "POST", 
						url: "sandik_vote/crud_vote.php?pg=ambil_kelas", 
						data: "level=" + level, 
						success: function(response) { 
							$("#kelas").html(response);
						}
					});
				});		
				 $("#kelas").change(function() {
					var kelas = $(this).val();
					console.log(kelas);
					$.ajax({
						type: "POST", 
						url: "sandik_vote/crud_vote.php?pg=ambil_siswa", 
						data: "kelas=" + kelas, 
						success: function(response) { 
							$("#kandidat").html(response);
						}
					});
				});				
			</script>
			
			                          
       <script>
	$('#formkandidat').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
            url: 'sandik_vote/crud_vote.php?pg=kandidat',
            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 elseif ($ac == 'edit') : ?>
    <?php
    $id = $_GET['id'];
    $siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM kandidat WHERE id='$id'"));
    ?>
    <div class='row'>
        <div class='col-md-2'></div>
        <div class='col-md-7'>
            <form id="form-edit" method='post'>
                <div class='box box-success'>
                    <div class='box-header with-border'>
                        <h3 class='box-title'>Edit</h3>
                        <div class='box-tools pull-right btn-group'>
                            <button type='submit' name='submit' class='btn btn-sm btn-success'><i class='fa fa-check'></i> Simpan</button>
                            <a href='?pg=<?= $pg ?>' class='btn btn-sm btn-danger' title='Batal'><i class='fa fa-times'></i></a>
                        </div>
                    </div><!-- /.box-header -->
                    <div class='box-body'>
                        <input type='hidden' name='idu' value="<?= $siswa['id'] ?>" />
                        
                        <div class='form-group'>
                            <label>Nama</label>
                            <input type='text' name='nama' class='form-control' value="<?= $siswa['nama'] ?>" required='true' />
                        </div>
                     
                            
                       <div class='row'>
                            <div class='col-md-12'>
                                <div class='form-group'>
                                    <label>Visi</label>
                                    <input type='text' name='visi' class='form-control' value="<?= $siswa['visi'] ?>" required='true' />
                                </div>
                            </div>
  </div>
                        <div class='row'>
                            <div class='col-md-12'>
                                <div class='form-group'>
                                    <label>Misi</label>
                                    <input type='text' name='misi' class='form-control' value="<?= $siswa['misi'] ?>" required='true' />
                                </div>
                            </div>

                      
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

<?php endif ?>
<script>
	$('#datatable1').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_vote/crud_vote.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>