/* eslint-disable @typescript-eslint/no-non-null-assertion, 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"; declare const cityssm: cityssmGlobal; (() => { const los = exports.los as globalTypes.LOS; 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; function renderLotOccupancies(responseJSON: { count: number; offset: number; lotOccupancies: recordTypes.LotOccupancy[]; }) { if (responseJSON.lotOccupancies.length === 0) { searchResultsContainerElement.innerHTML = `

There are no ${los.escapedAliases.occupancy} records that meet the search criteria.

`; return; } const resultsTbodyElement = document.createElement("tbody"); const nowDateString = cityssm.dateToString(new Date()); for (const lotOccupancy of responseJSON.lotOccupancies) { let occupancyTimeHTML = ""; if ( lotOccupancy.occupancyStartDateString! <= nowDateString && (lotOccupancy.occupancyEndDateString === "" || lotOccupancy.occupancyEndDateString! >= nowDateString) ) { occupancyTimeHTML = '' + ('') + ""; } else if (lotOccupancy.occupancyStartDateString! > nowDateString) { occupancyTimeHTML = '' + ('') + ""; } else { occupancyTimeHTML = '' + ('') + ""; } let occupantsHTML = ""; for (const occupant of lotOccupancy.lotOccupancyOccupants!) { occupantsHTML += '' + (' ') + cityssm.escapeHTML(occupant.occupantName || "") + "
"; } resultsTbodyElement.insertAdjacentHTML( "beforeend", "" + ('' + occupancyTimeHTML + "") + ("" + '' + cityssm.escapeHTML(lotOccupancy.occupancyType as string) + "" + "") + ("" + (lotOccupancy.lotName ? '' + cityssm.escapeHTML(lotOccupancy.lotName) + "" : '(No ' + los.escapedAliases.Lot + ")") + "
" + ('' + cityssm.escapeHTML(lotOccupancy.mapName || "") + "") + "") + ("" + lotOccupancy.occupancyStartDateString + "") + ("" + (lotOccupancy.occupancyEndDate ? lotOccupancy.occupancyEndDateString : '(No End Date)') + "") + ("" + occupantsHTML + "") + "" + (lotOccupancy.printEJS ? '' + '' + "" : "") + "" + "" ); } searchResultsContainerElement.innerHTML = `
${los.escapedAliases.Occupancy} Type ${los.escapedAliases.Lot} ${los.escapedAliases.OccupancyStartDate} End Date ${los.escapedAliases.Occupants} Print
`; searchResultsContainerElement.querySelector("table")!.append(resultsTbodyElement); searchResultsContainerElement.insertAdjacentHTML( "beforeend", los.getSearchResultsPagerHTML(limit, responseJSON.offset, responseJSON.count) ); if (responseJSON.offset > 0) { searchResultsContainerElement .querySelector("button[data-page='previous']")! .addEventListener("click", previousAndGetLotOccupancies); } if (limit + responseJSON.offset < responseJSON.count) { searchResultsContainerElement .querySelector("button[data-page='next']")! .addEventListener("click", nextAndGetLotOccupancies); } } function getLotOccupancies() { searchResultsContainerElement.innerHTML = los.getLoadingParagraphHTML( `Loading ${exports.aliases.occupancies}...` ); cityssm.postJSON( los.urlPrefix + "/lotOccupancies/doSearchLotOccupancies", searchFilterFormElement, renderLotOccupancies ); } function resetOffsetAndGetLotOccupancies() { offsetElement.value = "0"; getLotOccupancies(); } function previousAndGetLotOccupancies() { offsetElement.value = Math.max( Number.parseInt(offsetElement.value, 10) - limit, 0 ).toString(); getLotOccupancies(); } function nextAndGetLotOccupancies() { offsetElement.value = (Number.parseInt(offsetElement.value, 10) + limit).toString(); getLotOccupancies(); } const filterElements = searchFilterFormElement.querySelectorAll("input, select") as NodeListOf< HTMLInputElement | HTMLSelectElement >; for (const filterElement of filterElements) { filterElement.addEventListener("change", resetOffsetAndGetLotOccupancies); } searchFilterFormElement.addEventListener("submit", (formEvent) => { formEvent.preventDefault(); resetOffsetAndGetLotOccupancies(); }); getLotOccupancies(); })();