<?php
// Admin dashboard - protected page with modal system
session_start();

// Permission Enforcement - General Settings (manage_settings covers logo, site title, colors, etc.)
require_once __DIR__ . '/helpers/subadmin_page_guard.php';
require_once __DIR__ . '/helpers/get_back_button.php';

// Check permissions - allows full admins and subadmins with view access
$pageGuard = new SubAdminPageGuard('manage_settings');
$pageGuard->enforceView();

// Load DatabaseHelper for cross-platform compatibility
require_once __DIR__ . '/helpers/database_helper.php';

$username = $_SESSION['username'] ?? 'Admin';
$action = '';
$flash = '';

// Data files and upload paths
$dataDir = __DIR__ . '/data';
if (!is_dir($dataDir)) @mkdir($dataDir, 0755, true);
$configFile = $dataDir . '/site_config.json';
$pagesFile = $dataDir . '/pages.json';
$categoriesFile = $dataDir . '/categories.json';
$marqueeFile = $dataDir . '/marquee.json';
$footerMarqueeFile = $dataDir . '/footer_marquee.json';
$uploadDir = __DIR__ . '/uploads';
if (!is_dir($uploadDir)) @mkdir($uploadDir, 0755, true);
$pagesDir = __DIR__ . '/pages';
if (!is_dir($pagesDir)) @mkdir($pagesDir, 0755, true);

// Load current config using DatabaseHelper
$siteConfig = ['title' => "Esa's Fashion", 'logo' => '', 'social_media' => [], 'header_bg_color' => '#ffffff', 'footer_bg_color' => '#fafafa'];
$c = DatabaseHelper::loadJson('site_config.json', $siteConfig);
if (is_array($c)) $siteConfig = array_merge($siteConfig, $c);

// Load marquee settings using DatabaseHelper
$marquee = ['enabled' => false, 'text' => '', 'speed' => '30s', 'background_color' => '#F07E2D', 'text_color' => '#ffffff', 'font_size' => '18px'];
$m = DatabaseHelper::loadJson('marquee.json', $marquee);
if (is_array($m)) $marquee = array_merge($marquee, $m);

// Load footer marquee settings using DatabaseHelper
$footerMarquee = ['enabled' => false, 'text' => '', 'speed' => '35s', 'background_color' => '#eb73cf', 'text_color' => '#ffffff', 'font_size' => '16px'];
$fm = DatabaseHelper::loadJson('footer_marquee.json', $footerMarquee);
if (is_array($fm)) $footerMarquee = array_merge($footerMarquee, $fm);

// Load categories using DatabaseHelper
$categories = DatabaseHelper::loadJson('categories.json', []);

// Load pages using DatabaseHelper
$pages = DatabaseHelper::loadJson('pages.json', []);

// Load slider settings using DatabaseHelper
$slider = ['hero' => ['title' => 'Women Fashion', 'subtitle' => 'Let get a look', 'button_text' => 'Shop', 'button_link' => 'shop.php', 'position' => 'left', 'bold_title' => true, 'multiline_enabled' => false], 'slides' => []];
$s = DatabaseHelper::loadJson('slider.json', $slider);
if (is_array($s)) $slider = array_merge($slider, $s);

