<?php
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../../app/Models/Document.php';
require_once __DIR__.'/../../app/Models/DocumentTemplate.php';

$user = requireAuth(['admin']);
Document::refreshStatuses();

$filter = $_GET['status'] ?? '';
$sql = "SELECT d.*, u.name AS user_name, v.plate_number
        FROM documents d
        LEFT JOIN users u ON u.id=d.user_id
        LEFT JOIN vehicles v ON v.id=d.vehicle_id
        WHERE 1=1";
$p=[];
if (in_array($filter,['valid','expiring','expired'],true)) { $sql.=" AND d.status=?"; $p[]=$filter; }
$sql.=" ORDER BY d.updated_at DESC LIMIT 500";
$st=db()->prepare($sql); $st->execute($p); $docs=$st->fetchAll();

$pageTitle  = t('all_documents');
$activePage = BASE_URL.'/admin/documents.php';
require __DIR__.'/../../app/Views/shared/layout.php';
?>
<div class="page-header"><h2>📄 <?= t('all_documents') ?> <span style="font-size:14px;color:var(--text-muted)">(<?= count($docs) ?>)</span></h2></div>

<div style="display:flex;gap:8px;margin-bottom:16px;flex-wrap:wrap">
  <?php foreach(['' => t('all'), 'valid'=>t('doc_valid'), 'expiring'=>t('doc_expiring'), 'expired'=>t('doc_expired')] as $k=>$lbl): ?>
  <a href="?status=<?= $k ?>" class="btn btn-sm <?= $filter===$k?'btn-primary':'btn-secondary' ?>"><?= e($lbl) ?></a>
  <?php endforeach; ?>
</div>

<div class="card"><div style="overflow-x:auto">
  <table class="table" style="width:100%;border-collapse:collapse">
    <thead><tr style="border-bottom:1px solid var(--border)">
      <th style="padding:12px"><?= t('doc_type') ?></th>
      <th style="padding:12px"><?= t('owner') ?></th>
      <th style="padding:12px"><?= t('plate_number') ?></th>
      <th style="padding:12px"><?= t('expiry_date') ?></th>
      <th style="padding:12px"><?= t('status') ?></th>
      <th style="padding:12px"><?= t('view') ?></th>
    </tr></thead>
    <tbody>
    <?php if(empty($docs)): ?>
      <tr><td colspan="6" style="padding:24px;text-align:center;color:var(--text-muted)"><?= t('no_data') ?></td></tr>
    <?php else: foreach($docs as $d): ?>
      <tr style="border-bottom:1px solid var(--border)">
        <td style="padding:12px"><?= e(DocumentTemplate::icon($d['type']).' '.DocumentTemplate::label($d['type'])) ?></td>
        <td style="padding:12px"><?= e($d['user_name'] ?: '—') ?></td>
        <td style="padding:12px"><?= $d['plate_number']?'<span class="vehicle-plate-badge">'.e($d['plate_number']).'</span>':'—' ?></td>
        <td style="padding:12px"><?= e($d['expiry_date'] ?: '—') ?></td>
        <td style="padding:12px"><?= statusBadge($d['status'], lang()) ?></td>
        <td style="padding:12px">
          <?php if(!empty($d['file_path'])): ?>
          <a href="<?= BASE_URL.'/'.e(ltrim($d['file_path'],'/')) ?>" target="_blank" class="btn btn-sm btn-secondary">🔗</a>
          <?php else: ?>—<?php endif; ?>
        </td>
      </tr>
    <?php endforeach; endif; ?>
    </tbody>
  </table>
</div></div>
<?php require __DIR__.'/../../app/Views/shared/layout_end.php'; ?>
