import { Grid } from "gridjs/dist/gridjs.umd.js";
import "gridjs/dist/gridjs.umd.js";
import gridjs from "gridjs/dist/gridjs.umd.js";
import GlobalConfig, { addThousandSeparators } from "../global-config.js";

class ReportPbgPTSP {
    constructor() {
        this.table = null;
        this.initTableReportPbgPTSP();
    }
    initTableReportPbgPTSP() {
        let tableContainer = document.getElementById("table-report-pbg-ptsp");

        this.table = new Grid({
            columns: [
                { name: "Status" },
                {
                    name: "Total",
                    formatter: (cell) => cell,
                },
            ],
            pagination: {
                limit: 10,
                server: {
                    url: (prev, page) =>
                        `${prev}${prev.includes("?") ? "&" : "?"}page=${
                            page + 1
                        }`,
                },
            },
            sort: true,
            server: {
                url: `${GlobalConfig.apiHost}/api/report-pbg-ptsp`,
                headers: {
                    Authorization: `Bearer ${document
                        .querySelector('meta[name="api-token"]')
                        .getAttribute("content")}`,
                    "Content-Type": "application/json",
                },
                then: (response) => {
                    console.log("API Response:", response); // Debugging

                    // Pastikan response memiliki data
                    if (
                        !response ||
                        !response.data ||
                        !Array.isArray(response.data.data)
                    ) {
                        console.error("Error: Data is not an array", response);
                        return [];
                    }

                    return response.data.data.map((item) => [
                        item.status_name || "Unknown",
                        item.total,
                    ]);
                },
                total: (response) => response.data.total || 0, // Ambil total dari API pagination
            },
            width: "auto",
            fixedHeader: true,
        }).render(tableContainer);
    }
}
document.addEventListener("DOMContentLoaded", function (e) {
    new ReportPbgPTSP();
});
