Case_accounting/CaseAccounting/CaseAccountingProviderView/wwwroot/js/deal/deal-bind.js

97 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-05-19 21:07:14 +04:00
const saveBtn = document.getElementById("save-button");
const tbody = document.getElementById("scrollable-table__tbody");
const currentDealId = document.getElementById("deal-data").dataset.id;
var contracts = [];
var dataArray = [];
var currentDeal = null;
window.addEventListener("load", async () => {
try {
await $.ajax({
url: `/deal/getallcontracts`,
type: "GET",
contentType: "json"
}).done((result) => {
contracts = result;
console.log(contracts);
contracts.forEach((contract) => {
const { id, service, coast, date } = contract;
const row = tbody.insertRow();
row.setAttribute("data-id", id);
const cells = [service, coast, date];
cells.forEach((value) => {
const cell = row.insertCell();
cell.textContent = value;
});
row.addEventListener('click', () => addAndRemoveFromList(row));
});
});
await $.ajax({
url: `/deal/get?id=${currentDealId}`,
type: "GET",
contentType: "json"
}).done((result) => {
currentDeal = result;
console.log(currentDeal)
});
} catch (error) {
console.error(error);
}
});
saveBtn.addEventListener("click", () => {
if (!correctData()) {
return;
}
if (!validate()) {
return;
}
});
const correctData = function () {
return true;
};
const validate = function () {
return true;
};
saveBtn.addEventListener("click", () => {
let dealModel = {
"Id": currentDeal.id,
"Subject": currentDeal.subject,
"Responsibilities": currentDeal.responsibilities,
"Date": currentDeal.date,
"ContractViewModels": dataArray
};
console.log(dealModel);
console.log(dataArray);
$.ajax({
url: "/deal/update",
type: "POST",
contentType: "application/json",
data: JSON.stringify(dealModel)
}).done(() => {
window.location.href = "/Home/Deals";
});
});
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-primary");
} else {
dataArray.splice(index, 1);
row.classList.remove("bg-primary");
}
console.log(dataArray);
}