<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>

    <div class='row'>
	 <div class='col-md-7'>
              <div class="card">
             <div class="card-header">
                  <h5 class='card-title'><i class="fas fa-graduation-cap"></i> Proyek Penguatan Profil Pelajar Pancasila</h5>
                </div>
				
                <div class='card-body'>
                    <div class='table-responsive'>
                        <table style="font-size: 12px" id='example1' class='table table-hover'>
                            <thead >
                                <tr>
                                    <th width='3px' class="hidden-xs">No</th>
									  <th>Proyek</th>
									  <th>Kelas</th>									  
                                    <th>Judul Proyek</th>  									
                               			<th>Keterangan</th>					  
									<th width="5%">Action</th>
										</tr>
									</thead>							
							<tbody>							
                                <?php
								$no = 0;
								
								 $query = mysqli_query($koneksi, "select * FROM m_proyek ORDER BY kelas,ke ASC");
                            while ($tjn = mysqli_fetch_array($query)) {
							$ket = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM m_proyek WHERE kelas='$tjn[kelas]'"));
                                $no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
									 <td><b style="color:blue;"><?= $tjn['ke'] ?></b></td>
							        <td><b><?= $tjn['kelas'] ?></b></td>									
									<td ><?= $tjn['judul'] ?></td>									
									<td>
									<?php if($ket == 3){ ?>
									<b style="color: blue;">Proyek terpenuhi</b>
									<?php }else{ ?>
									<b style="color: red;">Sisa Projek <?= 3-$ket ?> </b>
									<?php } ?>
									</td>
                                   <td style="text-align:center">
                                       
										 <button data-id="<?= $tjn['id'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
                                   </td>
								  
							       </tr>
                            <?php } ?>
                        </tbody>
                        </table>
                    </div>
                    </div>
					</div>
                    </div>
				
                     <div class='col-md-5'>
            <div class="card">
             <div class="card-header">
			    <h5 class='card-title'>Master Proyek</h5>
			 </div>  
              <div class='card-body'>			 
					 <form id="formtujuan" action=''>
						<div class="col-md-6">
                              <label class="bold">Fase</label>
                                  <select id='fase' name='fase' class='form-select' required='true'>
                                      <option value=''></option>
                                  <?php $quem = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY fase"); ?>
                                                <?php while ($m = mysqli_fetch_array($quem)) : ?>
                                                    <option value="<?= $m['fase'] ?>"><?= $m['fase'] ?></option>
                                                <?php endwhile ?>
                                                    </select>
                                                </div>
												
												<div class="col-md-6">                                                
                                                   <label class="bold">Kelas</label>
                                                    <select id='kelas' name='kelas' class='form-select' required='true'>
                                                      
                                                    </select>
                                                </div>
												
												<div class="col-md-12">
                                                   <label class="bold">Proyek</label>
                                                    <select id='ke' name='ke' class='form-select' required='true'>
                                                        <option value=''></option>
                                                         <option value='Proyek 1'>Proyek 1</option>
														  <option value='Proyek 2'>Proyek 2</option>
														   <option value='Proyek 3'>Proyek 3</option>
                                                    </select>
                                                </div>
												
												<div class="col-md-12">
                                                   <label class="bold">Judul Proyek</label>
                                                   <input type="text" name="judul" class="form-select" required="true" >
                                                </div>
												
														<div class="col-md-12">
														  <label class="bold">Deskripsi Proyek</label>
														<textarea  name='deskripsi' class='form-conntrol' rows='8' cols='80' style='width:100%;' required="true" placeholder="Tuliskan secara singkat tentang deskripsi proyek yang dilakukan di sekolah anda."></textarea>
														 </div>   
														
											<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>
            </div>
        </div>
    </div>
              <script>
				  $("#fase").change(function() {
					var fase = $(this).val();
					console.log(fase);
					$.ajax({
						type: "POST", 
						url: "sandik_proyek/crud_proyek.php?pg=ambil_kelas", 
						data: "fase=" + fase, 
						success: function(response) { 
							$("#kelas").html(response);
						}
					});
				});						
			</script>	
	<script>
	 $('#formtujuan').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_proyek/crud_proyek.php?pg=tambah',
            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;
    });
	
	 $('#example1').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_proyek/crud_proyek.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>
	