// Handle POST actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';

    if ($action === 'update_title') {
        $newTitle = trim($_POST['site_title'] ?? '');
        if ($newTitle !== '') {
            $siteConfig['title'] = $newTitle;
            DatabaseHelper::saveJson('site_config.json', $siteConfig);
            $flash = 'Site title updated.';
        } else {
            $flash = 'Please provide a title.';
        }
    }

    if ($action === 'upload_logo' && !empty($_FILES['site_logo']) && $_FILES['site_logo']['error'] === UPLOAD_ERR_OK) {
        $tmp = $_FILES['site_logo']['tmp_name'];
        $name = basename($_FILES['site_logo']['name']);
        $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
        $allowed = ['png','jpg','jpeg','gif','svg'];
        if (!in_array($ext, $allowed)) {
            $flash = 'Logo must be an image (png,jpg,gif,svg).';
        } else {
            $target = $uploadDir . '/site_logo.' . $ext;
            if (move_uploaded_file($tmp, $target)) {
                $siteConfig['logo'] = 'uploads/site_logo.' . $ext;
                DatabaseHelper::saveJson('site_config.json', $siteConfig);
                $flash = 'Logo uploaded successfully.';
            } else {
                $flash = 'Failed to move uploaded file.';
            }
        }
    }

    // Slider image management
    if ($action === 'add_slider_image' && !empty($_FILES['slider_image']) && $_FILES['slider_image']['error'] === UPLOAD_ERR_OK) {
        $tmp = $_FILES['slider_image']['tmp_name'];
        $name = basename($_FILES['slider_image']['name']);
        $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
        $allowed = ['png','jpg','jpeg','gif','webp'];
        if (!in_array($ext, $allowed)) {
            $flash = 'Slider image must be an image (png, jpg, jpeg, gif, webp).';
        } else {
            $unique_name = 'slide_' . dechex(time()) . uniqid() . '.' . $ext;
            $target = $uploadDir . '/' . $unique_name;
            if (move_uploaded_file($tmp, $target)) {
                if (!is_array($slider['slides'])) $slider['slides'] = [];
                $slider['slides'][] = ['image' => 'uploads/' . $unique_name, 'alt' => trim($_POST['slider_alt'] ?? 'Slide')];
                DatabaseHelper::saveJson('slider.json', $slider);
                $flash = 'Slider image added successfully!';
            } else {
                $flash = 'Failed to move uploaded file.';
            }
        }
    }

    if ($action === 'delete_slider_image') {
        $image_path = $_POST['image_path'] ?? '';
        if ($image_path !== '') {
            if (is_array($slider['slides'])) {
                $slider['slides'] = array_values(array_filter($slider['slides'], function($slide) use ($image_path) {
                    return ($slide['image'] ?? '') !== $image_path;
                }));
                DatabaseHelper::saveJson('slider.json', $slider);
                // Delete the file from uploads folder
                $file_to_delete = __DIR__ . '/' . str_replace('../', '', $image_path);
                if (file_exists($file_to_delete)) @unlink($file_to_delete);
                $flash = 'Slider image deleted successfully!';
            }
        }
    }

    // Category management
    if ($action === 'add_category') {
        $name = trim($_POST['category_name'] ?? '');
        $slug = trim($_POST['category_slug'] ?? '');
        $desc = trim($_POST['category_desc'] ?? '');
        $parent_id = isset($_POST['parent_category']) && $_POST['parent_category'] !== '' ? (int)$_POST['parent_category'] : null;
        
        if ($name === '' || $slug === '') {
            $flash = 'Category name and slug are required.';
        } else {
            $slug = preg_replace('/[^a-z0-9\-]+/i', '-', strtolower($slug));
            $exists = false;
            foreach ($categories as $cat) { if (($cat['slug'] ?? '') === $slug) { $exists = true; break; } }
            if ($exists) {
                $flash = 'A category with that slug already exists.';
            } else {
                $id = 1;
                foreach ($categories as $cat) { if ($cat['id'] >= $id) $id = $cat['id'] + 1; }
                $categories[] = ['id' => $id, 'name' => $name, 'slug' => $slug, 'description' => $desc, 'parent_id' => $parent_id, 'children' => []];
                DatabaseHelper::saveJson('categories.json', $categories);
                $flash = 'Category added successfully.';
            }
        }
    }

    if ($action === 'edit_category') {
        $id = (int)($_POST['category_id'] ?? 0);
        $name = trim($_POST['category_name'] ?? '');
        $slug = trim($_POST['category_slug'] ?? '');
        $desc = trim($_POST['category_desc'] ?? '');
        $parent_id = isset($_POST['parent_category']) && $_POST['parent_category'] !== '' ? (int)$_POST['parent_category'] : null;
        
        if ($name === '' || $slug === '' || $id === 0) {
            $flash = 'All fields are required.';
        } else {
            $slug = preg_replace('/[^a-z0-9\-]+/i', '-', strtolower($slug));
            $found = false;
            foreach ($categories as $k => $cat) {
                if ($cat['id'] === $id) {
                    $categories[$k] = ['id' => $id, 'name' => $name, 'slug' => $slug, 'description' => $desc, 'parent_id' => $parent_id, 'children' => $cat['children'] ?? []];
                    $found = true;
                    break;
                }
            }
            if ($found) {
                DatabaseHelper::saveJson('categories.json', $categories);
                $flash = 'Category updated successfully.';
            } else {
                $flash = 'Category not found.';
            }
        }
    }

    if ($action === 'delete_category') {
        $id = (int)($_POST['category_id'] ?? 0);
        if ($id !== 0) {
            $categories = array_values(array_filter($categories, function($cat) use ($id){ return $cat['id'] !== $id; }));
            DatabaseHelper::saveJson('categories.json', $categories);
            $flash = 'Category deleted successfully.';
        }
    }

    // Page management
    if ($action === 'add_page') {
        $title = trim($_POST['page_title'] ?? '');
        $slug = trim($_POST['page_slug'] ?? '');
        $html_content = $_POST['page_html'] ?? '';
        if ($title === '' || $slug === '') {
            $flash = 'Page title and slug are required.';
        } else {
            $slug = preg_replace('/[^a-z0-9\-]+/i', '-', strtolower($slug));
            $exists = false;
            foreach ($pages as $pg) { if (isset($pg['slug']) && $pg['slug'] === $slug) { $exists = true; break; } }
            if ($exists) {
                $flash = 'A page with that slug already exists.';
            } else {
                $pages[] = ['title' => $title, 'slug' => $slug, 'created' => date('c'), 'html' => $html_content];
                DatabaseHelper::saveJson('pages.json', $pages);
                $pageFile = $pagesDir . '/' . $slug . '.php';
                $pageContent = "<?php\nrequire_once __DIR__ . '/../page-loader.php';\n";
                @file_put_contents($pageFile, $pageContent);
                $flash = 'Page added successfully.';
            }
        }
    }

    if ($action === 'edit_page') {
        $slug = trim($_POST['page_slug'] ?? '');
        $html_content = $_POST['page_html'] ?? '';
        if ($slug !== '') {
            foreach ($pages as $k => $pg) {
                if (($pg['slug'] ?? '') === $slug) {
                    $pages[$k]['html'] = $html_content;
                    DatabaseHelper::saveJson('pages.json', $pages);
                    $pageFile = $pagesDir . '/' . $slug . '.php';
                    $pageContent = "<?php\nrequire_once __DIR__ . '/../page-loader.php';\n";
                    @file_put_contents($pageFile, $pageContent);
                    $flash = 'Page updated successfully.';
                    break;
                }
            }
        }
    }

    if ($action === 'delete_page') {
        $slug = trim($_POST['page_slug'] ?? '');
        if ($slug !== '') {
            $pages = array_values(array_filter($pages, function($pg) use ($slug){ return ($pg['slug'] ?? '') !== $slug; }));
            DatabaseHelper::saveJson('pages.json', $pages);
            $gen = $pagesDir . '/' . $slug . '.php';
            if (file_exists($gen)) @unlink($gen);
            $flash = 'Page deleted.';
        }
    }

    // Social media management
    if ($action === 'add_social_media') {
        $platform = trim($_POST['social_platform'] ?? '');
        $name = trim($_POST['social_name'] ?? '');
        $link = trim($_POST['social_link'] ?? '');
        
        if ($platform === '' || $name === '' || $link === '') {
            $flash = 'All social media fields are required.';
        } else {
            if (!isset($siteConfig['social_media'])) $siteConfig['social_media'] = [];
            $id = 1;
            foreach ($siteConfig['social_media'] as $sm) { if ($sm['id'] >= $id) $id = $sm['id'] + 1; }
            $siteConfig['social_media'][] = ['id' => $id, 'platform' => $platform, 'name' => $name, 'link' => $link];
            DatabaseHelper::saveJson('site_config.json', $siteConfig);
            $flash = 'Social media account added successfully.';
        }
    }

    if ($action === 'edit_social_media') {
        $id = (int)($_POST['social_id'] ?? 0);
        $platform = trim($_POST['social_platform'] ?? '');
        $name = trim($_POST['social_name'] ?? '');
        $link = trim($_POST['social_link'] ?? '');
        
        if ($platform === '' || $name === '' || $link === '' || $id === 0) {
            $flash = 'All social media fields are required.';
        } else {
            $found = false;
            if (!isset($siteConfig['social_media'])) $siteConfig['social_media'] = [];
            foreach ($siteConfig['social_media'] as $k => $sm) {
                if ($sm['id'] === $id) {
                    $siteConfig['social_media'][$k] = ['id' => $id, 'platform' => $platform, 'name' => $name, 'link' => $link];
                    $found = true;
                    break;
                }
            }
            if ($found) {
                DatabaseHelper::saveJson('site_config.json', $siteConfig);
                $flash = 'Social media account updated successfully.';
            } else {
                $flash = 'Social media account not found.';
            }
        }
    }
    if ($action === 'delete_social_media') {
        $id = (int)($_POST['social_id'] ?? 0);
        if ($id !== 0) {
            if (!isset($siteConfig['social_media'])) $siteConfig['social_media'] = [];
            $siteConfig['social_media'] = array_values(array_filter($siteConfig['social_media'], function($sm) use ($id){ return $sm['id'] !== $id; }));
            DatabaseHelper::saveJson('site_config.json', $siteConfig);
            $flash = 'Social media account deleted successfully.';
        }
    }

    // Marquee settings
    if ($action === 'update_marquee') {
        $marquee['enabled'] = isset($_POST['marquee_enabled']) ? true : false;
        $marquee['text'] = trim($_POST['marquee_text'] ?? '');
        $marquee['speed'] = trim($_POST['marquee_speed'] ?? '30s');
        $marquee['background_color'] = trim($_POST['marquee_bg_color'] ?? '#F07E2D');
        $marquee['text_color'] = trim($_POST['marquee_text_color'] ?? '#ffffff');
        $marquee['font_size'] = trim($_POST['marquee_font_size'] ?? '18px');
        
        DatabaseHelper::saveJson('marquee.json', $marquee);
        $flash = 'Marquee updated successfully.';
    }

    // Footer Marquee settings
    if ($action === 'update_footer_marquee') {
        $footerMarquee['enabled'] = isset($_POST['footer_marquee_enabled']) ? true : false;
        $footerMarquee['text'] = trim($_POST['footer_marquee_text'] ?? '');
        $footerMarquee['speed'] = trim($_POST['footer_marquee_speed'] ?? '35s');
        $footerMarquee['background_color'] = trim($_POST['footer_marquee_bg_color'] ?? '#667eea');
        $footerMarquee['text_color'] = trim($_POST['footer_marquee_text_color'] ?? '#ffffff');
        $footerMarquee['font_size'] = trim($_POST['footer_marquee_font_size'] ?? '16px');
        
        DatabaseHelper::saveJson('footer_marquee.json', $footerMarquee);
        $flash = 'Footer Marquee updated successfully.';
    }

    // Color settings
    if ($action === 'update_colors') {
        $headerColor = trim($_POST['header_bg_color'] ?? '#ffffff');
        $footerColor = trim($_POST['footer_bg_color'] ?? '#fafafa');
        
        // Validate hex colors
        if (preg_match('/^#[0-9A-F]{6}$/i', $headerColor) && preg_match('/^#[0-9A-F]{6}$/i', $footerColor)) {
            $siteConfig['header_bg_color'] = $headerColor;
            $siteConfig['footer_bg_color'] = $footerColor;
            DatabaseHelper::saveJson('site_config.json', $siteConfig);
            $flash = 'Colors updated successfully.';
        } else {
            $flash = 'Please provide valid hex color codes.';
        }
    }

    // Reload after changes
    if (file_exists($configFile)) {
        $c = @json_decode(file_get_contents($configFile), true);
        if (is_array($c)) $siteConfig = array_merge($siteConfig, $c);
    }
    if (file_exists($categoriesFile)) {
        $cats = @json_decode(file_get_contents($categoriesFile), true);
        if (is_array($cats)) $categories = $cats; else $categories = [];
    }
    if (file_exists($pagesFile)) {
        $p = @json_decode(file_get_contents($pagesFile), true);
        if (is_array($p)) $pages = $p; else $pages = [];
    }
    if (file_exists($marqueeFile)) {
        $m = @json_decode(file_get_contents($marqueeFile), true);
        if (is_array($m)) $marquee = array_merge($marquee, $m);
    }
    if (file_exists($footerMarqueeFile)) {
        $fm = @json_decode(file_get_contents($footerMarqueeFile), true);
        if (is_array($fm)) $footerMarquee = array_merge($footerMarquee, $fm);
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard</title>
    <link rel="icon" href="sport-pics/logo-1.png?v=<?php echo time(); ?>" type="image/png">
    <link rel="stylesheet" href="styles.css">
    <style>
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 2000;
            justify-content: center;
            align-items: center;
            overflow-y: auto;
        }
        @keyframes scroll-left-marquee {
            0% { transform: translateX(100%); }
            100% { transform: translateX(-100%); }
        }
        .modal.show { display: flex; }
        .modal-content {
            background: #fff;
            padding: 24px;
            border-radius: 8px;
            max-width: 600px;
            width: 90%;
            max-height: 90vh;
            overflow-y: auto;
            position: relative;
        }
        .modal-close {
            position: absolute;
            top: 12px;
            right: 12px;
            font-size: 28px;
            cursor: pointer;
            color: #999;
        }
        .modal-close:hover { color: #333; }
        .modal h2 { margin-top: 0; }
        .form-group {
            display: flex;
            flex-direction: column;
            gap: 8px;
            margin-bottom: 12px;
        }
        .form-group label { font-weight: 600; font-size: 14px; }
        .form-group input, .form-group select, .form-group textarea {
            padding: 10px;
            border: 1px solid #e6e6e6;
            border-radius: 6px;
            font-size: 14px;
            font-family: inherit;
        }
        .form-group textarea { resize: vertical; min-height: 100px; }
        #adminSidebar {
            background: #fff;
            border-right: 1px solid #f0f0f0;
        }
        #adminSidebar a {
            color: #F07E2D;
            text-decoration: none;
            display: block;
            padding: 8px 10px;
            border-radius: 4px;
        }
        #adminSidebar a:hover {
            background: rgba(225, 123, 169, 0.1);
            color: #333;
        }
        .admin-sidebar-toggle {
            position: fixed;
            left: 10px;
            top: 10px;
            z-index: 999;
            background: #F07E2D;
            color: #fff;
            border: none;
            padding: 8px 12px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 18px;
            display: none;
        }
        #adminSidebar.collapsed {
            width: 0;
            overflow: hidden;
            transition: width 0.3s ease;
        }
        #adminSidebar:not(.collapsed) {
            transition: width 0.3s ease;
        }
        #adminMain.with-collapsed-sidebar {
            margin-left: 0;
        }
        @media (min-width: 900px) {
            body.with-admin-sidebar #adminSidebar { position: fixed; left: 0; top: 0; bottom: 0; width: 260px; }
            body.with-admin-sidebar #adminMain { margin-left: 260px; padding-left: 24px; }
        }
        @media (max-width: 900px) {
            #adminSidebar { width: 0; overflow: hidden; }
            #adminMain { margin-left: 0; }
            .admin-sidebar-toggle { display: block; }
        }
    </style>
