<?php
session_start();
include "../../config/setting.php";

// ==================== DATA STATISTIK ====================
$today = date("Y-m-d");
$thbln = date("Y-m");
$income = mysql_fetch_assoc(mysql_query("SELECT SUM(nominal) AS income FROM pembayaran WHERE status='LUNAS'"))['income'];
$revenue_month = mysql_fetch_assoc(mysql_query("SELECT SUM(nominal) AS income FROM pembayaran WHERE status='LUNAS' AND waktu_bayar LIKE '$thbln%'"))['income'];

$admin_stats = array(
    'total_users' => mysql_num_rows(mysql_query("SELECT id FROM user")),
    'total_members' => mysql_num_rows(mysql_query("SELECT id FROM anggota")),
    'total_seminars' => mysql_num_rows(mysql_query("SELECT id FROM seminar")),
    'total_seminar_participants' => mysql_num_rows(mysql_query("SELECT id FROM peserta_seminar")),
    'total_income' => $income,
    'active_seminars' => mysql_num_rows(mysql_query("SELECT id FROM seminar WHERE aktif='Y'")),
    'expired' => mysql_num_rows(mysql_query("SELECT id FROM anggota WHERE tgl_expired < '$today'")),
    'revenue_month' => $revenue
);

// Data pengguna baru (contoh)
$new_users = array(
    array('name' => 'Siti Rahayu', 'email' => 'siti@email.com', 'joined' => '2 jam lalu', 'type' => 'Anggota'),
    array('name' => 'Andi Wijaya', 'email' => 'andi@email.com', 'joined' => '5 jam lalu', 'type' => 'Umum'),
    array('name' => 'Rina Melati', 'email' => 'rina@email.com', 'joined' => '1 hari lalu', 'type' => 'Anggota'),
    array('name' => 'Bambang Susanto', 'email' => 'bambang@email.com', 'joined' => '2 hari lalu', 'type' => 'Umum'),
);

// Data seminar aktif (contoh)
$active_seminars = array(
    array('title' => 'Seminar Pendidikan Karakter', 'date' => '2024-03-15', 'participants' => 145, 'income' => 7250000),
    array('title' => 'Workshop Digital Learning', 'date' => '2024-03-20', 'participants' => 89, 'income' => 4450000),
    array('title' => 'Pelatihan Kurikulum Merdeka', 'date' => '2024-03-25', 'participants' => 203, 'income' => 10150000),
);

// Cek session login dan role admin
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true || $_SESSION['role'] !== 'admin') {
    header("Location: " . $http . "login");
    exit();
}

