<?php
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../../app/Models/Vehicle.php';
require_once __DIR__.'/../../app/Models/Violation.php';
require_once __DIR__.'/../../app/Models/TowLog.php';

$user = requireAuth(['officer']);

// Log recent searches
$logs = [];
try {
    $s = db()->prepare('SELECT plate_number,searched_at FROM search_logs WHERE officer_id=? ORDER BY searched_at DESC LIMIT 8');
    $s->execute([$user['id']]); $logs = $s->fetchAll();
} catch(Exception $e) {}

// إحصائيات الضابط
$totalViolations = 0; $unpaidCount = 0; $totalTows = 0; $activeTows = 0;
try {
    $s = db()->query("SELECT COUNT(*) FROM violations WHERE officer_id={$user['id']}"); $totalViolations = (int)$s->fetchColumn();
    $s = db()->query("SELECT COUNT(*) FROM violations WHERE officer_id={$user['id']} AND status='unpaid'"); $unpaidCount = (int)$s->fetchColumn();
    $s = db()->query("SELECT COUNT(*) FROM tow_logs WHERE officer_id={$user['id']}"); $totalTows = (int)$s->fetchColumn();
    $s = db()->query("SELECT COUNT(*) FROM tow_logs WHERE officer_id={$user['id']} AND status='active'"); $activeTows = (int)$s->fetchColumn();
} catch(Exception $e) {}

$pageTitle  = t('dashboard');
$activePage = BASE_URL.'/officer/dashboard.php';
require __DIR__.'/../../app/Views/shared/layout.php';
?>

<div class="page-header">
  <h2><?= t('welcome') ?>, <?= e(explode(' ',$user['name'])[0]) ?> 👮</h2>
</div>

<!-- Search Hero -->
<div class="search-hero">
  <h2>🔍 <?= t('vehicle_verification') ?></h2>
  <p><?= t('enter_plate') ?></p>
  <form method="GET" action="<?= BASE_URL ?>/officer/search.php">
    <div class="search-input-group">
      <input type="text" name="plate" class="form-control" placeholder="16-ABC-01"
             style="text-transform:uppercase;font-size:18px;letter-spacing:.08em;text-align:center" required>
      <button type="submit" class="btn btn-primary btn-lg">🔍</button>
    </div>
  </form>
  <div style="margin-top:16px;display:flex;gap:10px;justify-content:center;flex-wrap:wrap">
    <a href="<?= BASE_URL ?>/officer/scan.php"      class="btn btn-secondary">📷 <?= t('scan_qr') ?></a>
    <a href="<?= BASE_URL ?>/officer/violation.php" class="btn btn-warning">📋 <?= t('new_violation') ?></a>
    <a href="<?= BASE_URL ?>/officer/tow.php"       class="btn btn-danger">🚛 <?= t('tow_vehicle') ?></a>
  </div>
</div>

<!-- Stats -->
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:14px;margin-bottom:24px">
  <?php
  $stats = [
    ['icon'=>'📋','label'=>t('violations_list'),'val'=>$totalViolations,'color'=>'var(--accent)'],
    ['icon'=>'⏳','label'=>t('unpaid'),'val'=>$unpaidCount,'color'=>'#f59e0b'],
    ['icon'=>'🚛','label'=>t('tow_records'),'val'=>$totalTows,'color'=>'#7c3aed'],
    ['icon'=>'🔴','label'=>t('tow_active'),'val'=>$activeTows,'color'=>'#dc2626'],
  ];
  foreach($stats as $st): ?>
  <div class="card" style="padding:18px;text-align:center;border-top:3px solid <?= $st['color'] ?>">
    <div style="font-size:26px;margin-bottom:4px"><?= $st['icon'] ?></div>
    <div style="font-size:28px;font-weight:900;color:<?= $st['color'] ?>"><?= $st['val'] ?></div>
    <div style="font-size:12px;color:var(--text-muted);margin-top:2px"><?= $st['label'] ?></div>
  </div>
  <?php endforeach; ?>
</div>

<!-- Recent searches -->
<?php if(!empty($logs)): ?>
<div class="card">
  <div class="card-header"><span class="card-title">🕐 <?= t('recent_searches') ?></span></div>
  <div class="table-wrapper">
    <table>
      <thead><tr><th><?= t('plate_number') ?></th><th><?= t('verify_time') ?></th><th><?= t('actions') ?></th></tr></thead>
      <tbody>
        <?php foreach($logs as $l): ?>
        <tr>
          <td><a href="<?= BASE_URL ?>/officer/search.php?plate=<?= urlencode($l['plate_number']) ?>" style="font-family:monospace;font-weight:700"><?= e($l['plate_number']) ?></a></td>
          <td style="color:var(--text-muted);font-size:13px"><?= e($l['searched_at']) ?></td>
          <td>
            <a href="<?= BASE_URL ?>/officer/violation.php?plate=<?= urlencode($l['plate_number']) ?>" class="btn btn-sm btn-warning">📋</a>
            <a href="<?= BASE_URL ?>/officer/tow.php?plate=<?= urlencode($l['plate_number']) ?>" class="btn btn-sm btn-danger">🚛</a>
          </td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<?php endif; ?>

<?php require __DIR__.'/../../app/Views/shared/layout_end.php'; ?>
