<?php
session_start();
require_once __DIR__ . '/config/config.php';

/* =============================
   PROTEKSI ADMIN
============================= */
if (!isset($_SESSION['login']) || $_SESSION['role'] !== 'admin') {
    die("Akses ditolak!");
}

/* =============================
   AMBIL DATA USERS
============================= */
$sql = "SELECT id_user, nama_lengkap, username, role, status, last_login
        FROM users
        ORDER BY role ASC, id_user DESC";
$data = $conn->query($sql);

if (!$data) {
    die("Query error: " . $conn->error);
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Data Users</title>

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

<style>
body{
    background:#f4f6f9;
    padding:25px;
    font-family:"Segoe UI",sans-serif;
    font-size:13px;
}
.card-custom{
    background:#fff;
    padding:22px;
    border-radius:14px;
    box-shadow:0 8px 20px rgba(0,0,0,.08);
}
.table th{
    background:#f8f9fa;
    text-align:center;
    white-space:nowrap;
}
.table td{
    vertical-align:middle;
    white-space:nowrap;
}
.badge-role-admin{background:#0d6efd}
.badge-role-sekolah{background:#198754}
.badge-status-aktif{background:#20c997}
.badge-status-nonaktif{background:#dc3545}
</style>
</head>

<body>

<a href="dashboard_admin.php" class="btn btn-outline-secondary mb-3">
    ⬅ Kembali ke Dashboard
</a>

<div class="card-custom">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="mb-0">👤 Data Users</h4>
        <a href="users_tambah.php" class="btn btn-success">
            ➕ Tambah User
        </a>
    </div>

    <div class="table-responsive">
        <table class="table table-bordered table-hover align-middle">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Nama Lengkap</th>
                    <th>Username / NPSN</th>
                    <th>Role</th>
                    <th>Status</th>
                    <th>Last Login</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
            <?php $no=1; while($row=$data->fetch_assoc()){ ?>
                <tr>
                    <td class="text-center"><?= $no++ ?></td>
                    <td><?= htmlspecialchars($row['nama_lengkap']) ?></td>
                    <td><?= htmlspecialchars($row['username']) ?></td>

                    <td class="text-center">
                        <?php if($row['role']=='admin'){ ?>
                            <span class="badge badge-role-admin">Admin</span>
                        <?php } else { ?>
                            <span class="badge badge-role-sekolah">Sekolah</span>
                        <?php } ?>
                    </td>

                    <td class="text-center">
                        <?php if($row['status']=='aktif'){ ?>
                            <span class="badge badge-status-aktif">Aktif</span>
                        <?php } else { ?>
                            <span class="badge badge-status-nonaktif">Nonaktif</span>
                        <?php } ?>
                    </td>

                    <td class="text-center">
                        <?= $row['last_login']
                            ? date('d-m-Y H:i', strtotime($row['last_login']))
                            : '-' ?>
                    </td>

                    <td class="text-center">
                        <?php if($row['role'] != 'admin'){ ?>
                            <a href="users_edit.php?id=<?= $row['id_user'] ?>"
                               class="btn btn-warning btn-sm">✏ Edit</a>

                            <a href="users_hapus.php?id=<?= $row['id_user'] ?>"
                               class="btn btn-danger btn-sm"
                               onclick="return confirm('Yakin hapus user ini?')">
                               🗑 Hapus
                            </a>
                        <?php } else { ?>
                            <span class="text-muted">Admin</span>
                        <?php } ?>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
    </div>
</div>

</body>
</html>
