<?php
if (session_status() === PHP_SESSION_NONE) session_start();
require_once '../config/database.php';
require_once '../config/settings.php';
require_once '../includes/auth.php';
require_once '../includes/functions.php';

Auth::requireAdmin();
Auth::trackVisit('admin_dashboard');

$db = Database::getInstance()->getConnection();
$currentUser = Auth::currentUser();
$pageTitle = isset($pageTitle) ? $pageTitle : 'داشبورد مدیریت';

try {
    $pending_orders = $db->query("SELECT COUNT(*) FROM orders WHERE status IN ('pending', 'processing')")->fetchColumn() ?: 0;
    $unread_messages = $db->query("SELECT COUNT(*) FROM messages WHERE status = 'unread'")->fetchColumn() ?: 0;
    $pending_reviews = $db->query("SELECT COUNT(*) FROM reviews WHERE is_approved = 0")->fetchColumn() ?: 0;
    $today_orders = $db->query("SELECT COUNT(*) FROM orders WHERE DATE(created_at) = CURDATE()")->fetchColumn() ?: 0;
} catch (Exception $e) {
    $pending_orders = 0; $unread_messages = 0; $pending_reviews = 0; $today_orders = 0;
}

$current_page = basename($_SERVER['PHP_SELF']);

// منوی اصلی با ساختار دسته‌بندی شده
$menuItems = [
    ['section' => 'عمومی', 'items' => [
        ['url' => 'index.php', 'icon' => 'fas fa-th-large', 'label' => 'داشبورد', 'page' => 'index.php'],
        ['url' => 'analytics.php', 'icon' => 'fas fa-chart-line', 'label' => 'آنالیتیکس', 'page' => 'analytics.php'],
        ['url' => 'reports.php', 'icon' => 'fas fa-chart-bar', 'label' => 'گزارشات', 'page' => 'reports.php'],
    ]],
    ['section' => 'مدیریت', 'items' => [
        ['url' => 'orders.php', 'icon' => 'fas fa-shopping-cart', 'label' => 'سفارشات', 'page' => 'orders.php', 'badge' => $pending_orders],
        ['url' => 'products.php', 'icon' => 'fas fa-box-open', 'label' => 'محصولات', 'page' => 'products.php'],
        ['url' => 'categories.php', 'icon' => 'fas fa-tags', 'label' => 'دسته‌بندی‌ها', 'page' => 'categories.php'],
        ['url' => 'users.php', 'icon' => 'fas fa-users', 'label' => 'کاربران', 'page' => 'users.php'],
    ]],
    ['section' => 'ارتباطات', 'items' => [
        ['url' => 'messages.php', 'icon' => 'fas fa-envelope', 'label' => 'پیام‌ها', 'page' => 'messages.php', 'badge' => $unread_messages],
        ['url' => 'reviews.php', 'icon' => 'fas fa-star', 'label' => 'نظرات', 'page' => 'reviews.php', 'badge' => $pending_reviews],
    ]],
    ['section' => 'سیستم', 'items' => [
        ['url' => 'wallet.php', 'icon' => 'fas fa-wallet', 'label' => 'کیف پول', 'page' => 'wallet.php'],
        ['url' => 'backup.php', 'icon' => 'fas fa-database', 'label' => 'بک‌آپ', 'page' => 'backup.php'],
        ['url' => 'settings.php', 'icon' => 'fas fa-cog', 'label' => 'تنظیمات', 'page' => 'settings.php'],
    ]]
];
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="noindex, nofollow">
    <title><?= htmlspecialchars($pageTitle) ?> | پنل مدیریت</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
    /* ========================================
       ADMIN SIDEBAR — شیشه‌ای مدرن
       ======================================== */
    :root {
        --admin-primary: #8b5cf6;
        --admin-primary-hover: #7c3aed;
        --admin-primary-glow: rgba(139,92,246,0.35);
        --admin-bg: #0a0e1a;
        --admin-sidebar-bg: rgba(12,16,32,0.95);
        --admin-card-bg: rgba(255,255,255,0.04);
        --admin-card-border: rgba(255,255,255,0.08);
        --admin-text: #e2e8f0;
        --admin-text-muted: #64748b;
        --admin-success: #10b981;
        --admin-warning: #f59e0b;
        --admin-danger: #ef4444;
        --admin-info: #3b82f6;
    }
    * { margin:0; padding:0; box-sizing:border-box; }
    body {
        font-family: 'Vazirmatn', sans-serif;
        background: var(--admin-bg);
        color: var(--admin-text);
        min-height: 100vh;
        overflow-x: hidden;
    }
    body::before {
        content: '';
        position: fixed;
        top: -50%; left: -50%;
        width: 200%; height: 200%;
        background: radial-gradient(circle at 20% 30%, rgba(139,92,246,0.08) 0%, transparent 40%),
                    radial-gradient(circle at 80% 70%, rgba(6,182,212,0.06) 0%, transparent 40%),
                    radial-gradient(circle at 50% 10%, rgba(59,130,246,0.05) 0%, transparent 50%);
        z-index: -1;
        animation: bgFloat 25s ease-in-out infinite alternate;
    }
    @keyframes bgFloat {
        0% { transform: translate(0,0) rotate(0deg); }
        100% { transform: translate(-3%,-3%) rotate(2deg); }
    }

    /* Sidebar */
    .admin-sidebar {
        position: fixed;
        top: 0; right: 0;
        width: 260px;
        height: 100vh;
        background: var(--admin-sidebar-bg);
        backdrop-filter: blur(30px) saturate(180%);
        -webkit-backdrop-filter: blur(30px) saturate(180%);
        border-left: 1px solid var(--admin-card-border);
        display: flex;
        flex-direction: column;
        z-index: 100;
        transition: transform 0.35s cubic-bezier(0.16,1,0.3,1);
    }

    /* Sidebar Header */
    .sidebar-brand {
        padding: 20px;
        border-bottom: 1px solid var(--admin-card-border);
    }
    .brand-logo {
        display: flex;
        align-items: center;
        gap: 12px;
        text-decoration: none;
    }
    .brand-icon {
        width: 42px; height: 42px;
        background: linear-gradient(135deg, var(--admin-primary), #06b6d4);
        border-radius: 12px;
        display: flex; align-items: center; justify-content: center;
        color: #fff; font-size: 1.1rem;
        box-shadow: 0 4px 16px rgba(139,92,246,0.4);
    }
    .brand-text {
        font-size: 1.05rem; font-weight: 800;
        background: linear-gradient(135deg, var(--admin-primary), #06b6d4);
        -webkit-background-clip: text; background-clip: text; color: transparent;
    }
    .brand-sub {
        font-size: 0.7rem; color: var(--admin-text-muted); display: block;
    }

    /* Sidebar Nav */
    .sidebar-nav {
        flex: 1;
        overflow-y: auto;
        padding: 12px;
    }
    .sidebar-nav::-webkit-scrollbar { width: 4px; }
    .sidebar-nav::-webkit-scrollbar-thumb { background: var(--admin-card-border); border-radius: 4px; }

    .nav-section {
        margin-bottom: 8px;
    }
    .nav-section-title {
        font-size: 0.68rem;
        font-weight: 700;
        color: var(--admin-text-muted);
        text-transform: uppercase;
        letter-spacing: 1px;
        padding: 8px 12px 6px;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .nav-section-title::after {
        content: '';
        flex: 1;
        height: 1px;
        background: var(--admin-card-border);
    }

    .nav-item {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 11px 14px;
        border-radius: 12px;
        color: var(--admin-text-muted);
        text-decoration: none;
        font-size: 0.88rem;
        font-weight: 500;
        transition: all 0.25s;
        margin-bottom: 2px;
        position: relative;
    }
    .nav-item:hover {
        background: rgba(255,255,255,0.06);
        color: var(--admin-text);
    }
    .nav-item.active {
        background: linear-gradient(135deg, rgba(139,92,246,0.15), rgba(6,182,212,0.08));
        color: #fff;
        font-weight: 600;
        border: 1px solid rgba(139,92,246,0.2);
    }
    .nav-item.active::before {
        content: '';
        position: absolute;
        right: 0; top: 50%;
        transform: translateY(-50%);
        width: 3px; height: 24px;
        background: var(--admin-primary);
        border-radius: 0 4px 4px 0;
    }
    .nav-item i {
        width: 20px;
        text-align: center;
        font-size: 0.9rem;
        flex-shrink: 0;
    }
    .nav-item.active i {
        color: var(--admin-primary);
    }
    .nav-badge {
        margin-right: auto;
        background: var(--admin-danger);
        color: #fff;
        font-size: 0.65rem;
        font-weight: 700;
        min-width: 20px; height: 20px;
        border-radius: 10px;
        display: flex; align-items: center; justify-content: center;
        padding: 0 6px;
        box-shadow: 0 2px 8px rgba(239,68,68,0.4);
    }
    .nav-badge.info { background: var(--admin-info); box-shadow: 0 2px 8px rgba(59,130,246,0.4); }
    .nav-badge.warning { background: var(--admin-warning); box-shadow: 0 2px 8px rgba(245,158,11,0.4); }

    /* Sidebar Footer */
    .sidebar-user {
        padding: 16px;
        border-top: 1px solid var(--admin-card-border);
        display: flex;
        align-items: center;
        gap: 12px;
    }
    .user-avatar-admin {
        width: 40px; height: 40px;
        border-radius: 12px;
        background: linear-gradient(135deg, var(--admin-primary), #06b6d4);
        display: flex; align-items: center; justify-content: center;
        color: #fff; font-size: 0.9rem; font-weight: 700;
        flex-shrink: 0;
    }
    .user-info-admin {
        flex: 1; min-width: 0;
    }
    .user-info-admin h4 {
        font-size: 0.85rem; font-weight: 600; margin-bottom: 2px;
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    .user-info-admin p {
        font-size: 0.72rem; color: var(--admin-text-muted);
    }
    .logout-btn-admin {
        width: 34px; height: 34px;
        border-radius: 10px;
        background: rgba(239,68,68,0.1);
        border: 1px solid rgba(239,68,68,0.2);
        color: var(--admin-danger);
        display: flex; align-items: center; justify-content: center;
        text-decoration: none;
        transition: all 0.25s;
        flex-shrink: 0;
    }
    .logout-btn-admin:hover {
        background: var(--admin-danger);
        color: #fff;
        transform: scale(1.05);
    }

    /* Main Content */
    .admin-main {
        margin-right: 260px;
        min-height: 100vh;
    }

    /* Top Header */
    .admin-topbar {
        position: sticky;
        top: 0;
        z-index: 50;
        height: 60px;
        background: rgba(10,14,26,0.8);
        backdrop-filter: blur(20px);
        -webkit-backdrop-filter: blur(20px);
        border-bottom: 1px solid var(--admin-card-border);
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 24px;
        gap: 16px;
    }
    .topbar-right {
        display: flex; align-items: center; gap: 12px;
    }
    .menu-toggle {
        display: none;
        width: 40px; height: 40px;
        border-radius: 10px;
        background: var(--admin-card-bg);
        border: 1px solid var(--admin-card-border);
        color: var(--admin-text);
        align-items: center; justify-content: center;
        cursor: pointer;
        font-size: 1.1rem;
        transition: all 0.2s;
    }
    .menu-toggle:hover { background: rgba(255,255,255,0.08); }
    .topbar-search {
        position: relative;
    }
    .topbar-search input {
        background: var(--admin-card-bg);
        border: 1px solid var(--admin-card-border);
        border-radius: 10px;
        padding: 8px 14px 8px 36px;
        color: var(--admin-text);
        font-size: 0.85rem;
        width: 280px;
        font-family: inherit;
        transition: all 0.25s;
    }
    .topbar-search input:focus {
        outline: none;
        border-color: var(--admin-primary);
        box-shadow: 0 0 0 3px var(--admin-primary-glow);
        width: 340px;
    }
    .topbar-search input::placeholder { color: var(--admin-text-muted); }
    .topbar-search i {
        position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
        color: var(--admin-text-muted); font-size: 0.85rem;
    }
    .topbar-left {
        display: flex; align-items: center; gap: 8px;
    }
    .topbar-btn {
        width: 38px; height: 38px;
        border-radius: 10px;
        background: var(--admin-card-bg);
        border: 1px solid var(--admin-card-border);
        color: var(--admin-text-muted);
        display: flex; align-items: center; justify-content: center;
        cursor: pointer;
        transition: all 0.25s;
        position: relative;
        text-decoration: none;
        font-size: 0.9rem;
    }
    .topbar-btn:hover {
        background: rgba(255,255,255,0.08);
        color: var(--admin-text);
        transform: translateY(-1px);
    }
    .topbar-btn .dot {
        position: absolute; top: 6px; right: 6px;
        width: 8px; height: 8px;
        border-radius: 50%;
        background: var(--admin-danger);
        box-shadow: 0 0 0 2px var(--admin-bg);
    }

    /* Dashboard Content */
    .dashboard-content {
        padding: 24px;
    }

    /* Overlay */
    .admin-overlay {
        position: fixed; inset: 0;
        background: rgba(0,0,0,0.6);
        z-index: 90;
        opacity: 0; visibility: hidden;
        transition: all 0.3s;
    }
    .admin-overlay.active { opacity: 1; visibility: visible; }

    /* Responsive */
    @media (max-width: 992px) {
        .admin-sidebar {
            transform: translateX(100%);
        }
        .admin-sidebar.active {
            transform: translateX(0);
        }
        .admin-main {
            margin-right: 0;
        }
        .menu-toggle {
            display: flex;
        }
        .topbar-search input { width: 200px; }
        .topbar-search input:focus { width: 240px; }
    }
    @media (max-width: 600px) {
        .dashboard-content { padding: 16px; }
        .topbar-search { display: none; }
        .admin-topbar { padding: 0 16px; }
    }
    </style>
</head>
<body>

<!-- Overlay -->
<div class="admin-overlay" id="adminOverlay"></div>

<!-- Sidebar -->
<aside class="admin-sidebar" id="adminSidebar">
    <!-- Brand -->
    <div class="sidebar-brand">
        <a href="index.php" class="brand-logo">
            <div class="brand-icon"><i class="fas fa-crown"></i></div>
            <div>
                <span class="brand-text">کیف‌لند</span>
                <span class="brand-sub">پنل مدیریت</span>
            </div>
        </a>
    </div>

    <!-- Navigation -->
    <nav class="sidebar-nav">
        <?php foreach ($menuItems as $section): ?>
        <div class="nav-section">
            <div class="nav-section-title"><?= $section['section'] ?></div>
            <?php foreach ($section['items'] as $item): ?>
            <a href="<?= $item['url'] ?>" class="nav-item <?= $current_page === $item['page'] ? 'active' : '' ?>">
                <i class="<?= $item['icon'] ?>"></i>
                <span><?= $item['label'] ?></span>
                <?php if (!empty($item['badge'])): ?>
                <span class="nav-badge <?= $item['badge'] > 5 ? 'warning' : '' ?>"><?= $item['badge'] ?></span>
                <?php endif; ?>
            </a>
            <?php endforeach; ?>
        </div>
        <?php endforeach; ?>
    </nav>

    <!-- User Profile -->
    <div class="sidebar-user">
        <div class="user-avatar-admin">
            <?= mb_substr($currentUser['full_name'] ?? 'A', 0, 1) ?>
        </div>
        <div class="user-info-admin">
            <h4><?= htmlspecialchars($currentUser['full_name'] ?? $currentUser['username'] ?? 'مدیر') ?></h4>
            <p><?= ($currentUser['role'] === 'admin') ? 'مدیر ارشد' : 'مدیر سیستم' ?></p>
        </div>
        <a href="logout.php" class="logout-btn-admin" title="خروج">
            <i class="fas fa-sign-out-alt"></i>
        </a>
    </div>
</aside>

<!-- Main Content -->
<div class="admin-main">
    <!-- Top Bar -->
    <header class="admin-topbar">
        <div class="topbar-right">
            <button class="menu-toggle" id="menuToggle" aria-label="منو">
                <i class="fas fa-bars"></i>
            </button>
            <div class="topbar-search">
                <i class="fas fa-search"></i>
                <input type="text" placeholder="جستجو در پنل مدیریت...">
            </div>
        </div>
        <div class="topbar-left">
            <a href="messages.php" class="topbar-btn" title="پیام‌ها">
                <i class="fas fa-envelope"></i>
                <?php if ($unread_messages > 0): ?><span class="dot"></span><?php endif; ?>
            </a>
            <a href="orders.php" class="topbar-btn" title="سفارشات">
                <i class="fas fa-bell"></i>
                <?php if ($pending_orders > 0): ?><span class="dot"></span><?php endif; ?>
            </a>
            <a href="../index.php" class="topbar-btn" title="مشاهده سایت" target="_blank">
                <i class="fas fa-external-link-alt"></i>
            </a>
        </div>
    </header>

    <div class="dashboard-content">

<script>
document.addEventListener('DOMContentLoaded', function() {
    const sidebar = document.getElementById('adminSidebar');
    const menuBtn = document.getElementById('menuToggle');
    const overlay = document.getElementById('adminOverlay');

    function openMenu() {
        sidebar.classList.add('active');
        overlay.classList.add('active');
        document.body.style.overflow = 'hidden';
    }
    function closeMenu() {
        sidebar.classList.remove('active');
        overlay.classList.remove('active');
        document.body.style.overflow = '';
    }
    if (menuBtn) menuBtn.addEventListener('click', openMenu);
    if (overlay) overlay.addEventListener('click', closeMenu);
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape' && sidebar.classList.contains('active')) closeMenu();
    });
    window.addEventListener('resize', function() {
        if (window.innerWidth > 992 && sidebar.classList.contains('active')) closeMenu();
    });
});
</script>
