<?php
session_start();
require "../config.php";

if(!isset($_SESSION['login']) || $_SESSION['role'] != 'superadmin'){
    header("Location: ../login.php");
    exit;
}

$data = $conn->query("SELECT * FROM admin ORDER BY id DESC");

$totalAdmin = $conn->query("SELECT COUNT(*) jml FROM admin")->fetch_assoc()['jml'];
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Manajemen User</title>

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

<style>

body{
    margin:0;
    background:#f4f6f9;
    font-family:'Segoe UI',sans-serif;
}

/* SIDEBAR */
.sidebar{
    position:fixed;
    left:0;
    top:0;
    width:260px;
    height:100vh;
    background:#0d6efd;
    color:white;
}

.sidebar .logo{
    text-align:center;
    padding:25px 15px;
    border-bottom:1px solid rgba(255,255,255,.15);
}

.sidebar .logo img{
    width:90px;
    margin-bottom:10px;
}

.sidebar .logo h5{
    margin:0;
    font-weight:600;
}

.sidebar .logo small{
    opacity:.8;
}

.menu{
    display:block;
    color:white;
    text-decoration:none;
    padding:15px 25px;
    transition:.3s;
}

.menu:hover,
.menu.active{
    background:rgba(255,255,255,.15);
    color:white;
}

.menu i{
    width:25px;
}

/* CONTENT */
.content{
    margin-left:260px;
    padding:30px;
}

/* CARD */
.card-dashboard{
    border:none;
    border-radius:20px;
    box-shadow:0 5px 20px rgba(0,0,0,.08);
}

.stat-number{
    font-size:38px;
    font-weight:bold;
    color:#0d6efd;
}

/* TABLE */
.table-card{
    border:none;
    border-radius:20px;
    overflow:hidden;
    box-shadow:0 5px 20px rgba(0,0,0,.08);
}

.table thead{
    background:#0d6efd;
    color:white;
}

.btn{
    border-radius:10px;
}

</style>
</head>

<body>

<!-- SIDEBAR -->
<div class="sidebar">

    <div class="logo">
        <img src="../assets/bws1.png">
        <h5>SI PETESA</h5>
        <small>Balai Wilayah Sungai Kalimantan IV</small>
    </div>

    <a href="index.php" class="menu active">
        <i class="fas fa-users"></i>
        Manajemen User
    </a>

    <a href="logout.php" class="menu">
        <i class="fas fa-sign-out-alt"></i>
        Logout
    </a>

</div>

<!-- CONTENT -->
<div class="content">

    <div class="d-flex justify-content-between align-items-center mb-4">

        <div>
            <h3 class="mb-1">Manajemen User</h3>
            <small class="text-muted">
                Kelola akun administrator sistem
            </small>
        </div>

        <a href="tambah.php" class="btn btn-primary">
            <i class="fas fa-user-plus"></i>
            Tambah User
        </a>

    </div>

    <!-- CARD TOTAL USER -->
    <div class="card card-dashboard mb-4">
        <div class="card-body">

            <div class="row align-items-center">

                <div class="col-md-6">
                    <h6>Total Akun Admin</h6>

                    <div class="stat-number">
                        <?= $totalAdmin ?>
                    </div>
                </div>

                <div class="col-md-6 text-end">
                    <i class="fas fa-users fa-4x text-primary"></i>
                </div>

            </div>

        </div>
    </div>

    <!-- TABEL -->
    <div class="card table-card">

        <div class="card-header bg-white">
            <h5 class="mb-0">Data Administrator</h5>
        </div>

        <div class="card-body">

            <div class="table-responsive">

                <table class="table table-hover align-middle">

                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>Nama</th>
                        <th>Email</th>
                        <th>Role</th>
                        <th>Dibuat</th>
                        <th width="220">Aksi</th>
                    </tr>
                    </thead>

                    <tbody>

                    <?php while($d = $data->fetch_assoc()){ ?>

                    <tr>

                        <td><?= $d['id'] ?></td>

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

                        <td><?= htmlspecialchars($d['email']) ?></td>

                        <td>

                        <?php
                        $warna='secondary';

                        if($d['role']=='superadmin') $warna='danger';
                        elseif($d['role']=='admin1') $warna='primary';
                        elseif($d['role']=='admin2') $warna='success';
                        elseif($d['role']=='admin3') $warna='warning';
                        ?>

                        <span class="badge bg-<?= $warna ?>">
                            <?= strtoupper($d['role']) ?>
                        </span>

                        </td>

                        <td><?= $d['dibuat_pada'] ?></td>

                        <td>

                            <a href="edit.php?id=<?= $d['id'] ?>"
                            class="btn btn-warning btn-sm">
                                <i class="fas fa-edit"></i>
                            </a>

                            <a href="reset_password.php?id=<?= $d['id'] ?>"
                            class="btn btn-info btn-sm text-white">
                                <i class="fas fa-key"></i>
                            </a>

                            <?php if($d['id'] != $_SESSION['id']){ ?>

                            <a href="hapus.php?id=<?= $d['id'] ?>"
                            class="btn btn-danger btn-sm"
                            onclick="return confirm('Hapus akun ini?')">
                                <i class="fas fa-trash"></i>
                            </a>

                            <?php } ?>

                        </td>

                    </tr>

                    <?php } ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</div>

</body>
</html>

