function getQueryParam(name) {
return new URLSearchParams(window.location.search).get(name);
}
function loadDetail() {
const kode = getQueryParam("kode");
let total = 0;
let tbody = "";
fetch(`/WebNew/DetailItem/GetDetail?kode=${kode}`)
.then(res => {
if (!res.ok) throw new Error("Data tidak ditemukan");
return res.json();
})
.then(data => {
document.getElementById("output").innerText = data.output || "-";
document.getElementById("jenis").innerText = data.jenis || "-";
let thead = "
";
(data.table.columns || []).forEach(col => {
thead += `| ${col} | `;
});
thead += "
";
document.getElementById("thead").innerHTML = thead;
(data.table.data || []).forEach(row => {
tbody += "";
(data.table.fields || []).forEach(field => {
let value = row[field] ?? "-";
if (field.toLowerCase().includes("biaya")) {
let num = Number(value) || 0;
total += num;
value = "Rp " + num.toLocaleString();
}
tbody += `| ${value} | `;
});
tbody += "
";
});
tbody += `
|
Total
|
Rp ${total.toLocaleString()}
|
`;
document.getElementById("tbody").innerHTML = tbody;
})
.catch(err => {
console.error(err);
document.getElementById("tbody").innerHTML =
"| Data tidak ditemukan |
";
});
}
document.addEventListener("DOMContentLoaded", function () {
loadDetail();
const search = document.getElementById("search");
if (search) {
search.addEventListener("keyup", function () {
const keyword = this.value.toLowerCase();
const rows = document.querySelectorAll("#tbody tr");
rows.forEach(row => {
const text = row.innerText.toLowerCase();
row.style.display = text.includes(keyword) ? "" : "none";
});
});
}
});
function exportDetailExcel() {
const table = document.querySelector(".custom-table");
if (!table) {
alert("Table tidak ditemukan");
return;
}
if (typeof XLSX === "undefined") {
alert("Library XLSX belum ter-load");
return;
}
const output = document.getElementById("output").innerText;
const jenis = document.getElementById("jenis").innerText;
const tahun = new Date().getFullYear();
const fileName = `detail-${output}-${tahun}.xlsx`
.replace(/\s+/g, "-");
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.table_to_sheet(table);
XLSX.utils.sheet_add_aoa(ws, [
["Rincian Analisis"],
[`Output: ${output}`],
[`Jenis: ${jenis}`],
[]
], { origin: "A1" });
XLSX.utils.book_append_sheet(wb, ws, "Detail");
XLSX.writeFile(wb, fileName);
}