<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
$ac = dekripsi($ac);

$tugas = mysqli_fetch_array(mysqli_query($koneksi, "select * from tugas where id_tugas='$ac'"))
?>
<div class='row'>
    <div class='col-md-12'>
         <div class="card">
             <div class="card-header">
                <h5 class='card-title'><i class="fas fa-file-signature    "></i> Detail Tugas Siswa</h5>
            </div>
            <div class='card-body'>
                <table class='table table-bordered table-striped'>
                    <tr>
                        <th width='150'>Mata Pelajaran</th>
                        <td width='10'>:</td>
                        <td><?= $tugas['mapel'] ?></td>

                    </tr>

                    <tr>
                        <th>Tgl Mulai</th>
                        <td width='10'>:</td>
                        <td><?= $tugas['tgl_mulai'] ?></td>
                    </tr>
                    <tr>
                        <th>Tgl Selesai</th>
                        <td width='10'>:</td>
                        <td><?= $tugas['tgl_selesai'] ?></td>
                    </tr>
					<?php if($tugas['file']<>''){ ?>
					 <tr>
                        <th>Download</th>
                        <td width='10'>:</td>
                        <td> <a href="<?= $homeurl . '/berkas/' . $tugas['file'] ?>" class="btn btn-sm btn-danger"><i class="fas fa-download fa-fw"></i> Download Tugas</a></td>
                    </tr>
					<?php } ?>
                </table>
                <br>
                 <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
                                      <li class="nav-item" role="presentation">
                                        <button class="nav-link active" id="pills-home-tab" data-bs-toggle="pill" data-bs-target="#pills-home" type="button" role="tab" aria-controls="pills-home" aria-selected="true">Tugas Belajar</button>
                                            </li>
                                          <li class="nav-item" role="presentation">
                                               <button class="nav-link" id="pills-profile-tab" data-bs-toggle="pill" data-bs-target="#pills-profile" type="button" role="tab" aria-controls="pills-profile" aria-selected="false">Jawaban Tugas</button>
                                                 </li>
                                                    
                                                </ul>
                   <div class="tab-content" id="pills-tabContent">
                     <div class="tab-pane fade active show" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
                            <?php if ($tugas['file'] <> null) {
								$pecah=explode('.',$tugas['file']);
						$ekstensi=$pecah[1];
					?>
					<?php if($ekstensi=='mp4'){ ?>
					<video src="<?= $homeurl ?>/berkas/<?= $tugas['file'] ?>" controls autoplay width="100%" height="315"></video>
					 <?php } ?>
					 <?php if($ekstensi=='jpg' OR $ekstensi=='png'){ ?>
					<img src="<?= $homeurl ?>/berkas/<?= $tugas['file'] ?>" controls autoplay width="100%" height="315">
					 <?php } ?>
					 <?php if($ekstensi=='pdf'){ ?>
					<iframe  src="<?= $homeurl ?>/berkas/<?= $tugas['file'] ?>" controls autoplay width="100%" height="315"></iframe>
					 <?php } ?>
                                <?php } ?>
                                <center>
                                    <h3><?= $tugas['judul'] ?></h3>
                                </center>
                                <p><?= $tugas['tugas'] ?></p>
                        </div>
                         <div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">
                            <?php
                            $kondisi = array(
                                'id_siswa' => $_SESSION['id_siswa'],
                                'id_tugas' => $tugas['id_tugas']
                            );
                            $jawab_tugas = fetch($koneksi, 'jawaban_tugas', $kondisi);
                            if ($jawab_tugas) {
                                $jawaban = $jawab_tugas['jawaban'];
                            } else {
                                $jawaban = "";
                            }
                            ?>
                            <?php if ($jawab_tugas['nilai'] <> '') { ?>
                                <div class="alert alert-custom" role="alert">
                                <div class="custom-alert-icon icon-dark"><i class="fa fa-check fa-2x"></i></div>
                                   <div class="alert-content">
                                      <span class="alert-text" style="text-align:justify">Jawaban Telah dinilai</span>
                                                    </div>
                                                </div>
                                <h1>Nilai Kamu : <?= $jawab_tugas['nilai'] ?></h1>
                            <?php } else { ?>
                             <p>
                                <form id='formjawaban'>
                                    <input type="hidden" name="id_tugas" value="<?= $tugas['id_tugas'] ?>">
                                    <div class="form-group">
                                        <label class="bold">Lembar Jawaban</label>
                                        <textarea class="form-control" name="jawaban" id="txtjawaban" rows="10"><?= $jawaban ?></textarea>
                                    </div><p>
                                    <?php if ($jawab_tugas['file'] == '') { ?>
                                        <div class="form-group">
                                            <label class="bold">Upload File</label>
                                            <input type="file" class="form-control-file" name="file" aria-describedby="fileHelpId">
                                            <p><small id="fileHelpId" class="form-text text-muted">Jika jawaban diupload</small>
                                        </div><p>
                                    <?php } else { ?>

                                        <div class="alert alert-success" role="alert">
                                            <strong>file jawaban berhasil dikirim</strong>
                                            <a href='<?= $homeurl ?>/tugas/<?= $jawab_tugas['file'] ?>' target="_blank">Lihat file</a>
                                        </div>

                                    <?php  } ?>

                                    <div class="right">
                                        <button type="submit" class="btn btn-primary"><i class="fas fa-check-circle"></i>Simpan</button>
                                    </div>
                                </form>
                            <?php  } ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $('#formjawaban').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
        var homeurl = '<?= $homeurl ?>';

        $.ajax({
            type: 'POST',
            url: homeurl + '/simpantugas.php',
            enctype: 'multipart/form-data',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {

                if (data = 'ok') {
                    toastr.success("jawaban berhasil dikirim");

                } else {
                    toastr.error("jawaban gagal dikirim");
                }


            }
        });
        return false;
    });
</script>
<script>
    tinymce.init({
        selector: '#txtjawaban',
        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'
        ],

        toolbar: 'bold italic fontselect fontsizeselect | alignleft aligncenter alignright bullist numlist  backcolor forecolor | emoticons code | imagetools link image paste ',
        fontsize_formats: '8pt 10pt 12pt 14pt 18pt 24pt 36pt',
        paste_data_images: true,
        paste_as_text: 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>