<?php
// ১. সেশন স্টার্ট করা 
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// ২. ডাটাবেস কানেকশন
if (!isset($pdo)) {
    if (file_exists('../includes/db.php')) {
        require_once '../includes/db.php';
    }
}

// সাইটের নাম তুলে আনা
$site_name = 'DIGISHOP';
if (isset($pdo)) {
    try {
        $stmt_settings = $pdo->query("SELECT site_name FROM settings LIMIT 1");
        if ($stmt_settings) {
            $fetched_name = $stmt_settings->fetchColumn();
            if (!empty($fetched_name)) {
                $site_name = $fetched_name;
            }
        }
    } catch (Exception $e) {}
}

// বর্তমানে কোন পেজে আছে সেটা চেক করার জন্য
$current_page = basename($_SERVER['PHP_SELF']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Admin Dashboard - <?php echo htmlspecialchars($site_name); ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #84cc16; /* Light Green */
            --primary-light: #f4fce3;
            --bg-main: #f8f9fa; 
            --bg-white: #ffffff;
            --text-dark: #111827;
            --text-gray: #4b5563;
            --text-muted: #9ca3af;
            --border-color: #e5e7eb;
            --sidebar-width: 260px;
        }

        body { 
            margin: 0; font-family: 'Inter', sans-serif; background-color: var(--bg-main); color: var(--text-dark); overflow-x: hidden;
        }
        
        /* ================= Sidebar Design ================= */
        .admin-sidebar { 
            width: var(--sidebar-width); background-color: var(--bg-white); border-right: 1px solid var(--border-color);
            display: flex; flex-direction: column; position: fixed; height: 100vh; left: 0; top: 0; z-index: 1001;
            transition: transform 0.3s ease-in-out;
        }

        .sidebar-brand { 
            padding: 24px 20px; font-size: 1.25rem; font-weight: 800; color: var(--text-dark);
            border-bottom: 1px solid transparent; text-transform: uppercase; letter-spacing: 0.5px;
            display: flex; align-items: center; justify-content: space-between;
        }
        
        .sidebar-brand-text { display: flex; align-items: center; gap: 10px; color: var(--primary); }
        .sidebar-brand-text span { color: var(--text-dark); } 
        
        .close-sidebar { display: none; background: none; border: none; color: var(--text-gray); font-size: 1.5rem; cursor: pointer; padding: 0; }

        .sidebar-menu { 
            padding: 10px 15px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; height: calc(100vh - 75px);
        }
        
        .sidebar-menu::-webkit-scrollbar { width: 4px; }
        .sidebar-menu::-webkit-scrollbar-thumb { background-color: #e5e7eb; border-radius: 10px; }

        /* Professional Simple Menu Style */
        .sidebar-menu a { 
            color: var(--text-gray); text-decoration: none; padding: 10px 12px; font-size: 0.9rem; font-weight: 500; 
            border-radius: 8px; transition: all 0.2s ease; display: flex; align-items: center; gap: 12px;
            position: relative;
        }
        .sidebar-menu a:hover { background-color: #f3f4f6; color: var(--text-dark); }
        .sidebar-menu a.active { background-color: var(--primary-light); color: var(--primary); font-weight: 600; }
        
        /* Solid Filled Icons */
        .sidebar-menu a svg { width: 18px; height: 18px; fill: currentColor; opacity: 0.8; transition: 0.2s;}
        .sidebar-menu a.active svg { opacity: 1; }

        .menu-label { font-size: 0.7rem; text-transform: uppercase; color: var(--text-muted); font-weight: 700; margin: 20px 0 8px 12px; letter-spacing: 0.5px; }
        .menu-badge { margin-left: auto; background: var(--primary); color: #fff; padding: 2px 8px; border-radius: 20px; font-size: 0.7rem; font-weight: 700; }

        /* ================= Main Content & Topbar ================= */
        .main-wrapper { 
            margin-left: var(--sidebar-width); min-height: 100vh; display: flex; flex-direction: column; transition: margin-left 0.3s ease-in-out;
        }
        .top-header {
            background: var(--bg-white); height: 65px; border-bottom: 1px solid var(--border-color);
            display: flex; align-items: center; padding: 0 25px; position: sticky; top: 0; z-index: 999;
        }
        
        .mobile-menu-btn { display: none; background: none; border: none; color: var(--text-dark); cursor: pointer; padding: 5px 5px 5px 0; margin-right: auto;}
        
        .main-content { padding: 30px; }
        .sidebar-overlay { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(17, 24, 39, 0.4); z-index: 1000; }

        /* ================= Global Components ================= */
        .page-title { font-size: 1.5rem; font-weight: 800; margin: 0 0 20px 0; color: var(--text-dark); }
        .admin-card { background: var(--bg-white); border-radius: 12px; padding: 20px; border: 1px solid var(--border-color); margin-bottom: 20px; width: 100%; box-sizing: border-box; }
        .form-control { width: 100%; padding: 12px 15px; border: 1px solid var(--border-color); border-radius: 8px; font-size: 0.95rem; font-family: inherit; background: #f9fafb; outline: none; transition: 0.3s; box-sizing: border-box; }
        .form-control:focus { border-color: var(--primary); background: var(--bg-white); box-shadow: 0 0 0 3px var(--primary-light); }
        .btn-admin { background: var(--primary); color: white; padding: 10px 20px; border: none; border-radius: 8px; font-weight: 600; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; transition: 0.3s; }
        .btn-admin:hover { background: #65a30d; }

        /* ================= Mobile Responsiveness ================= */
        @media (max-width: 992px) {
            .admin-sidebar { transform: translateX(-100%); }
            .admin-sidebar.show { transform: translateX(0); }
            .main-wrapper { margin-left: 0; width: 100%; }
            .mobile-menu-btn { display: block; }
            .top-header { padding: 0 20px; }
            .close-sidebar { display: block; }
            .main-content { padding: 20px 15px; }
        }
    </style>
</head>
<body>

<div class="sidebar-overlay" id="sidebarOverlay"></div>

<div class="admin-sidebar" id="adminSidebar">
    <div class="sidebar-brand">
        <div class="sidebar-brand-text">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2L2 12h3v8h6v-6h2v6h6v-8h3L12 2z"/></svg>
            <span><?php echo htmlspecialchars($site_name); ?></span>
        </div>
        <button class="close-sidebar" id="closeSidebarBtn">
            <svg width="24" height="24" fill="none" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/></svg>
        </button>
    </div>
    
    <div class="sidebar-menu">
        <a href="dashboard.php" class="<?php echo ($current_page == 'dashboard.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M3 13h8V3H3v10zm0 8h8v-6H3v6zm10 0h8V11h-8v10zm0-18v6h8V3h-8z"/></svg>
            Dashboard
        </a>
        <a href="statements.php" class="<?php echo ($current_page == 'statements.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z"/></svg>
            Statements
        </a>

        <div class="menu-label">Sales</div>
        
        <a href="manage-orders.php" class="<?php echo ($current_page == 'manage-orders.php' || $current_page == 'orders.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M17 1H7c-1.1 0-2 .9-2 2v18c0 1.1.9 2 2 2h10c1.1 0 2-.9 2-2V3c0-1.1-.9-2-2-2zm0 18H7V5h10v14zm-4-2h2v-2h-2v2zm0-4h2v-2h-2v2zm0-4h2V7h-2v2zm-4 8h2v-2H9v2zm0-4h2v-2H9v2zm0-4h2V7H9v2z"/></svg>
            Orders <span class="menu-badge">New</span>
        </a>
        <a href="users.php" class="<?php echo ($current_page == 'users.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5c-1.66 0-3 1.34-3 3s1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5C6.34 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z"/></svg>
            Customers
        </a>
        <a href="support-tickets.php" class="<?php echo ($current_page == 'support-tickets.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M20 2H4c-1.1 0-1.99.9-1.99 2L2 22l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm-7 12h-2v-2h2v2zm0-4h-2V6h2v4z"/></svg>
            Tickets
        </a>

        <div class="menu-label">Catalog</div>

        <a href="manage-categories.php" class="<?php echo ($current_page == 'manage-categories.php' || $current_page == 'categories.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M10 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z"/></svg>
            Categories
        </a>
        <a href="manage-products.php" class="<?php echo ($current_page == 'manage-products.php' || $current_page == 'add-product.php' || $current_page == 'edit-product.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M20 6h-4V4c0-1.11-.89-2-2-2h-4c-1.11 0-2 .89-2 2v2H4c-1.11 0-1.99.89-1.99 2L2 19c0 1.11.89 2 2 2h16c1.11 0 2-.89 2-2V8c0-1.11-.89-2-2-2zm-6 0h-4V4h4v2z"/></svg>
            Products
        </a>
        <a href="manage-reviews.php" class="<?php echo ($current_page == 'manage-reviews.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M21.99 4c0-1.1-.89-2-1.99-2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h14l4 4-.01-18zM18 14H6v-2h12v2zm0-3H6V9h12v2zm0-3H6V6h12v2z"/></svg>
            Reviews
        </a>
        <a href="manage-vouchers.php" class="<?php echo ($current_page == 'manage-vouchers.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M21.41 11.58l-9-9C12.05 2.22 11.55 2 11 2H4c-1.1 0-2 .9-2 2v7c0 .55.22 1.05.59 1.41l9 9c.36.36.86.58 1.41.58.55 0 1.05-.22 1.41-.59l7-7c.37-.36.59-.86.59-1.41 0-.55-.23-1.06-.59-1.41zM5.5 7C4.67 7 4 6.33 4 5.5S4.67 4 5.5 4 7 4.67 7 5.5 6.33 7 5.5 7z"/></svg>
            Vouchers
        </a>

        <div class="menu-label">System</div>

        <a href="payment-gateways.php" class="<?php echo ($current_page == 'payment-gateways.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M20 4H4c-1.11 0-1.99.89-1.99 2L2 18c0 1.11.89 2 2 2h16c1.11 0 2-.89 2-2V6c0-1.11-.89-2-2-2zm0 14H4v-6h16v6zm0-10H4V6h16v2z"/></svg>
            Gateways
        </a>
        <a href="transactions.php" class="<?php echo ($current_page == 'transactions.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1.41 16.09V20h-2.67v-1.93c-1.71-.36-3.16-1.46-3.27-3.4h1.96c.1 1.05.82 1.87 2.65 1.87 1.96 0 2.4-.98 2.4-1.59 0-.83-.44-1.61-2.67-2.14-2.48-.6-4.18-1.62-4.18-3.67 0-1.72 1.39-2.84 3.11-3.21V4h2.67v1.95c1.86.45 2.79 1.86 2.85 3.39H14.3c-.05-1.11-.64-1.87-2.22-1.87-1.5 0-2.4.68-2.4 1.64 0 .84.65 1.39 2.67 1.91s4.18 1.39 4.18 3.91c-.01 1.83-1.38 2.83-3.12 3.16z"/></svg>
            Transactions
        </a>
        <a href="manage-sliders.php" class="<?php echo ($current_page == 'manage-sliders.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M22 16V4c0-1.1-.9-2-2-2H8c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2zm-11-4l2.03 2.71L16 11l4 5H8l3-4zM2 6v14c0 1.1.9 2 2 2h14v-2H4V6H2z"/></svg>
            Sliders
        </a>
        <a href="manage-pages.php" class="<?php echo ($current_page == 'manage-pages.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm2 16H8v-2h8v2zm0-4H8v-2h8v2zm-3-5V3.5L18.5 9H13z"/></svg>
            Pages
        </a>
        <a href="settings.php" class="<?php echo ($current_page == 'settings.php') ? 'active' : ''; ?>">
            <svg viewBox="0 0 24 24"><path d="M19.14,12.94c0.04-0.3,0.06-0.61,0.06-0.94c0-0.32-0.02-0.64-0.06-0.94l2.03-1.58c0.18-0.14,0.23-0.41,0.12-0.61 l-1.92-3.32c-0.12-0.22-0.37-0.29-0.59-0.22l-2.39,0.96c-0.5-0.38-1.03-0.7-1.62-0.94L14.4,2.81c-0.04-0.24-0.24-0.41-0.48-0.41 h-3.84c-0.24,0-0.43,0.17-0.47,0.41L9.25,5.35C8.66,5.59,8.12,5.92,7.63,6.29L5.24,5.33c-0.22-0.08-0.47,0-0.59,0.22L2.73,8.87 C2.62,9.08,2.66,9.34,2.86,9.48l2.03,1.58C4.84,11.36,4.8,11.69,4.8,12s0.02,0.64,0.06,0.94l-2.03,1.58 c-0.18,0.14-0.23,0.41-0.12,0.61l1.92,3.32c0.12,0.22,0.37,0.29,0.59,0.22l2.39-0.96c0.5,0.38,1.03,0.7,1.62,0.94l0.36,2.54 c0.05,0.24,0.24,0.41,0.48,0.41h3.84c0.24,0,0.43-0.17,0.47-0.41l0.36-2.54c0.59-0.24,1.13-0.56,1.62-0.94l2.39,0.96 c0.22,0.08,0.47,0,0.59-0.22l1.92-3.32c0.12-0.22,0.07-0.49-0.12-0.61L19.14,12.94z M12,15.6c-1.98,0-3.6-1.62-3.6-3.6 s1.62-3.6,3.6-3.6s3.6,1.62,3.6,3.6S13.98,15.6,12,15.6z"/></svg>
            Settings
        </a>
        
        <a href="../index.php" target="_blank" style="margin-top: 15px; color: var(--primary); background: var(--primary-light); font-weight: 600;">
            <svg viewBox="0 0 24 24"><path d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z"/></svg>
            Live
        </a>

        <a href="logout.php" style="color: #ef4444; margin-bottom: 20px;">
            <svg viewBox="0 0 24 24"><path d="M10.09 15.59L11.5 17l5-5-5-5-1.41 1.41L12.67 11H3v2h9.67l-2.58 2.59zM19 3H5c-1.11 0-2 .9-2 2v4h2V5h14v14H5v-4H3v4c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"/></svg>
            Logout
        </a>
    </div>
</div>

<div class="main-wrapper">
    <header class="top-header">
        <button class="mobile-menu-btn" id="openSidebarBtn">
            <svg width="26" height="26" fill="none" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16"/></svg>
        </button>
    </header>
    
    <div class="main-content">

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const sidebar = document.getElementById('adminSidebar');
            const openBtn = document.getElementById('openSidebarBtn');
            const closeBtn = document.getElementById('closeSidebarBtn');
            const overlay = document.getElementById('sidebarOverlay');

            function toggleSidebar() {
                sidebar.classList.toggle('show');
                if(sidebar.classList.contains('show')) {
                    overlay.style.display = 'block';
                } else {
                    overlay.style.display = 'none';
                }
            }

            openBtn.addEventListener('click', toggleSidebar);
            closeBtn.addEventListener('click', toggleSidebar);
            overlay.addEventListener('click', toggleSidebar);
        });
    </script>
