<?php
session_start();
include 'config.php';

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

$user_id = $_SESSION['user_id'];

$limit = 10;

$halaman = isset($_GET['page']) ? (int)$_GET['page'] : 1;

if ($halaman < 1) {
    $halaman = 1;
}

$start = ($halaman - 1) * $limit;

/* =========================
   TOTAL DATA
========================= */
$totalQuery = mysqli_query(
    $conn,
    "
    SELECT COUNT(*) AS total
    FROM absensi
    WHERE user_id = '$user_id'
    "
);

$totalData = mysqli_fetch_assoc($totalQuery)['total'];
$totalHalaman = ceil($totalData / $limit);

/* =========================
   DATA ABSENSI
========================= */
$query = mysqli_query(
    $conn,
    "
    SELECT *
    FROM absensi
    WHERE user_id = '$user_id'
    ORDER BY tanggal DESC, id DESC
    LIMIT $start, $limit
    "
);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Riwayat Absensi Saya</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

    <style>
        body {
            background: #f5f7fb;
        }

        .card {
            border: none;
            border-radius: 12px;
        }

        @media (max-width: 768px) {

            .card {
                border-radius: 12px;
            }

            .card-body {
                padding: 12px;
            }

            h5 {
                font-size: 16px;
            }

            .pagination {
                font-size: 12px;
            }

            .table {
                font-size: 13px;
            }
        }
    </style>
</head>

<body>

<div class="container mt-4">

    <div class="card shadow">

        <div class="card-header bg-primary text-white">
            <h5 class="mb-0">Riwayat Absensi Saya</h5>
        </div>

        <div class="card-body">

            <div class="table-responsive">

                <table class="table table-bordered table-striped align-middle">

                    <thead class="table-dark text-center">
                        <tr>
                            <th width="60">No</th>
                            <th>Tanggal</th>
                            <th>Jam Masuk</th>
                            <th>Jam Pulang</th>
                            <th>Status</th>
                        </tr>
                    </thead>

                    <tbody>

                    <?php
                    $no = $start + 1;

                    if (mysqli_num_rows($query) > 0) {

                        while ($row = mysqli_fetch_assoc($query)) {
                    ?>

                        <tr class="text-center">
                            <td><?= $no++ ?></td>
                            <td><?= $row['tanggal'] ?></td>
                            <td><?= $row['jam_masuk'] ?: '-' ?></td>
                            <td><?= $row['jam_pulang'] ?: '-' ?></td>
                            <td>

                                <?php
                                $badge = "secondary";

                                if ($row['status'] == 'Hadir') {
                                    $badge = "success";
                                } elseif ($row['status'] == 'Sakit') {
                                    $badge = "warning";
                                } elseif ($row['status'] == 'Izin') {
                                    $badge = "info";
                                } elseif ($row['status'] == 'Cuti') {
                                    $badge = "primary";
                                } elseif ($row['status'] == 'Alpha') {
                                    $badge = "danger";
                                }
                                ?>

                                <span class="badge bg-<?= $badge ?>">
                                    <?= $row['status'] ?>
                                </span>

                            </td>
                        </tr>

                    <?php
                        }

                    } else {
                    ?>

                        <tr>
                            <td colspan="5" class="text-center text-muted">
                                Belum ada data absensi
                            </td>
                        </tr>

                    <?php } ?>

                    </tbody>

                </table>

            </div>

            <!-- PAGINATION -->
            <?php if ($totalHalaman > 1) { ?>

            <nav class="mt-3">

                <ul class="pagination justify-content-center">

                    <?php if ($halaman > 1) { ?>
                        <li class="page-item">
                            <a class="page-link" href="?page=<?= $halaman - 1 ?>">
                                Previous
                            </a>
                        </li>
                    <?php } ?>

                    <?php for ($i = 1; $i <= $totalHalaman; $i++) { ?>
                        <li class="page-item <?= ($i == $halaman ? 'active' : '') ?>">
                            <a class="page-link" href="?page=<?= $i ?>">
                                <?= $i ?>
                            </a>
                        </li>
                    <?php } ?>

                    <?php if ($halaman < $totalHalaman) { ?>
                        <li class="page-item">
                            <a class="page-link" href="?page=<?= $halaman + 1 ?>">
                                Next
                            </a>
                        </li>
                    <?php } ?>

                </ul>

            </nav>

            <?php } ?>

        </div>

        <div class="card-footer text-center">

            <a href="dashboard.php" class="btn btn-secondary">
                Kembali ke Dashboard
            </a>

        </div>

    </div>

</div>

</body>
</html>