<?php
if(session_status() === PHP_SESSION_NONE){
    session_start();
}

require_once __DIR__ . '/../config.php';

/* ===== CEK LOGIN ===== */

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

/* ===== HITUNG DATA ===== */

$jml_masuk = $conn->query("
SELECT COUNT(*) as jml 
FROM pengajuan 
WHERE status='Menunggu'
")->fetch_assoc()['jml'];

$jml_diterima = $conn->query("
SELECT COUNT(*) as jml 
FROM pengajuan 
WHERE status='Diterima'
")->fetch_assoc()['jml'];

$jml_ditolak = $conn->query("
SELECT COUNT(*) as jml 
FROM pengajuan 
WHERE status='Ditolak'
")->fetch_assoc()['jml'];

$jml_selesai = $conn->query("
SELECT COUNT(*) as jml 
FROM pengajuan 
WHERE no_surat IS NOT NULL
")->fetch_assoc()['jml'];
?>

<!DOCTYPE html>
<html lang="id">

<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Admin Panel</title>

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

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>

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

/* ===== SIDEBAR ===== */

.sidebar{
    width:250px;
    height:100vh;
    position:fixed;
    top:0;
    left:0;
    background:#0f172a;
    color:white;
    overflow-y:auto;
    z-index:999;
}

.sidebar-header{
    padding:22px;
    font-size:22px;
    font-weight:bold;
    border-bottom:1px solid rgba(255,255,255,0.1);
}

.sidebar a{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:10px;
    padding:14px 20px;
    color:#cbd5e1;
    text-decoration:none;
    transition:0.2s;
    font-size:15px;
}

.sidebar a:hover{
    background:#1e293b;
    color:white;
}

.sidebar a.active{
    background:#2563eb;
    color:white;
}

.menu-left{
    display:flex;
    align-items:center;
    gap:10px;
}

/* ===== CONTENT ===== */

.content{
    margin-left:250px;
    padding:25px;
}

/* ===== CARD ===== */

.card{
    border:none;
    border-radius:16px;
    overflow:hidden;
}

.card-header{
    font-weight:bold;
}

/* ===== TABLE ===== */

.table{
    vertical-align:middle;
}

.table th{
    background:#f8fafc;
    width:30%;
}

/* ===== BADGE ===== */

.badge{
    font-size:13px;
    padding:7px 10px;
}

/* ===== RESPONSIVE ===== */

@media(max-width:768px){

    .sidebar{
        width:100%;
        height:auto;
        position:relative;
    }

    .content{
        margin-left:0;
    }

}

</style>

</head>

<body>

<!-- SIDEBAR -->

<div class="sidebar">

<div class="sidebar-header">
📁 Admin Panel
</div>

<?php if($_SESSION['role'] == 'admin1'){ ?>

<a href="surat_masuk.php">

<div class="menu-left">
<span>📥</span>
<span>Surat Masuk</span>
</div>

<span class="badge bg-danger">
<?= $jml_masuk ?>
</span>

</a>

<a href="surat_diterima.php">

<div class="menu-left">
<span>✅</span>
<span>Surat Diterima</span>
</div>

<span class="badge bg-success">
<?= $jml_diterima ?>
</span>

</a>

<a href="surat_ditolak.php">

<div class="menu-left">
<span>❌</span>
<span>Surat Ditolak</span>
</div>

<span class="badge bg-secondary">
<?= $jml_ditolak ?>
</span>

</a>

<a href="surat_selesai.php">

<div class="menu-left">
<span>📦</span>
<span>Surat Selesai</span>
</div>

<span class="badge bg-primary">
<?= $jml_selesai ?>
</span>

</a>

<a href="rekap_triwulan.php">

<div class="menu-left">
<span>📋</span>
<span>Rekap Triwulan</span>
</div>

</a>

<a href="survei.php">

<div class="menu-left">
<span>📊</span>
<span>Survei</span>
</div>

</a>

<?php } elseif($_SESSION['role'] == 'admin2'){ ?>

<a href="../admin2/surat_diterima.php">

<div class="menu-left">
<span>📄</span>
<span>Surat Diterima</span>
</div>

</a>

<a href="../admin2/surat_selesai.php">

<div class="menu-left">
<span>📦</span>
<span>Surat Selesai</span>
</div>

</a>

<?php } ?>

</div>

<!-- CONTENT -->

<div class="content">