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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $marca = $_POST['marca'];
    $model = $_POST['model'];
    $an = $_POST['an'];
    $nr_inmatriculare = $_POST['nr_inmatriculare'];
    $vin = $_POST['vin'];
    $pret_euro = $_POST['pret'];
    $status = $_POST['status'];
    $detalii = $_POST['detalii'];
    $pret = $pret_euro * 5.0;

    $stmt = $conn->prepare("INSERT INTO cars (marca, model, an, nr_inmatriculare, pret, status, detalii, vin, poza) VALUES (?, ?, ?, ?, ?, ?, ?, ?, '')");
    $stmt->bind_param("ssisdsss", $marca, $model, $an, $nr_inmatriculare, $pret, $status, $detalii, $vin);
    $stmt->execute();
    $car_id = $stmt->insert_id;

    $nr_doc = count($_FILES['documente']['name']);
    for ($i = 0; $i < $nr_doc; $i++) {
        $doc_nume = $_FILES['documente']['name'][$i];
        $doc_tmp = $_FILES['documente']['tmp_name'][$i];
        $doc_dest = "uploads/" . basename($doc_nume);
        if (move_uploaded_file($doc_tmp, $doc_dest)) {
            $titlu = pathinfo($doc_nume, PATHINFO_FILENAME);
            $stmt_doc = $conn->prepare("INSERT INTO documents (car_id, titlu, fisier) VALUES (?, ?, ?)");
            $stmt_doc->bind_param("iss", $car_id, $titlu, $doc_nume);
            $stmt_doc->execute();
        }
    }

    if (!empty($_FILES['poze']['name'][0])) {
        foreach ($_FILES['poze']['name'] as $i => $poza_nume) {
            $poza_tmp = $_FILES['poze']['tmp_name'][$i];
            $poza_dest = "uploads/" . basename($poza_nume);
            if (move_uploaded_file($poza_tmp, $poza_dest)) {
                $stmt_photo = $conn->prepare("INSERT INTO car_photos (car_id, filename) VALUES (?, ?)");
                $stmt_photo->bind_param("is", $car_id, $poza_nume);
                $stmt_photo->execute();
            }
        }
    }

    header("Location: cars.php");
    exit();
}
?>

<div class="container mt-5">
    <h2>Adaugă Mașină</h2>
    <form method="post" enctype="multipart/form-data">
        <input name="marca" class="form-control mb-2" placeholder="Marca" required>
        <input name="model" class="form-control mb-2" placeholder="Model" required>
        <input name="an" type="number" class="form-control mb-2" placeholder="An fabricație" required>
        <input name="nr_inmatriculare" class="form-control mb-2" placeholder="Nr. Înmatriculare" required>
        <input name="vin" class="form-control mb-2" placeholder="Serie șasiu (VIN)" required>
        <input name="pret" type="number" step="0.01" class="form-control mb-2" placeholder="Preț (€)" required>
        <select name="status" class="form-control mb-2">
            <option value="in stoc">În stoc</option>
            <option value="vanduta">Vândută</option>
            <option value="in curs">În curs de achiziție</option>
        </select>
        <textarea name="detalii" class="form-control mb-2" placeholder="Detalii..."></textarea>
        <label class="form-label">Documente:</label>
        <input type="file" name="documente[]" class="form-control mb-3" accept=".pdf,.doc,.docx" multiple required>
        <label class="form-label">Poze:</label>
        <input type="file" name="poze[]" class="form-control mb-4" accept="image/*" multiple required>
        <button type="submit" class="btn btn-success">Adaugă Mașină</button>
    </form>
</div>
</body>
</html>
