<?php
require_once __DIR__ . '/bootstrap.php';
require_login();
$flash = get_flash();
$currentPhp = $_SERVER['PHP_SELF'] ?? '';
$role = current_user()['role'] ?? 'user';
?>
<!doctype html>
<html lang="es">
<head>
  <meta charset="utf-8">
  <title><?= e(company_name()) ?></title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="<?= e(base_url('assets/css/app.css')) ?>" rel="stylesheet">
</head>
<body>
<div class="container-fluid">
  <div class="row">
    <aside class="col-12 col-lg-2 col-md-3 sidebar p-3">
      <div class="brand-box">
        <img src="<?= e(public_logo_url()) ?>" alt="<?= e(company_name()) ?>">
        <div class="brand-title"><?= e(company_name()) ?></div>
        <div class="brand-subtitle">ERP RRHH v5.1 Comercial</div>
      </div>
      <div class="sidebar-group-label">Principal</div>
      <a class="<?= str_contains($currentPhp, 'dashboard.php') ? 'active' : '' ?>" href="<?= e(base_url('dashboard.php')) ?>">Dashboard</a>
      <a class="<?= str_contains($currentPhp, '/employees/') ? 'active' : '' ?>" href="<?= e(base_url('employees/index.php')) ?>">Empleados</a>
      <a class="<?= str_contains($currentPhp, '/attendance/') ? 'active' : '' ?>" href="<?= e(base_url('attendance/index.php')) ?>">Asistencia</a>
      <a class="<?= str_contains($currentPhp, '/leaves/') ? 'active' : '' ?>" href="<?= e(base_url('leaves/index.php')) ?>">Permisos y vacaciones</a>
      <a class="<?= str_contains($currentPhp, '/recruitment/') ? 'active' : '' ?>" href="<?= e(base_url('recruitment/index.php')) ?>">Reclutamiento</a>
      <?php if (can_manage_payroll()): ?>
      <a class="<?= str_contains($currentPhp, '/payroll/') ? 'active' : '' ?>" href="<?= e(base_url('payroll/index.php')) ?>">Nómina</a>
      <a class="<?= str_contains($currentPhp, '/advances/') ? 'active' : '' ?>" href="<?= e(base_url('advances/index.php')) ?>">Adelantos</a>
      <?php endif; ?>
      <a class="<?= str_contains($currentPhp, '/reports/') ? 'active' : '' ?>" href="<?= e(base_url('reports/index.php')) ?>">Reportes</a>
      <?php if (can_manage_master_data()): ?>
      <div class="sidebar-group-label">Maestros</div>
      <a class="<?= str_contains($currentPhp, '/departments/') ? 'active' : '' ?>" href="<?= e(base_url('departments/index.php')) ?>">Departamentos</a>
      <a class="<?= str_contains($currentPhp, '/positions/') ? 'active' : '' ?>" href="<?= e(base_url('positions/index.php')) ?>">Cargos</a>
      <div class="sidebar-group-label">Administración</div>
      <a class="<?= str_contains($currentPhp, '/settings/') ? 'active' : '' ?>" href="<?= e(base_url('settings/index.php')) ?>">Empresa</a>
      <?php endif; ?>
      <?php if (role_is('admin')): ?>
      <a class="<?= str_contains($currentPhp, '/users/') ? 'active' : '' ?>" href="<?= e(base_url('users/index.php')) ?>">Usuarios</a>
      <a class="<?= str_contains($currentPhp, '/audit_logs/') ? 'active' : '' ?>" href="<?= e(base_url('audit_logs/index.php')) ?>">Auditoría</a>
      <a class="<?= str_contains($currentPhp, '/install/') ? 'active' : '' ?>" href="<?= e(base_url('install/index.php')) ?>">Instalador</a>
      <?php endif; ?>
      <a href="<?= e(base_url('logout.php')) ?>">Salir</a>
    </aside>
    <main class="col-12 col-lg-10 col-md-9 content">
      <div class="topbar d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
	<div><div class="page-title"><?= e(company_name()) ?></div><div class="text-muted">Usuario: <?= e(current_user()['name'] ?? current_user()['full_name'] ?? current_user()['email'] ?? 'Usuario') ?> · Rol: <?= e(strtoupper($role)) ?></div></div>
        <div class="badge-soft">Versión 5.1 · adelantos + cierre de nómina</div>
      </div>
      <?php if ($flash): ?><div class="alert alert-<?= e($flash['type']) ?> rounded-4 border-0 shadow-sm"><?= e($flash['message']) ?></div><?php endif; ?>
