<?php
include 'includes/db.php';
include 'includes/header.php';

$view = $_GET['view'] ?? 'table';
$masini = $conn->query("SELECT * FROM cars WHERE status != 'sters' ORDER BY id DESC");
?>

<div class="container mt-4">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h2>Lista Mașinilor</h2>
    <form method="get">
      <select name="view" class="form-select" onchange="this.form.submit()">
        <option value="table" <?= $view === 'table' ? 'selected' : '' ?>>Tabel</option>
        <option value="grid" <?= $view === 'grid' ? 'selected' : '' ?>>Carduri</option>
      </select>
    </form>
  </div>

  <?php if ($view === 'table'): ?>
  <table class="table table-bordered table-striped align-middle text-center">
    <thead class="table-dark">
      <tr>
        <th>Thumbnail</th>
        <th>Marcă</th>
        <th>Model</th>
        <th>An</th>
        <th>Preț (€)</th>
        <th>Status</th>
        <th>Acțiuni</th>
      </tr>
    </thead>
    <tbody>
      <?php while($car = $masini->fetch_assoc()): 
        $car_id = $car['id'];
        $photo = $conn->query("SELECT filename FROM car_photos WHERE car_id = $car_id LIMIT 1")->fetch_assoc();
        $thumb = $photo ? 'uploads/' . $photo['filename'] : 'https://via.placeholder.com/100';
      ?>
      <tr>
        <td><img src="<?= $thumb ?>" style="width: 100px; height: 100px; object-fit: cover;" class="rounded shadow-sm"></td>
        <td><?= htmlspecialchars($car['marca']) ?></td>
        <td><?= htmlspecialchars($car['model']) ?></td>
        <td><?= $car['an'] ?></td>
        <td><?= number_format($car['pret'] / 5, 2) ?></td>
        <td><?= $car['status'] ?></td>
        <td>
          <a href="car_details.php?id=<?= $car_id ?>" class="btn btn-sm btn-info">Detalii</a>
          <a href="edit_car.php?id=<?= $car_id ?>" class="btn btn-sm btn-primary">Editează</a>
          <a href="delete_car.php?id=<?= $car_id ?>" class="btn btn-sm btn-danger" onclick="return confirm('Ștergi această mașină?')">Șterge</a>
        </td>
      </tr>
      <?php endwhile; ?>
    </tbody>
  </table>

  <?php else: ?>
  <div class="row">
    <?php mysqli_data_seek($masini, 0); while($car = $masini->fetch_assoc()): 
      $car_id = $car['id'];
      $photo = $conn->query("SELECT filename FROM car_photos WHERE car_id = $car_id LIMIT 1")->fetch_assoc();
      $thumb = $photo ? 'uploads/' . $photo['filename'] : 'https://via.placeholder.com/300';
    ?>
    <div class="col-12 col-sm-6 col-md-4 col-lg-3 mb-4">
      <div class="card h-100 shadow-sm">
        <img src="<?= $thumb ?>" class="card-img-top" style="object-fit: cover; width: 100%; height: 180px;">
        <div class="card-body">
          <h5 class="card-title"><?= htmlspecialchars($car['marca']) . ' ' . htmlspecialchars($car['model']) ?></h5>
          <p class="card-text mb-1"><strong>VIN:</strong> <?= htmlspecialchars($car['vin']) ?></p>
          <p class="card-text"><strong>Status:</strong> <?= $car['status'] ?></p>
        </div>
        <div class="card-footer d-flex justify-content-between">
          <a href="car_details.php?id=<?= $car_id ?>" class="btn btn-sm btn-outline-info">Detalii</a>
          <a href="edit_car.php?id=<?= $car_id ?>" class="btn btn-sm btn-outline-primary">Editează</a>
          <a href="delete_car.php?id=<?= $car_id ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Ștergi această mașină?')">Șterge</a>
        </div>
      </div>
    </div>
    <?php endwhile; ?>
  </div>
  <?php endif; ?>
</div>
</body>
</html>
