<?php defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!'); ?>
<?php if ($ac == '') { ?>
	
    <div class='row'>
        <div class='col-md-12'>
            <div class="card">
             <div class="card-header">	
					<h5 class='card-title'>TUGAS BELAJAR</h5>
					<div class='pull-right '>
					 <a href="?pg=inputtugas" class='btn btn-primary'><i class="material-icons">add</i>Tugas</a>
						
                    </div>
                </div>
					
				 <div class="card-body">	
                    <div id='tablemateri' class='table-responsive'>
                        <table id="datatable1" class='table table-bordered'>
                            <thead>
                                <tr>
                                    <th width='5px'>#</th>
                                    <th>MAPEL</th>
                                    <th>MULAI</th>
                                    <th>SELESAI</th>
                                    <th>ROMBEL</th>
                                    <th>FILE</th>									
                                    <th></th>
									
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($user['level'] == 'guru') {
                                    $tugasQ = mysqli_query($koneksi, "SELECT * FROM tugas where id_guru='$_SESSION[id_user]'");
                                } else {
                                    $tugasQ = mysqli_query($koneksi, "SELECT * FROM tugas ");
                                }
                                ?>
                                <?php while ($tugas = mysqli_fetch_array($tugasQ)) : ?>
                                    <?php
                                    $guru = mysqli_fetch_array(mysqli_query($koneksi, "select * FROM users where id_user='$tugas[id_guru]'"));
                                    $no++
                                    ?>
                                    <tr>
                                        <td><?= $no ?></td>
                                        <td>
                                            <?= $tugas['mapel'] ?>
                                        </td>
										
                                        <td style="text-align:center">
                                            <?= $tugas['tgl_mulai'] ?>
                                        </td>
                                        <td style="text-align:center">
                                            <?= $tugas['tgl_selesai'] ?>
                                        </td>
                                        <td>
                                            <?php $kelas = unserialize($tugas['kelas']);
                                            foreach ($kelas as $kelas) {
                                                echo $kelas . " ";
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php if ($tugas['file'] <> '') { ?>
                                                <a href="../datafile/<?= $tugas['file'] ?>"  target="_blank">Lihat</a>
                                            <?php } ?>
                                        </td>
										 
                                        <td style="text-align:center">
                                            <div class=''>
                                                <a href='?pg=tugas&ac=jawaban&id=<?= $tugas['id_tugas'] ?>' class='btn btn-sm btn-success'><i class='material-icons'>visibility</i></a>
                                              
                                                <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modaledit<?= $tugas['id_tugas'] ?>">
                                                    <i class="material-icons">edit</i>
                                                </button>
                                                <button data-id='<?= $tugas['id_tugas'] ?>' class="hapus btn btn-danger btn-sm"><i class="material-icons">delete</i></button>
                                            </div>
                                        </td>
										</tr>
                                    
                                    <div class="modal fade" id="modaledit<?= $tugas['id_tugas'] ?>" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
                                        <div class="modal-dialog modal-lg" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">

                                                   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                </div>
												<form id="formedittugas<?= $tugas['id_tugas'] ?>">
													<div class="modal-body">
                                                        <input type="hidden" value="<?= $tugas['id_tugas'] ?>" name='id'>
                                                        <div class="form-group">
														<label class="bold">Mata Pelajaran</label>
                                                            <select name='mapel' class='form-select' style='width:100%' required>
                                                                <option value='<?= $tugas['mapel'] ?>' selected><?= $tugas['mapel'] ?></option>
                                                                 <?php $que = mysqli_query($koneksi, "SELECT * FROM mata_pelajaran "); ?>
                                                                <?php while ($mapel = mysqli_fetch_array($que)) : ?>

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

                                                                <?php endwhile ?>
                                                            </select>
                                                        </div>
														<p>
                                                        <div class="form-group">
														<label class="bold">Judul Tugas</label>
                                                            <input type="text" class="form-control" name="judul" aria-describedby="helpId" placeholder="Judul Tugas" value="<?= $tugas['judul'] ?>" required>
                                                        </div><p>
														
                                                        <div class="form-group">
														<label class="bold">Isi Tugas</label>
                                                            <textarea name='isitugas' class='editor1' rows='10' cols='80' style='width:100%;'><?= $tugas['tugas'] ?></textarea>
                                                        </div><p>
                                                         <div class="row">
                                                                <div class='col-md-6'>
                                                                    <label class="bold">Tanggal Mulai</label>
                                                                    <input type='text' name='tgl_mulai' class='tgl form-control' autocomplete='off' required='true' value="<?= $tugas['tgl_mulai'] ?>" />
                                                                </div>
                                                                <div class='col-md-6'>
                                                                    <label class="bold">Tanggal Selesai</label>
                                                                    <input type='text' name='tgl_selesai' class='tgl form-control' autocomplete='off' required='true' value="<?= $tugas['tgl_selesai'] ?>" />
                                                                </div>
                                                        </div>
														<p>
                                                        <div class="form-group">
                                                            <label for="file" class="bold">File Pendukung</label>
                                                            <input type="file" class="form-control-file" name="file" id="file" placeholder="" aria-describedby="fileHelpId">
                                                            <small id="fileHelpId" class="form-text text-muted">format file (doc/docx/xls/xlsx/pdf)</small>
                                                        </div>
                                                   
                                                     <div class='modal-footer'>
											   <div class='box-tools kanan btn-group'>
												    <button type='submit' id="blockui-3" class='btn btn-primary'>SIMPAN</button>
                                                         <button type='button' class='btn btn-light' data-bs-dismiss='modal'>BATAL</button>
														</div>
													</div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
					             <script>
                                         $('#formedittugas<?= $tugas['id_tugas'] ?>').submit(function(e) {
                                            e.preventDefault();
                                           var data = new FormData(this);
      
                                            $.ajax({
                                                type: 'POST',
                                                 url: 'cbtserver_learn/edit_tugas.php',
                                                enctype: 'multipart/form-data',
												data: data,
												cache: false,
												contentType: false,
												processData: false,
												success: function(data) {
												
													setTimeout(function() {
														window.location.reload();
													}, 2000);

												}
											});
											return false;
										});
 
                                    </script>
                                    
									 
                                <?php endwhile ?>
									</tbody>
								</table>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div> 
	</div>
  </div>
<?php } elseif ($ac == 'jawaban') { ?>
    <?php $id = $_GET['id']; ?>
    <div class='row'>
        <div class='col-md-12'>
            <div class="card">
             <div class="card-header">	
					<h4 class='box-title'>JAWABAN TUGAS</h4>
					<div class='pull-right '>
                  <button class='btn btn-primary' onclick="frames['frameresult'].print()"><i class='material-icons'>print</i>Nilai</button>
            
				  </div>
                    </div>
      
                <div class='card-body' id="tablejawaban">
				 <div class='table-responsive'>
                    <table id="datatable1" class="table table-bordered table-hover">
                        <thead>
                            <tr>
                                <th width="5%">No</th>
                                <th>Nama Siswa</th>
                                <th width="5%">Kelas</th>
								<th>Jawaban</th>
                                <th>File</th>
                                <th width="5%">Nilai</th>
                                <th width="15%">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php

                            $jawabx = mysqli_query($koneksi, "select * from jawaban_tugas where id_tugas='$id'");
                            $no = 0;
                            while ($jawab = mysqli_fetch_array($jawabx)) {
                                $no++;
                                $siswa = fetch($koneksi, 'siswa', ['id_siswa' => $jawab['id_siswa']]);
								$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban_tugas WHERE id_siswa='$jawab[id_siswa]' AND id_tugas='$jawab[id_tugas]' AND nilai >0 ")); 
                           
						  ?>
                                <tr>
                                    <td scope="row"><?= $no ?></td>
                                    <td><?= $siswa['nama'] ?></td>
                                    <td><?= $siswa['kelas'] ?></td>
									<td><?= $jawab['jawaban'] ?></td>
                                    <td>
                                  <?php if ($jawab['file'] <> null) { ?>
							 <a href="../datafile/<?= $jawab['file'] ?>"  class="btn btn-sm btn-link" target="_blank">Lihat</a>
								  <?php } ?>
                                    </td>
                                    <td><?= $jawab['nilai'] ?></td>

                                    <td>
                                       
                                        <?php if($jumlah==0){ ?>
										<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modalnilai<?= $no ?>">
                                            <i class="material-icons">edit</i> Nilai
                                        </button>
										<?php }else{ ?>
										<button type="button" class="btn btn-default btn-sm" disabled>
                                           <i class="material-icons">lock</i>
                                        </button>
										<?php } ?>
                                        <button data-id='<?= $jawab['id_jawaban'] ?>' class="hapus btn btn-danger btn-sm"><i class="material-icons">delete</i></button>
                                      
                                        <div class="modal fade" id="modalnilai<?= $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">Input Nilai</h5>
												   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
														</div>
                                                    <form id="formnilaitugas<?= $jawab['id_jawaban'] ?>">
                                                        
                                                        <div class="modal-body">
                                                            <input type="hidden" class="form-control" name="id" value="<?= $jawab['id_jawaban'] ?>">
                                                            <div class="form-group-sm">
                                                                <label class="bold">Nilai</label>
                                                                <input type="number" class="form-control" name="nilai<?= $jawab['id_jawaban'] ?>" required="true">
                                                                
                                                            </div>
                                                            <div class="form-group">
                                                                <label class="bold">Jawaban</label>
                                                                <p><?= $jawab['jawaban'] ?>
                                                            </div>
															<div class='modal-footer'>
													<div class='box-tools pull-right btn-group'>
													<button type='submit' class='btn  btn-primary'>Simpan</button>
														
															</div>
																</div>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                        <script>
                                            $("#formnilaitugas<?= $jawab['id_jawaban'] ?>").submit(function(e) {
                                                e.preventDefault();
                                                var id = '<?= $jawab['id_jawaban'] ?>';
                                                $.ajax({
                                                    type: "POST",
                                                    url: "cbtserver_learn/tugas/simpan_nilai.php",
                                                    data: $(this).serialize(),
                                                    success: function(result) {
                                                        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>

                                    </td>
                                </tr>
                            <?php } ?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
	<iframe id='loadframe' name='frameresult' src='cbtserver_learn/print_jawaban.php?id=<?= $_GET['id'] ?>' style='display:none'></iframe>
<?php } ?>

<script>
   
    $('#datatable1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Apa anda yakin?',
            text: "akan menghapus tugas ini!",
            type:'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'cbtserver_learn/hapus_tugas.php',
                    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;
        })

    });
	
    $('#tablejawaban').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Apa anda yakin?',
            text: "akan menghapus nilai ini!",
            type:'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'cbtserver_learn/tugas/hapus_nilai.php',
                    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>
<script>
    tinymce.init({
        selector: '.editor1',
        plugins: [
            'advlist autolink lists link image charmap print preview hr anchor pagebreak',
            'searchreplace wordcount visualblocks visualchars code fullscreen',
            'insertdatetime media nonbreaking save table contextmenu directionality',
            'emoticons template paste textcolor colorpicker textpattern imagetools uploadimage paste formula'
        ],

        toolbar: 'bold italic fontselect fontsizeselect | alignleft aligncenter alignright bullist numlist  backcolor forecolor | formula code | imagetools link image paste ',
        fontsize_formats: '8pt 10pt 12pt 14pt 18pt 24pt 36pt',
        paste_data_images: true,

        images_upload_handler: function(blobInfo, success, failure) {
            success('data:' + blobInfo.blob().type + ';base64,' + blobInfo.base64());
        },
        image_class_list: [{
            title: 'Responsive',
            value: 'img-responsive'
        }],
        setup: function(editor) {
            editor.on('change', function() {
                tinymce.triggerSave();
            });
        }
    });
</script>