const Territory_list = { init: async () => { let html = await fetch('/lib/pages/territory/list/index.html').then((response) => response.text()); app.innerHTML = html; let selectStatus = document.getElementById('list-controls-filter-status'); let territory_list_filter = localStorage.getItem("territory_list_filter") ? Number(localStorage.getItem("territory_list_filter")) : 0; selectStatus.value = territory_list_filter; if (USER.mode == 2) { document.getElementById("historyButton").style.display = ""; } if (USER.possibilities.can_add_territory) { document.getElementById("constructorButton").style.display = ""; } // Застосовуємо режим сортування Territory_list.sort(localStorage.getItem('territory_list_sort')); if (localStorage.getItem('territory_list_entrances') == 'true') { document.getElementById('territory_entrances_true').setAttribute('data-state', '') document.getElementById('territory_entrances_false').setAttribute('data-state', 'active') } else { document.getElementById('territory_entrances_true').setAttribute('data-state', 'active') document.getElementById('territory_entrances_false').setAttribute('data-state', '') } }, // Сортування sort(mode) { const idx = Math.max(1, Math.min(4, Number(mode) || 1)); ['sort_1', 'sort_2', 'sort_3', 'sort_4'].forEach((id, i) => { document.getElementById(id)?.setAttribute('data-state', i + 1 === idx ? 'active' : ''); }); localStorage.setItem('territory_list_sort', idx); Territory_list.house.setHTML(); Territory_list.homestead.setHTML(); }, house: { list: [], loadAPI: async function (url) { const uuid = localStorage.getItem("uuid"); const response = await fetch(url, { method: 'GET', headers: { "Content-Type": "application/json", "Authorization": uuid } }); Territory_list.house.list = await response.json(); return Territory_list.house.list; }, setHTML: async function () { const block_house = document.getElementById('list-house'); const territory_entrances = localStorage.getItem('territory_list_entrances') === 'true'; const sort_mode = localStorage.getItem('territory_list_sort') ?? "1"; const territory_list_filter = Number(localStorage.getItem("territory_list_filter") ?? 0); const url = `${CONFIG.api}houses/list${territory_entrances ? '/entrances' : ''}`; let list = this.list.length > 0 ? this.list : await this.loadAPI(url); const isEnd = territory_list_filter === "2"; const field = isEnd ? "end" : "start"; const compare = { "1": (a, b) => this.compareAB(a, b, territory_entrances, 'asc'), "2": (a, b) => this.compareAB(a, b, territory_entrances, 'desc'), "3": (a, b) => (a.history?.date?.[field] ?? 0) - (b.history?.date?.[field] ?? 0), "4": (a, b) => (b.history?.date?.[field] ?? 0) - (a.history?.date?.[field] ?? 0), }[sort_mode] ?? ((a, b) => a.id - b.id); list.sort(compare); let html = ""; for (const element of list) { const qty = element.entrance?.quantity ?? 0; const work = element.entrance?.working ?? 0; const statusMatch = territory_list_filter === 0 || (territory_list_filter === 1 && qty === work && !territory_entrances) || (territory_list_filter === 1 && element.working === true) || (territory_list_filter === 2 && qty !== work && !territory_entrances) || (territory_list_filter === 2 && element.working === false); if (statusMatch) { html += this.renderCard({ element, territory_entrances }); } } block_house.innerHTML = html; }, compareAB: (a, b, entrances, order = 'asc') => { const dir = order === 'asc' ? 1 : -1; const ah = entrances ? a.house : a; const bh = entrances ? b.house : b; const tA = ah.title?.toLowerCase() ?? ''; const tB = bh.title?.toLowerCase() ?? ''; if (tA < tB) return -1 * dir; if (tA > tB) return 1 * dir; const nA = ah.number?.toLowerCase() ?? ''; const nB = bh.number?.toLowerCase() ?? ''; if (nA < nB) return -1 * dir; if (nA > nB) return 1 * dir; return 0; }, renderCard: ({ element, territory_entrances }) => { if (territory_entrances) { const working = element.working; const bg = working ? `background: ${colorGroup(element.history.group_id)} color: #fff;` : ""; const person = working ? `Територію опрацьовує:

${element.history.name === 'Групова' ? 'Група ' + element.history.group_id : element.history.name}

` : `Територія не опрацьовується`; return `

${element.house.title} ${element.house.number} (${element.title})

${person}
`; } else { const qty = element.entrance.quantity; const work = element.entrance.working; const progress = ((work / qty) * 100).toFixed(1); return `

${element.title} ${element.number}

Вільні під'їзди:

${qty - work} / ${qty}

`; } }, territoryType: (type) => { localStorage.setItem('territory_list_entrances', type); document.getElementById('territory_entrances_true').setAttribute('data-state', type === 'false' ? 'active' : ''); document.getElementById('territory_entrances_false').setAttribute('data-state', type === 'true' ? 'active' : ''); Territory_list.house.list = []; Territory_list.house.setHTML(); } }, homestead: { list: [], loadAPI: async function () { const uuid = localStorage.getItem("uuid"); const URL = `${CONFIG.api}homestead/list`; const response = await fetch(URL, { method: 'GET', headers: { "Content-Type": "application/json", "Authorization": uuid } }); this.list = await response.json(); return this.list; }, setHTML: async function () { const block = document.getElementById('list-homestead'); const sortMode = localStorage.getItem('territory_list_sort') ?? "1"; const territory_list_filter = Number(localStorage.getItem("territory_list_filter") ?? 0); let list = this.list.length > 0 ? this.list : await this.loadAPI(); const compare = { "1": (a, b) => a.id - b.id, "2": (a, b) => b.id - a.id, "3": (a, b) => (a.history?.date?.start ?? 0) - (b.history?.date?.start ?? 0), "4": (a, b) => (b.history?.date?.start ?? 0) - (a.history?.date?.start ?? 0), }[sortMode] ?? ((a, b) => a.id - b.id); list.sort(compare); let html = ""; for (const element of list) { const statusMatch = territory_list_filter === 0 || (territory_list_filter === 1 && element.working) || (territory_list_filter === 2 && !element.working); if (statusMatch) { html += this.renderCard(element); } } block.innerHTML = html; }, renderCard: (element) => { const working = element.working; const bg = working ? `background: ${colorGroup(element.history.group_id)} color: #fff;` : ""; const person = working ? `Територію опрацьовує:

${element.history.name === 'Групова' ? 'Група ' + element.history.group_id : element.history.name}

` : `Територія не опрацьовується`; return `

${element.title} ${element.number}

${person}
`; } }, filter: () => { let selectStatus = document.getElementById('list-controls-filter-status').value; localStorage.setItem("territory_list_filter", selectStatus); Territory_list.house.setHTML(); Territory_list.homestead.setHTML(); }, report: async () => { const uuid = localStorage.getItem("uuid"); const url = `${CONFIG.api}generator/report/territories`; await fetch(url, { method: 'GET', headers: { "Content-Type": "application/json", "Authorization": uuid } }).then(response => { if (!response.ok) throw new Error('Network response was not ok'); return response.blob(); }) .then(blob => { const link = document.createElement('a'); const url = window.URL.createObjectURL(blob); link.href = url; link.download = 'report.xlsx'; document.body.appendChild(link); link.click(); link.remove(); window.URL.revokeObjectURL(url); }) .catch(err => { console.error('Fetch error:', err); }); } }