</head>
<body class="with-admin-sidebar">

<button class="admin-sidebar-toggle" onclick="toggleAdminSidebar()">☰</button>

<aside id="adminSidebar" style="width:260px; background:#fff; padding:12px; box-shadow:0 2px 8px rgba(0,0,0,0.04); position:fixed; top:0; left:0; bottom:0; overflow:auto; z-index:1000;">
    <div style="display:flex; align-items:center; gap:10px; padding:8px; margin-bottom:12px;">
        <div style="width:44px;height:44px; border-radius:6px; overflow:hidden; background:#f4f4f4; display:flex;align-items:center;justify-content:center;">
            <?php if (!empty($siteConfig['logo'])): 
                $logoPath = $siteConfig['logo'];
            ?>
                <img src="<?php echo htmlspecialchars($logoPath); ?>?v=<?php echo time(); ?>" alt="logo" style="max-width:100%; max-height:100%;">
            <?php else: ?>
                <span style="font-weight:700;color:#666;">E</span>
            <?php endif; ?>
        </div>
        <div>
            <div style="font-weight:700; font-size:15px;"><?php echo htmlspecialchars($siteConfig['title']); ?></div>
            <div style="font-size:12px; color:#666;">Admin</div>
        </div>
    </div>

    <nav style="margin-top:12px;">
        <ul style="list-style:none; padding:0; margin:0;">
            <li><a href="#" onclick="openModal('siteTitleModal'); return false;">Site Title</a></li>
            <li><a href="#" onclick="openModal('logoModal'); return false;">Logo</a></li>
            <li><a href="#" onclick="openModal('sliderImagesModal'); return false;">Slider Images</a></li>
            <li><a href="#" onclick="openModal('colorsModal'); return false;"> header/footer Colors</a></li>
            <li><a href="#" onclick="openModal('categoriesModal'); return false;">Categories</a></li>
            <!-- <li><a href="#" onclick="openModal('pagesModal'); return false;">Pages</a></li> -->
            <li><a href="admin_manage_pages.php">manage page</a></li>
            <li><a href="#" onclick="openModal('socialMediaModal'); return false;"> Add Social Media</a></li>
            <li><a href="admin_blogs.php">Manage  Blog </a></li>
            <li><a href="admin_add_blog.php">Add Blog Post</a></li>
            <!-- <li><a href="run_blog_migration.php" style="color:#ff9800;">⚙ Blog Setup</a></li> -->
            <li><a href="admin_products.php">Products</a></li>
            <li><a href="admin_manage_users.php">Users</a></li>
            <li><a href="admin_subadmins.php">Sub-admins</a></li>
            <li><a href="admin_orders.php">Orders</a></li>
            <li><a href="admin_add_news.php">Banners</a></li>
            <li><a href="#" onclick="openModal('marqueeModal'); return false;">Marquee Text</a></li>
            <li><a href="#" onclick="openModal('footerMarqueeModal'); return false;">Footer Marquee</a></li>
            <li><a href="admin_manage_homepage.php">Featured categories</a></li>
            <li><a href="admin_manage_carousel.php">Carousel Categories</a></li>
            <li><a href="admin_manage_slider.php">Slider Settings</a></li>
            <li><a href="admin_manage_awards.php">Awards Management</a></li>
            <li><a href="admin_manage_latest_products.php">Latest Products</a></li>
            <li><a href="admin_manage_video.php">Video Section</a></li>
            <li><a href="admin_manage_footer.php">Footer Settings</a></li>
            <li ><a href="admin_seo_manager.php"> SEO Manager</a></li>

        </ul>
    </nav>

    <div style="margin-top:16px; font-size:13px; border-top:1px solid #e6e6e6; padding-top:12px;">
        <a href="index.php" style="text-decoration:none; color:#F07E2D;">View store</a> · 
        <a href="logout_admin.php" style="text-decoration:none; color:#F07E2D;">Logout</a>
    </div>
