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

$id = intval($_GET['id']);
$result = $conn->query("SELECT * FROM cars WHERE id = $id LIMIT 1");
if ($result->num_rows === 0) {
  echo "<div class='container mt-5'><div class='alert alert-danger'>Mașina nu a fost găsită.</div></div>";
  exit;
}
$car = $result->fetch_assoc();
$photos = $conn->query("SELECT * FROM car_photos WHERE car_id = $id");
$docs = $conn->query("SELECT * FROM documents WHERE car_id = $id");
?>

<div class="container mt-4">
  <h2>Detalii Mașină</h2>
  <ul class="list-group mb-3">
    <li class="list-group-item"><strong>Marcă:</strong> <?= htmlspecialchars($car['marca']) ?></li>
    <li class="list-group-item"><strong>Model:</strong> <?= htmlspecialchars($car['model']) ?></li>
    <li class="list-group-item"><strong>An fabricație:</strong> <?= $car['an'] ?></li>
    <li class="list-group-item"><strong>Nr. Înmatriculare:</strong> <?= htmlspecialchars($car['nr_inmatriculare']) ?></li>
    <li class="list-group-item"><strong>Serie VIN:</strong> <?= htmlspecialchars($car['vin']) ?></li>
    <li class="list-group-item"><strong>Preț estimat (€):</strong> <?= number_format($car['pret'] / 5, 2) ?></li>
    <li class="list-group-item"><strong>Status:</strong> <?= htmlspecialchars($car['status']) ?></li>
    <li class="list-group-item"><strong>Detalii:</strong> <?= nl2br(htmlspecialchars($car['detalii'])) ?></li>
  </ul>

  <h5>Galerie Foto</h5>
  <div class="row mb-4">
    <?php while($p = $photos->fetch_assoc()): ?>
      <div class="col-6 col-md-3 mb-3 text-center">
        <img src="uploads/<?= $p['filename'] ?>" class="img-fluid rounded shadow-sm" style="object-fit:cover; width:100%; height:150px;">
        <a href="uploads/<?= $p['filename'] ?>" target="_blank" class="btn btn-sm btn-outline-primary mt-2">Deschide</a>
        <a href="uploads/<?= $p['filename'] ?>" download class="btn btn-sm btn-outline-secondary mt-1">Descarcă</a>
      </div>
    <?php endwhile; ?>
  </div>

  <h5>Documente</h5>
  <ul class="list-group mb-5">
    <?php while($d = $docs->fetch_assoc()): ?>
    <li class="list-group-item d-flex justify-content-between align-items-center">
      <?= htmlspecialchars($d['titlu']) ?>
      <div>
        <a href="uploads/<?= $d['fisier'] ?>" target="_blank" class="btn btn-sm btn-primary">Deschide</a>
        <a href="uploads/<?= $d['fisier'] ?>" download class="btn btn-sm btn-secondary">Descarcă</a>
      </div>
    </li>
    <?php endwhile; ?>
  </ul>

  <a href="cars.php" class="btn btn-secondary">Înapoi la listă</a>
</div>
</body>
</html>
