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

    <div class='row'>
	 <div class='col-md-12'>
             <div class='panel panel-default'>
               <div class="panel-heading" style="height:45px">
                    <div class='box-tools pull-right '>
					 
                    </div>
					<h4 class='box-title'><i class="fas fa-graduation-cap"></i> Tujuan Pembelajaran</h4>
                </div>
				<div class='box-header with-border '>
			   <div class='box-tools pull-right '>
			   <table>
			   <tr>
			   <td>
			   <div class="form-group-sm">
			  <select name='jenjang' class='form-control' >
              <option value=""><?= $setting['sekolah'] ?></option>
               </select>
			   </div>
				</td>
			   <td>
			   <div class="form-group-sm">
			  <select name='jenjang' class='form-control' >
              <option value=""><?= $setting['jenjang'] ?></option>
               </select>
			   </div>
				</td>
				<td>
				<div class="form-group-sm">
			  <select name='jenjang' class='form-control'>
               <option value=""><?= $setting['kota'] ?></option>
               </select>
			   </div>
				</td>
				<td>
				<div class="form-group-sm">
			  <select name='jenjang' class='form-control' >
               <option value=""><?= $setting['kecamatan'] ?></option>
               </select>
			   </div>
				</td>
				</tr>					
			</table>
			</div>
			 <h4 class='box-title'></h4>
			  </div>
			  <div class="box-body">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true" >Menu E-Rapor</a></li>
                    </ul>
					<div class="tab-content">
                    <div class="tab-pane active" id="tab_1" >
				<div class="panel panel-default">
				 <div class='box-body'>
       	<?php if($pengawas['level']=='guru'){ ?>	 
        <div class='col-md-8'>
			<?php }else{ ?>
		 <div class='col-md-12'>
			<?php } ?>
           <div class="panel panel-default">
                <div class='box-header'>
                    <h4 class='box-title'>Tujuan Pembelajaran</h4>
                    <div class='box-tools pull-right'>
					
                </div>
				</div>
					
                <div class='box-body'>
                    <div class='table-responsive'>
                        <table style="font-size: 11px" id='example1' class='table table-hover'>
                            <thead >
                                <tr>
                                    <th width='3px' class="hidden-xs">No</th>
									  <th>Tingkat</th>
                                    <th>Mapel</th>  
									<th>Kode TP</th>  
                                        <th>Tujuan Pembelajaran</th>
                                    <?php if($pengawas['level']=='guru'){ ?>	 
									<th>Action</th>
									<?php } ?>
										</tr>
									</thead>							
							<tbody>							
                                <?php
								$no = 0;
								if($pengawas['level']=='guru'){
								 $query = mysqli_query($koneksi, "select * from tujuan 
								 JOIN jadwal_proto ON jadwal_proto.kode=tujuan.kodemap
								 WHERE jadwal_proto.idguru='$_SESSION[id_pengawas]' ");
								 }else{
									 $query = mysqli_query($koneksi, "select * from tujuan");
								}
                            while ($tjn = mysqli_fetch_array($query)) {
							
                                $no++;
                            ?>
							 <tr>
                                    <td><?= $no; ?></td>
							        <td><?= $tjn['tingkat'] ?></td>
									<td ><?= $tjn['kodemap'] ?></td>
									<td ><?= $tjn['tp'] ?></td>
									<td><?= $tjn['tujuan'] ?></td>
									  <?php if($pengawas['level']=='guru'){ ?>
                                   <td style="text-align:center">
								 
                                       <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#modaledit<?= $no ?>">
                                                    <i class="fas fa-edit    "></i>
                                                </button>
										 <button data-id="<?= $tjn['id_tujuan'] ?>" class="hapus btn-xs btn btn-danger"><i class="fas fa-trash"></i></button>
                                   </td>
								   <?php } ?>
								   
								   <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'>
                                    <button class='close' data-dismiss='modal'><span aria-hidden='true'><i class='glyphicon glyphicon-remove'></i></span></button>
                                    <h4 class='modal-title'></h4>
                                         </div>
                                                <form id="formedit<?= $no ?>">
												<input type="hidden" name="id" value="<?= $tjn['id_tujuan'] ?>" >
                                       <div class='modal-body'>
									    <div class='row'>
									
														<div class="col-md-12">
                                                             <div class="form-group-sm">
														  <label>Tujuan Pembelajaran</label>
														<textarea  name="tujuan" class="form-control" rows="8" required><?= $tjn['tujuan'] ?></textarea>
														 </div>   
														</div> 
														
														
											
											</div>
												
                                                <div class='modal-footer' >
                                            <div class='box-tools pull-right btn-group' >
                                                <button type='submit' class='btn btn-sm btn-success'><i class='fa fa-check'></i> Simpan</button>
                                              
                                            </div>
                                        </div>
										
                                    </form>
                                            </div>
                                        </div>
                                    </div>
                                    <script>
                        $("#formedit<?= $no ?>").submit(function(e) {
                             e.preventDefault();
						$.ajax({
							type: 'POST',
							url: 'sandik_ikm/crud_tujuan.php?pg=edit',
							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.error({
										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>
   			
							       </tr>
                            <?php } ?>
                        </tbody>
                        </table>
                    </div>
                    </div>
					</div>
                    </div>
					<?php if($pengawas['level']=='guru'){ ?>
                     <div class='col-md-4'>
            <div class="panel panel-default">
                <div class='box-header'>                    
					 <form id="formtujuan" action=''>
                    <div class='box-tools pull-right'>
					   <button type='submit'  class='btn btn-sm btn-success'><i class='fa fa-check'></i> Simpan</button>
								</div>
								</div>
								
									 <div class='box-body'>
									 <div class='row'>
									  <div class="col-md-12">
                                        <div class='form-group-sm'>
                                                   <label>Pilih Fase</label>
                                                    <select name='fase' id="fase" class='form-control' required='true'>
                                                    <option value=''></option>
                                                         <?php $qt = mysqli_query($koneksi, "SELECT * FROM siswa  GROUP BY fase"); ?>
                                                <?php while ($mt = mysqli_fetch_array($qt)) : ?>
                                                    <option value="<?= $mt['fase'] ?>"><?= $mt['fase'] ?></option>
                                                <?php endwhile ?>
                                                    </select>   
														
                                                </div>
												</div>
									
									<div class="col-md-8">
                                        <div class='form-group-sm'>
                                                    <label>Tingkat</label>
                                                  <select id="tingkat" name='tingkat' class='form-control' style='width:100%' required>
                                                   <option value=''></option>
                                               
                                            </select>
                                                </div>
												</div>
												 <div class="col-md-4">
                                        <div class='form-group-sm'>
                                                   <label>Pilih TP</label>
                                                    <select name='tp' class='form-control' required='true'>
                                                        <option value=''></option>
                                                         <?php 
														 for($i=1;$i<=20;$i++){
														?>
                                                    <option value="TP<?= $i ?>">TP<?= $i ?></option>
                                                <?php } ?>
                                                    </select>
                                                </div>
												</div>
												
												<div class="col-md-12">
                                          <div class='form-group-sm'>
                                                   <label>Mapel</label>
                                                   <select id='mapel' name='mapel' class='form-control' style='width:100%' required>
                                                    <?php if($pengawas['level']=='guru'){ ?>
                                                <?php $que = mysqli_query($koneksi, "SELECT * FROM jadwal_proto WHERE idguru='$_SESSION[id_pengawas]' GROUP BY kode"); ?>
												<?php }else{ ?>
												<?php $que = mysqli_query($koneksi, "SELECT * FROM jadwal_proto  GROUP BY kode"); ?>
												<?php } ?>
											   <?php while ($mapel = mysqli_fetch_array($que)) : ?>

                                                    <option value="<?= $mapel['kode'] ?>"><?= $mapel['mapel'] ?></option>

                                                <?php endwhile ?>
                                            </select>
                                                </div>
												</div>
												
														<div class="col-md-12">
                                                             <div class="form-group-sm">
														  <label>Tujuan Pembelajaran</label>
														<textarea  name='tujuan' class='form-conntrol' rows='8' cols='80' style='width:100%;' required="true" placeholder="isikan tujuan pembelajaran dalam satu semester"></textarea>
														 </div>   
														</div> 
														
											<div class="col-md-12">
                                          <div class='form-group-sm'>
                                                    <label>Guru Mapel</label>
                                                   <select id='guru' name='guru' class='form-control' style='width:100%' required>
                                                   
													 <?php
													 if($pengawas['level']=='admin'){ 
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM pengawas WHERE level<>'admin' ");
													 }else{
														$kelasQ = mysqli_query($koneksi, "SELECT * FROM pengawas WHERE id_pengawas='$_SESSION[id_pengawas]' ");
													 }
                                                        while ($ekstra = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$ekstra[id_pengawas]'>$ekstra[nama]</option>";
                                                        }
                                                        ?>
													
                                                    </select>
                                                </div>
												</div>
											
											   </div>
                                            </div>
                                        </div>
										
                                    </form>
                               
                        </div>
                    </div>
                </div>
            </div>
		<?php } ?>
        </div>
    </div>
  <script>
				  $("#fase").change(function() {
					var fase = $(this).val();
					console.log(fase);
					$.ajax({
						type: "POST", 
						url: "sandik_ikm/crud_tujuan.php?pg=ambil_tingkat", 
						data: "fase=" + fase, 
						success: function(response) { 
							$("#tingkat").html(response);
						}
					});
				});						
			</script>	
	<script>
	 $('#formtujuan').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_ikm/crud_tujuan.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.error({
                        title: 'Gagal!',
                        message: 'TP pada mapel tersebut 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!',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'iya, hapus'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_ikm/crud_tujuan.php?pg=hapus',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                        iziToast.success({
                            title: 'Sukses!',
                            message: 'Data Berhasil dihapus',
                            position: 'topRight'
                        });
                        setTimeout(function() {
                            window.location.reload();
                        }, 2000);
                    }
                });
            }
            return false;
        })

    });
	</script>
	