70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
document.addEventListener("DOMContentLoaded", function () {
|
|
const yearDropdown = document.getElementById("year");
|
|
const errorDiv = document.getElementById("yearError");
|
|
const form = document.querySelector("form");
|
|
|
|
const tableName = form.id.toLowerCase(); // ao, cit, etc.
|
|
|
|
const currentYear = new Date().getFullYear();
|
|
const startYear = 1990;
|
|
|
|
/* ---------- Fill Year Dropdown ---------- */
|
|
for (let y = currentYear; y >= startYear; y--) {
|
|
let nextYear = y + 1;
|
|
|
|
let option = document.createElement("option");
|
|
option.value = y; // IMPORTANT
|
|
option.textContent = `AY ${y}-${nextYear}`;
|
|
|
|
yearDropdown.appendChild(option);
|
|
}
|
|
|
|
/* ---------- Validate on Change ---------- */
|
|
yearDropdown.addEventListener("change", function () {
|
|
const selectedYear = this.value;
|
|
|
|
// If empty → block submit
|
|
if (!selectedYear) {
|
|
errorDiv.style.display = "block";
|
|
errorDiv.innerText = "Please select an Assessment Year.";
|
|
form.onsubmit = () => false;
|
|
return;
|
|
}
|
|
|
|
fetch("/check_year", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
table: tableName,
|
|
year: selectedYear
|
|
})
|
|
})
|
|
.then(res => res.json())
|
|
.then(data => {
|
|
if (data.exists) {
|
|
errorDiv.style.display = "block";
|
|
errorDiv.innerText = `AY ${selectedYear}-${parseInt(selectedYear) + 1} already exists!`;
|
|
form.onsubmit = () => false;
|
|
} else {
|
|
errorDiv.style.display = "none";
|
|
errorDiv.innerText = "";
|
|
form.onsubmit = null; // allow submit
|
|
}
|
|
})
|
|
.catch(() => {
|
|
errorDiv.style.display = "block";
|
|
errorDiv.innerText = "Error validating year. Please try again.";
|
|
form.onsubmit = () => false;
|
|
});
|
|
});
|
|
|
|
/* ---------- Final Safety Check on Submit ---------- */
|
|
form.addEventListener("submit", function (e) {
|
|
if (!yearDropdown.value) {
|
|
e.preventDefault();
|
|
errorDiv.style.display = "block";
|
|
errorDiv.innerText = "Assessment Year is required.";
|
|
}
|
|
});
|
|
});
|