/* 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 getLotOccupancies = () => { const offset = Number.parseInt(offsetElement.value, 10); searchResultsContainerElement.innerHTML = '
' + '
' + "Loading " + exports.aliases.occupancies + "..." + "
"; cityssm.postJSON( urlPrefix + "/lotOccupancies/doSearchLotOccupancies", searchFilterFormElement, (responseJSON: { count: number; lotOccupancies: recordTypes.LotOccupancy[]; }) => { if (responseJSON.lotOccupancies.length === 0) { searchResultsContainerElement.innerHTML = '
' + '

There are no ' + exports.aliases.occupancy.toLowerCase() + " 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 ' + cityssm.escapeHTML(exports.aliases.lot) + ")") + "
" + '' + cityssm.escapeHTML(lotOccupancy.mapName || "") + "" + "") + ("" + lotOccupancy.occupancyStartDateString + "") + ("" + (lotOccupancy.occupancyEndDate ? lotOccupancy.occupancyEndDateString : '(No End Date)') + "") + "" + occupantsHTML + "" + "" ); } searchResultsContainerElement.innerHTML = '' + "" + "" + "" + "" + "" + "" + "" + "" + "
" + exports.aliases.occupancy + " Type" + exports.aliases.lot + "Start DateEnd Date" + exports.aliases.occupants + "
" + '
' + ('
' + '
' + "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", previousAndGetLotOccupancies ); } if (limit + offset < responseJSON.count) { searchResultsContainerElement .querySelector("button[data-page='next']") .addEventListener("click", nextAndGetLotOccupancies); } } ); }; const resetOffsetAndGetLotOccupancies = () => { offsetElement.value = "0"; getLotOccupancies(); }; const previousAndGetLotOccupancies = () => { offsetElement.value = Math.max( Number.parseInt(offsetElement.value, 10) - limit, 0 ).toString(); getLotOccupancies(); }; const nextAndGetLotOccupancies = () => { offsetElement.value = ( Number.parseInt(offsetElement.value, 10) + limit ).toString(); getLotOccupancies(); }; const filterElements = searchFilterFormElement.querySelectorAll( "input, select" ) as NodeListOf; for (const filterElement of filterElements) { filterElement.addEventListener( "change", resetOffsetAndGetLotOccupancies ); } searchFilterFormElement.addEventListener("submit", (formEvent) => { formEvent.preventDefault(); resetOffsetAndGetLotOccupancies(); }); getLotOccupancies(); })();