</aside>

<main class="container" style="margin-top:24px; margin-bottom:24px;">
    <section id="adminMain" style="margin-left:260px; padding-left:24px;">
        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:12px;">
            <div>
                <h1 style="font-size:22px; margin:0 0 4px 0;">Admin Dashboard</h1>
                <div style="color:#666; font-size:13px;">Click any option in the sidebar to manage it</div>
            </div>
            <div>
                <a href="logout_admin.php" class="btn" style="text-decoration:none; background:#a5a3a3; color:#fff; padding:8px 12px; border-radius:6px;">Logout</a>
            </div>
        </div>

        <?php if (!empty($flash)): ?>
            <div style="background:#eef8ee; color:#085; padding:10px 12px; border-radius:6px; margin-bottom:12px; border-left:4px solid #085;"><?php echo htmlspecialchars($flash); ?></div>
        <?php endif; ?>

        <div style="padding:20px; background:#fff; border-radius:8px; text-align:center; color:#666;">
            <p style="margin:0;">Welcome to the Admin Dashboard</p>
            <p style="margin:8px 0 0 0; font-size:14px;">All options are available in the sidebar as popups</p>
        </div>
    </section>
</main>

<!-- Modals -->

<!-- Site Title Modal -->
<div id="siteTitleModal" class="modal">
    <div class="modal-content">
        <span class="modal-close" onclick="closeModal('siteTitleModal')">&times;</span>
        <h2>Site Title</h2>
        <form method="post" class="form-group">
            <input type="hidden" name="action" value="update_title">
            <label>Title:</label>
            <input type="text" name="site_title" value="<?php echo htmlspecialchars($siteConfig['title']); ?>" required>
            <button class="btn" type="submit" style="margin-top:12px;">Save Title</button>
        </form>
    </div>
