/* eslint-disable unicorn/prefer-module */
import type * as recordTypes from "../types/recordTypes";
import type { cityssmGlobal } from "@cityssm/bulma-webapp-js/src/types";
declare const cityssm: cityssmGlobal;
(() => {
const urlPrefix = document.querySelector("main").dataset.urlPrefix;
const searchFilterFormElement = document.querySelector(
"#form--searchFilters"
) as HTMLFormElement;
const searchResultsContainerElement = document.querySelector(
"#container--searchResults"
) as HTMLElement;
const limit = Number.parseInt(
(document.querySelector("#searchFilter--limit") as HTMLInputElement)
.value,
10
);
const offsetElement = document.querySelector(
"#searchFilter--offset"
) as HTMLInputElement;
const getLots = () => {
const offset = Number.parseInt(offsetElement.value, 10);
searchResultsContainerElement.innerHTML =
'
' +
'
' +
"Loading " +
exports.aliases.lots +
"..." +
"
";
cityssm.postJSON(
urlPrefix + "/lots/doSearchLots",
searchFilterFormElement,
(responseJSON: { count: number; lots: recordTypes.Lot[] }) => {
if (responseJSON.lots.length === 0) {
searchResultsContainerElement.innerHTML =
'' +
'
There are no ' +
exports.aliases.lots.toLowerCase() +
" that meet the search criteria.
" +
"
";
return;
}
const resultsTbodyElement = document.createElement("tbody");
for (const lot of responseJSON.lots) {
resultsTbodyElement.insertAdjacentHTML(
"beforeend",
"" +
("| " +
'' +
lot.lotName +
"" +
" | ") +
("" +
'' +
lot.mapName +
"" +
" | ") +
"" +
lot.lotType +
" | " +
("" +
lot.lotStatus +
" " +
(lot.lotOccupancyCount > 0
? 'Currently Occupied'
: "") +
" | ") +
"
"
);
}
searchResultsContainerElement.innerHTML =
'' +
"" +
"| " +
exports.aliases.lot +
" | " +
"" +
exports.aliases.map +
" | " +
"" +
exports.aliases.lot +
" Type | " +
"Status | " +
"
" +
"" +
'' +
('
' +
'
' +
"Displaying " +
(offset + 1).toString() +
" to " +
Math.min(responseJSON.count, limit + offset) +
" of " +
responseJSON.count +
"
" +
"
") +
('
' +
(offset > 0
? '
' +
'" +
"
"
: "") +
(limit + offset < responseJSON.count
? '
' +
'" +
"
"
: "") +
"
") +
"
";
searchResultsContainerElement
.querySelector("table")
.append(resultsTbodyElement);
if (offset > 0) {
searchResultsContainerElement
.querySelector("button[data-page='previous']")
.addEventListener("click", previousAndGetLots);
}
if (limit + offset < responseJSON.count) {
searchResultsContainerElement
.querySelector("button[data-page='next']")
.addEventListener("click", nextAndGetLots);
}
}
);
};
const resetOffsetAndGetLots = () => {
offsetElement.value = "0";
getLots();
};
const previousAndGetLots = () => {
offsetElement.value = Math.max(
Number.parseInt(offsetElement.value, 10) - limit,
0
).toString();
getLots();
};
const nextAndGetLots = () => {
offsetElement.value = (
Number.parseInt(offsetElement.value, 10) + limit
).toString();
getLots();
};
const filterElements = searchFilterFormElement.querySelectorAll(
"input, select"
) as NodeListOf;
for (const filterElement of filterElements) {
filterElement.addEventListener("change", resetOffsetAndGetLots);
}
searchFilterFormElement.addEventListener("submit", (formEvent) => {
formEvent.preventDefault();
resetOffsetAndGetLots();
});
getLots();
})();