<?php
require_once __DIR__ . "/../../app/config/auth.php";
require_login();
require_once __DIR__ . "/../../app/config/app.php";
require_once __DIR__ . "/../../app/config/session.php";
require_once __DIR__ . "/../../app/config/koneksi.php";

$currentPage = "kritik_saran";
function esc($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }

// ===================== PAGINATION =====================
$limit = 10;
$page  = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$start = ($page - 1) * $limit;

// Hitung total data status Baru
$totalQuery = mysqli_query($koneksi,
    "SELECT COUNT(*) AS total FROM kritik_saran WHERE status='baru'"
);
$totalData = mysqli_fetch_assoc($totalQuery)['total'];
$totalPage = ceil($totalData / $limit);

// Ambil data terbaru dengan status Baru
$query = mysqli_query($koneksi,
    "SELECT * FROM kritik_saran
     WHERE status='baru'
     ORDER BY id DESC
     LIMIT $start, $limit"
);

$adj = 2;
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Kritik & Saran Baru</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/sidebar.css'); ?>">
<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/layout.css'); ?>">
<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/dashboard.css'); ?>">
<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/widgets.css'); ?>">

<style>
.badge-baru { background:#198754; }
</style>

</head>
<body class="dashboard-page">

<?php include __DIR__ . "/../../assets/component/dashboard_sidebar.php"; ?>

<div id="content-wrapper" class="content-wrapper">
<?php include __DIR__ . "/../../assets/component/dashboard_navbar.php"; ?>

<main class="container-fluid mt-4">

    <h1 class="page-title">Kritik & Saran Baru</h1>
    <p class="text-muted small">Masukan yang belum dibaca</p>

    <div class="card mt-4">
        <div class="card-header bg-success text-white fw-bold">
            Daftar Pesan Baru
        </div>

        <div class="card-body table-responsive">
            <table class="table table-hover table-bordered text-center">
                <thead class="table-success">
                    <tr>
                        <th>No</th>
                        <th>Nama</th>
                        <th>No HP</th>
                        <th>Isi Pesan</th>
                        <th>Status</th>
                        <th>Tanggal</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                <?php 
                $no = $start + 1;
                if($totalData > 0):
                while($row = mysqli_fetch_assoc($query)):
                ?>
                <tr>
                    <td><?= $no++; ?></td>
                    <td><?= esc($row['nama']); ?></td>
                    <td><?= esc($row['no_hp']); ?></td>
                    <td class="text-start"><?= esc(substr($row['pesan'],0,40)); ?>...</td>
                    <td><span class="badge badge-baru">Baru</span></td>
                    <td><?= esc($row['tanggal']); ?><br><?= esc($row['jam']); ?></td>

                    <td>
                        <a href="baca_kritiksaran.php?id=<?= $row['id']; ?>" class="btn btn-sm btn-info">
                            <i class="fa fa-eye"></i>
                        </a>
                        <a href="hapus_kritiksaran.php?id=<?= $row['id']; ?>"
                           onclick="return confirm('Yakin hapus pesan ini?')"
                           class="btn btn-sm btn-danger">
                           <i class="fa fa-trash"></i>
                        </a>
                    </td>
                </tr>
                <?php endwhile; else: ?>
                <tr>
                    <td colspan="7" class="text-center text-muted">
                        Tidak ada pesan baru.
                    </td>
                </tr>
                <?php endif; ?>
                </tbody>
            </table>

            <!-- PAGINATION -->
            <?php if ($totalPage > 1): ?>
            <nav>
                <ul class="pagination justify-content-center mt-3">

                    <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=1">&laquo;</a>
                    </li>
                    <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=<?= $page - 1 ?>">&lsaquo;</a>
                    </li>

                    <?php
                    $startPage = max(1, $page - $adj);
                    $endPage   = min($totalPage, $page + $adj);

                    if ($startPage > 1):
                        echo '<li class="page-item"><a class="page-link" href="?page=1">1</a></li>';
                        if ($startPage > 2)
                            echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
                    endif;

                    for($i = $startPage; $i <= $endPage; $i++):
                    ?>
                        <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                            <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
                        </li>
                    <?php endfor;

                    if ($endPage < $totalPage):
                        if ($endPage < $totalPage - 1)
                            echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
                        echo '<li class="page-item"><a class="page-link" href="?page='.$totalPage.'">'.$totalPage.'</a></li>';
                    endif;
                    ?>

                    <li class="page-item <?= ($page >= $totalPage) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=<?= $page + 1 ?>">&rsaquo;</a>
                    </li>
                    <li class="page-item <?= ($page >= $totalPage) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=<?= $totalPage ?>">&raquo;</a>
                    </li>

                </ul>
            </nav>
            <?php endif; ?>

        </div>
    </div>

</main>

</div>

<?php include __DIR__ . "/../../assets/component/dashboard_footer.php"; ?>

</body>
</html>