</div>

<!-- Logo Modal -->
<div id="logoModal" class="modal">
    <div class="modal-content">
        <span class="modal-close" onclick="closeModal('logoModal')">&times;</span>
        <h2>Upload Logo</h2>
        <div style="margin-bottom:16px; padding:12px; border:1px solid #eee; border-radius:6px; text-align:center; background:#f9f9f9;">
            <?php if (!empty($siteConfig['logo'])): 
                $logoPath = $siteConfig['logo'];
            ?>
                <img src="<?php echo htmlspecialchars($logoPath); ?>?v=<?php echo filemtime(__DIR__ . '/' . $logoPath); ?>" alt="logo" style="max-width:100%; max-height:120px;">
            <?php else: ?>
                <span style="color:#999;">No logo</span>
            <?php endif; ?>
        </div>
        <form method="post" enctype="multipart/form-data" class="form-group">
            <input type="hidden" name="action" value="upload_logo">
            <label>Choose Logo File:</label>
            <input type="file" name="site_logo" accept="image/*" required>
            <button class="btn" type="submit" style="margin-top:12px;">Upload Logo</button>
        </form>
    </div>
</div>

<!-- Slider Images Modal -->
<div id="sliderImagesModal" class="modal">
    <div class="modal-content">
        <span class="modal-close" onclick="closeModal('sliderImagesModal')">&times;</span>
        <h2>Manage Slider Images</h2>
        
        <!-- Add New Slider Image -->
        <div style="background:#f9f9f9; padding:15px; border-radius:6px; margin-bottom:20px;">
            <h3 style="margin-top:0; margin-bottom:15px; font-size:14px; color:#333;">Add New Slider Image</h3>
            <form method="post" enctype="multipart/form-data" style="display:flex; flex-direction:column; gap:10px;">
                <input type="hidden" name="action" value="add_slider_image">
                <div>
                    <label style="display:block; margin-bottom:6px; font-weight:600; font-size:13px;">Choose Image:</label>
                    <input type="file" name="slider_image" accept="image/*" required style="padding:8px; border:1px solid #ddd; border-radius:4px; width:100%; font-size:13px;">
                </div>
                <div>
                    <label style="display:block; margin-bottom:6px; font-weight:600; font-size:13px;">Alt Text (optional):</label>
                    <input type="text" name="slider_alt" placeholder="e.g., Summer Collection" style="padding:8px; border:1px solid #ddd; border-radius:4px; width:100%; font-size:13px;">
                </div>
                <button type="submit" class="btn" style="margin-top:8px;">Upload Image</button>
            </form>
        </div>

        <!-- Existing Slider Images -->
        <div>
            <h3 style="margin-top:0; margin-bottom:12px; font-size:14px; color:#333;">Current Slider Images</h3>
            <?php if (!empty($slider['slides']) && is_array($slider['slides'])): ?>
                <div style="display:grid; grid-template-columns:repeat(auto-fill, minmax(180px, 1fr)); gap:12px;">
                    <?php foreach ($slider['slides'] as $idx => $slide): ?>
                        <div style="border:1px solid #ddd; border-radius:6px; overflow:hidden; background:#fff; display:flex; flex-direction:column;">
                            <div style="width:100%; height:140px; overflow:hidden; background:#f5f5f5; display:flex; align-items:center; justify-content:center;">
                                <img src="<?php echo htmlspecialchars($slide['image']); ?>" alt="<?php echo htmlspecialchars($slide['alt'] ?? 'Slide'); ?>" style="max-width:100%; max-height:100%; object-fit:cover;">
                            </div>
                            <div style="padding:10px; flex:1; display:flex; flex-direction:column; justify-content:space-between;">
                                <small style="color:#666; word-break:break-word;"><?php echo htmlspecialchars($slide['alt'] ?? 'Slide'); ?></small>
                                <form method="post" style="margin:0; margin-top:8px;">
                                    <input type="hidden" name="action" value="delete_slider_image">
                                    <input type="hidden" name="image_path" value="<?php echo htmlspecialchars($slide['image']); ?>">
                                    <button type="submit" class="btn" style="width:100%; padding:6px; font-size:12px; background:#e74c3c; color:white; border:none; border-radius:4px; cursor:pointer;">Delete</button>
                                </form>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div style="padding:15px; background:#f9f9f9; border-radius:6px; text-align:center; color:#999;">
                    <p>No slider images yet. Add one to get started!</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Categories Modal -->
<div id="categoriesModal" class="modal">
    <div class="modal-content">
        <span class="modal-close" onclick="closeModal('categoriesModal')">&times;</span>
        <h2>Manage Categories</h2>

        <h3>Existing Categories</h3>
        <ul style="list-style:none; padding:0;">
            <?php foreach ($categories as $cat): ?>
                <li style="margin-bottom:8px; padding:10px; background:#f9f9f9; border-radius:4px; display:flex; gap:8px; align-items:center;">
                    <div style="flex:1;">
                        <strong><?php echo htmlspecialchars($cat['name']); ?></strong>
                        <?php if (!empty($cat['parent_id'])): ?>
                            <span style="font-size:12px; color:#999;"> (Subcategory)</span>
                        <?php endif; ?>
                    </div>
                    <button type="button" onclick="editCat('<?php echo $cat['id']; ?>', '<?php echo htmlspecialchars($cat['name']); ?>', '<?php echo htmlspecialchars($cat['slug'] ?? ''); ?>', '<?php echo htmlspecialchars($cat['description'] ?? ''); ?>', '<?php echo $cat['parent_id'] ?? ''; ?>')" class="btn" style="padding:4px 8px; font-size:12px;">Edit</button>
                    <form method="post" style="display:inline;">
                        <input type="hidden" name="action" value="delete_category">
                        <input type="hidden" name="category_id" value="<?php echo $cat['id']; ?>">
                        <button type="submit" class="btn" style="padding:4px 8px; font-size:12px; background:#f2dede; color:#a00; border:1px solid #a00;">Delete</button>
                    </form>
                </li>
            <?php endforeach; ?>
        </ul>

        <h3 style="margin-top:16px;">Add New Category</h3>
        <form method="post" class="form-group">
            <input type="hidden" name="action" value="add_category">
            <label>Category Name:</label>
            <input type="text" name="category_name" required>
            <label>Slug:</label>
            <input type="text" name="category_slug" required>
            <label>Description:</label>
            <textarea name="category_desc"></textarea>
            <label>Parent Category (optional):</label>
            <select name="parent_category">
                <option value="">-- No Parent (Top Level) --</option>
                <?php foreach ($categories as $cat):
                    if (empty($cat['parent_id'])): ?>
                    <option value="<?php echo $cat['id']; ?>"><?php echo htmlspecialchars($cat['name']); ?></option>
                    <?php endif;
                endforeach; ?>
            </select>
            <button class="btn" type="submit" style="margin-top:12px;">Add Category</button>
        </form>

        <h3 id="editCatTitle" style="margin-top:16px; display:none;">Edit Category</h3>
        <form method="post" id="editCatForm" class="form-group" style="display:none;">
            <input type="hidden" name="action" value="edit_category">
            <input type="hidden" name="category_id" id="editCatId">
            <label>Category Name:</label>
            <input type="text" name="category_name" id="editCatName" required>
            <label>Slug:</label>
            <input type="text" name="category_slug" id="editCatSlug" required>
            <label>Description:</label>
            <textarea name="category_desc" id="editCatDesc"></textarea>
            <label>Parent Category (optional):</label>
            <select name="parent_category" id="editCatParent">
                <option value="">-- No Parent (Top Level) --</option>
                <?php foreach ($categories as $cat):
                    if (empty($cat['parent_id'])): ?>
                    <option value="<?php echo $cat['id']; ?>"><?php echo htmlspecialchars($cat['name']); ?></option>
                    <?php endif;
                endforeach; ?>
            </select>
            <div style="display:flex; gap:8px; margin-top:12px;">
                <button class="btn" type="submit">Save Changes</button>
                <button type="button" onclick="cancelEditCat()" class="btn" style="background:#999;">Cancel</button>
            </div>
        </form>
    </div>
