<?php

namespace GriffinForms\Admin\Js\Local;

class Lists extends \GriffinForms\Admin\Js\Local\Format
{
    protected function localJs()
    {
        $js = '';
        $js .= $this->showSuccessAlert() . PHP_EOL;
        $js .= $this->bulkActionBtn() . PHP_EOL;
        $js .= $this->deleteItem() . PHP_EOL;
        $js .= $this->listJs() . PHP_EOL;
        return $js;
    }

    protected function showSuccessAlert()
    {
        return '
        const successMessage = sessionStorage.getItem("griffinforms_success_message");
            
            if (successMessage) {
                // Parse and display the success message at the top of the page
                const messages = JSON.parse(successMessage);
                const messageContainer = $("<div class=\"notice notice-success is-dismissible mt-3 p-1\"><ul class=\"m-2 p-1\"></ul></div>");

                messages.forEach(function(message) {
                    messageContainer.find("ul").append("<li>" + message + "</li>");
                });

                $(".wrap h1").after(messageContainer);

                // Remove message from sessionStorage after displaying it
                sessionStorage.removeItem("griffinforms_success_message");
            }
        ';
    }

    protected function bulkActionBtn()
    {
        return '
        $("#bulk-action-selector-top").change(btnAction);
        $("#bulk-action-selector-bottom").change(btnAction);

        function btnAction() {
            let action = $(this).val();
            
            if (action == -1) {
                $(".action").attr("data-bs-toggle", "");
                $(".action").attr("data-bs-target", "");
                $(".action").addClass("disabled");
                return;
            }
            
            let modalId = "#griffinforms-" + action + "-modal";
            $(".action").attr("data-bs-toggle", "modal");
            $(".action").attr("data-bs-target", modalId);
            $(".action").removeClass("disabled");
        }
        ';
    }

    // Redundant function, but kept for consistency. Used to directly delete a list item without modal confirmation.
    public function deleteItem()
    {
        return '$(".griffinforms-delete-item").click(deleteItem);
        function deleteItem() {
                let row = $(this).parents("tr");
                row.fadeOut();
                const data = {
                    "action": "deleteItem",
                    "item_type": "' . esc_js($this->data['item_type']) . '",
                    "item_id": $(this).attr("data-gf-id"),
                    "nonce": $(this).attr("data-gf-nonce")
                };

                jQuery.post(ajaxurl, data, function(response) {
                    if (response == 1) {
                        row.remove();
                        message = "' . esc_js($this->lang->getText($this->data['item_type'] . 'deleted')) . '";
                        location.reload();
                    } else {
                        row.fadeIn();
                        message = "' . esc_js($this->lang->getText($this->data['item_type'] . 'unable_to_delete')) . '";
                        griffinformsSnackBar(message);
                    }
                });
        };';
    }

    protected function listJs()
    {
        
    }
}