import { Grid } from "gridjs/dist/gridjs.umd.js"; import gridjs from "gridjs/dist/gridjs.umd.js"; import "gridjs/dist/gridjs.umd.js"; import GlobalConfig from "../global-config"; import { addThousandSeparators } from "../global-config"; import Swal from "sweetalert2"; class Taxation { constructor() { this.toastMessage = document.getElementById("toast-message"); this.toastElement = document.getElementById("toastNotification"); this.toast = new bootstrap.Toast(this.toastElement); this.table = null; this.initTableTaxation(); this.initEvents(); this.handleExportToExcel(); } handleExportToExcel() { const button = document.getElementById("btn-export-excel"); if (!button) { console.error("Button not found: #btn-export-excel"); return; } const exportUrl = button.getAttribute("data-url"); button.addEventListener("click", function () { button.disabled = true; fetch(exportUrl, { method: "GET", credentials: "include", headers: { Authorization: "Bearer " + document .querySelector('meta[name="api-token"]') .getAttribute("content"), }, }) .then(function (response) { if (!response.ok) { throw new Error( "Error fetching data: " + response.statusText ); } return response.blob(); }) .then(function (blob) { const url = window.URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = "laporan-rekap-data-pembayaran.xlsx"; document.body.appendChild(a); a.click(); document.body.removeChild(a); window.URL.revokeObjectURL(url); }) .catch(function (error) { console.error("Error fetching data:", error); }) .finally(function () { button.disabled = false; }); }); } initEvents() { document.body.addEventListener("click", (event) => { const deleteButton = event.target.closest(".btn-delete-taxation"); if (deleteButton) { event.preventDefault(); this.handleDelete(deleteButton); } }); } handleDelete(deleteButton) { const id = deleteButton.getAttribute("data-id"); Swal.fire({ title: "Are you sure?", text: "You won't be able to revert this!", icon: "warning", showCancelButton: true, confirmButtonColor: "#3085d6", cancelButtonColor: "#d33", confirmButtonText: "Yes, delete it!", }).then((result) => { if (result.isConfirmed) { fetch(`${GlobalConfig.apiHost}/api/taxs/${id}`, { method: "DELETE", credentials: "include", headers: { Authorization: `Bearer ${document .querySelector('meta[name="api-token"]') .getAttribute("content")}`, "Content-Type": "application/json", }, }) .then((response) => { if (response.ok) { return response.json(); } else { return response.json().then((error) => { throw new Error( error.message || "Delete failed!" ); }); } }) .then((result) => { this.toastMessage.innerText = result.message || "Data deleted successfully!"; this.toast.show(); if (typeof this.table !== "undefined") { this.table.updateConfig({}).forceRender(); } }) .catch((error) => { console.error("Error deleting item:", error); this.toastMessage.innerText = error.message || "An error occurred!"; this.toast.show(); }); } }); } initTableTaxation() { let tableContainer = document.getElementById("table-taxation"); if (!tableContainer) { console.error("Table container not found!"); return; } // Clear previous table content tableContainer.innerHTML = ""; let canUpdate = tableContainer.getAttribute("data-updater") === "1"; let canDelete = tableContainer.getAttribute("data-destroyer") === "1"; let menuId = tableContainer.getAttribute("data-menuId"); this.table = new Grid({ columns: [ "ID", { name: "Tax No" }, { name: "Tax Code" }, { name: "WP Name" }, { name: "Business Name" }, { name: "Address" }, { name: "Start Validity" }, { name: "End Validity" }, { name: "Tax Value" }, { name: "Subdistrict" }, { name: "Village" }, { name: "Action", formatter: (cell) => { let buttons = ""; if (canUpdate) { buttons += ` `; } if (canDelete) { buttons += ` `; } if (!canUpdate && !canDelete) { buttons = `No Privilege`; } return gridjs.html( `