/* eslint-disable unicorn/prefer-module */
import type * as globalTypes from "../types/globalTypes";
import type * as recordTypes from "../types/recordTypes";
import type {
cityssmGlobal
} from "@cityssm/bulma-webapp-js/src/types";
import type {
BulmaJS,
StringConfigProperties
} from "@cityssm/bulma-js/types";
declare const cityssm: cityssmGlobal;
declare const bulmaJS: BulmaJS;
(() => {
const los = (exports.los as globalTypes.LOS);
const urlPrefix = document.querySelector("main").dataset.urlPrefix;
const lotOccupancyId = (document.querySelector("#lotOccupancy--lotOccupancyId") as HTMLInputElement).value;
const isCreate = (lotOccupancyId === "");
/*
* Main form
*/
let hasUnsavedChanges = false;
let refreshAfterSave = isCreate;
const setUnsavedChanges = () => {
if (!hasUnsavedChanges) {
hasUnsavedChanges = true;
cityssm.enableNavBlocker();
}
};
const clearUnsavedChanges = () => {
hasUnsavedChanges = false;
cityssm.disableNavBlocker();
};
const formElement = document.querySelector("#form--lotOccupancy") as HTMLFormElement;
formElement.addEventListener("submit", (formEvent) => {
formEvent.preventDefault();
cityssm.postJSON(urlPrefix + "/lotOccupancies/" + (isCreate ? "doCreateLotOccupancy" : "doUpdateLotOccupancy"),
formElement,
(responseJSON: {
success: boolean;
lotOccupancyId ? : number;
errorMessage ? : string;
}) => {
if (responseJSON.success) {
clearUnsavedChanges();
if (isCreate || refreshAfterSave) {
window.location.href = urlPrefix + "/lotOccupancies/" + responseJSON.lotOccupancyId + "/edit?t=" + Date.now();
} else {
bulmaJS.alert({
message: exports.aliases.occupancy + " Updated Successfully",
contextualColorName: "success"
});
}
} else {
bulmaJS.alert({
title: "Error Saving " + exports.aliases.occupancy,
message: responseJSON.errorMessage,
contextualColorName: "danger"
});
}
});
});
const formInputElements = formElement.querySelectorAll("input, select");
for (const formInputElement of formInputElements) {
formInputElement.addEventListener("change", setUnsavedChanges);
}
// Occupancy Type
const occupancyTypeIdElement = document.querySelector("#lotOccupancy--occupancyTypeId") as HTMLSelectElement;
if (isCreate) {
const lotOccupancyFieldsContainerElement = document.querySelector("#container--lotOccupancyFields") as HTMLElement;
occupancyTypeIdElement.addEventListener("change", () => {
if (occupancyTypeIdElement.value === "") {
lotOccupancyFieldsContainerElement.innerHTML = "
" +
"
Select the " + exports.aliases.occupancy.toLowerCase() + " type to load the available fields.
" +
"
";
return
}
cityssm.postJSON(urlPrefix + "/lotOccupancies/doGetOccupancyTypeFields", {
occupancyTypeId: occupancyTypeIdElement.value
},
(responseJSON: {
occupancyTypeFields: recordTypes.OccupancyTypeField[]
}) => {
if (responseJSON.occupancyTypeFields.length === 0) {
lotOccupancyFieldsContainerElement.innerHTML = "" +
"
There are no additional fields for this " + exports.aliases.occupancy.toLowerCase() + " type.
" +
"
";
return
}
lotOccupancyFieldsContainerElement.innerHTML = "";
let occupancyTypeFieldIds = "";
for (const occupancyTypeField of responseJSON.occupancyTypeFields) {
occupancyTypeFieldIds += "," + occupancyTypeField.occupancyTypeFieldId;
const fieldElement = document.createElement("div");
fieldElement.className = "field";
fieldElement.innerHTML = "" +
"";
fieldElement.querySelector("label").textContent = occupancyTypeField.occupancyTypeField;
const inputElement = document.createElement("input");
inputElement.className = "input";
inputElement.id = "lotOccupancy--lotOccupancyFieldValue_" + occupancyTypeField.occupancyTypeFieldId;
inputElement.name = "lotOccupancyFieldValue_" + occupancyTypeField.occupancyTypeFieldId;
inputElement.type = "text";
inputElement.required = occupancyTypeField.isRequired;
inputElement.minLength = occupancyTypeField.minimumLength;
inputElement.maxLength = occupancyTypeField.maximumLength;
if (occupancyTypeField.pattern && occupancyTypeField.pattern !== "") {
inputElement.pattern = occupancyTypeField.pattern;
}
fieldElement.querySelector(".control").append(inputElement);
lotOccupancyFieldsContainerElement.append(fieldElement);
}
lotOccupancyFieldsContainerElement.insertAdjacentHTML("beforeend",
"");
});
});
} else {
const originalOccupancyTypeId = occupancyTypeIdElement.value;
occupancyTypeIdElement.addEventListener("change", () => {
if (occupancyTypeIdElement.value !== originalOccupancyTypeId) {
bulmaJS.confirm({
title: "Confirm Change",
message: "Are you sure you want to change the " + exports.aliases.occupancy.toLowerCase() + " type?\n" +
"This change affects the additional fields associated with this record, and may also affect the available fees.",
contextualColorName: "warning",
okButton: {
text: "Yes, Keep the Change",
callbackFunction: () => {
refreshAfterSave = true;
}
},
cancelButton: {
text: "Revert the Change",
callbackFunction: () => {
occupancyTypeIdElement.value = originalOccupancyTypeId;
}
}
});
}
});
}
// Lot Selector
document.querySelector("#lotOccupancy--lotName").addEventListener("click", (clickEvent) => {
const currentLotName = (clickEvent.currentTarget as HTMLInputElement).value;
let lotSelectCloseModalFunction: () => void;
let lotSelectFormElement: HTMLFormElement;
let lotSelectResultsElement: HTMLElement;
const selectLot = (clickEvent: Event) => {
clickEvent.preventDefault();
const selectedLotElement = clickEvent.currentTarget as HTMLElement;
(document.querySelector("#lotOccupancy--lotId") as HTMLInputElement).value = selectedLotElement.dataset.lotId;
(document.querySelector("#lotOccupancy--lotName") as HTMLInputElement).value = selectedLotElement.dataset.lotName;
setUnsavedChanges();
lotSelectCloseModalFunction();
};
const searchLots = () => {
lotSelectResultsElement.innerHTML = "" +
"
" +
"Searching..." +
"
";
cityssm.postJSON(urlPrefix + "/lots/doSearchLots", lotSelectFormElement, (responseJSON: {
count: number;
lots: recordTypes.Lot[];
}) => {
if (responseJSON.count === 0) {
lotSelectResultsElement.innerHTML = "" +
"
" +
"No results." +
"
" +
"
";
return;
}
const panelElement = document.createElement("div");
panelElement.className = "panel";
for (const lot of responseJSON.lots) {
const panelBlockElement = document.createElement("a");
panelBlockElement.className = "panel-block is-block";
panelBlockElement.href = "#";
panelBlockElement.dataset.lotId = lot.lotId.toString();
panelBlockElement.dataset.lotName = lot.lotName;
panelBlockElement.innerHTML = "" +
("
" +
cityssm.escapeHTML(lot.lotName) + "
" +
"" + cityssm.escapeHTML(lot.mapName) + "" +
"
") +
("
" +
cityssm.escapeHTML(lot.lotStatus as string) + "
" +
"" +
(lot.lotOccupancyCount > 0 ?
"Currently Occupied" : "") +
"" +
"
") +
"
";
panelBlockElement.addEventListener("click", selectLot);
panelElement.append(panelBlockElement);
}
lotSelectResultsElement.innerHTML = "";
lotSelectResultsElement.append(panelElement);
});
}
cityssm.openHtmlModal("lotOccupancy-selectLot", {
onshow: (modalElement) => {
los.populateAliases(modalElement);
},
onshown: (modalElement, closeModalFunction) => {
bulmaJS.toggleHtmlClipped();
lotSelectCloseModalFunction = closeModalFunction;
const lotNameFilterElement = modalElement.querySelector("#lotSelect--lotName") as HTMLInputElement;
lotNameFilterElement.value = currentLotName;
lotNameFilterElement.focus();
lotNameFilterElement.addEventListener("change", searchLots);
modalElement.querySelector("#lotSelect--occupancyStatus").addEventListener("change", searchLots);
lotSelectFormElement = modalElement.querySelector("#form--lotSelect");
lotSelectResultsElement = modalElement.querySelector("#resultsContainer--lotSelect");
lotSelectFormElement.addEventListener("submit", (submitEvent) => {
submitEvent.preventDefault();
});
searchLots();
},
onremoved: () => {
bulmaJS.toggleHtmlClipped();
}
});
});
// Start Date
document.querySelector("#lotOccupancy--occupancyStartDateString").addEventListener("change", () => {
(document.querySelector("#lotOccupancy--occupancyEndDateString") as HTMLInputElement).min =
(document.querySelector("#lotOccupancy--occupancyStartDateString") as HTMLInputElement).value;
});
los.initializeUnlockFieldButtons(formElement);
/*
* Occupants
*/
if (!isCreate) {
let lotOccupancyOccupants: recordTypes.LotOccupancyOccupant[] = exports.lotOccupancyOccupants;
const openEditLotOccupancyOccupant = (clickEvent: Event) => {
const lotOccupantIndex = Number.parseInt((clickEvent.currentTarget as HTMLElement).closest("tr").dataset.lotOccupantIndex, 10);
const lotOccupancyOccupant = lotOccupancyOccupants.find((currentLotOccupancyOccupant) => {
return currentLotOccupancyOccupant.lotOccupantIndex === lotOccupantIndex;
});
let editFormElement: HTMLFormElement;
let editCloseModalFunction: () => void;
const editOccupant = (submitEvent: SubmitEvent) => {
submitEvent.preventDefault();
cityssm.postJSON(urlPrefix + "/lotOccupancies/doUpdateLotOccupancyOccupant",
editFormElement,
(responseJSON: {
success: boolean;errorMessage ? : string;lotOccupancyOccupants ? : recordTypes.LotOccupancyOccupant[];
}) => {
if (responseJSON.success) {
lotOccupancyOccupants = responseJSON.lotOccupancyOccupants;
editCloseModalFunction();
renderLotOccupancyOccupants();
} else {
bulmaJS.alert({
title: "Error Updating " + exports.aliases.occupant,
message: responseJSON.errorMessage,
contextualColorName: "danger"
});
}
});
};
cityssm.openHtmlModal("lotOccupancy-editOccupant", {
onshow: (modalElement) => {
los.populateAliases(modalElement);
(modalElement.querySelector("#lotOccupancyOccupantEdit--lotOccupancyId") as HTMLInputElement).value = lotOccupancyId;
(modalElement.querySelector("#lotOccupancyOccupantEdit--lotOccupantIndex") as HTMLInputElement).value = lotOccupantIndex.toString();
const lotOccupantTypeSelectElement = modalElement.querySelector("#lotOccupancyOccupantEdit--lotOccupantTypeId") as HTMLSelectElement;
let lotOccupantTypeSelected = false;
for (const lotOccupantType of (exports.lotOccupantTypes as recordTypes.LotOccupantType[])) {
const optionElement = document.createElement("option");
optionElement.value = lotOccupantType.lotOccupantTypeId.toString();
optionElement.textContent = lotOccupantType.lotOccupantType;
if (lotOccupantType.lotOccupantTypeId === lotOccupancyOccupant.lotOccupantTypeId) {
optionElement.selected = true;
lotOccupantTypeSelected = true;
}
lotOccupantTypeSelectElement.append(optionElement);
}
if (!lotOccupantTypeSelected) {
const optionElement = document.createElement("option");
optionElement.value = lotOccupancyOccupant.lotOccupantTypeId.toString();
optionElement.textContent = lotOccupancyOccupant.lotOccupantType as string;
optionElement.selected = true;
lotOccupantTypeSelectElement.append(optionElement);
}
(modalElement.querySelector("#lotOccupancyOccupantEdit--occupantName") as HTMLInputElement).value = lotOccupancyOccupant.occupantName;
(modalElement.querySelector("#lotOccupancyOccupantEdit--occupantAddress1") as HTMLInputElement).value = lotOccupancyOccupant.occupantAddress1;
(modalElement.querySelector("#lotOccupancyOccupantEdit--occupantAddress2") as HTMLInputElement).value = lotOccupancyOccupant.occupantAddress2;
(modalElement.querySelector("#lotOccupancyOccupantEdit--occupantCity") as HTMLInputElement).value = lotOccupancyOccupant.occupantCity;
(modalElement.querySelector("#lotOccupancyOccupantEdit--occupantProvince") as HTMLInputElement).value = lotOccupancyOccupant.occupantProvince;
(modalElement.querySelector("#lotOccupancyOccupantEdit--occupantPostalCode") as HTMLInputElement).value = lotOccupancyOccupant.occupantPostalCode;
(modalElement.querySelector("#lotOccupancyOccupantEdit--occupantPhoneNumber") as HTMLInputElement).value = lotOccupancyOccupant.occupantPhoneNumber;
},
onshown: (modalElement, closeModalFunction) => {
bulmaJS.toggleHtmlClipped();
editFormElement = modalElement.querySelector("form");
editFormElement.addEventListener("submit", editOccupant);
editCloseModalFunction = closeModalFunction;
},
onremoved: () => {
bulmaJS.toggleHtmlClipped();
}
});
};
const deleteLotOccupancyOccupant = (clickEvent: Event) => {
const lotOccupantIndex = (clickEvent.currentTarget as HTMLElement).closest("tr").dataset.lotOccupantIndex;
const doDelete = () => {
cityssm.postJSON(urlPrefix + "/lotOccupancies/doDeleteLotOccupancyOccupant", {
lotOccupancyId,
lotOccupantIndex
},
(responseJSON: {
success: boolean;errorMessage ? : string;lotOccupancyOccupants: recordTypes.LotOccupancyOccupant[];
}) => {
if (responseJSON.success) {
lotOccupancyOccupants = responseJSON.lotOccupancyOccupants;
renderLotOccupancyOccupants();
} else {
bulmaJS.alert({
title: "Error Removing " + exports.aliases.occupant,
message: responseJSON.errorMessage,
contextualColorName: "danger"
});
}
});
};
bulmaJS.confirm({
title: "Remove " + exports.aliases.occupant + "?",
message: "Are you sure you want to remove this " + exports.aliases.occupant.toLowerCase() + "?",
okButton: {
text: "Yes, Remove " + exports.aliases.occupant,
callbackFunction: doDelete
},
contextualColorName: "warning"
});
};
const renderLotOccupancyOccupants = () => {
const occupantsContainer = document.querySelector("#container--lotOccupancyOccupants") as HTMLElement;
cityssm.clearElement(occupantsContainer);
if (lotOccupancyOccupants.length === 0) {
occupantsContainer.innerHTML = "" +
"
There are no " + exports.aliases.occupants.toLowerCase() + " associated with this record.
" +
"
";
return;
}
const tableElement = document.createElement("table");
tableElement.className = "table is-fullwidth is-striped is-hoverable";
tableElement.innerHTML = "" +
"| " + exports.aliases.occupant + " Type | " +
"" + exports.aliases.occupant + " | " +
"Address | " +
"Phone Number | " +
" | " +
"
" +
"";
for (const lotOccupancyOccupant of lotOccupancyOccupants) {
const tableRowElement = document.createElement("tr");
tableRowElement.dataset.lotOccupantIndex = lotOccupancyOccupant.lotOccupantIndex.toString();
tableRowElement.innerHTML = ("" + cityssm.escapeHTML(lotOccupancyOccupant.lotOccupantType as string) + " | ") +
("" + cityssm.escapeHTML(lotOccupancyOccupant.occupantName) + " | ") +
("" +
cityssm.escapeHTML(lotOccupancyOccupant.occupantAddress1) + " " +
(lotOccupancyOccupant.occupantAddress2 ? cityssm.escapeHTML(lotOccupancyOccupant.occupantAddress2) + " " : "") +
cityssm.escapeHTML(lotOccupancyOccupant.occupantCity) + ", " + cityssm.escapeHTML(lotOccupancyOccupant.occupantProvince) + " " +
cityssm.escapeHTML(lotOccupancyOccupant.occupantPostalCode) +
" | ") +
("" + cityssm.escapeHTML(lotOccupancyOccupant.occupantPhoneNumber) + " | ") +
("" +
" " +
("") +
("") +
" " +
" | ");
tableRowElement.querySelector(".button--edit").addEventListener("click", openEditLotOccupancyOccupant);
tableRowElement.querySelector(".button--delete").addEventListener("click", deleteLotOccupancyOccupant);
tableElement.querySelector("tbody").append(tableRowElement);
}
occupantsContainer.append(tableElement);
};
document.querySelector("#button--addOccupant").addEventListener("click", () => {
let addFormElement: HTMLFormElement;
let addCloseModalFunction: () => void;
const addOccupant = (submitEvent: SubmitEvent) => {
submitEvent.preventDefault();
cityssm.postJSON(urlPrefix + "/lotOccupancies/doAddLotOccupancyOccupant",
addFormElement,
(responseJSON: {
success: boolean;errorMessage ? : string;lotOccupancyOccupants ? : recordTypes.LotOccupancyOccupant[];
}) => {
if (responseJSON.success) {
lotOccupancyOccupants = responseJSON.lotOccupancyOccupants;
addCloseModalFunction();
renderLotOccupancyOccupants();
} else {
bulmaJS.alert({
title: "Error Adding " + exports.aliases.occupant,
message: responseJSON.errorMessage,
contextualColorName: "danger"
});
}
});
};
cityssm.openHtmlModal("lotOccupancy-addOccupant", {
onshow: (modalElement) => {
los.populateAliases(modalElement);
(modalElement.querySelector("#lotOccupancyOccupantAdd--lotOccupancyId") as HTMLInputElement).value = lotOccupancyId;
const lotOccupantTypeSelectElement = modalElement.querySelector("#lotOccupancyOccupantAdd--lotOccupantTypeId") as HTMLSelectElement;
for (const lotOccupantType of (exports.lotOccupantTypes as recordTypes.LotOccupantType[])) {
const optionElement = document.createElement("option");
optionElement.value = lotOccupantType.lotOccupantTypeId.toString();
optionElement.textContent = lotOccupantType.lotOccupantType;
lotOccupantTypeSelectElement.append(optionElement);
}
(modalElement.querySelector("#lotOccupancyOccupantAdd--occupantCity") as HTMLInputElement).value = exports.occupantCityDefault;
(modalElement.querySelector("#lotOccupancyOccupantAdd--occupantProvince") as HTMLInputElement).value = exports.occupantProvinceDefault;
},
onshown: (modalElement, closeModalFunction) => {
bulmaJS.toggleHtmlClipped();
addFormElement = modalElement.querySelector("form");
addFormElement.addEventListener("submit", addOccupant);
addCloseModalFunction = closeModalFunction;
},
onremoved: () => {
bulmaJS.toggleHtmlClipped();
}
});
});
renderLotOccupancyOccupants();
}
/*
* Comments
*/
/*
* Fees
*/
/*
* Transactions
*/
})();