<?php

namespace GriffinForms\Admin\Html\Modals;

class DeleteItems extends \GriffinForms\Admin\Html\Modals\Format
{
    protected function headerText()
    {
        return $this->lang->getText('delete_items', 'modal_header');
    }
    
    protected function itemsSelectedText()
    {
        echo '<span id="griffinforms-delete-items-confirm-text">';
        echo wp_kses_post($this->lang->getText('delete_items', 'modal_text'));
        echo '</span>';
    }
    
    protected function noItemsSelectedText()
    {
        echo '<span id="griffinforms-delete-noitems-text" style="display:none;">';
        echo esc_html($this->lang->getText('no_items_selected', 'modal_text'));
        echo '</span>';
    }

    protected function modalContent()
    {
        echo '<div class="text-center">';
        $this->itemsSelectedText();
        $this->noItemsSelectedText();
        echo '</div>';
        
    }
        
    protected function modalFooter()
    {
        echo '<div class="modal-footer border-top-0 gap-2">';
        $this->cancelBtn();
        $this->ActionBtn('delete', 'primary');
        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) {
                $("#<?php echo esc_attr($this->action_btn_id); ?>").attr("data-bs-dismiss", "");
                updateItemCount();
            });
            
            function updateItemCount() {
                let count = 0;
                $(".griffinforms-listitem-cb").each(function() {
                    if ($(this).is(":checked")) {
                        count++;
                    }
                });
                
                $("#griffinforms-delete-items-confirm-text").children("strong").text(count);
                showNoticeText(count);
            }
            
            function showNoticeText(count) {
                if (count === 0) {
                    $("#griffinforms-delete-noitems-text").show();
                    $("#griffinforms-delete-items-confirm-text").hide();
                    $("#<?php echo esc_attr($this->action_btn_id); ?>").hide();
                } else {
                    $("#griffinforms-delete-noitems-text").hide();
                    $("#griffinforms-delete-items-confirm-text").show();
                    $("#<?php echo esc_attr($this->action_btn_id); ?>").show();
                }
            }
            
            $("#<?php echo esc_js($this->action_btn_id); ?>").click(function() {
                const selectedItems = [];
                $(".griffinforms-listitem-cb").each(function() {
                    if ($(this).is(":checked")) {
                        selectedItems.push($(this).val());
                    }
                });
                
                data = {
                    "action": "deleteItems",
                    "item_type": "<?php echo esc_js($this->data['item_type']); ?>",
                    "item_ids": selectedItems,
                    "nonce": "<?php echo esc_js(wp_create_nonce('delete_items')); ?>"
                };
    
                $.post(ajaxurl, data, function(response) {
                    if (response) {
                        location.reload();
                    }
                });
            });
        });
        <?php
        $inline_js = ob_get_clean();
    
        wp_add_inline_script('griffinforms-global-js', $inline_js);
    }
}