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.js";
import GeneralTable from "../../table-generator.js";

// Ambil hak akses dari data-attribute
const tableElement = document.getElementById("spatial-planning-data-table");
const canUpdate = tableElement.getAttribute("data-updater") === "1";
const canDelete = tableElement.getAttribute("data-destroyer") === "1";

const dataSpatialPlanningColumns = [
    "No",
    "Nama",
    "KBLI",
    "Kegiatan",
    "Luas",
    "Lokasi",
    "Nomor",
    "Tanggal",
    {
        name: "Actions",
        widht: "120px",
        formatter: function (cell, row) {
            const id = row.cells[8].data;
            const model = "data/spatial-plannings";

            let actionButtons = '<div class="d-flex justify-items-end gap-10">';
            let hasPrivilege = false;

            // Tampilkan tombol Edit jika user punya akses update
            if (canUpdate) {
                hasPrivilege = true;
                actionButtons += `
                    <button class="btn btn-warning me-2 btn-edit" 
                        data-id="${id}" 
                        data-model="${model}">
                        <i class='bx bx-edit'></i>
                    </button>`;
            }

            // Tampilkan tombol Delete jika user punya akses delete
            if (canDelete) {
                hasPrivilege = true;
                actionButtons += `
                    <button class="btn btn-red btn-delete" 
                        data-id="${id}">
                        <i class='bx bxs-trash'></i>
                    </button>`;
            }

            actionButtons += '</div>';

            // Jika tidak memiliki akses, tampilkan teks "No Privilege"
            return gridjs.html(hasPrivilege ? actionButtons : '<span class="text-muted">No Privilege</span>');
        },
    },
];

document.addEventListener("DOMContentLoaded", () => {
    const table = new GeneralTable(
        "spatial-planning-data-table",
        `${GlobalConfig.apiHost}/api/spatial-plannings`,
        `${GlobalConfig.apiHost}`,
        dataSpatialPlanningColumns
    );

    table.processData = function (data) {
        return data.data.map((item) => {
            return [
                item.no,
                item.name,
                item.kbli,
                item.activities,
                item.area,
                item.location,
                item.number,
                item.date,
                item.id,
            ];
        });
    };

    table.init();
});
