<?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">	
               <div class='right '>
					 <a href="?pg=inputtugas" class='btn btn-outline-primary'><i class="fa fa-plus-circle"></i>Tugas</a>
							<a href="?" class='btn btn-outline-danger' ><i class="fas fa-times-circle"></i>Back</a>
                    </div>
					<h5 class='card-title'><i class="fas fa-rss"></i> Daftar Tugas Daring</h5>
                </div>
					
				 <div class="card-body">	
                    <div id='tablemateri' class='table-responsive'>
                        <table id="example1" class='table'>
                            <thead>
                                <tr>
                                    <th width='5px'>#</th>
                                    <th>Mata Pelajaran</th>
								
                                    <th>Judul Tugas</th>
                                    <th>Tgl Mulai</th>
                                    <th>Tgl Selesai</th>
                                    <th>Kelas</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>
                                            <?= $tugas['judul'] ?>
                                        </td>

                                        <td style="text-align:center">
                                            <?= $tugas['tgl_mulai'] ?>
                                        </td>
                                        <td style="text-align:center">
                                            <?= $tugas['tgl_selesai'] ?>
                                        </td>
                                        <td style="text-align:center">
                                            <?php $kelas = unserialize($tugas['kelas']);
                                            foreach ($kelas as $kelas) {
                                                echo $kelas . " ";
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php if ($tugas['file'] <> '') { ?>
                                                <a href="../berkas/<?= $tugas['file'] ?>"  class="btn btn-sm btn-link" 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-outline-success'><i class='fas fa-eye'></i> Lihat</a>
                                              
                                                <button type="button" class="btn btn-outline-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modaledit<?= $tugas['id_tugas'] ?>">
                                                    <i class="fas fa-edit"></i> Edit
                                                </button>
                                                <button data-id='<?= $tugas['id_tugas'] ?>' class="hapus btn btn-outline-danger btn-sm"><i class="fas fa-trash-alt    "></i></button>
                                            </div>
                                        </td>
										 
                                    </tr>
                                   
                                        <div class="modal fade" id="modaledit<?= $tugas['id_tugas'] ?>" tabindex="-1" aria-labelledby="modaledit<?= $tugas['id_tugas'] ?>" aria-hidden="true">
									   <div class="modal-dialog modal-lg">
										   <div class="modal-content">
											  <div class="modal-header">
												<h5 class="modal-title" id="tambahjadwal">Edit Tugas</h5>
												   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
														</div>
													<div class="modal-body">
                                                <form id="formedittugas<?= $tugas['id_tugas'] ?>">
                                                    
                                                        <input type="hidden" value="<?= $tugas['id_tugas'] ?>" name='id'>
                                                        <div class="form-group">
														<label class="bold">Mata Pelajaran</label>
                                                            <select name='mapel' class='form-control' 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'] ?>"><?= $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">
                                                            <textarea name='isitugas' class='editor1' rows='10' cols='80' style='width:100%;'><?= $tugas['tugas'] ?></textarea>
                                                        </div><p>
                                                        <div class='form-group'>
                                                            <div class='row'>
                                                                <div class='col-md-4'>
                                                                     <label class="bold">Pilih Kelas</label><br>
                                                                    <select name='kelas[]' id='soalkelas' class='form-control form-control-sm select2' multiple='multiple' style='width:100%' required='true'>
                                                                       
                                                                       <?php $lev = mysqli_query($koneksi, "SELECT * FROM kelas"); ?>
                                                                        <?php while ($kelas = mysqli_fetch_array($lev)) : ?>
                                                                            <?php if (in_array($kelas['id_kelas'], unserialize($tugas['kelas']))) : ?>
                                                                                <option value="<?= $kelas['id_kelas'] ?>" selected><?= $kelas['nama'] ?></option>
                                                                            <?php else : ?>
                                                                                <option value="<?= $kelas['id_kelas'] ?>"><?= $kelas['nama'] ?></option>
                                                                            <?php endif; ?>
                                                                        <?php endwhile ?>
                                                                    </select>
                                                                </div>
                                                                <div class='col-md-4'>
                                                                    <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-4'>
                                                                    <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>
                                                        </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>
													<hr>
                                                     <div class='modal-footer'>
											   <div class='box-tools pull-right btn-group'>
												    <button type='submit'  class='btn btn-outline-primary'><i class='fa fa-check-circle'></i>Simpan</button>
                                                         <button type='button' class='btn btn-outline-default' data-bs-dismiss='modal'><i class='fa fa-times-circle'></i>Close</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: 'sandik_learn/tugas/edit_tugas.php',
                                                 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 endwhile ?>
                            </tbody>
                        </table>
                    </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">	
					 <div class='right '>
                        <button class='btn btn-outline-primary' onclick="frames['frameresult'].print()"><i class='fa fa-print'></i>Nilai</button>
                  <a href="?pg=tugas" class='btn btn-outline-danger' ><i class="fas fa-times-circle"></i>Back</a>
				  </div>
                   
					<h4 class='box-title'><i class="fas fa-rss"></i>Tugas Daring</h4>
                    </div>
               
				
                <div class='card-body' id="tablejawaban">
				 <div class='table-responsive'>
                    <table id="example1" 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['id_kelas'] ?></td>
									<td><?= $jawab['jawaban'] ?></td>
                                    <td>
                                        <?php if ($jawab['file'] <> null) { ?>
                                            <a href="../tugas/<?= $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-outline-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modalnilai<?= $no ?>">
                                            <i class="fas fa-plus-circle"></i> Nilai
                                        </button>
										<?php }else{ ?>
										<button type="button" class="btn btn-outline-default btn-sm" data-bs-toggle="modal" data-bs-target="#modalnilai<?= $no ?>" disabled>
                                            <i class="fas fa-lock"></i> Nilai
                                        </button>
										<?php } ?>
                                        <button data-id='<?= $jawab['id_jawaban'] ?>' class="hapus btn btn-outline-danger btn-sm"><i class="fas fa-trash-alt    "></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-sm btn-outline-primary'><i class='fa fa-check-circle'></i> 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: "sandik_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>
	<iframe id='loadframe' name='frameresult' src='sandik_learn/print_jawaban.php?id=<?= $_GET['id'] ?>' style='display:none'></iframe>
<?php } ?>
<script>
function tampilkan(){
  var nama_kota=document.getElementById("formtugas").kategori.value;
  if (nama_kota=="KI3")
    {
        document.getElementById("tampil").innerHTML=
		"<option value='3.1'>3.1</option><option value='3.2'>3.2</option><option value='3.3'>3.3</option><option value='3.4'>3.4</option><option value='3.5'>3.5</option><option value='3.6'>3.6</option><option value='3.7'>3.7</option><option value='3.8'>3.8</option><option value='3.9'>3.9</option><option value='3.10'>3.10</option><option value='3.11'>3.11</option><option value='3.12'>3.12</option>";
    }
  else if (nama_kota=="KI4")
    {
        document.getElementById("tampil").innerHTML="<option value='4.1'>4.1</option><option value='4.2'>4.2</option><option value='4.3'>4.3</option><option value='4.4'>4.4</option><option value='4.5'>4.5</option><option value='4.6'>4.6</option><option value='4.7'>4.7</option><option value='4.8'>4.8</option><option value='4.9'>4.9</option><option value='4.10'>4.10</option><option value='4.11'>4.11</option><option value='4.12'>4.12</option>";
    }
}
</script>
<script>
   
    $('#example1').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: 'sandik_learn/tugas/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: 'sandik_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>