</div>

<!-- Pages Modal -->
<div id="pagesModal" class="modal">
    <div class="modal-content">
        <span class="modal-close" onclick="closeModal('pagesModal')">&times;</span>
        <h2>Manage Pages</h2>

        <h3>Existing Pages</h3>
        <ul style="list-style:none; padding:0;">
            <?php foreach ($pages as $pg): ?>
                <li style="margin-bottom:8px; padding:10px; background:#f9f9f9; border-radius:4px;">
                    <div><strong><?php echo htmlspecialchars($pg['title']); ?></strong></div>
                    <div style="font-size:12px; color:#999; margin:4px 0;"><?php echo htmlspecialchars($pg['slug']); ?></div>
                    <div style="display:flex; gap:6px; margin-top:8px;">
                        <button type="button" onclick="editPageForm('<?php echo htmlspecialchars($pg['slug']); ?>', '<?php echo htmlspecialchars($pg['title']); ?>', `<?php echo htmlspecialchars($pg['html'] ?? ''); ?>`)" class="btn" style="padding:4px 8px; font-size:12px;">Edit HTML</button>
                        <form method="post" style="display:inline;">
                            <input type="hidden" name="action" value="delete_page">
                            <input type="hidden" name="page_slug" value="<?php echo htmlspecialchars($pg['slug']); ?>">
                            <button type="submit" class="btn" style="padding:4px 8px; font-size:12px; background:#f2dede; color:#a00; border:1px solid #a00;">Delete</button>
                        </form>
                    </div>
                </li>
            <?php endforeach; ?>
        </ul>

        <h3 style="margin-top:16px;">Add New Page</h3>
        <form method="post" class="form-group">
            <input type="hidden" name="action" value="add_page">
            <label>Page Title:</label>
            <input type="text" name="page_title" required>
            <label>Page Slug:</label>
            <input type="text" name="page_slug" placeholder="page-url-slug" required>
            <label>HTML Content:</label>
            <textarea name="page_html" style="font-family:monospace;"></textarea>
            <button class="btn" type="submit" style="margin-top:12px;">Add Page</button>
        </form>

        <h3 id="editPageTitle" style="margin-top:16px; display:none;">Edit Page HTML</h3>
        <form method="post" id="editPageForm" class="form-group" style="display:none;">
            <input type="hidden" name="action" value="edit_page">
            <input type="hidden" name="page_slug" id="editPageSlug">
            <label>HTML Content:</label>
            <textarea name="page_html" id="editPageHtml" style="font-family:monospace;"></textarea>
            <div style="display:flex; gap:8px; margin-top:12px;">
                <button class="btn" type="submit">Save Changes</button>
                <button type="button" onclick="cancelEditPage()" class="btn" style="background:#999;">Cancel</button>
            </div>
        </form>
    </div>
</div>

<!-- Social Media Modal -->
<div id="socialMediaModal" class="modal">
    <div class="modal-content">
        <span class="modal-close" onclick="closeModal('socialMediaModal')">&times;</span>
        <h2>Manage Social Media</h2>

        <h3>Existing Accounts</h3>
        <ul style="list-style:none; padding:0;">
            <?php 
            $socialMedia = $siteConfig['social_media'] ?? [];
            foreach ($socialMedia as $sm): 
            ?>
                <li style="margin-bottom:8px; padding:10px; background:#f9f9f9; border-radius:4px;">
                    <div><strong><?php echo htmlspecialchars($sm['name']); ?></strong> <span style="color:#666;">(@<?php echo htmlspecialchars($sm['platform']); ?>)</span></div>
                    <div style="font-size:12px; color:#999; margin:4px 0; word-break:break-all;"><?php echo htmlspecialchars($sm['link']); ?></div>
                    <div style="display:flex; gap:6px; margin-top:8px;">
                        <button type="button" onclick="editSocialForm(<?php echo (int)$sm['id']; ?>, '<?php echo htmlspecialchars($sm['platform']); ?>', '<?php echo htmlspecialchars($sm['name']); ?>', '<?php echo htmlspecialchars($sm['link']); ?>')" class="btn" style="padding:4px 8px; font-size:12px;">Edit</button>
                        <form method="post" style="display:inline;">
                            <input type="hidden" name="action" value="delete_social_media">
                            <input type="hidden" name="social_id" value="<?php echo (int)$sm['id']; ?>">
                            <button type="submit" class="btn" style="padding:4px 8px; font-size:12px; background:#f2dede; color:#a00; border:1px solid #a00;">Delete</button>
                        </form>
                    </div>
                </li>
            <?php endforeach; ?>
        </ul>

        <h3 style="margin-top:16px;">Add New Account</h3>
        <form method="post" class="form-group">
            <input type="hidden" name="action" value="add_social_media">
            <label>Platform:</label>
            <input type="text" name="social_platform" placeholder="e.g., Instagram, Facebook, TikTok" required>
            <label>Account Name:</label>
            <input type="text" name="social_name" placeholder="e.g., @myaccount or My Store" required>
            <label>Link/URL:</label>
            <input type="url" name="social_link" placeholder="https://instagram.com/yourhandle" required>
            <button class="btn" type="submit" style="margin-top:12px;">Add Account</button>
        </form>

        <h3 id="editSocialTitle" style="margin-top:16px; display:none;">Edit Account</h3>
        <form method="post" id="editSocialForm" class="form-group" style="display:none;">
            <input type="hidden" name="action" value="edit_social_media">
            <input type="hidden" name="social_id" id="editSocialId">
            <label>Platform:</label>
            <input type="text" name="social_platform" id="editSocialPlatform" required>
            <label>Account Name:</label>
            <input type="text" name="social_name" id="editSocialName" required>
            <label>Link/URL:</label>
            <input type="url" name="social_link" id="editSocialLink" required>
            <div style="display:flex; gap:8px; margin-top:12px;">
                <button class="btn" type="submit">Save Changes</button>
                <button type="button" onclick="cancelEditSocial()" class="btn" style="background:#999;">Cancel</button>
            </div>
        </form>
    </div>
