<?php
declare(strict_types=1);

require_once __DIR__ . '/_bootstrap.php';
admin_require_login();

$resumo = admin_dashboard_resumo();
$corridasMes = admin_dashboard_corridas_mes();
$recentes = admin_dashboard_corridas_recentes();
$flash = admin_flash();

$maiorMes = 1;
foreach ($corridasMes as $mes) {
    $maiorMes = max($maiorMes, (int) $mes['total']);
}

$pontos = [];
$totalMeses = max(1, count($corridasMes) - 1);
foreach ($corridasMes as $i => $mes) {
    $x = 36 + ($i * (560 / $totalMeses));
    $y = 150 - (((int) $mes['total'] / $maiorMes) * 118);
    $pontos[] = round($x, 2) . ',' . round($y, 2);
}

$totalMotoristas = max(1, (int) $resumo['motoristas']);
$ativoPct = min(100, max(0, ((int) $resumo['motoristas_online'] / $totalMotoristas) * 100));
$bloqueadoPct = min(100, max(0, ((int) $resumo['motoristas_bloqueados'] / $totalMotoristas) * 100));
?>
<!doctype html>
<html lang="pt-BR">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bulo Admin - Dashboard</title>
  <link rel="stylesheet" href="assets/admin.css?v=20260626-1">
</head>
<body>
  <?php admin_sidebar('dashboard'); ?>

  <main class="page">
    <?php if ($flash): ?>
      <div class="flash <?= e($flash['type']) ?>"><?= e($flash['message']) ?></div>
    <?php endif; ?>

    <section class="dashboard-grid">
      <div class="stat-card stat-orange">
        <div>
          <strong><?= e((string) $resumo['passageiros']) ?></strong>
          <span>Total Passageiros</span>
        </div>
        <svg class="stat-icon" width="32" height="32" style="width:32px;height:32px;flex:0 0 32px;" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M12 12a4 4 0 1 0-4-4 4 4 0 0 0 4 4Zm0 2c-3.31 0-7 1.67-7 4v2h14v-2c0-2.33-3.69-4-7-4Z"/></svg>
      </div>
      <div class="stat-card stat-blue">
        <div>
          <strong><?= e((string) $resumo['motoristas']) ?></strong>
          <span>Total Motoristas</span>
        </div>
        <svg class="stat-icon" width="32" height="32" style="width:32px;height:32px;flex:0 0 32px;" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M17 11a3 3 0 1 0-3-3 3 3 0 0 0 3 3ZM7 11a3 3 0 1 0-3-3 3 3 0 0 0 3 3Zm10 2c-2.33 0-5 1.17-5 3v2h10v-2c0-1.83-2.67-3-5-3ZM7 13c-2.33 0-5 1.17-5 3v2h8v-2a3.32 3.32 0 0 1 1.12-2.39A8.85 8.85 0 0 0 7 13Z"/></svg>
      </div>
      <div class="stat-card stat-green">
        <div>
          <strong><?= e((string) $resumo['corridas']) ?></strong>
          <span>Total Corridas</span>
        </div>
        <svg class="stat-icon" width="32" height="32" style="width:32px;height:32px;flex:0 0 32px;" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M5 11h14l-1.5-4.5h-11L5 11Zm13.5 2h-13A2.5 2.5 0 0 0 3 15.5V20h2v-2h14v2h2v-4.5a2.5 2.5 0 0 0-2.5-2.5ZM7 16.5A1.5 1.5 0 1 1 7 13a1.5 1.5 0 0 1 0 3.5Zm10 0A1.5 1.5 0 1 1 17 13a1.5 1.5 0 0 1 0 3.5Z"/></svg>
      </div>
    </section>

    <section class="dashboard-row">
      <div class="panel chart-card">
        <h2>Estatistica de corridas</h2>
        <svg class="line-chart" viewBox="0 0 620 180" preserveAspectRatio="none">
          <g stroke="#d5dde8" stroke-width="1">
            <line x1="36" y1="32" x2="596" y2="32"></line>
            <line x1="36" y1="72" x2="596" y2="72"></line>
            <line x1="36" y1="112" x2="596" y2="112"></line>
            <line x1="36" y1="152" x2="596" y2="152"></line>
          </g>
          <?php if ($pontos): ?>
            <polyline points="<?= e(implode(' ', $pontos)) ?>" fill="none" stroke="#31dfad" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"></polyline>
          <?php endif; ?>
          <?php foreach ($corridasMes as $i => $mes): ?>
            <?php $x = 36 + ($i * (560 / $totalMeses)); ?>
            <text x="<?= e((string) round($x, 2)) ?>" y="174" text-anchor="middle" font-size="12" fill="#8b9bb0"><?= e(substr((string) $mes['mes'], 5, 2)) ?>/<?= e(substr((string) $mes['mes'], 2, 2)) ?></text>
          <?php endforeach; ?>
        </svg>
      </div>

      <div class="panel chart-card">
        <h2>Motoristas</h2>
        <div class="donut-wrap">
          <div
            style="width:154px;height:154px;border-radius:50%;background:conic-gradient(#1683f7 0 <?= e((string) $ativoPct) ?>%, #ffd21f <?= e((string) $ativoPct) ?>% <?= e((string) ($ativoPct + $bloqueadoPct)) ?>%, #e7edf4 <?= e((string) ($ativoPct + $bloqueadoPct)) ?>% 100%);display:grid;place-items:center;"
          >
            <div style="width:70px;height:70px;background:white;border-radius:50%;"></div>
          </div>
        </div>
        <div class="driver-summary">
          Total motoristas: <?= e((string) $resumo['motoristas']) ?><br>
          Online: <?= e((string) $resumo['motoristas_online']) ?><br>
          Bloqueados: <?= e((string) $resumo['motoristas_bloqueados']) ?>
        </div>
      </div>
    </section>

    <section class="panel" style="margin-bottom:16px;">
      <div class="panel-body">
        <h2 class="table-title">Corridas recentes</h2>
        <div class="table-wrap">
          <table>
            <thead>
              <tr>
                <th>ID</th>
                <th>Passageiro</th>
                <th>Motorista</th>
                <th>Data</th>
                <th>Valor</th>
                <th>Status</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($recentes as $corrida): ?>
                <tr>
                  <td>#<?= e((string) $corrida['id']) ?></td>
                  <td><?= e((string) $corrida['passageiro_nome']) ?></td>
                  <td><?= e((string) $corrida['motorista_nome']) ?></td>
                  <td><?= e((string) $corrida['criada_em']) ?></td>
                  <td><?= e(admin_money((float) ($corrida['valor'] ?? 0))) ?></td>
                  <td><span class="badge <?= e(admin_status_class((string) ($corrida['status'] === 'finalizada' ? 'aprovado' : 'pendente'))) ?>"><?= e((string) $corrida['status']) ?></span></td>
                </tr>
              <?php endforeach; ?>
              <?php if (!$recentes): ?>
                <tr><td colspan="6">Nenhuma corrida registrada ainda.</td></tr>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </section>

  </main>
</body>
</html>
