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

$user = requireAuth(['admin']);
$q    = trim($_GET['q'] ?? '');

$users = $vehicles = [];
if ($q !== '') {
    $like = '%'.$q.'%';

    // Users by name / email / phone
    $su = db()->prepare("SELECT id,name,email,phone,role FROM users
                         WHERE name LIKE ? OR email LIKE ? OR phone LIKE ?
                         ORDER BY name LIMIT 50");
    $su->execute([$like,$like,$like]);
    $users = $su->fetchAll();

    // Vehicles by plate / brand / model / owner name
    $sv = db()->prepare("SELECT v.id,v.plate_number,v.brand,v.model,v.year,
                                o.name AS owner_name, d.name AS driver_name
                         FROM vehicles v
                         LEFT JOIN users o ON o.id=v.owner_id
                         LEFT JOIN users d ON d.id=v.current_driver_id
                         WHERE v.plate_number LIKE ? OR v.brand LIKE ? OR v.model LIKE ? OR o.name LIKE ?
                         ORDER BY v.created_at DESC LIMIT 50");
    $sv->execute([$like,$like,$like,$like]);
    $vehicles = $sv->fetchAll();
}
$roleLabels = ['owner'=>t('owner_role'),'driver'=>t('driver_role'),'officer'=>t('officer_role'),'admin'=>t('admin_role')];

$pageTitle  = t('global_search');
$activePage = BASE_URL.'/admin/search.php';
require __DIR__.'/../../app/Views/shared/layout.php';
?>
<div class="page-header"><h2>🔎 <?= t('global_search') ?></h2></div>

<div class="card" style="margin-bottom:16px"><div class="card-body">
  <form method="GET" style="display:flex;gap:10px;align-items:end">
    <div class="form-group" style="flex:1;margin:0">
      <label class="form-label"><?= t('search') ?></label>
      <input type="text" name="q" class="form-control" value="<?= e($q) ?>" placeholder="<?= t('search_hint_admin') ?>" autofocus>
    </div>
    <button class="btn btn-primary">🔎 <?= t('search') ?></button>
  </form>
</div></div>

<?php if($q===''): ?>
  <div class="card"><div class="empty-state"><div class="empty-state-icon">🔎</div><p><?= t('search_hint_admin') ?></p></div></div>
<?php else: ?>

<!-- Vehicles results -->
<div class="card" style="margin-bottom:18px">
  <div class="card-header"><span class="card-title">🚗 <?= t('vehicles_results') ?> (<?= count($vehicles) ?>)</span></div>
  <div style="overflow-x:auto">
    <table style="width:100%;border-collapse:collapse">
      <thead><tr style="border-bottom:1px solid var(--border)">
        <th style="padding:10px"><?= t('plate_number') ?></th>
        <th style="padding:10px"><?= t('brand') ?>/<?= t('model') ?></th>
        <th style="padding:10px"><?= t('owner') ?></th>
        <th style="padding:10px"><?= t('current_driver') ?></th>
        <th style="padding:10px"></th>
      </tr></thead>
      <tbody>
      <?php if(empty($vehicles)): ?>
        <tr><td colspan="5" style="padding:18px;text-align:center;color:var(--text-muted)"><?= t('no_data') ?></td></tr>
      <?php else: foreach($vehicles as $v): ?>
        <tr style="border-bottom:1px solid var(--border)">
          <td style="padding:10px"><span class="vehicle-plate-badge"><?= e($v['plate_number']) ?></span></td>
          <td style="padding:10px;font-weight:600"><?= e($v['brand'].' '.$v['model']) ?></td>
          <td style="padding:10px"><?= e($v['owner_name'] ?: '—') ?></td>
          <td style="padding:10px"><?= e($v['driver_name'] ?: '—') ?></td>
          <td style="padding:10px;white-space:nowrap">
            <a href="<?= BASE_URL ?>/admin/documents.php" class="btn btn-sm btn-secondary">📄</a>
            <a href="<?= BASE_URL ?>/verify.php?plate=<?= urlencode($v['plate_number']) ?>" target="_blank" class="btn btn-sm btn-secondary">🔗</a>
          </td>
        </tr>
      <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>
</div>

<!-- Users results -->
<div class="card">
  <div class="card-header"><span class="card-title">👤 <?= t('users_results') ?> (<?= count($users) ?>)</span></div>
  <div style="overflow-x:auto">
    <table style="width:100%;border-collapse:collapse">
      <thead><tr style="border-bottom:1px solid var(--border)">
        <th style="padding:10px"><?= t('full_name') ?></th>
        <th style="padding:10px"><?= t('email') ?></th>
        <th style="padding:10px"><?= t('phone') ?></th>
        <th style="padding:10px"><?= t('role') ?></th>
        <th style="padding:10px"></th>
      </tr></thead>
      <tbody>
      <?php if(empty($users)): ?>
        <tr><td colspan="5" style="padding:18px;text-align:center;color:var(--text-muted)"><?= t('no_data') ?></td></tr>
      <?php else: foreach($users as $u): ?>
        <tr style="border-bottom:1px solid var(--border)">
          <td style="padding:10px;font-weight:600"><?= e($u['name']) ?></td>
          <td style="padding:10px"><?= e($u['email']) ?></td>
          <td style="padding:10px"><?= e($u['phone'] ?: '—') ?></td>
          <td style="padding:10px"><span class="badge badge-info"><?= e($roleLabels[$u['role']] ?? $u['role']) ?></span></td>
          <td style="padding:10px"><a href="<?= BASE_URL ?>/admin/users.php?q=<?= urlencode($u['email']) ?>" class="btn btn-sm btn-secondary">⚙️</a></td>
        </tr>
      <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php endif; ?>
<?php require __DIR__.'/../../app/Views/shared/layout_end.php'; ?>