// ==================== TENTUKAN MENU AKTIF ====================
$menu = isset($_GET['menu']) ? $_GET['menu'] : 'overview';
?>
<!DOCTYPE html>
<html lang="id" data-theme="light">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <base href="<?= $http ?>administrator/">
    <title>Admin Dashboard - <?php echo htmlspecialchars($set['title']); ?></title>
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    
    <!-- Lucide Icons -->
    <script src="https://unpkg.com/lucide@latest"></script>

    <!-- Custom Font: Inter -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Custom CSS -->
    <style>
        :root {
            --primary-color: #4361ee;
            --primary-dark: #3a56d4;
            --primary-light: #eef2ff;
            --secondary-color: #6c757d;
            --success-color: #10b981;
            --warning-color: #f59e0b;
            --info-color: #3b82f6;
            --danger-color: #ef4444;
            --gray-50: #f9fafb;
            --gray-100: #f3f4f6;
            --gray-200: #e5e7eb;
            --gray-300: #d1d5db;
            --gray-400: #9ca3af;
            --gray-600: #4b5563;
            --gray-700: #374151;
            --gray-800: #1f2937;
            --border-radius: 12px;
            --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
            --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
            --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
        }
        
        [data-theme="dark"] {
            --gray-50: #1f2937;
            --gray-100: #111827;
            --gray-200: #374151;
            --gray-300: #4b5563;
            --gray-400: #6b7280;
            --gray-600: #d1d5db;
            --gray-700: #e5e7eb;
            --gray-800: #f3f4f6;
        }
        
        a {
            text-decoration: none !important; 
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background-color: var(--gray-50);
            color: var(--gray-700);
            line-height: 1.6;
            transition: background-color 0.3s ease;
        }
        
        .dashboard-container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 0 1rem;
        }
        
        .card {
            background: white;
            border: 1px solid var(--gray-200);
            border-radius: var(--border-radius);
            transition: all 0.3s ease;
        }
        
        [data-theme="dark"] .card {
            background: var(--gray-100);
            border-color: var(--gray-200);
        }
        
        .stat-card {
            background: white;
            border: 1px solid var(--gray-200);
            border-radius: var(--border-radius);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
            position: relative;
            overflow: hidden;
        }
        
        .stat-card:hover {
            transform: translateY(-2px);
            box-shadow: var(--shadow);
        }
        
        .stat-icon {
            width: 35px;
            height: 35px;
            border-radius: 5px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }
        
        .nav-tab {
            padding: 12px 16px;
            border: none;
            background: transparent;
            color: var(--gray-600);
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 10px;
            width: 100%;
            text-align: left;
            border-radius: 8px;
            transition: all 0.2s ease;
        }
        
        .nav-tab:hover, .nav-tab.active {
            background-color: var(--primary-light);
            color: var(--primary-color);
        }
        
        [data-theme="dark"] .nav-tab:hover,
        [data-theme="dark"] .nav-tab.active {
            background-color: rgba(67, 97, 238, 0.1);
        }
        
        .chart-container {
            position: relative;
            height: 300px;
            width: 100%;
        }
        
        .setting-item {
            padding: 1.25rem;
            border-bottom: 1px solid var(--gray-200);
            transition: background-color 0.2s ease;
        }
        
        .setting-item:hover {
            background-color: var(--gray-50);
        }
        
        .setting-item:last-child {
            border-bottom: none;
        }
        
        .form-switch .form-check-input {
            width: 3em;
            height: 1.5em;
        }
        
        .tab-content {
            animation: fadeIn 0.3s ease;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .income-display {
            font-size: 1rem;
            font-weight: 700;
            color: var(--success-color);
        }
        
        .income-today {
            font-size: 1rem !important;
            font-weight: 700;
        }
        
        .growth-badge {
            font-size: 0.85rem;
            padding: 2px 8px;
            border-radius: 12px;
        }
        
        .table-admin {
            background: white;
            border-radius: var(--border-radius);
            overflow: hidden;
        }
        
        [data-theme="dark"] .table-admin {
            background: var(--gray-100);
        }
        
        .table-admin th {
            background-color: var(--gray-50);
            font-weight: 600;
            padding: 1rem;
            border-bottom: 2px solid var(--gray-200);
        }
        
        .table-admin td {
            padding: 1rem;
            vertical-align: middle;
        }
        
        .setting-section {
            background: var(--gray-50);
            border-radius: var(--border-radius);
            padding: 1.5rem;
            margin-bottom: 1.5rem;
        }
        
        [data-theme="dark"] .setting-section {
            background: var(--gray-100);
        }
        
        .danger-zone {
            border: 2px solid var(--danger-color);
            background: rgba(239, 68, 68, 0.05);
            border-radius: var(--border-radius);
            padding: 1.5rem;
        }
        
        .stat-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            border-radius: var(--border-radius) var(--border-radius) 0 0;
        }
        
        .stat-card.total-users::before { background: var(--primary-color); }
        .stat-card.total-members::before { background: var(--success-color); }
        .stat-card.total-seminars::before { background: var(--warning-color); }
        .stat-card.total-income::before { background: var(--info-color); }
        
        @media (max-width: 768px) {
            .dashboard-container {
                padding: 0 0.5rem;
            }
            
            .stat-card {
                margin-bottom: 1rem;
            }
            
            .income-display {
                font-size: 1.5rem;
            }
        }
        
        .alert-custom {
            border-radius: var(--border-radius);
            border: 1px solid;
        }
        
        .theme-card {
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .theme-card.active {
            border-color: var(--primary-color) !important;
            background-color: var(--primary-light);
        }
    </style>
</head>
<body class="min-vh-100">
    <!-- ==================== NOTIFIKASI ==================== -->
    <?php if (isset($_SESSION['success_message'])): ?>
    <div class="position-fixed top-0 end-0 p-3" style="z-index: 9999">
        <div class="alert alert-success alert-custom alert-dismissible fade show" role="alert">
            <i data-lucide="check-circle" class="me-2"></i>
            <?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    </div>
    <?php endif; ?>
    
    <?php if (isset($_SESSION['error_message'])): ?>
    <div class="position-fixed top-0 end-0 p-3" style="z-index: 9999">
        <div class="alert alert-danger alert-custom alert-dismissible fade show" role="alert">
            <i data-lucide="alert-circle" class="me-2"></i>
            <?php echo $_SESSION['error_message']; unset($_SESSION['error_message']); ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    </div>
    <?php endif; ?>

    <?= inc('navbar') ?>

    <!-- ==================== MAIN CONTENT ==================== -->
    <main class="py-4">
        <div class="dashboard-container">
            <div class="row g-4">
                <!-- Sidebar -->
                <div class="col-lg-3">
                    
                    <!-- Quick Actions -->
                    <div class="card p-4">
                        <h6 class="fw-bold mb-3">Aksi Cepat</h6>
                        <div class="d-flex flex-column gap-2">
                            <a href="users-add/" class="btn btn-outline-primary btn-sm d-flex align-items-center gap-2">
                                <i data-lucide="user-plus" width="16"></i>
                                Tambah Pengguna
                            </a>
                            <a href="seminars-add/" class="btn btn-outline-primary btn-sm d-flex align-items-center gap-2">
                                <i data-lucide="calendar-plus" width="16"></i>
                                Buat Seminar
                            </a>
                        </div>
                    </div>
                    
                    <div class="card p-4 mt-4">
                        <div class="text-center mb-4">
                            <div class="position-relative d-inline-block">
                                <div class="bg-primary bg-opacity-10 text-primary rounded-circle d-flex align-items-center justify-content-center fw-bold display-4 mb-3" style="width: 80px; height: 80px;">
                                    A
                                </div>
                            </div>
                            <h5 class="fw-bold mb-1">Administrator</h5>
                            <p class="text-muted small mb-3">System Administrator</p>
                            <span class="badge bg-primary rounded-pill px-3 py-2">
                                Super Admin
                            </span>
                        </div>

                        <nav class="d-flex flex-column gap-2">
                            <a href="overview/" class="nav-tab <?php echo $menu == 'overview' ? 'active' : ''; ?>">
                                <i data-lucide="layout-dashboard" width="18"></i>
                                <span>Dashboard</span>
                            </a>
                            <a href="users-manage/" class="nav-tab <?php echo $menu == 'users' ? 'active' : ''; ?>">
                                <i data-lucide="users" width="18"></i>
                                <span>Manajemen Pengguna</span>
                            </a>
                            <a href="membership-manage/" class="nav-tab <?php echo $menu == 'membership' ? 'active' : ''; ?>">
                                <i data-lucide="user-check" width="18"></i>
                                <span>Manajemen Keanggotaan</span>
                            </a>
                            <a href="seminars-manage/" class="nav-tab <?php echo $menu == 'seminars-manage' ? 'active' : ''; ?>">
                                <i data-lucide="calendar" width="18"></i>
                                <span>Manajemen Seminar</span>
                            </a>
                            <a href="finance-manage/" class="nav-tab <?php echo $menu == 'finance' ? 'active' : ''; ?>">
                                <i data-lucide="dollar-sign" width="18"></i>
                                <span>Keuangan</span>
                            </a>
                            <a href="settings/" class="nav-tab <?php echo $menu == 'settings' ? 'active' : ''; ?>">
                                <i data-lucide="settings" width="18"></i>
                                <span>Pengaturan Sistem</span>
                            </a>
                            <a href="<?php echo $http; ?>logout" class="nav-tab text-danger mt-2">
                                <i data-lucide="log-out" width="18"></i>
                                <span>Keluar</span>
                            </a>
                        </nav>
                    </div>
                </div>

                <!-- Main Content -->
                <div class="col-lg-9">
<?php
if (session_status() === PHP_SESSION_NONE) session_start();

$BASE_DIR = realpath(__DIR__);

$menu = isset($_GET['menu']) ? $_GET['menu'] : 'overview/';
$menu = trim($menu, '/');
$menu = preg_replace('/[^a-zA-Z0-9_-]/', '', $menu);

if ($menu === 'overview') {
    $file = $BASE_DIR . '/admin_overview.php';
} elseif (strpos($menu, '-') !== false) {
    list($module, $action) = explode('-', $menu, 2);
    $file = $BASE_DIR . '/admin_' . $module . '/' . $action . '.php';
} else {
    $file = $BASE_DIR . '/admin_' . $menu . '.php';
}

$path = realpath($file);

if ($path !== false && strpos($path, $BASE_DIR) === 0 && file_exists($path)) {
    include $path;
} else {
    include $BASE_DIR . '/admin_overview.php';
}
?>
                </div>
            </div>
        </div>
    </main>

    <?= inc('footer') ?>

    <!-- ==================== SCRIPTS ==================== -->
    <!-- Bootstrap Bundle JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

    <!-- Admin JS -->
    <script src="<?= $http ?>/assets/js/admin.js"></script>

    <script>
        // Inisialisasi icons
        document.addEventListener('DOMContentLoaded', function() {
            lucide.createIcons();
            <?php if($menu == 'overview'): ?>
            initializeAdminCharts();
            <?php endif; ?>
        });
    </script>
</body>
</html>