<?php
session_start();

// Si no está logueado, redirige a index.php
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    header("Location: index.php");
    exit;
} else {
    $username = $_SESSION['nombre'];
    $rol = isset($_SESSION['rol']) ? $_SESSION['rol'] : '';
}

include 'database/DBconnection.php';
?>
<!DOCTYPE html>
<html lang="es">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DIAMOND - SISTEMA</title>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.18/summernote-bs4.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.11.5/css/dataTables.bootstrap5.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.18/summernote-bs4.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://unpkg.com/dropzone@5/dist/min/dropzone.min.css" type="text/css" />
    <script src="https://unpkg.com/dropzone@5/dist/min/dropzone.min.js"></script>
    <script src="https://cdn.ckeditor.com/ckeditor5/40.0.0/classic/ckeditor.js"></script>
    <link rel="stylesheet" href="css/style.css">
    <link rel="shortcut icon" href="img/diamond.png" type="image/x-icon">
</head>

<body>
    <div class="wrapper">
        <aside id="sidebar">
            <div class="sidebar-header">

                <button id="toggle-btn" type="button">
                    <i class="fa-solid fa-bars"></i>
                </button>
            </div>
            <ul class="sidebar-nav">
                <li class="sidebar-item">
                    <a href="dashboard.php" class="sidebar-link <?php echo basename($_SERVER['PHP_SELF']) == 'dashboard.php' ? 'active' : ''; ?>">
                        <i class="fa-solid fa-gauge-high"></i>
                        <span>Dashboard</span>
                    </a>
                </li>

                <li class="sidebar-item">
                    <a href="usuarios.php" class="sidebar-link <?php echo basename($_SERVER['PHP_SELF']) == 'usuarios.php' ? 'active' : ''; ?>">
                        <i class="fa-solid fa-users"></i>
                        <span>Usuarios</span>
                    </a>
                </li>

                <li class="sidebar-item">
                    <a href="blog.php" class="sidebar-link">
                        <i class="fa-solid fa-pen-to-square"></i>
                        <span>Blog</span>
                    </a>
                </li>

                <li class="sidebar-item">
                    <a href="banners.php" class="sidebar-link <?php echo basename($_SERVER['PHP_SELF']) == 'banners.php' ? 'active' : ''; ?>">
                        <i class="fa-solid fa-images"></i>
                        <span>Banners</span>
                    </a>
                </li>

                <li class="sidebar-item">
                    <a href="categorias.php" class="sidebar-link <?php echo basename($_SERVER['PHP_SELF']) == 'categorias.php' ? 'active' : ''; ?>">
                        <i class="fa-solid fa-list"></i>
                        <span>Categorías</span>
                    </a>
                </li>

                <li class="sidebar-item">
                    <a href="marcas.php" class="sidebar-link <?php echo basename($_SERVER['PHP_SELF']) == 'marcas.php' ? 'active' : ''; ?>">
                        <i class="fa-solid fa-tag"></i>
                        <span>Marcas</span>
                    </a>
                </li>

                <li class="sidebar-item">
                    <a href="colores.php" class="sidebar-link <?php echo basename($_SERVER['PHP_SELF']) == 'colores.php' ? 'active' : ''; ?>">
                        <i class="fa-solid fa-palette"></i>
                        <span>Colores</span>
                    </a>
                </li>

                <li class="sidebar-item">
                    <a href="productos.php" class="sidebar-link <?php echo basename($_SERVER['PHP_SELF']) == 'productos.php' ? 'active' : ''; ?>">
                        <i class="fa-solid fa-box"></i>
                        <span>Productos</span>
                    </a>
                </li>



                <?php if ($rol === 'Administrador'): ?>
                    <li class="sidebar-item">
                        <a href="#" class="sidebar-link has-dropdown collapsed <?php echo in_array(basename($_SERVER['PHP_SELF']), ['configuracion.php', 'respaldos.php']) ? 'active' : ''; ?>" data-bs-toggle="collapse" data-bs-target="#configDropdown" aria-expanded="false" aria-controls="configDropdown">
                            <i class="fa-solid fa-gears"></i>
                            <span>Configuración</span>
                        </a>
                        <ul id="configDropdown" class="sidebar-dropdown list-unstyled collapse" data-bs-parent="#sidebar">
                            <li class="sidebar-item">
                                <a href="configuracion.php" class="sidebar-link <?php echo basename($_SERVER['PHP_SELF']) == 'configuracion.php' ? 'active' : ''; ?>">
                                    <i class="fa-solid fa-wrench"></i>
                                    <span>General</span>
                                </a>
                            </li>
                            <li class="sidebar-item">
                                <a href="respaldos.php" class="sidebar-link <?php echo basename($_SERVER['PHP_SELF']) == 'respaldos.php' ? 'active' : ''; ?>">
                                    <i class="fa-solid fa-database"></i>
                                    <span>Respaldos</span>
                                </a>
                            </li>
                        </ul>
                    </li>
                <?php endif; ?>
            </ul>
            <div class="sidebar-footer">
                <a href="#" id="logoutLink" class="sidebar-link">
                    <i class="fa-solid fa-arrow-right-from-bracket fa-flip-horizontal"></i>
                    <span>Cerrar sesión</span>
                </a>
            </div>
        </aside>
        <div class="main">
            <nav class="navbar">
                <ul>
                    <li>
                        <a href="dashboard.php">
                            <img src="img/logo_diamond.png" alt="Diamond Logo" class="nav-logo">
                        </a>
                    </li>
                    <li>
                        <span style="color: var(--primary-color);">Bienvenido: <b><?php echo htmlspecialchars($username); ?></b></span>
                    </li>
                </ul>
            </nav>
            <div class="p-3 scrollable-content">