document.addEventListener("DOMContentLoaded", function () {
    loadListPlantData();

    // function update plant
    // $(document).on("click", ".btn-edit-plant", function (event) {
    //     let button = event.target;

    //     let id = $(this).data("id");
    //     let plant = button.getAttribute("data-plant");
    //     console.log(id, plant);

    //     $("#plant_id").val(id);
    //     $("#plant").val(plant);
    // });

    // function deleter criteria
    $(document).on("click", "#btn-hapus", function () {
        let id = $(this).data("id");
        Swal.fire({
            html: '<div class="mt-3"><lord-icon src="https://cdn.lordicon.com/gsqxdxog.json" trigger="loop" colors="primary:#f7b84b,secondary:#f06548" style="width:100px;height:100px"></lord-icon><div class="mt-4 pt-2 fs-15 mx-5"><h4>Apakah anda benar ?</h4><p class="text-muted mx-4 mb-0">Anda benar, akan menghapus data tanaman ?</p></div></div>',
            showCancelButton: !0,
            confirmButtonClass: "btn btn-primary w-xs me-2 mb-1",
            confirmButtonText: "Ya, hapus!",
            cancelButtonClass: "btn btn-danger w-xs mb-1",
            buttonsStyling: !1,
            showCloseButton: !0,
        }).then((result) => {
            if (result.isConfirmed) {
                window.location.href = "/list-data-tanaman/deletePlant/" + id;
            }
        });
    });
});

let gridInstance;

function loadListPlantData() {
    const tableContainerListPlantData = document.getElementById(
        "table-list-plant-data"
    );

    if (tableContainerListPlantData) {
        tableContainerListPlantData.innerHTML = "";
    }

    $.ajax({
        url: `/list-data-tanaman/getListPlantData`,
        type: "GET",
        dataType: "json",
        success: function (data) {
            const tableDataListPlant = data["listPlantData"].map(
                (item, index) => [
                    index + 1,
                    item["plant"],
                    // <button type="button" class="btn btn-warning btn-edit-plant" data-bs-toggle="modal" data-bs-target="#btn-edit" data-id="${item["id"]}"
                    // data-plant="${item["plant"]}">
                    //     ubah
                    // </button>
                    gridjs.html(
                        `<button type="button" class="btn btn-danger btn-md" data-id="${item["id"]}" id="btn-hapus">hapus</button>`
                    ),
                ]
            );

            if (gridInstance) {
                gridInstance
                    .updateConfig({
                        data: tableDataListPlant,
                    })
                    .forceRender();
            } else {
                gridInstance = new gridjs.Grid({
                    columns: [
                        { name: "No", width: "10%" },
                        { name: "Tanaman", width: "60%" },
                        { name: "Aksi", width: "30%" },
                    ],
                    data: tableDataListPlant,
                    search: true,
                    sort: true,
                    pagination: {
                        limit: 5,
                    },
                }).render(tableContainerListPlantData);
            }
        },
    });
}
