84 lines
3 KiB
JavaScript
84 lines
3 KiB
JavaScript
// intelligence → predictions table
|
||
// filter/sort/pagination persisted via url query params
|
||
// depends on: app.js, intel-shared.js
|
||
|
||
let predictionsOffset = 0;
|
||
|
||
|
||
function syncUrl() {
|
||
const companyId = document.getElementById("i-company").value;
|
||
const sort = document.getElementById("i-sort").value;
|
||
queryWrite({
|
||
company_id: companyId,
|
||
sort: sort && sort !== "id" ? sort : "",
|
||
offset: predictionsOffset > 0 ? predictionsOffset : "",
|
||
});
|
||
}
|
||
|
||
|
||
async function loadPredictions() {
|
||
const companyId = document.getElementById("i-company").value;
|
||
const sort = document.getElementById("i-sort").value;
|
||
|
||
const params = new URLSearchParams({ limit: PAGE, offset: predictionsOffset });
|
||
if (companyId) params.set("company_id", companyId);
|
||
if (sort) params.set("sort", sort);
|
||
|
||
const data = await api(`/admin/api/intelligence/predictions?${params}`);
|
||
intelRows = data.rows;
|
||
|
||
document.getElementById("intel-thead").innerHTML = `
|
||
<tr><th>ID</th><th>Company</th><th>Event</th><th>Type</th><th>Direction</th><th>Magnitude</th><th>Timeframe</th><th>Rationale</th><th>Event date</th></tr>`;
|
||
|
||
document.getElementById("intel-tbody").innerHTML = data.rows.map(r => `
|
||
<tr style="cursor:pointer" onclick="openIntelDetail(${r.id}, 'predictions')">
|
||
<td style="color:var(--muted-dark); font-size:12px">${r.id}</td>
|
||
<td style="white-space:nowrap">${escapeHtml(r.company_name)}</td>
|
||
<td style="color:var(--muted-dark); font-size:12px">${r.event_id}</td>
|
||
<td><span class="badge null">${escapeHtml(r.type)}</span></td>
|
||
<td>${escapeHtml(r.direction || "—")}</td>
|
||
<td>${escapeHtml(r.magnitude || "—")}</td>
|
||
<td>${escapeHtml(r.timeframe || "—")}</td>
|
||
<td><span class="truncate" style="max-width:300px">${escapeHtml(r.rationale || "—")}</span></td>
|
||
<td style="color:var(--muted-dark); white-space:nowrap; font-size:12px">${r.event_date ? r.event_date.slice(0,10) : "—"}</td>
|
||
</tr>
|
||
`).join("");
|
||
|
||
const total = data.total;
|
||
document.getElementById("iPageInfo").textContent =
|
||
`${predictionsOffset + 1}–${Math.min(predictionsOffset + PAGE, total)} of ${total.toLocaleString()}`;
|
||
document.getElementById("iPrevBtn").disabled = predictionsOffset === 0;
|
||
document.getElementById("iNextBtn").disabled = predictionsOffset + PAGE >= total;
|
||
}
|
||
|
||
|
||
document.addEventListener("DOMContentLoaded", async () => {
|
||
document.getElementById("iPrevBtn").onclick = () => {
|
||
predictionsOffset = Math.max(0, predictionsOffset - PAGE);
|
||
syncUrl();
|
||
loadPredictions();
|
||
};
|
||
document.getElementById("iNextBtn").onclick = () => {
|
||
predictionsOffset += PAGE;
|
||
syncUrl();
|
||
loadPredictions();
|
||
};
|
||
document.getElementById("i-filter-btn").onclick = () => {
|
||
predictionsOffset = 0;
|
||
syncUrl();
|
||
loadPredictions();
|
||
};
|
||
|
||
const ok = await loadIntelStatsRow();
|
||
if (!ok) return;
|
||
|
||
await loadIntelCompanies();
|
||
|
||
queryApplyToInputs({
|
||
"i-company": "company_id",
|
||
"i-sort": "sort",
|
||
});
|
||
predictionsOffset = parseInt(queryGet("offset"), 10) || 0;
|
||
|
||
loadPredictions();
|
||
});
|