<?php
declare(strict_types=1);

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

$q = trim((string) ($_GET['q'] ?? ''));
$rows = admin_buscar_motoristas($q, false);
$cidades = admin_resumo_motoristas_por_cidade();
$flash = admin_flash();
?>
<!doctype html>
<html lang="pt-BR">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bulo Admin - Cadastros Motoristas</title>
  <link rel="stylesheet" href="assets/admin.css?v=20260626-1">
</head>
<body>
  <?php admin_sidebar('motoristas'); ?>

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

    <section class="panel">
      <div class="panel-header">
        <h1>Cadastros de motoristas</h1>
        <p>Busque por nome, e-mail, CPF, celular ou cidade. Os selos mostram o estado de cada documento e se o motorista está online ou offline.</p>
      </div>
      <div class="panel-body">
        <?php if ($cidades): ?>
          <div class="status-stack" style="margin-bottom:16px;">
            <?php foreach ($cidades as $cidade): ?>
              <span class="badge badge-muted">
                <?= e((string) $cidade['cidade']) ?>/<?= e((string) $cidade['uf']) ?>:
                <?= e((string) $cidade['total']) ?>
              </span>
            <?php endforeach; ?>
          </div>
        <?php endif; ?>

        <form method="get" action="cadastros_motoristas.php" style="margin-bottom:16px;">
          <input type="text" name="q" placeholder="Pesquisar..." value="<?= e($q) ?>">
        </form>

        <div style="overflow:auto;">
          <table style="width:100%; border-collapse:collapse;">
            <thead>
              <tr>
                <th style="text-align:left; border-bottom:1px solid #ddd; padding:8px;">Nome</th>
                <th style="text-align:left; border-bottom:1px solid #ddd; padding:8px;">Contato</th>
                <th style="text-align:left; border-bottom:1px solid #ddd; padding:8px;">Cidade</th>
                <th style="text-align:left; border-bottom:1px solid #ddd; padding:8px;">Documentos</th>
                <th style="text-align:left; border-bottom:1px solid #ddd; padding:8px;">Status</th>
                <th style="text-align:left; border-bottom:1px solid #ddd; padding:8px;">Criado em</th>
                <th style="text-align:left; border-bottom:1px solid #ddd; padding:8px;">Ações</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($rows as $r): ?>
                <tr>
                  <td style="padding:8px; border-bottom:1px solid #eee;">
                    <strong><?= e($r['nome']) ?></strong><br>
                    CPF: <?= e($r['cpf']) ?>
                  </td>
                  <td style="padding:8px; border-bottom:1px solid #eee;">
                    <?= e($r['email']) ?><br>
                    <?= e($r['celular']) ?>
                  </td>
                  <td style="padding:8px; border-bottom:1px solid #eee;">
                    <?= e((string) ($r['endereco_cidade'] ?? '--')) ?>/<?= e((string) ($r['endereco_uf'] ?? '--')) ?><br>
                    <small>CEP: <?= e((string) ($r['cep'] ?? '--')) ?></small>
                  </td>
                  <td style="padding:8px; border-bottom:1px solid #eee;">
                    <div class="status-stack">
                      <span class="badge <?= admin_status_class((string) $r['cnh_status']) ?>">
                        CNH: <?= e((string) $r['cnh_status']) ?>
                      </span>
                      <span class="badge <?= admin_status_class((string) ($r['documento_veiculo_status_real'] ?? $r['documento_veiculo_status'])) ?>">
                        Veículo: <?=  e((string) ($r['documento_veiculo_status_real'] ?? $r['documento_veiculo_status'])) ?>
                      </span>
                      <span class="badge <?= admin_status_class((string) $r['selfie_status']) ?>">
                        Foto: <?= e((string) $r['selfie_status']) ?>
                      </span>
                    </div>
                  </td>
                  <td style="padding:8px; border-bottom:1px solid #eee;">
                    <div class="status-stack">
                      <span class="badge <?= admin_status_class((string) ($r['status_verificacao_operacional'] ?? $r['status_verificacao'])) ?>">
                        <?= e((string) ($r['status_verificacao_operacional'] ?? $r['status_verificacao'])) ?>
                      </span>
                      <span class="badge <?= admin_disponibilidade_class((string) $r['disponivel']) ?>">
                        <?= e((string) $r['disponivel']) === 'sim' ? 'online' : 'offline' ?>
                      </span>
                    </div>
                  </td>
                  <td style="padding:8px; border-bottom:1px solid #eee;"><?= e((string) $r['criado_em']) ?></td>
                  <td style="padding:8px; border-bottom:1px solid #eee;">
                    <a class="button" href="visualizar_motorista.php?id=<?= e((string) $r['id']) ?>" style="margin-left:8px;">Visualizar</a>
                  </td>
                </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
      </div>
    </section>
  </main>
</body>
</html>
