Lisää saatavuuskyselyjen keräys ja listausnäkymä

Jokainen nettisivujen kautta tehty saatavuustarkistus tallennetaan
tietokantaan (osoite, postinumero, kaupunki, tulos, IP, referer).
Kyselyt näkyvät Asiakkaat > Saatavuuskyselyt -välilehdellä
sivutettuna taulukkona.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 02:19:24 +02:00
parent 9ba239478c
commit a38c5f4808
4 changed files with 143 additions and 3 deletions

View File

@@ -281,6 +281,8 @@ function switchToTab(target, subTab) {
loadCustomers();
if (subTab === 'archive') {
switchCustomerSubTab('customers-archive');
} else if (subTab === 'availability') {
switchCustomerSubTab('customers-availability');
} else {
switchCustomerSubTab('customers-list');
}
@@ -3495,6 +3497,7 @@ function switchCustomerSubTab(target) {
const content = document.getElementById('subtab-' + target);
if (content) content.classList.add('active');
if (target === 'customers-archive') { loadArchive(); window.location.hash = 'customers/archive'; }
else if (target === 'customers-availability') { loadAvailabilityQueries(); window.location.hash = 'customers/availability'; }
else { window.location.hash = 'customers'; }
}
@@ -3502,6 +3505,61 @@ document.querySelectorAll('#customers-sub-tab-bar .sub-tab').forEach(btn => {
btn.addEventListener('click', () => switchCustomerSubTab(btn.dataset.custSubtab));
});
// ==================== SAATAVUUSKYSELYT ====================
let availabilityPage = 0;
const AVAILABILITY_PER_PAGE = 50;
async function loadAvailabilityQueries(page = 0) {
availabilityPage = page;
const offset = page * AVAILABILITY_PER_PAGE;
try {
const data = await apiCall(`availability_queries&limit=${AVAILABILITY_PER_PAGE}&offset=${offset}`);
const tbody = document.querySelector('#availability-queries-table tbody');
const countEl = document.getElementById('availability-query-count');
countEl.textContent = `Yhteensä ${data.total} kyselyä`;
if (data.queries.length === 0) {
tbody.innerHTML = '<tr><td colspan="6" style="text-align:center;color:#888;padding:2rem;">Ei vielä kyselyjä</td></tr>';
} else {
tbody.innerHTML = data.queries.map(q => {
const date = q.created_at ? q.created_at.replace('T', ' ').substring(0, 16) : '';
const found = q.saatavilla == 1;
const badge = found
? '<span style="background:#e8f5e9;color:#2e7d32;padding:2px 8px;border-radius:10px;font-size:0.8rem;">Saatavilla</span>'
: '<span style="background:#fce4ec;color:#c62828;padding:2px 8px;border-radius:10px;font-size:0.8rem;">Ei saatavilla</span>';
let source = '';
if (q.referer) {
try { source = new URL(q.referer).hostname; } catch(e) { source = q.referer.substring(0, 30); }
}
return `<tr>
<td style="white-space:nowrap;">${esc(date)}</td>
<td>${esc(q.osoite)}</td>
<td>${esc(q.postinumero)}</td>
<td>${esc(q.kaupunki)}</td>
<td>${badge}</td>
<td style="font-size:0.8rem;color:#888;">${esc(source)}</td>
</tr>`;
}).join('');
}
// Sivutus
const totalPages = Math.ceil(data.total / AVAILABILITY_PER_PAGE);
const pagination = document.getElementById('availability-pagination');
if (totalPages > 1) {
let pagHtml = '';
if (page > 0) pagHtml += `<button class="btn-secondary" onclick="loadAvailabilityQueries(${page - 1})">← Edellinen</button>`;
pagHtml += `<span style="padding:6px 12px;color:#666;">Sivu ${page + 1} / ${totalPages}</span>`;
if (page < totalPages - 1) pagHtml += `<button class="btn-secondary" onclick="loadAvailabilityQueries(${page + 1})">Seuraava →</button>`;
pagination.innerHTML = pagHtml;
} else {
pagination.innerHTML = '';
}
} catch (e) {
console.error('Saatavuuskyselyjen lataus epäonnistui:', e);
}
}
// ==================== SIJAINNIT — YHDISTETTY LAITETILOIHIN ====================
// Sites-koodi poistettu: sijainnit hallitaan nyt Laitetilat-välilehdellä.