class auto {
    constructor(rendszam) {
        this.rendszam = rendszam;
        this.amount = [];
        this.date = [];
    }

    addkm(amount, date) {
        this.amount.push(amount);
        this.date.push(date);
    }

    getrendszam() {
        return this.rendszam;
    }

    getamount() {
        return this.amount;
    }
}

var autok = [];

document.addEventListener("DOMContentLoaded", function () {
    document.getElementById("former").addEventListener("submit", function (event) {
        event.preventDefault();

        const plate = document.getElementById("plate").value;
        const date = document.getElementById("date").value;
        const amount = document.getElementById("amount").value;

        console.log("Plate:", plate);
        console.log("Date:", date);
        console.log("Amount:", amount);

        let found = -1;
        const valaszlista = document.getElementById("valaszlista");
        valaszlista.innerHTML = "";

        for (let i = 0; i < autok.length; i++) {
            if (autok[i].getrendszam() === plate) {
                found = i;
                break;
            }
            valaszlista.innerHTML += `<option>${autok[i].getrendszam()}</option>`;
        }

        if (found === -1) {
            const a = new auto(plate);
            a.addkm(amount, date);
            autok.push(a);
            found = autok.length - 1;
        } else {
            autok[found].addkm(amount, date);
        }

        const korabbiallasok = document.getElementById("korabbiallasok");
        korabbiallasok.innerHTML = "";

        for (let i = 0; i < autok[found].getamount().length; i++) {
            korabbiallasok.innerHTML += `<li>${autok[found].getamount()[i]}</li>`;
        }
        for (let i = 0; i < autok.length; i++) {
            valaszlista.innerHTML += `<option>${autok[i].getrendszam()}</option>`;
        }

    });
});
