<?php
require_once __DIR__ . '/icons.php';
$user = current_user();
$currentScript = basename($_SERVER['SCRIPT_NAME']);
$uiTemplate = $user['ui_template'] ?? 'modern';
if (!in_array($uiTemplate, ['modern', 'classic', 'minimal'], true)) $uiTemplate = 'modern';
$uiColor = $user['ui_color'] ?? 'teal';
if (!array_key_exists($uiColor, ui_colors())) $uiColor = 'teal';
function nav_active2($script, $current) { return $script === $current ? ' active' : ''; }

$employeeNav = [
    ['dashboard.php', 'employee/dashboard.php', 'dashboard', 'داشبورد'],
    ['submit_claim.php', 'employee/submit_claim.php', 'plus-circle', 'ثبت درخواست جدید'],
    ['history.php', 'employee/history.php', 'clock', 'تاریخچه درخواست‌ها'],
    ['balance.php', 'employee/balance.php', 'coin', 'مانده حساب'],
];
$adminNav = [
    ['dashboard.php', 'admin/dashboard.php', 'dashboard', 'درخواست‌های در انتظار'],
    ['all_claims.php', 'admin/all_claims.php', 'list', 'همه درخواست‌ها'],
    ['users.php', 'admin/users.php', 'users', 'مدیریت کارمندان'],
    ['dependents.php', 'admin/dependents.php', 'family', 'افراد تبعی'],
    ['rows.php', 'admin/rows.php', 'grid', 'ردیف‌ها و سقف‌ها'],
    ['import_rows.php', 'admin/import_rows.php', 'upload', 'ایمپورت از اکسل'],
    ['reports.php', 'admin/reports.php', 'bar-chart', 'گزارش مانده پرسنل'],
    ['agreement.php', 'admin/agreement.php', 'file-check', 'تعهدنامه'],
    ['settings.php', 'admin/settings.php', 'settings', 'تنظیمات'],
];
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl" data-template="<?= htmlspecialchars($uiTemplate) ?>" data-color="<?= htmlspecialchars($uiColor) ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
(function () {
    var saved = localStorage.getItem('theme');
    if (saved === 'light' || saved === 'dark') {
        document.documentElement.setAttribute('data-theme', saved);
    }
})();
</script>
<title><?= isset($page_title) ? htmlspecialchars($page_title) . ' | ' : '' ?><?= htmlspecialchars(APP_NAME) ?></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/rastikerdar/vazirmatn@v33.003/Vazirmatn-font-face.css">
<link rel="stylesheet" href="<?= base_url('assets/css/style.css') ?>?v=<?= @filemtime(__DIR__ . '/../assets/css/style.css') ?: '1' ?>">
</head>
<body>
<?php if ($user): ?>
<div class="app-shell">
  <div class="sidebar-backdrop no-print" id="sidebarBackdrop"></div>
  <aside class="app-sidebar no-print" id="appSidebar">
    <div class="sidebar-brand">
      <img src="<?= base_url('assets/img/logo-mark.svg') ?>" alt="">
      <span><?= htmlspecialchars(APP_NAME) ?></span>
    </div>
    <nav class="sidebar-nav">
      <?php
      $navItems = $user['role'] === 'employee' ? $employeeNav : $adminNav;
      foreach ($navItems as [$script, $path, $iconName, $label]):
          if ($script === 'dependents.php' && $user['role'] === 'employee' && get_setting($pdo, 'dependents_enabled', '1') !== '1') continue;
      ?>
        <a class="sidebar-link<?= nav_active2($script, $currentScript) ?>" href="<?= base_url($path) ?>">
          <?= icon($iconName) ?><span><?= $label ?></span>
        </a>
      <?php endforeach; ?>
      <?php if ($user['role'] === 'employee'): ?>
        <a class="sidebar-link<?= nav_active2('profile.php', $currentScript) ?>" href="<?= base_url('employee/profile.php') ?>">
          <?= icon('user') ?><span>پروفایل من</span>
        </a>
      <?php endif; ?>
    </nav>
    <div class="sidebar-user">
      <div class="sidebar-user-top">
        <div class="sidebar-avatar"><?= htmlspecialchars(mb_substr(trim($user['full_name']), 0, 1, 'UTF-8')) ?></div>
        <div>
          <div class="sidebar-user-name"><?= htmlspecialchars($user['full_name']) ?></div>
          <div class="sidebar-user-role"><?= $user['role'] === 'admin' ? 'مدیر/مالی' : 'کارمند' ?></div>
        </div>
      </div>
      <a href="<?= base_url('logout.php') ?>" class="sidebar-logout"><?= icon('logout') ?><span>خروج</span></a>
      <div class="theme-switch" id="themeSwitch">
        <button type="button" data-theme-choice="auto" title="خودکار (هماهنگ با سیستم)">⚙</button>
        <button type="button" data-theme-choice="light" title="روشن">☀</button>
        <button type="button" data-theme-choice="dark" title="تیره">☾</button>
      </div>
    </div>
  </aside>

  <div class="app-main">
    <div class="app-topbar no-print">
      <button class="sidebar-toggle" id="sidebarToggle" type="button" aria-label="منو"><?= icon('menu') ?></button>
      <div class="app-topbar-title"><?= isset($page_title) ? htmlspecialchars($page_title) : '' ?></div>
    </div>
    <div class="app-content">
<?php else: ?>
<div class="app-content-plain">
<?php endif; ?>
<?php if (!empty($_SESSION['flash_success'])): ?>
  <div class="alert alert-success"><?= htmlspecialchars($_SESSION['flash_success']); unset($_SESSION['flash_success']); ?></div>
<?php endif; ?>
<?php if (!empty($_SESSION['flash_error'])): ?>
  <div class="alert alert-danger"><?= htmlspecialchars($_SESSION['flash_error']); unset($_SESSION['flash_error']); ?></div>
<?php endif; ?>
