<?php

namespace GriffinForms\Admin\Html\Modals;

class DeleteListItem extends \GriffinForms\Admin\Html\Modals\Format
{
    protected function headerText()
    {
        return $this->lang->getText('delete_list_item', 'modal_header');
    }
    
    protected function confirmationText()
    {
        echo '<span id="griffinforms-delete-items-confirm-text">';
        echo wp_kses_post($this->lang->getText('delete_list_item', 'modal_text'));
        echo '</span>';
    }
    
    protected function modalContent()
    {
        echo '<div class="p-3 gf-delete-modal-content">';

        echo '<div class="mb-3">';
        echo '<div class="modal-alert gf-delete-modal-alert" role="alert" style="display:none;"></div>';
        echo '</div>';

        echo '<div class="gf-delete-modal-warning mb-3">';
        echo '<span class="dashicons dashicons-warning"></span>';
        echo '<div class="gf-delete-modal-warning-copy">';
        echo '<div class="gf-delete-modal-warning-title">' . esc_html__('This action cannot be undone.', 'griffinforms-form-builder') . '</div>';
        echo '<div class="gf-delete-modal-warning-description">' . esc_html__('Please confirm you want to continue.', 'griffinforms-form-builder') . '</div>';
        echo '</div>';
        echo '</div>';

        echo '<div class="mb-2 fw-semibold gf-delete-modal-message">';
        $this->confirmationText();
        echo '</div>';

        echo '<div class="mt-3">';
        $this->additionalOptions();
        echo '</div>';
        
        echo '</div>';
        
    }

    protected function additionalOptions()
    {
        if ($this->data['item_type'] == 'form') {
            echo '<div class="">';
            echo '<label for="gf-also-delete-entries">';
            echo '<input name="gf-also-delete-entries" type="checkbox" id="gf-also-delete-entries" value="1">';
            echo esc_html($this->lang->getText('delete_form_submissions', 'modal_text'));
	        echo '</label>';
            echo '</div>';
        }
    }
        
    protected function modalFooter()
    {
        echo '<div class="modal-footer border-top-0 gap-2">';
        $this->cancelBtn();
        $this->ActionBtn('delete', 'danger');
        echo '</div>';  
    }
    
    public function modalJs()
    {
        ob_start();
        ?>
        jQuery(document).ready(function($) {
    
            let modal = document.getElementById("<?php echo esc_js($this->id); ?>");
            
            modal.addEventListener("show.bs.modal", function (event) {
                $(".modal-alert").hide().text("");
                $("#<?php echo esc_attr($this->action_btn_id); ?>").attr("data-bs-dismiss", "");
                const triggerElement = event.relatedTarget;
                if (!triggerElement) {
                    return;
                }
                let itemId = parseInt(triggerElement.getAttribute("data-gf-id"), 10);
                $("#<?php echo esc_attr($this->action_btn_id); ?>").attr("data-gf-itemid", itemId);

            });
            
            $("#<?php echo esc_js($this->action_btn_id); ?>").click(function() {
                const $actionBtn = $(this);
                if ($actionBtn.prop("disabled")) {
                    return;
                }
                $actionBtn.prop("disabled", true).text("<?php echo esc_js(__('Deleting...', 'griffinforms-form-builder')); ?>");
                let alsoDeleteSubmissions = 0;

                // Only include this field if the checkbox exists (i.e., if item_type is 'form')
                const checkbox = document.getElementById("gf-also-delete-entries");
                if (checkbox && checkbox.checked) {
                    alsoDeleteSubmissions = 1;
                }

                const data = {
                    "action": "deleteItem",
                    "item_type": "<?php echo esc_js($this->data['item_type']); ?>",
                    "item_id": $(this).attr("data-gf-itemid"),
                    "nonce": "<?php echo esc_js(wp_create_nonce('delete_item')); ?>",
                    "delete_submissions": alsoDeleteSubmissions
                };

                $.post(ajaxurl, data, function(response) {
                    if (response === '1' || response === 1) {
                        location.reload();
                    } else {
                        $(".modal-alert").text("<?php echo esc_js($this->lang->getText('delete_failed', 'modal_text')); ?>").show();
                        $actionBtn.prop("disabled", false).text("<?php echo esc_js($this->lang->getText('delete', 'action_btn')); ?>");
                    }
                }).fail(function() {
                    $(".modal-alert").text("<?php echo esc_js($this->lang->getText('ajax_request_failed', 'modal_text')); ?>").show();
                    $actionBtn.prop("disabled", false).text("<?php echo esc_js($this->lang->getText('delete', 'action_btn')); ?>");
                });
            });
        });

        <?php
        $inline_js = ob_get_clean();
    
        wp_add_inline_script('griffinforms-global-js', $inline_js);
    }
}
