<?php

namespace GriffinForms\Admin\App\Html\Widgets\RightSidebar;

final class SingleFormSummary extends \GriffinForms\Admin\App\Html\Widgets\RightSidebar\Format
{

    public function getHtml()
    {
        $title = __('Form Summary', 'griffinforms-form-builder');
        $this->accordionHtml($title);
    }

    public function getFormSummaryContent()
    {
        $nonce_status = isset($_POST['nonce']) ? wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'single_form_summary') : false;
        
        if ($nonce_status === false) {
            $this->nonceError();
        }
        
        $this->item_id = isset($_POST['form_id']) ? absint(wp_unslash($_POST['form_id'])) : 0;
        $this->item = $this->sql->itemValues($this->item_id, 'form');
        $this->formProperties();
        $this->formActions();
        wp_die();
    }

    private function formProperties()
    {
        echo '<div id="' . esc_attr($this->getHtmlId('props')) . '" class="border-bottom p-4">';
        $this->itemName();
        $this->itemDescription();
        $this->formSubmissionsComplete();
        $this->formSubmissionsIncomplete();
        $this->itemCreatedOn();
        $this->itemCreatedBy();
        echo '</div>';
    }

    protected function formSubmissionsComplete()
    {
        if (isset($this->item->lifetime_submission_count) && is_numeric($this->item->lifetime_submission_count)) {
            $count = (int) $this->item->lifetime_submission_count;
        } else {
            $count = $this->sql->submissionCount($this->item_id, 1);
        }
        $this->propRow(__('Completed Submissions', 'griffinforms-form-builder'), $count);
    }

    protected function formSubmissionsIncomplete()
    {
        $count = $this->sql->submissionCount($this->item_id, 0);
        $this->propRow(__('Incomplete Submissions', 'griffinforms-form-builder'), $count);
    }

    private function formActions()
    {
        echo '<div id="' . esc_attr($this->getHtmlId('actions')) . '" class="align-self-end d-grid gap-2 p-4">';
        $this->editFormBtn();
        $this->deleteFormBtn();
        echo '</div>';
    }

    private function editFormBtn()
    {
        echo '<button id="' . esc_attr($this->getHtmlId('editbtn')) . '" class="btn btn-outline-dark rounded-1 btn-sm" type="button" data-gf-id="' . absint($this->item_id) . '">';
        esc_html_e('Edit Form', 'griffinforms-form-builder');
        echo '</button>';
    }

    private function deleteFormBtn()
    {
        echo '<button class="btn btn-danger rounded-1 btn-sm" ';
        echo 'id="' . esc_attr($this->getHtmlId('deletebtn')) . '" ';
        echo 'data-gf-formid="' . esc_attr($this->item_id) . '" ';
        echo 'type="button" data-bs-toggle="modal" data-bs-target="#griffinforms-app-deleteform-modal">';
        esc_html_e('Delete Form', 'griffinforms-form-builder');
        echo '</button>';
    }

    public function widgetJs()
    {
        $this->getFormSummaryJs();
        $this->editFormBtnJs();
    }

    private function getFormSummaryJs()
    {
        $form_details_nonce = wp_create_nonce('single_form_summary');

        echo 
        'document.addEventListener("form-selected", () => {

            let count = selectedForms.length;
            if (count !== 1) {
                $("#' . esc_attr($this->getHtmlId()) . '").hide();
                return;
            }

            $("#' . esc_attr($this->getHtmlId()) . '").find(".accordion-body").html("");
            $("#' . esc_attr($this->getHtmlId()) . '").show();
            $("#' . esc_attr($this->getHtmlId('spinner')) . '").show();

            const data = {
                "action": "getFormSummaryContent",
                "form_id": selectedForms[0],
                "nonce": "' . esc_js($form_details_nonce) . '"
            };

            $.post(ajaxurl, data, function(response) {
                $("#' . esc_attr($this->getHtmlId('content')) . '").find(".accordion-body").html(response);
                $("#' . esc_attr($this->getHtmlId('spinner')) . '").hide();
            });
        });
        ' . PHP_EOL;
    }

    protected function editFormBtnJs()
    {
        echo '
        $("#griffinforms-app-rightsidebar").on("click", "#' . esc_attr($this->getHtmlId('editbtn')) . '", function(){
            sessionStorage.setItem("gfFormId", $(this).attr("data-gf-id"));
            document.dispatchEvent(formLayout);
        });
        ';
    }

    public function __construct()
    {
        $this->setAll();
        add_action('wp_ajax_getFormSummaryContent', array($this, 'getFormSummaryContent'));
    }
}
