<?php
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../../app/Models/Vehicle.php';
require_once __DIR__.'/../../app/Models/Document.php';
$user     = requireAuth(['driver']);
$vehicles = Vehicle::byDriver($user['id']);
$pageTitle=t('linked_vehicles'); $activePage=BASE_URL.'/driver/vehicles.php';
require __DIR__.'/../../app/Views/shared/layout.php';
?>
<?php if(empty($vehicles)): ?>
<div class="card"><div class="empty-state">
  <div class="empty-state-icon">🚗</div>
  <h3><?= t('no_vehicles') ?></h3>
  <p><?= lang()==='ar'?'تواصل مع مالك السيارة ليضيفك كسائق':'Contact the vehicle owner to be added as a driver' ?></p>
</div></div>
<?php else: ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:18px">
  <?php foreach($vehicles as $v):
    $docs = Document::byVehicle($v['id']);
    $isCur = $v['current_driver_id'] == $user['id'];
  ?>
  <div class="card">
    <div style="background:linear-gradient(135deg,var(--accent),#0f4c9e);padding:18px;color:#fff">
      <div style="display:flex;justify-content:space-between;align-items:flex-start">
        <div>
          <div style="font-size:18px;font-weight:800"><?= e($v['brand'].' '.$v['model']) ?></div>
          <div style="opacity:.7;font-size:13px"><?= e($v['year']??'') ?> <?= e($v['color']??'') ?></div>
        </div>
        <div style="background:rgba(255,255,255,.2);padding:5px 12px;border-radius:4px;font-family:monospace;font-weight:800">
          <?= e($v['plate_number']) ?>
        </div>
      </div>
      <?php if($isCur): ?>
      <div style="margin-top:10px;background:rgba(255,255,255,.15);padding:4px 10px;border-radius:4px;font-size:12px;display:inline-block">
        ⭐ <?= t('current_driver_label') ?>
      </div>
      <?php endif; ?>
    </div>
    <div class="card-body" style="padding:14px">
      <div style="font-size:12px;color:var(--text-muted);margin-bottom:8px"><?= t('owner') ?>: <strong><?= e($v['owner_name']??'—') ?></strong></div>
      <div style="display:flex;gap:8px;flex-wrap:wrap">
        <?php foreach(['insurance'=>'🛡️','gray_card'=>'📋','technical'=>'🔧'] as $tp=>$ic):
          $doc = null;
          foreach($docs as $d) if($d['type']===$tp) { $doc=$d; break; }
          $st = $doc ? docStatus($doc['expiry_date']) : null;
          $clr = $st==='valid'?'var(--success)':($st==='expired'?'var(--danger)':'var(--text-muted)');
        ?>
        <span style="font-size:12px;color:<?= $clr ?>"><?= $ic ?> <?= substr(t('doc_'.str_replace('_','',$tp)),0,8) ?></span>
        <?php endforeach; ?>
      </div>
    </div>
  </div>
  <?php endforeach; ?>
</div>
<?php endif; ?>
<?php require __DIR__.'/../../app/Views/shared/layout_end.php'; ?>
