function getSpecific(kljuc) {
    window.location.href = `/vnos.html?kljuc=${kljuc}`;
}

function getLast() {
    const url = `https://ssnj.dcrubro.com/api/vnosi`;
    fetch(url, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            query: "*",
            limit: 4
        })
    })
    .then(response => response.json())
    .then(data => {
        if (!data.success) {
            console.error("API Error:", data.message);
            return;
        }

        for (let entry of data.data) {
            let build = `
            <div class="entry-card" onclick="getSpecific('${entry.PK}')">
                <h3 class="entry-title">${entry.kljuc}</h3>
                <p class="entry-definition">${entry.tip}</p>
                ${entry.vsebina}
                <p class="entry-example">${entry.primer}</p>
            </div>
            `;

            document.getElementById("entry-container").innerHTML += build;
        }
    })
    .catch((error) => {
        console.error("Error:", error);
    });
}

function search() {
    const field = document.getElementById("search-input");
    const val = field.value;
    if (!val || val === "" || val === "*") { return; }

    const url = `https://ssnj.dcrubro.com/api/vnosi`;
    fetch(url, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            query: `*${val}*`, // Potencialno mora biti tukaj en "*" wildcard
            limit: -1,
            sort: true
        })
    })
    .then(response => response.json())
    .then(data => {
        if (!data.success) {
            console.error("API Error:", data.message);
            return;
        }

        console.log(data.data);
        let len = data.data.length;
        document.getElementById("page-title").innerText = `Rezultati iskanja (${len}):`;
        document.getElementById("entry-container").innerHTML = "";
       
        if (len <= 0) {
            document.getElementById("entry-container").innerHTML = `
                <p class="page-paragraph">Ni bilo najednih zadetkov za poizvedbo "${val}".</p>
            `;
            return;
        }

        for (let entry of data.data) {
            let build = `
            <div class="entry-card" onclick="getSpecific('${entry.PK}')">
                <h3 class="entry-title">${entry.kljuc}</h3>
                <p class="entry-definition">${entry.tip}</p>
                ${entry.vsebina}
                <p class="entry-example">${entry.primer}</p>
            </div>
            `;

            document.getElementById("entry-container").innerHTML += build;
        }       
    })
    .catch((error) => {
        console.error("Error:", error);
    });
}

document.getElementById("search-input").addEventListener("keydown", (event) => {
    if (event.key === "Enter") {
        event.preventDefault();
        search();
    }
});

function addEntry() {
    window.location.href = "/new.html";
}

// DELAJ DELAJ
getLast();