<?php
session_start();

if (!isset($_SESSION['usu_codigo'])) {
    header("Location: sba_login.php");
    exit;
}

$rol = $_SESSION['rol']; // 'ADMIN' o 'CAJERO'
$page = $_GET['page'] ?? 'inicio';
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <title>SBA | Panel Principal</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="dist/css/style.min.css">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <style>
        :root {
            --sidebar-width: 260px;
            --primary-dark: #1c2d3d;
            --accent-color: #3498db;
            --bg-light: #f4f7f6;
        }

        body { 
            font-family: 'Poppins', sans-serif; 
            background: var(--bg-light); 
            margin: 0; 
            display: flex;
            min-height: 100vh;
        }

        /* SIDEBAR STYLES */
        .sidebar {
            width: var(--sidebar-width);
            background: var(--primary-dark);
            color: #fff;
            display: flex;
            flex-direction: column;
            position: fixed;
            height: 100%;
            transition: 0.3s;
            z-index: 100;
        }

        .sidebar-header {
            padding: 30px 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .sidebar-menu {
            list-style: none;
            padding: 20px 0;
            margin: 0;
            flex-grow: 1;
        }

        .sidebar-menu li a {
            padding: 15px 25px;
            display: flex;
            align-items: center;
            color: rgba(255,255,255,0.7);
            text-decoration: none;
            transition: 0.3s;
            border-left: 4px solid transparent;
        }

        .sidebar-menu li a i { margin-right: 15px; width: 20px; text-align: center; }

        .sidebar-menu li a:hover, .sidebar-menu li a.active {
            background: rgba(255,255,255,0.05);
            color: #fff;
            border-left-color: var(--accent-color);
        }

        /* MAIN CONTENT AREA */
        .main-content {
            margin-left: var(--sidebar-width);
            width: calc(100% - var(--sidebar-width));
            display: flex;
            flex-direction: column;
        }

        header {
            background: #fff;
            padding: 15px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        }

        .user-info { display: flex; align-items: center; gap: 10px; font-weight: 600; }
        .user-info i { color: var(--accent-color); font-size: 20px; }

        main { padding: 40px; }

        /* DASHBOARD CARDS */
        .dashboard-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 25px;
        }

        .card-stat {
            background: #fff;
            border-radius: 15px;
            padding: 30px;
            text-align: center;
            box-shadow: 0 10px 20px rgba(0,0,0,0.05);
            transition: 0.3s;
            border: 1px solid transparent;
        }

        .card-stat:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.1);
            border-color: var(--accent-color);
        }

        .card-stat i {
            font-size: 40px;
            margin-bottom: 20px;
            color: var(--primary-dark);
        }

        .btn-action {
            display: block;
            margin-top: 20px;
            padding: 12px;
            background: var(--primary-dark);
            color: #fff;
            border-radius: 10px;
            text-decoration: none;
            font-weight: 600;
        }

        .btn-action:hover { background: var(--accent-color); text-decoration: none; color: white; }

        footer {
            padding: 20px;
            text-align: center;
            color: #888;
            font-size: 13px;
            margin-top: auto;
        }

        @media (max-width: 768px) {
            .sidebar { width: 70px; }
            .sidebar-header h2, .sidebar-menu span { display: none; }
            .main-content { margin-left: 70px; width: calc(100% - 70px); }
        }
    </style>
</head>
<body>

<aside class="sidebar">
    <div class="sidebar-header">
        <h2 style="font-size: 22px; margin:0;">SBA</h2>
        <small style="color: #6c757d;">Sistema Gestión de Bar</small>
    </div>
    <ul class="sidebar-menu">
        <li>
            <a href="index.php?page=inicio" class="<?= $page=='inicio'?'active':'' ?>">
                <i class="fa fa-home"></i> <span>Dashboard</span>
            </a>
        </li>
        
        <?php if ($rol == 'ADMIN' || $rol == 'CAJERO'): ?>
        <li>
            <a href="index.php?page=ventas" class="<?= $page=='ventas'?'active':'' ?>">
                <i class="fa fa-shopping-cart"></i> <span>Ventas POS</span>
            </a>
        </li>
        <li>
            <a href="index.php?page=caja" class="<?= $page=='caja'?'active':'' ?>">
                <i class="fa fa-cash-register"></i> <span>Caja</span>
            </a>
        </li>
        <?php endif; ?>

        <?php if ($rol == 'ADMIN'): ?>
        <li>
            <a href="index.php?page=beneficiarios" class="<?= $page=='beneficiarios'?'active':'' ?>">
                <i class="fa fa-address-book"></i> <span>Beneficiarios</span>
            </a>
        </li>
        <li>
            <a href="index.php?page=productos" class="<?= $page=='productos'?'active':'' ?>">
                <i class="fa fa-boxes"></i> <span>Productos</span>
            </a>
        </li>
        <li>
            <a href="index.php?page=reportes" class="<?= $page=='reportes'?'active':'' ?>">
                <i class="fa fa-chart-line"></i> <span>Reportes</span>
            </a>
        </li>
        <li>
            <a href="index.php?page=usuarios" class="<?= $page=='usuarios'?'active':'' ?>">
                <i class="fa fa-users-cog"></i> <span>Usuarios</span>
            </a>
        </li>
        <li>
            <a href="index.php?page=configuracion" class="<?= $page=='configuracion'?'active':'' ?>">
                <i class="fa fa-cogs"></i> <span>Configuración</span>
            </a>
        </li>
        <?php endif; ?>
        
        <li style="margin-top: 20px;">
            <a href="salir.php" style="color: #ff7675;">
                <i class="fa fa-sign-out-alt"></i> <span>Cerrar Sesión</span>
            </a>
        </li>
    </ul>
