<?php
require_once "connection.php";

if (isset($_GET['namaMerk']) && isset($_GET['namaJenis']) && isset($_GET['namaTipe'])) {
    $namaMerk = mysqli_real_escape_string($con, $_GET['namaMerk']);
    $namaJenis = mysqli_real_escape_string($con, $_GET['namaJenis']);
    $namaTipe = mysqli_real_escape_string($con, $_GET['namaTipe']);

    $sql = "SELECT tahun 
            FROM tblTahunKendaraan 
            WHERE idMerk IN (
                    SELECT idMerk FROM tblMerkKendaraan WHERE namaMerk = '$namaMerk'
            )
            AND idJenis IN (
                    SELECT idJenis FROM tblJenisKendaraan WHERE namaJenis = '$namaJenis'
            )
            AND idTipe IN (
                    SELECT idTipe FROM tblTipeKendaraan WHERE namaTipe = '$namaTipe'
            )";

    $result = $con->query($sql);

    if ($result && $result->num_rows > 0) {
        $return_arr['tahun_kendaraan'] = array();
        while ($row = $result->fetch_assoc()) {
            $return_arr['tahun_kendaraan'][] = array(
                "tahun" => $row['tahun']
            );
        }
        echo json_encode($return_arr);
    } else {
        echo json_encode(array("tahun_kendaraan" => []));
    }
} else {
    echo json_encode(array("error" => "Parameter tidak lengkap"));
}
?>
