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

class PbgTaskAssignments {
    init() {
        this.initTablePbgTaskAssignments();
    }

    initTablePbgTaskAssignments() {
        let tableContainer = document.getElementById(
            "table-pbg-task-assignments"
        );

        let uuid = document.getElementById("uuid").value;

        new Grid({
            columns: [
                "ID",
                "Nama",
                "Email",
                "Nomor Telepon",
                "Keahlian",
                "Status",
            ],
            search: {
                server: {
                    url: (prev, keyword) => `${prev}?search=${keyword}`,
                },
                debounceTimeout: 1000,
            },
            pagination: {
                limit: 15,
                server: {
                    url: (prev, page) =>
                        `${prev}${prev.includes("?") ? "&" : "?"}page=${
                            page + 1
                        }`,
                },
            },
            sort: true,
            server: {
                url: `${GlobalConfig.apiHost}/api/task-assignments/${uuid}`,
                credentials: "include",
                headers: {
                    Authorization: `Bearer ${document
                        .querySelector('meta[name="api-token"]')
                        .getAttribute("content")}`,
                    "Content-Type": "application/json",
                },
                then: (data) =>
                    data.data.map((item) => [
                        item.id,
                        item.name,
                        item.email,
                        item.phone_number,
                        item.expertise,
                        item.status_name,
                    ]),
                total: (data) => data.meta.total,
            },
        }).render(tableContainer);
    }
}

document.addEventListener("DOMContentLoaded", function (e) {
    new PbgTaskAssignments().init();
});