</div>

<!-- Colors Modal -->
<div id="colorsModal" class="modal">
    <div class="modal-content">
        <span class="modal-close" onclick="closeModal('colorsModal')">&times;</span>
        <h2>Customize Colors</h2>
        <p style="color:#666; font-size:14px; margin-bottom:16px;">Choose background colors for header and footer</p>

        <form method="post" class="form-group">
            <input type="hidden" name="action" value="update_colors">
            
            <label style="font-weight:600;">Header Background Color:</label>
            <div style="display:flex; gap:10px; align-items:center;">
                <input type="color" name="header_bg_color" value="<?php echo htmlspecialchars($siteConfig['header_bg_color']); ?>" style="width:80px; height:40px; border:1px solid #e6e6e6; border-radius:6px; cursor:pointer;">
                <input type="text" name="header_bg_color_text" value="<?php echo htmlspecialchars($siteConfig['header_bg_color']); ?>" placeholder="#ffffff" style="padding:10px; border:1px solid #e6e6e6; border-radius:6px; flex:1;">
            </div>

            <label style="font-weight:600; margin-top:16px;">Footer Background Color:</label>
            <div style="display:flex; gap:10px; align-items:center;">
                <input type="color" name="footer_bg_color" value="<?php echo htmlspecialchars($siteConfig['footer_bg_color']); ?>" style="width:80px; height:40px; border:1px solid #e6e6e6; border-radius:6px; cursor:pointer;">
                <input type="text" name="footer_bg_color_text" value="<?php echo htmlspecialchars($siteConfig['footer_bg_color']); ?>" placeholder="#fafafa" style="padding:10px; border:1px solid #e6e6e6; border-radius:6px; flex:1;">
            </div>

            <div style="margin-top:20px; padding:12px; background:#f0f0f0; border-radius:6px;">
                <div style="font-size:13px; color:#666;"><strong>Preview:</strong></div>
                <div style="margin-top:8px; display:flex; gap:8px;">
                    <div style="flex:1; padding:16px; background:<?php echo htmlspecialchars($siteConfig['header_bg_color']); ?>; border-radius:4px; border:1px solid #ddd; text-align:center; font-size:12px; color:#666;">
                        Header
                    </div>
                    <div style="flex:1; padding:16px; background:<?php echo htmlspecialchars($siteConfig['footer_bg_color']); ?>; border-radius:4px; border:1px solid #ddd; text-align:center; font-size:12px; color:#666;">
                        Footer
                    </div>
                </div>
            </div>

            <button class="btn" type="submit" style="margin-top:16px; width:100%;">Save Colors</button>
        </form>
    </div>
</div>

<!-- Marquee Modal -->
<div id="marqueeModal" class="modal">
    <div class="modal-content">
        <span class="modal-close" onclick="closeModal('marqueeModal')">&times;</span>
        <h2>Marquee Text Settings</h2>
        
        <form method="post" class="form-group">
            <input type="hidden" name="action" value="update_marquee">
            
            <label style="display:flex; align-items:center; gap:8px; margin-bottom:16px;">
                <input type="checkbox" name="marquee_enabled" <?php echo $marquee['enabled'] ? 'checked' : ''; ?> style="width:18px; height:18px; cursor:pointer;">
                <span style="font-weight:600;">Enable Marquee</span>
            </label>
            
            <label>Marquee Text:</label>
            <textarea name="marquee_text" style="padding:10px; border:1px solid #e6e6e6; border-radius:6px; font-size:14px; font-family:inherit; min-height:100px;"><?php echo htmlspecialchars($marquee['text']); ?></textarea>
            
            <label>Scroll Speed (e.g., 30s, 45s, 60s):</label>
            <input type="text" name="marquee_speed" value="<?php echo htmlspecialchars($marquee['speed']); ?>" placeholder="30s">
            
            <label>Text Color:</label>
            <input type="color" name="marquee_text_color" value="<?php echo htmlspecialchars($marquee['text_color']); ?>" style="width:100%; height:50px; border:1px solid #e6e6e6; border-radius:6px; cursor:pointer;">
            
            <label>Background Color:</label>
            <input type="color" name="marquee_bg_color" value="<?php echo htmlspecialchars($marquee['background_color']); ?>" style="width:100%; height:50px; border:1px solid #e6e6e6; border-radius:6px; cursor:pointer;">
            
            <label>Font Size (e.g., 14px, 18px, 24px):</label>
            <input type="text" name="marquee_font_size" value="<?php echo htmlspecialchars($marquee['font_size']); ?>" placeholder="18px">
            
            <div style="margin-top:20px; padding:16px; background:#f0f0f0; border-radius:6px;">
                <div style="font-size:13px; color:#666; margin-bottom:8px;"><strong>Live Preview:</strong></div>
                <div style="background:<?php echo htmlspecialchars($marquee['background_color']); ?>; border-radius:4px; overflow:hidden;">
                    <div style="padding:12px; color:<?php echo htmlspecialchars($marquee['text_color']); ?>; font-size:<?php echo htmlspecialchars($marquee['font_size']); ?>; white-space:nowrap; animation:scroll-left-marquee 30s linear infinite; display:inline-block; padding-left:100%;">
                        <?php echo htmlspecialchars($marquee['text']); ?>
                    </div>
                </div>
            </div>
            
            <button class="btn" type="submit" style="margin-top:16px; width:100%;">Save Marquee</button>
        </form>
    </div>
