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

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

if ($_SESSION['role'] != 'admin') {
    die("Akses ditolak");
}

$data = mysqli_query(
    $conn,
    "SELECT * FROM users ORDER BY nama ASC"
);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Data User</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;
        }

        .table th,
        .table td {
            vertical-align: middle;
        }

        .btn {
            border-radius: 8px;
        }

        /* Tampilan Mobile */
        @media (max-width: 768px) {

            h4 {
                font-size: 18px;
            }

            .table {
                font-size: 13px;
            }

            .btn-sm {
                width: 100%;
                margin-bottom: 5px;
            }

            .aksi-group {
                min-width: 140px;
            }

        }

    </style>
</head>

<body>

<div class="container mt-4">

    <div class="card shadow">

        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">

            <h4 class="mb-0">
                Data User
            </h4>

            <a href="tambah_user.php" class="btn btn-light btn-sm">
                + Tambah User
            </a>

        </div>

        <div class="card-body">

            <div class="table-responsive">

                <table class="table table-bordered table-striped table-hover">

                    <thead class="table-dark text-center">

                        <tr>
                            <th width="60">No</th>
                            <th>Nama</th>
                            <th>Username</th>
                            <th width="120">Role</th>
                            <th width="220">Aksi</th>
                            <th width="170">Absensi</th>
                        </tr>

                    </thead>

                    <tbody>

                    <?php
                    $no = 1;

                    if (mysqli_num_rows($data) > 0) {

                        while ($u = mysqli_fetch_assoc($data)) {
                    ?>

                        <tr>

                            <td class="text-center">
                                <?= $no++ ?>
                            </td>

                            <td>
                                <?= htmlspecialchars($u['nama']) ?>
                            </td>

                            <td>
                                <?= htmlspecialchars($u['username']) ?>
                            </td>

                            <td class="text-center">

                                <?php if ($u['role'] == 'admin') { ?>

                                    <span class="badge bg-danger">
                                        Admin
                                    </span>

                                <?php } else { ?>

                                    <span class="badge bg-success">
                                        User
                                    </span>

                                <?php } ?>

                            </td>

                            <td class="text-center aksi-group">

                                <a href="edit_user.php?id=<?= $u['id'] ?>"
                                   class="btn btn-warning btn-sm">

                                    Edit

                                </a>

                                <a href="hapus_user.php?id=<?= $u['id'] ?>"
                                   class="btn btn-danger btn-sm"
                                   onclick="return confirm('Yakin hapus user ini beserta seluruh data absensi?')">

                                    Hapus

                                </a>

                            </td>

                            <td class="text-center">

                                <a href="absensi_manual.php?id=<?= $u['id'] ?>"
                                   class="btn btn-primary btn-sm">

                                    Kelola Absensi

                                </a>

                            </td>

                        </tr>

                    <?php
                        }

                    } else {
                    ?>

                        <tr>

                            <td colspan="6" class="text-center text-muted">

                                Belum ada data user

                            </td>

                        </tr>

                    <?php } ?>

                    </tbody>

                </table>

            </div>

        </div>

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

            <a href="dashboard.php" class="btn btn-secondary">

                Kembali ke Dashboard

            </a>

        </div>

    </div>

</div>

</body>
</html>