<?php
date_default_timezone_set('America/Lima');

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$userRole = $_SESSION['user_role'] ?? '';

// Define navigation items for each role
$navItems = [
    'dashboard' => [
        'url' => 'dashboard.php',
        'icon' => 'fa-tachometer-alt',
        'text' => 'Dashboard',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'kanban' => [
        'url' => 'kanban.php',
        'icon' => 'fa-columns',
        'text' => 'Kanban',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'cobertura' => [
        'url' => 'cobertura.php',
        'icon' => 'fa-map-marked-alt',
        'text' => 'Cobertura',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'cobertura_xpress' => [
        'url' => 'cobertura_xpress.php',
        'icon' => 'fa-shipping-fast',
        'text' => 'Cobertura Xpress',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'buscador_general' => [
        'url' => 'buscador_general.php',
        'icon' => 'fa-search',
        'text' => 'Buscador General',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'completados' => [
        'url' => 'completados.php',
        'icon' => 'fa-check-circle',
        'text' => 'Pedidos Completados',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'listos_para_recojo' => [
        'url' => 'listos_para_recojo.php',
        'icon' => 'fa-box-open',
        'text' => 'Pedido Listo para Recoger',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'pedidos_en_transito' => [
        'url' => 'pedidos_en_transito.php',
        'icon' => 'fa-truck-fast',
        'text' => 'Pedidos En Transito',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'pedidos' => [
        'url' => 'pedidos.php',
        'icon' => 'fa-clipboard-list',
        'text' => 'Pedidos Rotulados',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'agregar_pedido' => [
        'url' => 'pedido_form.php',
        'icon' => 'fa-plus-circle',
        'text' => 'Agregar Pedido',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'productos' => [
        'url' => 'productos.php',
        'icon' => 'fa-box',
        'text' => 'Gestionar Productos',
        'roles' => ['Administrador', 'admin']
    ],
    'finanzas_group' => [
        'icon' => 'fa-dollar-sign',
        'text' => 'Finanzas',
        'roles' => ['Administrador', 'admin'],
        'submenu' => [
            'finanzas' => [
                'url' => 'finanzas.php',
                'icon' => 'fa-dollar-sign',
                'text' => 'Finanzas',
                'roles' => ['Administrador', 'admin']
            ],
            'recaudo_esperado' => [
                'url' => 'recaudo_esperado.php',
                'icon' => 'fa-chart-line',
                'text' => 'Recaudo Esperado',
                'roles' => ['Administrador', 'admin']
            ],
            'rentabilidad' => [
                'url' => 'rentabilidad.php',
                'icon' => 'fa-chart-pie',
                'text' => 'Rentabilidad de Producto',
                'roles' => ['Administrador', 'admin']
            ],
        ]
    ],
    'info_producto' => [
        'url' => 'info_producto.php',
        'icon' => 'fa-info-circle',
        'text' => 'Info Producto',
        'roles' => ['Administrador', 'admin', 'Control Logistico']
    ],
    'pedidos_duplicados' => [
        'url' => 'pedidos_duplicados.php',
        'icon' => 'fa-copy',
        'text' => 'Pedidos Duplicados',
        'roles' => ['Administrador', 'admin', 'Asesor', 'Control Logistico']
    ],
    'gestiones' => [
        'url' => 'gestiones.php',
        'icon' => 'fa-cogs',
        'text' => 'Gestiones',
        'roles' => ['Administrador', 'admin', 'superadmin', 'Asesor', 'Control Logistico']
    ],
    'manage_users' => [
        'url' => 'manage_users.php',
        'icon' => 'fa-users',
        'text' => 'Gestionar Usuarios',
        'roles' => ['Administrador', 'admin']
    ],
    'configuracion' => [
        'url' => 'configuracion.php',
        'icon' => 'fa-cog',
        'text' => 'Configuración',
        'roles' => ['Administrador', 'admin']
    ]
];

?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo isset($pageTitle) ? htmlspecialchars($pageTitle) : 'FLOOWER CRM'; ?></title>
    <meta name="description" content="CRM de seguimiento de pedidos para Floower Store.">
    <meta name="keywords" content="dashboard, call center, payment validation, order tracking, crm">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link rel="stylesheet" href="assets/css/style.css?v=<?php echo time(); ?>">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
    
</head>
<body>
    
    <div class="sidebar">
        <a href="pedidos.php" class="navbar-brand"><h3>FLOOWER CRM</h3></a>
        <ul class="nav flex-column">
            <?php
            $currentPage = basename($_SERVER['PHP_SELF']);

            foreach ($navItems as $key => $item):
                if (in_array($userRole, $item['roles'])):
                    if (isset($item['submenu'])):
                        $isSubmenuActive = false;
                        foreach ($item['submenu'] as $sub_item) {
                            if ($currentPage == $sub_item['url']) {
                                $isSubmenuActive = true;
                                break;
                            }
                        }
            ?>
                        <li class="nav-item has-submenu <?php echo $isSubmenuActive ? 'active' : ''; ?>">
                            <a href="#" class="nav-link"><i class="fas <?php echo $item['icon']; ?>"></i> <?php echo $item['text']; ?></a>
                            <ul class="submenu <?php echo $isSubmenuActive ? 'show' : ''; ?>">
                                <?php foreach ($item['submenu'] as $sub_item):
                                    if (in_array($userRole, $sub_item['roles'])):
                                        $isActive = $currentPage == $sub_item['url'] ? 'active' : '';
                                ?>
                                        <li class="nav-item">
                                            <a href="<?php echo $sub_item['url']; ?>" class="nav-link <?php echo $isActive; ?>"><i class="fas <?php echo $sub_item['icon']; ?>"></i> <?php echo $sub_item['text']; ?></a>
                                        </li>
                                <?php
                                    endif;
                                endforeach; ?>
                            </ul>
                        </li>
                    <?php else:
                        $isActive = $currentPage == $item['url'] ? 'active' : '';
                    ?>
                        <li class="nav-item">
                            <a href="<?php echo $item['url']; ?>" class="nav-link <?php echo $isActive; ?>"><i class="fas <?php echo $item['icon']; ?>"></i> <?php echo $item['text']; ?></a>
                        </li>
                    <?php endif;
                endif;
            endforeach;
            ?>
            <li class="nav-item mt-auto">
                <a href="logout.php" class="nav-link"><i class="fas fa-sign-out-alt"></i> Cerrar Sesión</a>
            </li>
        </ul>
    </div>

    <div class="content">
        <div class="container-fluid">
            <h1 class="mt-4"><?php echo isset($pageTitle) ? htmlspecialchars($pageTitle) : 'Bienvenido'; ?></h1>