2023-05-20 09:55:10 +04:00

115 lines
3.8 KiB
JavaScript

const updateBtn = document.getElementById("update-button");
const nameInput = document.getElementById("name-input");
const applicantInput = document.getElementById("applicant-input");
const defendantInput = document.getElementById("defendant-input");
const annotationInput = document.getElementById("annotation-input");
const dateInput = document.getElementById("date-input");
const specializationSelect = document.getElementById("specialization-select");
const caseId = document.getElementById("vb-id").dataset.id;
const errorP = document.getElementById("error-p");
const errorDivShell = document.getElementById("error-div-shell");
var specializations = [];
const formatDate = (dateString) => {
const date = new Date(dateString);
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const day = ('0' + date.getDate()).slice(-2);
return `${year}-${month}-${day}`;
};
window.addEventListener("load", async () => {
try {
await $.ajax({
url: `/case/get?id=${caseId}`,
type: "GET",
contentType: "json"
}).done((result) => {
nameInput.value = result.name;
applicantInput.value = result.applicant;
defendantInput.value = result.defendant;
annotationInput.value = result.annotation;
dateInput.value = formatDate(result.date);
});
await $.ajax({
url: `/case/getallspecializations`,
type: "GET",
contentType: "json"
}).done((result) => {
specializations = result;
specializations.forEach((specialization) => {
const option = document.createElement("option");
option.value = specialization.id;
option.innerHTML = specialization.name;
specializationSelect.appendChild(option);
specializationSelect.selectedIndex = -1;
});
});
} catch (error) {
console.error(error);
}
});
const correctData = function () {
return true;
};
const validate = function () {
if (nameInput.value === "") {
errorDivShell.style.gridTemplateRows = "1fr";
errorP.innerHTML = "Заполните поле 'Название дела'";
return false;
}
if (applicantInput.value === "") {
errorDivShell.style.gridTemplateRows = "1fr";
errorP.innerHTML = "Заполните поле 'Истец'";
return false;
}
if (defendantInput.value === "") {
errorDivShell.style.gridTemplateRows = "1fr";
errorP.innerHTML = "Заполните поле 'Ответчик'";
return false;
}
if (annotationInput.value === "") {
errorDivShell.style.gridTemplateRows = "1fr";
errorP.innerHTML = "Заполните поле 'Примечание'";
return false;
}
if (specializationSelect.value === '') {
errorDivShell.style.gridTemplateRows = "1fr";
errorP.innerHTML = "Выберите 'Специализацию'";
return false;
}
return true;
};
updateBtn.addEventListener("click", () => {
if (!correctData()) {
return;
}
if (!validate()) {
return;
}
let caseModel = {
"Id": parseInt(caseId),
"Name": nameInput.value,
"Applicant": applicantInput.value,
"Defendant": defendantInput.value,
"Date": dateInput.value,
"Annotation": annotationInput.value,
"SpecializationId": parseInt(specializationSelect.value),
};
console.log(caseModel)
$.ajax({
url: "/case/update",
type: "POST",
contentType: "application/json",
data: JSON.stringify(caseModel)
}).done(() => {
window.location.href = "/Home/Cases";
});
});