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

74 lines
2.3 KiB
JavaScript
Raw Normal View History

const createBtn = document.getElementById("create-button")
const tbody = document.getElementById("scrollable-table__tbody")
const nameInput = document.getElementById("name-input")
const surnameInput = document.getElementById("surname-input")
const patronymicInput = document.getElementById("patronymic-input")
const experienceInput = document.getElementById("experience-input")
var contracts = []
var dataArray = [];
window.addEventListener('load', () => {
$.ajax({
url: "/contract/getallbyuser",
type: "GET",
contentType: "json"
}).done((result) => {
contracts = result;
contracts.forEach((contract) => createRowForStudentsTable(contract));
});
})
createBtn.addEventListener('click', () => {
var lawyer = {
"Name": nameInput.value,
"Surname": surnameInput.value,
"Patronymic": patronymicInput.value,
"Experience": parseInt(experienceInput.value),
"LawyerContracts": dataArray,
}
$.ajax({
url: "/lawyer/create",
type: "POST",
contentType: "application/json",
data: JSON.stringify(lawyer)
}).done(() => {
window.location.href = "/Home/Lawyers";
});
})
const createRowForContractsTable = (contract) => {
const { id, service, coast, date } = contract;
const row = tbody.insertRow();
row.setAttribute("data-id", id);
const cells = [service, coast, formatDate(date)];
cells.forEach((value) => {
const cell = row.insertCell();
cell.textContent = value;
});
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);
console.log(contracts.find(x => x.id === id))
var index = dataArray.indexOf(contracts.find(x => x.id === id));
if (index === -1) {
dataArray.push(contracts.find(x => x.id === id));
row.classList.add("bg-success");
} else {
dataArray.splice(index, 1);
row.classList.remove("bg-success");
}
console.log(dataArray);
}