<div class="row">
    <div class="col-md-12">
        <h1 class="page-header">Manajemen File Tugas</h1>
    </div>
</div>

<script>
$(document).ready( function () {
  var table = $('#tugas2').DataTable();

// #myInput is a <input type="text"> element
$('#myInput').on( 'keyup', function () {
    table.search( this.value ).draw();
} );
} );
  </script>

<?php
if (@$_SESSION['admin'] || ['pengajar'] ) {
if (@$_GET['action'] == '') { ?>
	<div class="row">
		<div class="col-md-12">
	        <div class="panel panel-default">
            <div class="alert alert-info alert-has-icon">
      <div class="alert-icon"><i class="far fa-lightbulb"></i></div>
      <div class="alert-body">
        <div class="alert-title">Info</div>
        Anda dapat menambahkan dan mengahpus file Tugas untuk mapel yang anda ajar.
      </div>
      </div>
	            <div class="panel-heading"><a href="?page=tugas2&action=tambah" class="btn btn-info btn-sm"><i class="fas fa-plus"></i>Tambah Data</a>
              </div>
	            <div class="panel-body">
	                <div class="table-responsive">
	                    <table class="table table-striped table-bordered table-hover" id="tugas2">
	                        <thead>
	                            <tr>
	                                <th>#</th>
	                                <th>Judul</th>
	                                <th>Kelas</th>
	                                <th>Mapel</th>
	                                <th>Nama File</th>
	                                <th>Tanggal Posting</th>
	                                <th>Pembuat</th>
	                                <th>Dilihat</th>
	                                <th>Opsi</th>
	                            </tr>
	                        </thead>
	                        <tbody>
	                        <?php
                            $no = 1;
                            if (@$_SESSION[admin]) {
                                $sql_tugas2 = mysqli_query($db, "SELECT * FROM tb_file_tugas2 JOIN tb_kelas ON tb_file_tugas2.id_kelas = tb_kelas.id_kelas JOIN tb_mapel ON tb_file_tugas2.id_mapel = tb_mapel.id") or die($db->error);
                            } elseif (@$_SESSION[pengajar]) {
                                $sql_tugas2 = mysqli_query($db, "SELECT * FROM tb_file_tugas2 JOIN tb_kelas ON tb_file_tugas2.id_kelas = tb_kelas.id_kelas JOIN tb_mapel ON tb_file_tugas2.id_mapel = tb_mapel.id WHERE pembuat = '$_SESSION[pengajar]'") or die($db->error);
                            }
                            if (mysqli_num_rows($sql_tugas2) > 0) {
                                while ($data_tugas2 = mysqli_fetch_array($sql_tugas2)) { ?>
									<tr>
										<td align="center"><?php echo $no++; ?></td>
										<td><?php echo $data_tugas2['judul']; ?></td>
										<td><?php echo $data_tugas2['nama_kelas']; ?></td>
										<td><?php echo $data_tugas2['mapel']; ?></td>
										<td><?php echo $data_tugas2['nama_file']; ?></td>
										<td><?php echo tgl_indo($data_tugas2['tgl_posting']); ?></td>
										<td>
											<?php
                                            if ($data_tugas2['pembuat'] == 'admin') {
                                                echo "Admin";
                                            } else {
                                                $sql_pengajar = mysqli_query($db, "SELECT * FROM tb_pengajar WHERE id_pengajar = '$data_tugas2[pembuat]'") or die($db->error);
                                                $data_pengajar = mysqli_fetch_array($sql_pengajar);
                                                echo $data_pengajar['nama_lengkap'];
                                            } ?>
										</td>
										<td><?php echo $data_tugas2['hits']." kali"; ?></td>
										<td align="center">
	                                        <a onclick="return confirm('Yakin akan menghapus data?');" href="?page=tugas2&action=hapus&IDtugas2=<?php echo $data_tugas2['id_tugas2']; ?>" class="btn btn-icon icon-left btn-danger"><i class="fas fa-trash"></i>Hapus</a>
                                        </td>
									</tr>
								<?php
                                }
                            } else {
                                echo '<tr><td colspan="9" align="center">Data tidak ditemukan</td></tr>';
                            } ?>
	                        </tbody>
	                    </table>
	                </div>
	            </div>
	        </div>
	    </div>
	</div>
<?php
} if (@$_GET['action'] == 'tambah') { ?>
	<div class="row">
		<div class="col-md-6">
            <div class="panel panel-default">
                <div class="panel-heading">Tambah File Tugas &nbsp; <a href="?page=tugas2" class="btn btn-primary btn-sm"><i class="fas fa-arrow-left"></i>Kembali</a></div>
                <div class="panel-body">
	                <?php
                    if (@$_SESSION[admin]) { ?>
	                	<form method="post" enctype="multipart/form-data">
	                    	<div class="form-group">
	                            <label>Judul *</label>
	                            <input type="text" name="judul" class="form-control" required />
	                        </div>
	                        <div class="form-group">
	                            <label>Mapel *</label>
	                            <select name="mapel" class="form-control" required>
	                            	<option value="">- Pilih -</option>
	                            	<?php
                                    $sql_mapel = mysqli_query($db, "SELECT * FROM tb_mapel") or die($db->error);
                                    while ($data_mapel = mysqli_fetch_array($sql_mapel)) {
                                        echo '<option value="'.$data_mapel['id'].'">'.$data_mapel['mapel'].'</option>';
                                    } ?>
	                            </select>
	                        </div>
	                        <div class="form-group">
	                            <label>Kelas *</label>
	                            <select name="kelas" class="form-control" required>
	                            	<option value="">- Pilih -</option>
	                            	<?php
                                    $sql_kelas = mysqli_query($db, "SELECT * FROM tb_kelas") or die($db->error);
                                    while ($data_kelas = mysqli_fetch_array($sql_kelas)) {
                                        echo '<option value="'.$data_kelas['id_kelas'].'">'.$data_kelas['nama_kelas'].'</option>';
                                    } ?>
	                            </select>
	                        </div>
	                        <div class="form-group">
	                            <label>File *</label>
	                            <input type="file" name="tugas2" class="form-control" required />
	                        </div>
	                        <div class="form-group">
	                            <input type="submit" name="simpan" value="Simpan" class="btn btn-success" />
	                            <input type="reset" value="Reset" class="btn btn-danger" />
	                        </div>
	                    </form>
					<?php
                    } elseif (@$_SESSION[pengajar]) { ?>
	                    <form method="post" enctype="multipart/form-data">
	                    	<div class="form-group">
	                            <label>Judul *</label>
	                            <input type="text" name="judul" class="form-control" required />
	                        </div>
	                        <div class="form-group">
	                            <label>Mapel yang Anda Ajar *</label>
	                            <select name="mapel" class="form-control" required>
	                            	<option value="">- Pilih -</option>
	                            	<?php
                                    $sql_mapel_ajar = mysqli_query($db, "SELECT DISTINCT(id_mapel) FROM tb_mapel_ajar WHERE id_pengajar = '$_SESSION[pengajar]'") or die($db->error);
                                    while ($data_mapel_ajar = mysqli_fetch_array($sql_mapel_ajar)) {
                                        $sql_mapel = mysqli_query($db, "SELECT * FROM tb_mapel WHERE id = '$data_mapel_ajar[id_mapel]'") or die($db->error);
                                        $data_mapel = mysqli_fetch_array($sql_mapel);
                                        echo '<option value="'.$data_mapel_ajar['id_mapel'].'">'.$data_mapel['mapel'].'</option>';
                                    } ?>
	                            </select>
	                        </div>
	                        <div class="form-group">
	                            <label>Kelas yang Anda Ajar *</label>
	                            <select name="kelas" class="form-control" required>
	                            	<option value="">- Pilih -</option>
	                            	<?php
                                    $sql_kelas = mysqli_query($db, "SELECT * FROM tb_kelas_ajar JOIN tb_kelas ON tb_kelas_ajar.id_kelas = tb_kelas.id_kelas WHERE tb_kelas_ajar.id_pengajar = '$_SESSION[pengajar]'") or die($db->error);
                                    while ($data_kelas = mysqli_fetch_array($sql_kelas)) {
                                        echo '<option value="'.$data_kelas['id_kelas'].'">'.$data_kelas['nama_kelas'].'</option>';
                                    } ?>
	                            </select>
	                        </div>
	                        <div class="form-group">
	                            <label>File *</label>
	                            <input type="file" name="tugas2" class="form-control" required />
	                        </div>
	                        <div class="form-group">
	                            <input type="submit" name="simpan" value="Simpan" class="btn btn-success" />
	                            <input type="reset" value="Reset" class="btn btn-danger" />
	                        </div>
	                    </form>
                    <?php
                    }

                    if (@$_POST['simpan']) {
                        $judul = @mysqli_real_escape_string($db, $_POST['judul']);
                        $mapel = @mysqli_real_escape_string($db, $_POST['mapel']);
                        $kelas = @mysqli_real_escape_string($db, $_POST['kelas']);

                        $sumber = @$_FILES['tugas2']['tmp_name'];
                        $target = 'file_tugas2/';
                        $nama_file = @$_FILES['tugas2']['name'];

                        if (move_uploaded_file($sumber, $target.$nama_file)) {
                            if (@$_SESSION[admin]) {
                                mysqli_query($db, "INSERT INTO tb_file_tugas2 VALUES('', '$judul', '$kelas', '$mapel', '$nama_file', now(), 'admin', '0')") or die($db->error);
                            } elseif (@$_SESSION[pengajar]) {
                                mysqli_query($db, "INSERT INTO tb_file_tugas2 VALUES('', '$judul', '$kelas', '$mapel', '$nama_file', now(), '$_SESSION[pengajar]', '0')") or die($db->error);
                            }
                            echo '<script>window.location="?page=tugas2";</script>';
                        } else {
                            echo '<script>alert("Gagal menambah tugas2, file gagal diupload, coba lagi!");</script>';
                        }
                    } ?>
                </div>
            </div>
        </div>
	</div>
<?php
} elseif (@$_GET['action'] == 'hapus') {
                        mysqli_query($db, "DELETE FROM tb_file_tugas2 WHERE id_tugas2 = '$_GET[IDtugas2]'") or die($db->error);
                        echo "<script>window.location='?page=tugas2';</script>";
                      }
                    } ?>
