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/getallgroups", type: "GET", contentType: "json" }).done((result) => { groups = result; console.log(groups) }); await $.ajax({ url: `/document/get?id=${currentDocumentId}`, type: "GET", contentType: "json" }).done((result) => { currentDocument = result; console.log(currentDocument) }); groups.forEach((group) => createRowForGroupsTable(group)); }) createBtn.addEventListener('click', () => { var documentGroupsUpdate = { "Id": currentDocument.id, "Name": currentDocument.name, "Date": currentDocument.date, "DocumentStudents": currentDocument.documentStudents, "DocumentGroups": dataArray, } $.ajax({ url: "/document/update", type: "POST", contentType: "application/json", data: JSON.stringify(documentGroupsUpdate) }).done(() => { window.location.href = "/Home/Documents"; }); }) const createRowForGroupsTable = (group) => { const { id, name } = group; const row = tbody.insertRow(); row.setAttribute("data-id", id); const cells = [name]; cells.forEach((value) => { const cell = row.insertCell(); cell.textContent = value; }); console.log(currentDocument) if (currentDocument.documentEdGroups?.find(x => parseInt(x.id) === parseInt(group.id))) { row.classList.add("bg-success"); dataArray.push(group); } 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(groups.find(x => x.id === id)); if (index === -1) { dataArray.push(groups.find(x => x.id === id)); row.classList.add("bg-success"); } else { dataArray.splice(index, 1); row.classList.remove("bg-success"); } }