</aside>

<div class="main-content">
    <header>
        <div class="breadcrumb">
            <span class="text-muted">Panel</span> / <strong style="text-transform: capitalize;"><?= str_replace('_', ' ', $page) ?></strong>
        </div>
        <div class="user-info">
            <i class="fa fa-user-circle"></i>
            <span><?= htmlspecialchars($_SESSION['nombre'] ?? 'Usuario') ?></span>
            <span class="badge" style="background:#eee; color:#333; font-size:10px; padding:3px 8px; border-radius:5px;"><?= $rol ?></span>
        </div>
    </header>

    <main>
        <?php
        switch ($page) {
            case 'ventas':
                include "vista/pos.php";
                break;

            case 'productos':
                if ($rol == 'ADMIN') include "admin_productos.php";
                else echo "<h3>No tienes permisos.</h3>";
                break;

            case 'caja':
                if ($rol == 'ADMIN' || $rol == 'CAJERO') include "caja.php";
                else echo "<h3>No tienes permisos.</h3>";
                break;

            case 'beneficiarios':
                if ($rol == 'ADMIN') include "vista/beneficiarios.php";
                else echo "<h3>No tienes permisos.</h3>";
                break;

            case 'usuarios':
                if ($rol == 'ADMIN') include "usuarios.php";
                else echo "<h3>No tienes permisos.</h3>";
                break;
            
            case 'reportes':
                if ($rol == 'ADMIN') include "vista/reportes.php";
                else echo "<h3>No tienes permisos.</h3>";
                break;

            case 'configuracion':
                if ($rol == 'ADMIN') include "vista/configuracion.php";
                else echo "<h3>No tienes permisos.</h3>";
                break;

            case 'ventas_lista':
                include "vista/ventas_lista.php";
                break;

            case 'detalle_profesor': 
                include "vista/detalle_profesor.php"; 
                break;
                
            case 'cobrar_profesor': 
                include "vista/cobrar_profesor.php";
                break;

            case 'inicio':
            default:
                ?>
                <h2 style="margin-bottom: 30px; font-weight: 600;">Resumen del Sistema</h2>
                
                <div class="dashboard-grid">
                    <?php if ($rol == 'ADMIN' || $rol == 'CAJERO'): ?>
                    <div class="card-stat">
                        <i class="fa fa-shopping-basket"></i>
                        <h3>Punto de Venta</h3>
                        <p class="text-muted">Realiza ventas y factura rápido.</p>
                        <a href="index.php?page=ventas" class="btn-action">Nueva Venta</a>
                    </div>
                    
                    <div class="card-stat">
                        <i class="fa fa-calculator"></i>
                        <h3>Caja y Totales</h3>
                        <p class="text-muted">Arqueo de caja y movimientos diarios.</p>
                        <a href="index.php?page=caja" class="btn-action">Ver Caja</a>
                    </div>
                    <?php endif; ?>

                    <?php if ($rol == 'ADMIN'): ?>
                    <div class="card-stat">
                        <i class="fa fa-address-book"></i>
                        <h3>Beneficiarios</h3>
                        <p class="text-muted">Control de créditos y saldos.</p>
                        <a href="index.php?page=beneficiarios" class="btn-action">Gestionar</a>
                    </div>

                    <div class="card-stat">
                        <i class="fa fa-warehouse"></i>
                        <h3>Inventario</h3>
                        <p class="text-muted">Precios, stock y nuevos productos.</p>
                        <a href="index.php?page=productos" class="btn-action">Gestionar</a>
                    </div>

                    <div class="card-stat">
                        <i class="fa fa-chart-pie"></i>
                        <h3>Estadísticas</h3>
                        <p class="text-muted">Ventas por rango y reportes.</p>
                        <a href="index.php?page=reportes" class="btn-action" >Ver Reportes</a>
                    </div>

                    <div class="card-stat" style="border: 1px dashed #d33;">
                        <i class="fa fa-tools" style="color: #d33;"></i>
                        <h3>Configuración</h3>
                        <p class="text-muted">Encerar base y mantenimiento.</p>
                        <a href="index.php?page=configuracion" class="btn-action" style="background: #d33;">Mantenimiento</a>
                    </div>
                    <?php endif; ?>
                </div>
                <?php
                break;
        }
        ?>
    </main>

    <footer>
        &copy; <?= date("Y") ?> <strong>SBA</strong> - Sistema Bar | Desarrollado por FDLabs | Conectado como <?= $rol ?>
    </footer>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>