<?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">
			 <div class="right">
			 <a href="?" class="btn btn-outline-dark btn-sm"><i class="fa fa-times-circle"></i>Back</a>
			 </div>
					<h5 class='card-title'><i class="fas fa-graduation-cap"></i> Lingkup Materi</h5>
                </div>
			
                <div class='card-body'>
                    <div class='table-responsive'>
                        <table style="font-size: 11px" id='example1' class='table table-hover'>
                            <thead >
                                <tr>
                                    <th>No</th>
									  <th>Tingkat</th>
                                    <th>Mapel</th>
									<th>Kode</th>
                                    <th>Lingkup Materi</th>	                            									
									<th width="15%">Action</th>						
										</tr>
									</thead>							
							<tbody>							
                                <?php
								$no = 0;
								 if($user['level']=='guru'):
								 $query = mysqli_query($koneksi, "select * from lingkup JOIN jadwal_mapel ON jadwal_mapel.id_jadwal=lingkup.idjadwal WHERE jadwal_mapel.guru='$user[id_user]' ORDER BY id DESC");
                                else:
							    $query = mysqli_query($koneksi, "select * from lingkup JOIN jadwal_mapel ON jadwal_mapel.id_jadwal=lingkup.idjadwal ORDER BY id DESC ");
                                 endif;
							while ($tjn = mysqli_fetch_array($query)) {
							
                                $no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
							        <td><?= $tjn['tingkat'] ?> - <?= $tjn['kelas'] ?></td>
									<td ><?= $tjn['kode'] ?></td>
									<td ><?= $tjn['lm'] ?></td>
									<td><?= $tjn['materi'] ?></td>
									
                                   <td style="text-align:center">			  
                                       <button type="button" class="btn btn-outline-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modaledit<?= $no ?>" >
                                                    <i class="fas fa-edit    "></i>
                                                </button>
										 <button data-id="<?= $tjn['id'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
                                 
                                     </td>
								  
								   <div class="modal fade" id="modaledit<?= $no ?>" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
                                         <div class="modal-dialog">
									   <div class="modal-content">
										  <div class="modal-header">
											<h5 class="modal-title" id="tambahjadwal">Edit Lingkup Materi</h5>
											   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
													</div>
													  <div class="modal-body">
                                                <form id="formedit<?= $no ?>">
												<input type="hidden" name="id" value="<?= $tjn['id'] ?>" >
                                       
														<div class="col-md-12">
														  <label class="bold">Lingkup Materi</label>
														<textarea  name="materi" class="form-control" rows="5" required><?= $tjn['materi'] ?></textarea>
														 </div>   
														<p>
                                             
                                            <div class='right' >
                                                <button type='submit' class='btn btn-outline-success'><i class='fa fa-check-circle'></i> Simpan</button>
                                               
                                            </div>
                                       
                                    </form>
                                            </div>
                                        </div>
                                    </div>
                                    <script>
                        $("#formedit<?= $no ?>").submit(function(e) {
                          e.preventDefault();
                            $.ajax({
							type: 'POST',
							url: 'sandik_ptm/m_proto/crud_tujuan.php?pg=editmateri',
							data: $(this).serialize(),
							success: function(data) {
									 iziToast.info(
            {
                title: 'Sukses!',
                message: 'Lingkup Materi 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>
   			
							       </tr>
                            <?php } ?>
                        </tbody>
                        </table>
                    </div>
                    </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 data ini!',
			type:'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'iya, hapus'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_ptm/m_proto/crud_tujuan.php?pg=hapusmateri',
                    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>
					
					
				  <div class='col-md-4'>	
              <div class="card">
             <div class="card-header"></div>   
                   <div class='card-body'>			 
					 <form id="formlingkup" action=''>
						<div class="col-md-12">
                          <label class="bold">Kode</label>
                              <select name='lm' class='form-select' required='true'>
                                   <option value=''></option>
                                     <?php for($i=1;$i<=14;$i++){ ?>
                                      <option value="LM<?= $i ?>">LM<?= $i ?></option>
                                                <?php } ?>
                                                    </select>
                                                </div>
												
									<div class="col-md-12">
                                      <label class="bold">Tingkat</label>
                                       <select id='tingkat' name='tingkat' class='form-select' required='true'>
									   <?php if($user['level']=='guru'): ?>
                                          <option value=''></option>
                                            <?php $qt = mysqli_query($koneksi, "SELECT * FROM jadwal_mapel JOIN kelas ON kelas.id_kelas=jadwal_mapel.kelas WHERE jadwal_mapel.guru='$user[id_user]' AND kelas.kurikulum='Merdeka' GROUP BY tingkat"); ?>
                                                <?php while ($mt = mysqli_fetch_array($qt)) : ?>
                                                    <option value="<?= $mt['tingkat'] ?>"><?= $mt['tingkat'] ?></option>
                                                <?php endwhile ?>
                                                    </select>   
													<?php else: ?>
													  <option value=''></option>
                                            <?php $qt = mysqli_query($koneksi, "SELECT * FROM jadwal_mapel JOIN kelas ON kelas.id_kelas=jadwal_mapel.kelas WHERE kelas.kurikulum='Merdeka' GROUP BY tingkat"); ?>
                                                <?php while ($mt = mysqli_fetch_array($qt)) : ?>
                                                    <option value="<?= $mt['tingkat'] ?>"><?= $mt['tingkat'] ?></option>
                                                <?php endwhile ?>
                                                    </select>   
													 <?php endif; ?>
                                                </div>
												
												<div class="col-md-12">
                                                   <label class="bold">Mapel</label>
                                                   <select id="mapel" name='mapel' class='form-select' style='width:100%' required>
                                              <?php if($user['level']=='guru'): ?>
											  <option value="">Pilih Mapel</option>
											  <?php $que = mysqli_query($koneksi, "SELECT * FROM jadwal_mapel JOIN kelas ON kelas.id_kelas=jadwal_mapel.kelas WHERE guru='$user[id_user]' AND kelas.kurikulum='Merdeka'"); ?>	
											   <?php while ($mapel = mysqli_fetch_array($que)) : ?>
                                                    <option value="<?= $mapel['id_jadwal'] ?>"><?= $mapel['mapel'] ?> <?= $mapel['kelas'] ?></option>
                                                <?php endwhile ?>
                                            </select>
											<?php else: ?>
													 <option value="">Pilih Mapel</option>
											  <?php $que = mysqli_query($koneksi, "SELECT * FROM jadwal_mapel JOIN kelas ON kelas.id_kelas=jadwal_mapel.kelas WHERE  kelas.kurikulum='Merdeka'"); ?>	
											   <?php while ($mapel = mysqli_fetch_array($que)) : ?>
                                                    <option value="<?= $mapel['id_jadwal'] ?>"><?= $mapel['mapel'] ?> <?= $mapel['kelas'] ?></option>
                                                <?php endwhile ?>
                                            </select>
													 <?php endif; ?>
                                                </div>
												<div class="col-md-12">
											      <label class="bold">Lingkup Materi</label>
												    <textarea  name="materi" class="form-control" rows="5" required placeholder="isikan sesuai BAB dalam satu semester"></textarea>
														 </div>   
														
												<div class="col-md-12">
                                                   <label class="bold">Guru Mapel</label>
                                                   <select id='guru' name='guru' class='form-select' style='width:100%' required>
													 <?php
													 if($user['level']=='admin'){ 
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM users WHERE level<>'admin' ");
													 }else{
														$kelasQ = mysqli_query($koneksi, "SELECT * FROM users WHERE id_user='$_SESSION[id_user]' ");
													 }
                                                        while ($ekstra = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$ekstra[id_user]'>$ekstra[nama]</option>";
                                                        }
                                                        ?>
													
                                                    </select>
													
                                                </div>
											  <p>
											 <div class="right">
											 <button type='submit' name='submit' class='btn btn-outline-success'><i class='fa fa-check-circle' ></i> Simpan</button>
											 </div>
                                    </form>
                               </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
  
	<script>
	 $('#formlingkup').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_ptm/m_proto/crud_tujuan.php?pg=materi',
           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: 'Lingkup Materi 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: 'Tujuan Pembelajaran 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>
