<?php

namespace GriffinForms\Admin\Html\Modals;

class DeleteItem extends \GriffinForms\Admin\Html\Modals\Format
{   
    protected function headerText()
    {
        $text = $this->lang->getText('delete_item');
        return $text;
    }
    
    protected function modalContent()
    {
        echo '<div class="row">';
        echo '<div class="col-md-12 mb-2">';
        echo esc_html($this->lang->getText('confirm_delete_item'));
        echo ' ';
        echo '<span id="griffinforms-modal-checkitemrelations" class="text-primary" role="button">';
        echo esc_html($this->lang->getText('check_item_relations'));
        echo '</span>';
        echo '</div>';
        $this->waitAlert();
        echo '<div id="griffinforms-modal-iteminformation" class="col-md-12">';
        $this->searchSpinner();
        echo '</div>';
        echo '</div>';
        
    }
    
    protected function waitAlert()
    {
        $form_count = $this->data['sql']->getItemCount('form');
        if (absint($form_count) > 10) {
            echo '<div id="griffinforms-modal-waitalert" class="col-md-12 text-muted" style="visibility: hidden;">';
            echo esc_html($this->lang->getText('may_take_time'));
            echo '</div>'; 
        }
    }
    
    protected function searchSpinner()
    {
        echo '<div class=\"spinner-border spinner-border-sm text-primary\" role=\"status\">';
        echo '<span class=\"visually-hidden\">Loading...</span>';
        echo '</div>';
    }
    
    protected function modalFooter()
    {
        echo '<div class="modal-footer border-0">';
        $this->cancelBtn();
        $this->actionBtn('delete_btn');
        echo '</div>';
        
    }
    
    public function modalJs()
    {
        $nonce_info = wp_create_nonce('item_information');
        $form_id = isset($_GET['form_id']) ? absint($_GET['form_id']) : 0;
        ob_start();
        ?>
        jQuery(document).ready(function($) {
        
            var deleteModal, targetButton, actionButton, spinnerHtml, itemType, itemId, parentType, parentId, dataInfo;
            
            spinnerHtml = "<?php $this->searchSpinner(); ?>";
    
            deleteItemModal = document.getElementById("<?php echo esc_js($this->id); ?>");
            
            deleteItemModal.addEventListener("show.bs.modal", function (event) {
                                        
                $("#griffinforms-modal-waitalert").css("visibility", "hidden");
                $("#griffinforms-modal-iteminformation").css("visibility", "hidden");
                $("#griffinforms-modal-iteminformation").html(spinnerHtml);
                
                targetButton = event.relatedTarget;
                actionButton = $("#griffinforms-modalaction-deleteitem").get();
                itemType = $(targetButton).attr("data-item-type");
                itemId = $(targetButton).attr("data-item-id");
                parentCssId = $(targetButton).parents(".griffinforms-children-container").attr("id");
                idArray = parentCssId.split("-");
                parentType = idArray[1];
                parentId = idArray[2];
                
                $(actionButton).attr("data-item-type", itemType);
                $(actionButton).attr("data-item-id", itemId);
                $(actionButton).attr("data-parent-type", parentType);
                $(actionButton).attr("data-parent-id", parentId);
            });
            
            $("#griffinforms-modal-checkitemrelations").click(function() {
                
                $("#griffinforms-modal-waitalert").css("visibility", "visible");
                $("#griffinforms-modal-iteminformation").css("visibility", "visible");
    
                dataInfo = {
                    "action": "itemInformation",
                    "item_type": itemType,
                    "item_id": itemId,
                    "parent_type": parentType,
                    "parent_id": parentId,
                    "form_id": <?php echo esc_js($form_id); ?>,
                    "nonce": "<?php echo esc_js($nonce_info); ?>"
                };
    
                $.post(ajaxurl, dataInfo, function(response) {
                    $("#griffinforms-modal-iteminformation").html(response);
                    $("#griffinforms-modal-iteminformation").css("visibility", "visible");
                });
            });
        });
        <?php
        $inline_js = ob_get_clean();
    
        wp_add_inline_script('griffinforms-global-js', $inline_js);
    }
}