import type { cityssmGlobal } from '@cityssm/bulma-webapp-js/src/types.js' import type { Contract } from '../../types/recordTypes.js' import type { Sunrise } from './types.js' declare const cityssm: cityssmGlobal declare const exports: Record ;(() => { const los = exports.sunrise as Sunrise 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 // eslint-disable-next-line complexity function renderContracts(rawResponseJSON: unknown): void { const responseJSON = rawResponseJSON as { count: number offset: number contracts: Contract[] } if (responseJSON.contracts.length === 0) { searchResultsContainerElement.innerHTML = `

There are no contracts that meet the search criteria.

` return } const resultsTbodyElement = document.createElement('tbody') const nowDateString = cityssm.dateToString(new Date()) for (const contract of responseJSON.contracts) { let contractTimeHTML = '' if ( contract.contractStartDateString! <= nowDateString && (contract.contractEndDateString === '' || contract.contractEndDateString! >= nowDateString) ) { contractTimeHTML = ` ` } else if (contract.contractStartDateString! > nowDateString) { contractTimeHTML = ` ` } else { contractTimeHTML = ` ` } let deceasedHTML = '' for (const interment of contract.contractInterments ?? []) { deceasedHTML += `
  • ${cityssm.escapeHTML(interment.deceasedName ?? '')}
  • ` } const feeTotal = ( contract.contractFees?.reduce( (soFar, currentFee): number => soFar + ((currentFee.feeAmount ?? 0) + (currentFee.taxAmount ?? 0)) * (currentFee.quantity ?? 0), 0 ) ?? 0 ).toFixed(2) const transactionTotal = ( contract.contractTransactions?.reduce( (soFar, currentTransaction): number => soFar + currentTransaction.transactionAmount, 0 ) ?? 0 ).toFixed(2) let feeIconHTML = '' if (feeTotal !== '0.00' || transactionTotal !== '0.00') { feeIconHTML = ` ` } // eslint-disable-next-line no-unsanitized/method resultsTbodyElement.insertAdjacentHTML( 'beforeend', ` ${contractTimeHTML} ${cityssm.escapeHTML(contract.contractType ?? '')}
    #${contract.contractId} ${ (contract.burialSiteId ?? -1) === -1 ? `(No Burial Site)` : ` ${cityssm.escapeHTML(contract.burialSiteName ?? '')} ` }
    ${cityssm.escapeHTML(contract.cemeteryName ?? '')} ${contract.contractStartDateString} ${ contract.contractEndDate ? contract.contractEndDateString : '(No End Date)' } ${ deceasedHTML === '' ? '' : `` } ${feeIconHTML} ${ contract.printEJS ? ` ` : '' }` ) } searchResultsContainerElement.innerHTML = `
    Contract Type Burial Site Contract Date End Date Recipient / Deceased Fees and Transactions Print
    ` searchResultsContainerElement .querySelector('table') ?.append(resultsTbodyElement) // eslint-disable-next-line no-unsanitized/method searchResultsContainerElement.insertAdjacentHTML( 'beforeend', los.getSearchResultsPagerHTML( limit, responseJSON.offset, responseJSON.count ) ) searchResultsContainerElement .querySelector("button[data-page='previous']") ?.addEventListener('click', previousAndGetContracts) searchResultsContainerElement .querySelector("button[data-page='next']") ?.addEventListener('click', nextAndGetContracts) } function getContracts(): void { // eslint-disable-next-line no-unsanitized/property searchResultsContainerElement.innerHTML = los.getLoadingParagraphHTML( `Loading Contracts...` ) cityssm.postJSON( `${los.urlPrefix}/contracts/doSearchContracts`, searchFilterFormElement, renderContracts ) } function resetOffsetAndGetContracts(): void { offsetElement.value = '0' getContracts() } function previousAndGetContracts(): void { offsetElement.value = Math.max( Number.parseInt(offsetElement.value, 10) - limit, 0 ).toString() getContracts() } function nextAndGetContracts(): void { offsetElement.value = ( Number.parseInt(offsetElement.value, 10) + limit ).toString() getContracts() } const filterElements = searchFilterFormElement.querySelectorAll('input, select') for (const filterElement of filterElements) { filterElement.addEventListener('change', resetOffsetAndGetContracts) } searchFilterFormElement.addEventListener('submit', (formEvent) => { formEvent.preventDefault() }) getContracts() })()