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";

const dataAdvertisementsColumns = [
    "No",
    "Nama Wajib Pajak",
    "NPWPD",
    "Jenis Reklame",
    "Isi Reklame",
    "Alamat Wajib Pajak",
    "Lokasi Reklame",
    "Desa",
    "Kecamatan",
    "Kontak",
    {
        name: "Actions",
        widht: "120px",
        formatter: function(cell, row) {
            const id = row.cells[10].data;
            const model = "data/advertisements";
            return gridjs.html(`
                <div class="d-flex justify-items-end gap-10">
                    <button class="btn btn-warning me-2 btn-edit" 
                        data-id="${id}" 
                        data-model="${model}">
                        <i class='bx bx-edit' ></i></button>
                    <button class="btn btn-red btn-delete" 
                            data-id="${id}">
                            <i class='bx bxs-trash' ></i></button>
                </div>
            `);
        }
    }
];

document.addEventListener("DOMContentLoaded", () => {
    const table = new GeneralTable(
        "reklame-data-table",
        `${GlobalConfig.apiHost}/api/advertisements`,
        `${GlobalConfig.apiHost}`,
        dataAdvertisementsColumns
    );

    table.processData = function (data) {
        return data.data.map((item) => {
            return [
                item.no,
                item.business_name,
                item.npwpd,
                item.advertisement_type,
                item.advertisement_content,
                item.business_address,
                item.advertisement_location,
                item.village_name,
                item.district_name,
                item.contact,
                item.id,
            ];
        });
    };

    table.init();
});