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

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

$linked  = Vehicle::byDriver($user['id']);
$license = Document::byUser($user['id'], 'license')[0] ?? null;
$licSt   = $license ? docStatus($license['expiry_date']) : null;

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

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

<div class="stats-grid">
  <div class="stat-card">
    <div class="stat-icon blue">🚗</div>
    <div><div class="stat-value"><?= count($linked) ?></div><div class="stat-label"><?= t('linked_vehicles') ?></div></div>
  </div>
  <div class="stat-card">
    <div class="stat-icon <?= $licSt==='valid'?'green':($licSt==='expired'?'red':'yellow') ?>">🪪</div>
    <div>
      <div class="stat-value" style="font-size:16px"><?= $licSt ? t('doc_'.str_replace('ing','',str_replace('expiring','expiring',$licSt))) : t('not_set') ?></div>
      <div class="stat-label"><?= t('my_license') ?></div>
    </div>
  </div>
</div>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px">
  <div class="card">
    <div class="card-header">
      <span class="card-title">👤 <?= t('my_profile') ?></span>
      <a href="<?= BASE_URL ?>/driver/profile.php" class="btn btn-sm btn-secondary"><?= t('edit') ?></a>
    </div>
    <div class="card-body">
      <div style="display:flex;align-items:center;gap:14px;margin-bottom:14px">
        <div class="sidebar-avatar" style="width:52px;height:52px;font-size:20px"><?= avatarInitials($user['name']) ?></div>
        <div>
          <div style="font-size:16px;font-weight:700"><?= e($user['name']) ?></div>
          <div style="color:var(--text-muted);font-size:13px"><?= e($user['email']) ?></div>
        </div>
      </div>
      <div class="verify-row" style="padding:8px 0;border:none">
        <span class="verify-key"><?= t('phone') ?></span>
        <span class="verify-val"><?= e($user['phone']??'—') ?></span>
      </div>
    </div>
  </div>

  <div class="card">
    <div class="card-header">
      <span class="card-title">🪪 <?= t('my_license') ?></span>
      <a href="<?= BASE_URL ?>/driver/license.php" class="btn btn-sm btn-secondary"><?= $license?t('edit'):t('upload_doc') ?></a>
    </div>
    <div class="card-body">
      <?php if($license): ?>
        <div style="display:flex;align-items:center;gap:14px">
          <div style="font-size:40px">🪪</div>
          <div>
            <div><?= t('expiry_date') ?>: <strong><?= e($license['expiry_date']) ?></strong></div>
            <div style="margin-top:8px"><?= statusBadge($licSt, lang()) ?></div>
          </div>
        </div>
      <?php else: ?>
        <div class="empty-state" style="padding:16px">
          <div class="empty-state-icon" style="font-size:36px">🪪</div>
          <p><?= t('no_documents') ?></p>
          <a href="<?= BASE_URL ?>/driver/license.php" class="btn btn-primary btn-sm" style="margin-top:8px">+ <?= t('upload_doc') ?></a>
        </div>
      <?php endif; ?>
    </div>
  </div>
</div>

<?php if(!empty($linked)): ?>
<div class="card" style="margin-top:20px">
  <div class="card-header">
    <span class="card-title">🚗 <?= t('linked_vehicles') ?></span>
    <a href="<?= BASE_URL ?>/driver/vehicles.php" class="btn btn-sm btn-secondary"><?= t('all') ?></a>
  </div>
  <?php foreach(array_slice($linked,0,3) as $v): ?>
  <div class="verify-row">
    <div>
      <div class="font-bold"><?= e($v['brand'].' '.$v['model']) ?></div>
      <span class="vehicle-plate-badge"><?= e($v['plate_number']) ?></span>
    </div>
    <div style="text-align:end">
      <?php if($v['current_driver_id'] == $user['id']): ?>
      <span class="badge badge-success">⭐ <?= t('current_driver_label') ?></span>
      <?php endif; ?>
      <div style="font-size:12px;color:var(--text-muted)"><?= e($v['owner_name']??'') ?></div>
    </div>
  </div>
  <?php endforeach; ?>
</div>
<?php endif; ?>

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