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

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

$nama_admin = $_SESSION['nama'] ?? 'Administrator';

// Hitung Statistik untuk Badge
$total_pelanggan = (int)$pdo->query("SELECT COUNT(*) FROM pelanggan WHERE status='aktif'")->fetchColumn();
$total_tagihan = (int)$pdo->query("SELECT COUNT(*) FROM tagihan WHERE status='belum_lunas'")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Dashboard Admin - SPAM</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
body { background-color: #f4f7fe; font-family: 'Segoe UI', sans-serif; }
.header-section {
    background: linear-gradient(135deg, #3A7CA5 0%, #2C5F7D 100%); /* BIRU SAGE */
    color: white;
    padding: 30px 20px 40px;
    border-bottom-left-radius: 40px;
    border-bottom-right-radius: 40px;
    margin-bottom: -20px;
}
.header-content h5 { font-weight: 300; opacity: 0.9; margin-bottom: 2px; font-size: 0.95rem; }
.header-content h2 { font-weight: 800; font-size: 1.6rem; margin-bottom: 5px; }
.admin-tag {
    display: inline-block;
    background: rgba(255,255,255,0.2);
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    backdrop-filter: blur(5px);
}
.menu-container { padding: 0 15px; }
.menu-card {
    position: relative;
    border: none;
    border-radius: 22px;
    text-decoration: none;
    color: #333;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 25px 10px;
    background: white;
    box-shadow: 0 8px 15px rgba(0,0,0,0.04);
    transition: all 0.2s ease;
    height: 100%;
}
.menu-card:active { transform: scale(0.95); }
.count-badge {
    position: absolute;
    top: 12px;
    right: 12px;
    background: #f8f9fa;
    color: #3A7CA5;
    font-weight: 800;
    font-size: 0.8rem;
    min-width: 30px;
    height: 25px;
    padding: 0 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    border: 1px solid #eee;
}
.menu-icon { font-size: 2.5rem; margin-bottom: 12px; }
.menu-label { font-size: 1rem; font-weight: 700; color: #444; text-align: center;}
.bg-pelanggan { color: #3A7CA5; }
.bg-tarif { color: #E67E22; }
.bg-laporan { color: #F1C40F; }
.bg-kas { color: #2ECC71; }
.bg-berita { color: #9B59B6; }
.bg-setting { color: #1ABC9C; }
.btn-logout {
    display: inline-flex;
    align-items: center;
    padding: 8px 25px;
    border-radius: 30px;
    font-size: 0.85rem;
    font-weight: 700;
    margin: 25px 0 10px;
    border: 2px solid #e74c3c;
    color: #e74c3c;
    text-decoration: none;
}
.app-footer { font-size: 0.65rem; color: #aaa; letter-spacing: 0.5px; padding-bottom: 20px; }
</style>
</head>
<body>
<div class="header-section">
    <div class="header-content">
        <h5>Selamat datang,</h5>
        <h2><?= htmlspecialchars($nama_admin) ?></h2>
        <div class="admin-tag">
            <i class="fa fa-user-shield me-1"></i> Administrator Sistem SPAM
        </div>
    </div>
</div>

<div class="menu-container">
    <div class="row g-3">
        <div class="col-6">
            <a href="manajemen_pelanggan.php" class="menu-card">
                <div class="count-badge"><?= $total_pelanggan ?></div>
                <i class="fa-solid fa-users menu-icon bg-pelanggan"></i>
                <span class="menu-label">Pelanggan</span>
            </a>
        </div>
        <div class="col-6">
            <a href="manajemen_tarif.php" class="menu-card">
                <i class="fa-solid fa-money-bill-wave menu-icon bg-tarif"></i>
                <span class="menu-label">Manajemen Tarif</span>
            </a>
        </div>
        <div class="col-6">
            <a href="laporan_keuangan.php" class="menu-card">
                <div class="count-badge"><?= $total_tagihan ?></div>
                <i class="fa-solid fa-file-invoice-dollar menu-icon bg-laporan"></i>
                <span class="menu-label">Laporan Keuangan</span>
            </a>
        </div>
        <div class="col-6">
            <a href="buku_kas.php" class="menu-card">
                <i class="fa-solid fa-book menu-icon bg-kas"></i>
                <span class="menu-label">Buku Kas</span>
            </a>
        </div>
        <div class="col-6">
            <a href="manajemen_berita.php" class="menu-card">
                <i class="fa-solid fa-newspaper menu-icon bg-berita"></i>
                <span class="menu-label">Berita</span>
            </a>
        </div>
        <div class="col-6">
            <a href="pengaturan.php" class="menu-card">
                <i class="fa-solid fa-gear menu-icon bg-setting"></i>
                <span class="menu-label">Pengaturan</span>
            </a>
        </div>
    </div>
    
    <div class="text-center">
        <a href="../logout.php" class="btn-logout" onclick="return confirm('Yakin ingin keluar?')">
            <i class="fa-solid fa-arrow-right-from-bracket me-2"></i> KELUAR
        </a>
        <div class="app-footer">SPAM Desa Gemuruh v.1.0</div>
    </div>
</div>
</body>
</html>