<?php

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

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

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

    public function getFormsSummaryContent()
    {
        $this->checkNonce('multi_form_summary');
        
        if (!isset($_POST['form_ids'])) {
            wp_die();
        }
        
        $this->item_id = json_decode(wp_unslash($_POST['form_ids']), true);
        if (!is_array($this->item_id) || json_last_error() !== JSON_ERROR_NONE || empty($this->item_id)) {
            wp_die();
        }
        $this->item_id = array_map('absint', $this->item_id);

        $this->formProperties();
        $this->formActions();
        wp_die();
    }

    private function formProperties()
    {
        echo '<div id="' . esc_attr($this->getHtmlId('props')) . '" class="p-4">';

        $this->formsSelectedCount();
        $this->formsSubmissionsComplete();
        $this->formsSubmissionsIncomplete();
        echo '</div>';
    }

    protected function formsSelectedCount()
    {
        $count = count($this->item_id);
        // translators: %d refers to the number of forms selected
        $this->propRow(__('Selected', 'griffinforms-form-builder'), sprintf(__(' %d forms', 'griffinforms-form-builder'), $count));
    }
    protected function formsSubmissionsComplete()
    {
        if (method_exists($this->sql, 'lifetimeSubmissionCountMulti')) {
            $count = $this->sql->lifetimeSubmissionCountMulti($this->item_id);
        } else {
            $count = $this->sql->submissionCountMulti($this->item_id, 1);
        }
        // translators: %d refers to the number of times the form has been submitted completely
        $this->propRow(__('Complete', 'griffinforms-form-builder'), sprintf(__(' %d times', 'griffinforms-form-builder'), $count));
    }

    protected function formsSubmissionsIncomplete()
    {
        $count = $this->sql->submissionCountMulti($this->item_id, 0);
        // translators: %d refers to the number of times the form has been submitted partially
        $this->propRow(__('Incomplete', 'griffinforms-form-builder'), sprintf(__(' %d times', '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->deleteFormBtn();
        echo '</div>';
    }

    private function deleteFormBtn()
    {
        echo '<button class="btn btn-danger rounded-1 btn-sm" type="button">';
        // translators: %d refers to the number of forms to be deleted
        echo esc_html(sprintf(__('Delete %d Forms', 'griffinforms-form-builder'), absint(count($this->item_id))));
        echo '</button>';
    }

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

    private function getFormSummaryJs()
    {
        $forms_details_nonce = wp_create_nonce('multi_form_summary');

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

            let count = selectedForms.length;
            if (count < 2) {
                $("#' . 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": "getFormsSummaryContent",
                "form_ids": JSON.stringify(selectedForms),
                "nonce": "' . esc_js($forms_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;
    }

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