<?php
/**
 * Fichier  : views/dashboard.php
 * Version  : 2.6.73
 * Date     : 2026-07-11
 * Description : Tableau de bord — accueil traduit par clés explicites
 *
 * Changelog :
 *   v2.6.73 — I18N MODULES SAFE ARCHI1 : connexion du rendu statique aux catalogues, sans balayage DOM.
 */
$settings = getSettings();
$company  = $settings['companyName'] ?? 'MageStock';
$userName = appUserDisplayName($user ?? null);
$now      = new DateTimeImmutable('now');
$hour     = (int)$now->format('G');
$greetingKey = $hour < 12 ? 'dashboard.greeting.morning' : ($hour < 18 ? 'dashboard.greeting.afternoon' : 'dashboard.greeting.evening');
$greetingFallback = $hour < 12 ? 'Bonjour' : ($hour < 18 ? 'Bon après-midi' : 'Bonsoir');
$greeting = appTranslate($greetingKey, $lang, [], $greetingFallback);
$dateStr  = appFormatLongDate($now, $lang);
$dashT = static fn(string $key, string $fallback): string => appTranslate($key, $lang, [], $fallback);
?>
<link rel="stylesheet" href="<?= htmlspecialchars(assetUrl('css/dashboard.css?v=2.6.73')) ?>">

<div class="page-body" id="pgDashboard">

  <!-- ═══════ HEADER ACCUEIL ═══════ -->
  <div class="dash-welcome">
    <div class="dash-welcome-txt">
      <h1 class="dash-welcome-h1"><?= htmlspecialchars($greeting) ?>, <?= htmlspecialchars($userName) ?></h1>
      <p class="dash-welcome-sub"><?= htmlspecialchars($company) ?> — <?= htmlspecialchars($dateStr) ?></p>
    </div>
    <div class="dash-shortcuts">
      <a href="<?= htmlspecialchars(pageUrl('scanner')) ?>" class="dash-sc"><span class="dash-sc-ico">📷</span><span><?= htmlspecialchars($dashT('dashboard.shortcut.scanner','Scanner')) ?></span></a>
      <a href="<?= htmlspecialchars(pageUrl('movements')) ?>" class="dash-sc"><span class="dash-sc-ico">📦</span><span><?= htmlspecialchars($dashT('dashboard.shortcut.movement','Mouvement')) ?></span></a>
      <a href="<?= htmlspecialchars(pageUrl('inventory')) ?>" class="dash-sc"><span class="dash-sc-ico">📋</span><span><?= htmlspecialchars($dashT('dashboard.shortcut.inventory','Inventaire')) ?></span></a>
      <a href="<?= htmlspecialchars(pageUrl('orders')) ?>" class="dash-sc"><span class="dash-sc-ico">🛒</span><span><?= htmlspecialchars($dashT('dashboard.shortcut.orders','Commandes')) ?></span></a>
    </div>
  </div>

  <!-- ═══════ KPIs (6 cards) ═══════ -->
  <div class="dash-kpi-grid" id="kpiRow"></div>

  <!-- ═══════ GRAPHIQUES ═══════ -->
  <div class="dash-charts">
    <div class="card dash-chart-card">
      <div class="card-header"><h3 class="card-title"><?= htmlspecialchars($dashT('dashboard.chart.movements_14_days','Mouvements (14 jours)')) ?></h3></div>
      <div class="card-body"><div class="dash-chart-wrap"><canvas id="dashChartMov"></canvas></div></div>
    </div>
    <div class="card dash-chart-card">
      <div class="card-header"><h3 class="card-title"><?= htmlspecialchars($dashT('dashboard.chart.valuation_by_warehouse','Valorisation par magasin')) ?></h3></div>
      <div class="card-body"><div class="dash-chart-wrap"><canvas id="dashChartVal"></canvas></div></div>
    </div>
  </div>

  <!-- ═══════ ACTIVITÉ + ALERTES ═══════ -->
  <div class="dash-bottom">
    <div class="card">
      <div class="card-header">
        <h3 class="card-title"><?= htmlspecialchars($dashT('dashboard.section.recent_activity','Activité récente')) ?></h3>
        <a href="<?= htmlspecialchars(pageUrl('movements')) ?>" class="btn btn-ghost btn-sm"><?= htmlspecialchars($dashT('dashboard.action.view_all','Voir tout →')) ?></a>
      </div>
      <div class="card-body card-body-flush">
        <div class="dash-activity" id="activityFeed"></div>
      </div>
      <div class="dash-feed-footer" id="activityFooter"></div>
    </div>
    <div class="card">
      <div class="card-header">
        <h3 class="card-title"><?= htmlspecialchars($dashT('dashboard.section.active_alerts','Alertes actives')) ?></h3>
        <a href="<?= htmlspecialchars(pageUrl('alerts')) ?>" class="btn btn-ghost btn-sm"><?= htmlspecialchars($dashT('dashboard.action.manage','Gérer →')) ?></a>
      </div>
      <div class="card-body" id="alertsWidget" style="padding:12px"></div>
    </div>
  </div>

</div>
