<?php
session_start();
if (!isset($_SESSION["login"])) {
    header("Location: login.php");
    exit;
}
require 'header.php';

?>

<?php

// paginition
// konfigurasi
$jumlahDataPerHalaman = 5;
$jumlahData = count(query("SELECT * FROM keluhan"));
$jumlahHalaman = ceil($jumlahData / $jumlahDataPerHalaman);
if (isset($_GET["halaman"])) {
    $halamanAktif = $_GET["halaman"];
} else {
    $halamanAktif = 1;
}
$awalData = ($jumlahDataPerHalaman * $halamanAktif) - $jumlahDataPerHalaman;

$mhs = query("SELECT * FROM keluhan order by status LIMIT $awalData, $jumlahDataPerHalaman");

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar keluhan</title>
</head>

<body>
    <div class="container">



        <!-- navigasi -->
        <center>
            <?php if ($halamanAktif > 1) : ?>
                <a href="?halaman=<?= $halamanAktif - 1; ?>">&laquo;</a>
            <?php endif; ?>


            <?php
            for ($i = 1; $i <= $jumlahHalaman; $i++) : ?>
                <?php if ($i == $halamanAktif) : ?>
                    <a href="?halaman=<?php echo $i; ?>" style="font-weight: bold; color: red;"><?= $i; ?></a>
                <?php else : ?>
                    <a href="?halaman=<?php echo $i; ?>"><?= $i; ?></a>
                <?php endif; ?>
            <?php endfor; ?>

            <?php if ($halamanAktif < $jumlahHalaman) : ?>
                <a href="?halaman=<?= $halamanAktif + 1; ?>">&raquo;</a>
            <?php endif; ?>

        </center>
        <br>


        <table class="table table-striped">
            <thead>
                <tr>
                    <th scope="col">No.</th>
                    <th scope="col">Kategori</th>
                    <th scope="col">Username</th>
                    <th scope="col">Subjek</th>
                    <th scope="col">Date</th>
                    <th scope="col">Status</th>
                    <th scope="col">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <?php $i = 1 ?>
                    <?php foreach ($mhs as $row) : ?>
                        <?php
                        $coba = $row['id_jenis'];
                        $cobalagi = query("SELECT * FROM jeniskeluhan WHERE id_jenis = $coba");
                        ?>
                        <td><?php echo $i; ?></td>
                        <td>
                            <?php foreach ($cobalagi as $row33) : ?>
                                <?= $row33["jenis"]; ?>
                            <?php endforeach; ?>
                        </td>
                        <?php
                        $coba22 = $row['id_user'];
                        $cobalagi22 = query("SELECT * FROM user WHERE id = $coba22");
                        ?>
                        <td>
                            <?php foreach ($cobalagi22 as $row22) : ?>
                                <?= $row22["username"]; ?>
                            <?php endforeach; ?>
                        </td>
                        <td>
                            <a href="Admin_MenjawabKeluhan.php?id_keluhan=<?php echo $row["id_keluhan"] ?>" class="tulisan"><?= $row["subjek"]; ?></a>
                        </td>
                        <td>
                            <?= $row["tanggal"]; ?>
                        </td>
                        <td>
                            <?php
                            $status = $row["status"] == "sudah";
                            ?>
                            <?php if ($status) : ?>
                                <button type="button" class="btn btn-outline-success">ANSWERED</button>
                            <?php endif; ?>
                            <?php if (!$status) : ?>
                                <button type="button" class="btn btn-outline-danger">WAIT</button>
                            <?php endif; ?>

                        </td>
                        <td>
                            <?php
                            $status = $row["status"] == "sudah";
                            ?>
                            <?php if ($status) : ?>
                                <a href="Admin_UbahJawaban.php?id_keluhan=<?php echo $row["id_keluhan"] ?>">ubah</a> |
                                <a href="Admin_HapusKeluhan.php?id=<?php echo $row["id_keluhan"] ?>" onclick="return confirm('yakin?');">hapus</a>
                            <?php endif; ?>
                            <?php if (!$status) : ?>
                                <a href="Admin_HapusKeluhan.php?id=<?php echo $row["id_keluhan"] ?>" onclick="return confirm('yakin?');">hapus</a>
                            <?php endif; ?>
                        </td>
                        <?php
                        ?>
                </tr>
                <?php $i++; ?>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</body>

<?php
require 'footer.php';
?>

</html>