<?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>Editare Mașină</h2>
  <form method="post" action="update_car.php?id=<?= $id ?>" enctype="multipart/form-data">
    <input name="marca" value="<?= $car['marca'] ?>" class="form-control mb-2" placeholder="Marca">
    <input name="model" value="<?= $car['model'] ?>" class="form-control mb-2" placeholder="Model">
    <input name="an" value="<?= $car['an'] ?>" type="number" class="form-control mb-2" placeholder="An">
    <input name="nr_inmatriculare" value="<?= $car['nr_inmatriculare'] ?>" class="form-control mb-2" placeholder="Nr. Înmatriculare">
    <input name="vin" value="<?= $car['vin'] ?>" class="form-control mb-2" placeholder="Serie VIN">
    <input name="pret" value="<?= number_format($car['pret'] / 5, 2) ?>" type="number" class="form-control mb-2" placeholder="Preț (€)">
    <select name="status" class="form-control mb-2">
      <option value="in stoc" <?= $car['status'] == 'in stoc' ? 'selected' : '' ?>>În stoc</option>
      <option value="vanduta" <?= $car['status'] == 'vanduta' ? 'selected' : '' ?>>Vândută</option>
      <option value="in curs" <?= $car['status'] == 'in curs' ? 'selected' : '' ?>>În curs de achiziție</option>
    </select>
    <textarea name="detalii" class="form-control mb-3" placeholder="Detalii"><?= $car['detalii'] ?></textarea>

    <label>Documente existente:</label>
    <ul class="list-group mb-3">
      <?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-secondary">Descarcă</a>
          <a href="delete_document.php?id=<?= $d['id'] ?>&car_id=<?= $id ?>" class="btn btn-sm btn-danger">Șterge</a>
        </div>
      </li>
      <?php endwhile; ?>
    </ul>

    <label>Adaugă documente noi:</label>
    <input type="file" name="documente[]" class="form-control mb-3" multiple accept=".pdf,.doc,.docx">

    <label>Poze existente:</label><br>
    <div class="row">
      <?php while($p = $photos->fetch_assoc()): ?>
      <div class="col-4 col-md-2 mb-3 text-center">
        <img src="uploads/<?= $p['filename'] ?>" class="img-fluid rounded mb-1" style="object-fit: cover; width: 100px; height: 100px;">
        <br>
        <a href="uploads/<?= $p['filename'] ?>" download class="btn btn-sm btn-outline-secondary">Descarcă</a>
        <a href="delete_photo.php?id=<?= $p['id'] ?>&car_id=<?= $id ?>" class="btn btn-sm btn-outline-danger mt-1">Șterge</a>
      </div>
      <?php endwhile; ?>
    </div>

    <label>Adaugă poze noi:</label>
    <input type="file" name="poze[]" class="form-control mb-4" multiple accept="image/*">

    <button type="submit" class="btn btn-success">Salvează</button>
    <a href="cars.php" class="btn btn-secondary">Anulează</a>
  </form>
</div>
</body>
</html>
