<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/access_guards.php';
auth_require_login();
$pdo = crm_pdo();
gp_require_staff_access($pdo);
$page_title = 'Vehicles';

$rows = $pdo->query("
    SELECT id, license_plate, brand, model, vehicle_type, active,
           insurance_expiry, kteo_expiry, next_service_date
    FROM vehicles ORDER BY license_plate ASC
")->fetchAll();
$total = count($rows);
$activeCount = count(array_filter($rows, fn($r) => (int)$r['active'] === 1));
$today = date('Y-m-d');
$soon = date('Y-m-d', strtotime('+30 days'));

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>Vehicles</h1>
    <span>Fleet management · <?php echo $activeCount; ?> active / <?php echo $total; ?> total</span>
  </div>
  <div class="page-header-actions">
    <a class="btn btn-primary btn-sm" href="edit.php">+ New Vehicle</a>
  </div>
</div>

<div class="card">
  <div class="list-toolbar">
    <div class="list-toolbar-left">
      <div class="list-search-wrap">
        <span class="search-icon">🔍</span>
        <input type="text" class="list-search-input" data-table-search="vehicles-table" placeholder="Search vehicles..." autocomplete="off">
      </div>
      <span class="list-search-count" data-search-count><?php echo $total; ?> total</span>
    </div>
  </div>
  <?php if (!$rows): ?>
    <div class="empty-state">
      <div class="empty-state-icon">🚛</div>
      <div class="empty-state-title">No vehicles yet</div>
      <div class="empty-state-desc">Click "+ New Vehicle" to add fleet vehicles</div>
    </div>
  <?php else: ?>
    <div class="table-responsive">
      <table class="table" id="vehicles-table">
        <thead><tr><th>Plate</th><th>Brand / Model</th><th>Type</th><th>Insurance</th><th>KTEO</th><th>Next Service</th><th>Status</th><th></th></tr></thead>
        <tbody>
          <?php foreach ($rows as $row):
            $insClass = '';
            if ($row['insurance_expiry'] && $row['insurance_expiry'] !== '0000-00-00') {
                $insClass = $row['insurance_expiry'] < $today ? 'badge-danger' : ($row['insurance_expiry'] <= $soon ? 'badge-warning' : '');
            }
            $kteoClass = '';
            if ($row['kteo_expiry'] && $row['kteo_expiry'] !== '0000-00-00') {
                $kteoClass = $row['kteo_expiry'] < $today ? 'badge-danger' : ($row['kteo_expiry'] <= $soon ? 'badge-warning' : '');
            }
          ?>
          <tr>
            <td><strong><?php echo h($row['license_plate']); ?></strong></td>
            <td><?php echo h(($row['brand'] ?? '') . ' ' . ($row['model'] ?? '')); ?></td>
            <td><?php echo h($row['vehicle_type']); ?></td>
            <td><?php if ($row['insurance_expiry'] && $row['insurance_expiry'] !== '0000-00-00'): ?><span class="badge <?php echo $insClass; ?>"><?php echo h(gp_date($row['insurance_expiry'])); ?></span><?php else: ?>-<?php endif; ?></td>
            <td><?php if ($row['kteo_expiry'] && $row['kteo_expiry'] !== '0000-00-00'): ?><span class="badge <?php echo $kteoClass; ?>"><?php echo h(gp_date($row['kteo_expiry'])); ?></span><?php else: ?>-<?php endif; ?></td>
            <td><?php echo ($row['next_service_date'] && $row['next_service_date'] !== '0000-00-00') ? h(gp_date($row['next_service_date'])) : '-'; ?></td>
            <td><span class="badge <?php echo (int)$row['active'] === 1 ? 'badge-success' : 'badge-muted'; ?>"><?php echo (int)$row['active'] === 1 ? 'Active' : 'Inactive'; ?></span></td>
            <td><a class="btn btn-sm btn-primary" href="edit.php?id=<?php echo (int)$row['id']; ?>" style="box-shadow:none;">Open</a></td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  <?php endif; ?>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