</div>

<!-- Footer Marquee Modal -->
<div id="footerMarqueeModal" class="modal">
    <div class="modal-content">
        <span class="modal-close" onclick="closeModal('footerMarqueeModal')">&times;</span>
        <h2>Footer Marquee Settings</h2>
        
        <form method="post" class="form-group">
            <input type="hidden" name="action" value="update_footer_marquee">
            
            <label style="display:flex; align-items:center; gap:8px; margin-bottom:16px;">
                <input type="checkbox" name="footer_marquee_enabled" <?php echo $footerMarquee['enabled'] ? 'checked' : ''; ?> style="width:18px; height:18px; cursor:pointer;">
                <span style="font-weight:600;">Enable Footer Marquee</span>
            </label>
            
            <label>Footer Marquee Text:</label>
            <textarea name="footer_marquee_text" style="padding:10px; border:1px solid #e6e6e6; border-radius:6px; font-size:14px; font-family:inherit; min-height:100px;"><?php echo htmlspecialchars($footerMarquee['text']); ?></textarea>
            
            <label>Scroll Speed (e.g., 30s, 45s, 60s):</label>
            <input type="text" name="footer_marquee_speed" value="<?php echo htmlspecialchars($footerMarquee['speed']); ?>" placeholder="35s">
            
            <label>Text Color:</label>
            <input type="color" name="footer_marquee_text_color" value="<?php echo htmlspecialchars($footerMarquee['text_color']); ?>" style="width:100%; height:50px; border:1px solid #e6e6e6; border-radius:6px; cursor:pointer;">
            
            <label>Background Color:</label>
            <input type="color" name="footer_marquee_bg_color" value="<?php echo htmlspecialchars($footerMarquee['background_color']); ?>" style="width:100%; height:50px; border:1px solid #e6e6e6; border-radius:6px; cursor:pointer;">
            
            <label>Font Size (e.g., 14px, 16px, 18px):</label>
            <input type="text" name="footer_marquee_font_size" value="<?php echo htmlspecialchars($footerMarquee['font_size']); ?>" placeholder="16px">
            
            <div style="margin-top:20px; padding:16px; background:#f0f0f0; border-radius:6px;">
                <div style="font-size:13px; color:#666; margin-bottom:8px;"><strong>Live Preview:</strong></div>
                <div style="background:<?php echo htmlspecialchars($footerMarquee['background_color']); ?>; border-radius:4px; overflow:hidden;">
                    <div style="padding:12px; color:<?php echo htmlspecialchars($footerMarquee['text_color']); ?>; font-size:<?php echo htmlspecialchars($footerMarquee['font_size']); ?>; white-space:nowrap; animation:scroll-left-marquee 35s linear infinite; display:inline-block; padding-left:100%;">
                        <?php echo htmlspecialchars($footerMarquee['text']); ?>
                    </div>
                </div>
            </div>
            
            <button class="btn" type="submit" style="margin-top:16px; width:100%;">Save Footer Marquee</button>
        </form>
    </div>
</div>

<!-- <php include __DIR__ . '/footer.php'; ?> -->

<script>
function openModal(id) {
    document.getElementById(id).classList.add('show');
}
function closeModal(id) {
    document.getElementById(id).classList.remove('show');
}
function editCat(id, name, slug, desc, parent) {
    document.getElementById('editCatTitle').style.display = 'block';
    document.getElementById('editCatForm').style.display = 'flex';
    document.getElementById('editCatId').value = id;
    document.getElementById('editCatName').value = name;
    document.getElementById('editCatSlug').value = slug;
    document.getElementById('editCatDesc').value = desc;
    document.getElementById('editCatParent').value = parent || '';
}
function cancelEditCat() {
    document.getElementById('editCatTitle').style.display = 'none';
    document.getElementById('editCatForm').style.display = 'none';
}
function editPageForm(slug, title, html) {
    document.getElementById('editPageTitle').style.display = 'block';
    document.getElementById('editPageForm').style.display = 'flex';
    document.getElementById('editPageSlug').value = slug;
    document.getElementById('editPageHtml').value = html;
}
function cancelEditPage() {
    document.getElementById('editPageTitle').style.display = 'none';
    document.getElementById('editPageForm').style.display = 'none';
}
// Close modal on outside click
document.querySelectorAll('.modal').forEach(modal => {
    modal.addEventListener('click', function(e) {
        if (e.target === this) this.classList.remove('show');
    });
});
function toggleAdminSidebar() {
    const sidebar = document.getElementById('adminSidebar');
    const mainContent = document.getElementById('adminMain');
    sidebar.classList.toggle('collapsed');
    mainContent.classList.toggle('with-collapsed-sidebar');
    localStorage.setItem('adminSidebarCollapsed', sidebar.classList.contains('collapsed') ? '1' : '0');
}
function editSocialForm(id, platform, name, link) {
    document.getElementById('editSocialTitle').style.display = 'block';
    document.getElementById('editSocialForm').style.display = 'flex';
    document.getElementById('editSocialId').value = id;
    document.getElementById('editSocialPlatform').value = platform;
    document.getElementById('editSocialName').value = name;
    document.getElementById('editSocialLink').value = link;
}
function cancelEditSocial() {
    document.getElementById('editSocialTitle').style.display = 'none';
    document.getElementById('editSocialForm').style.display = 'none';
}
// Restore sidebar state on page load
document.addEventListener('DOMContentLoaded', function() {
    const isCollapsed = localStorage.getItem('adminSidebarCollapsed') === '1';
    if (isCollapsed) {
        document.getElementById('adminSidebar').classList.add('collapsed');
        document.getElementById('adminMain').classList.add('with-collapsed-sidebar');
    }
});
</script>
</body>
</html>
