Case_accounting/CaseAccounting/CaseAccountingCustomerView/wwwroot/js/Lawyers/lawyer-bind.js

86 lines
2.6 KiB
JavaScript

const createBtn = document.getElementById("create-button");
const tbody = document.getElementById("scrollable-table__tbody");
const nameInput = document.getElementById("name-input");
const currentLawyerId = document.getElementById("lawyer-data").dataset.id;
var cases = [];
var dataArray = [];
var currentLawyer = null;
window.addEventListener('load', async () => {
await $.ajax({
url: "/lawyer/getallcases",
type: "GET",
contentType: "json"
}).done((result) => {
cases = result;
console.log(cases)
});
await $.ajax({
url: `/lawyer/get?id=${currentLawyerId}`,
type: "GET",
contentType: "json"
}).done((result) => {
currentLawyer = result;
console.log(currentLawyer)
});
groups.forEach((case) => createRowForCasesTable(case));
})
createBtn.addEventListener('click', () => {
var lawyerCasesUpdate = {
"Id": currentLawyer.id,
"Name": currentLawyer.name,
"Surname": currentLawyer.Surname,
"Patronymic": currentLawyer.Patronymic,
"SpecializationId": currentLawyer.SpecializationId,
"ContractViewModels": currentLawyer.lawyerContracts,
"LawyerCases": dataArray,
}
$.ajax({
url: "/lawyer/update",
type: "POST",
contentType: "application/json",
data: JSON.stringify(lawyerCasesUpdate)
}).done(() => {
window.location.href = "/Home/Lawyers";
});
})
const createRowForCasesTable = (case) => {
const { id, name, applicant, defendant, annotation, date, specializationId } = case;
const row = tbody.insertRow();
row.setAttribute("data-id", id);
const cells = [name];
cells.forEach((value) => {
const cell = row.insertCell();
cell.textContent = value;
});
console.log(currentLawyer)
if (currentLawyer.lawyerCases?.find(x => parseInt(x.id) === parseInt(case.id))) {
row.classList.add("bg-success");
dataArray.push(case);
}
row.addEventListener('click', () => addAndRemoveFromList(row));
};
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}`;
};
const addAndRemoveFromList = (row) => {
var id = parseInt(row.dataset.id);
var index = dataArray.indexOf(cases.find(x => x.id === id));
if (index === -1) {
dataArray.push(cases.find(x => x.id === id));
row.classList.add("bg-success");
} else {
dataArray.splice(index, 1);
row.classList.remove("bg-success");
}
}