<?php

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

use GriffinForms\Admin\Html\Elements\LogsTimeline;

final class FormLogsTimeline extends \GriffinForms\Admin\App\Html\Widgets\RightSidebar\Format
{
    public function getHtml()
    {
        $title = __('Recent Logs', 'griffinforms-form-builder');
        $this->accordionHtml($title);
    }

    protected function accordionBody()
    {
        echo '<div class="p-3">';
        echo '<div class="text-muted small">' . esc_html__('Loading logs...', 'griffinforms-form-builder') . '</div>';
        echo '</div>';
    }

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

    private function refreshTimelineJs()
    {
        $nonce = wp_create_nonce('form_logs_timeline');

        echo '
        const refreshFormLogsTimeline = () => {
            const formElement = $("#griffinforms-app-presentationarea").find("[data-gf-itemtype=form]");
            const formId = formElement.attr("data-gf-itemid");
            if (!formId) {
                $("#' . esc_attr($this->getHtmlId()) . '").hide();
                return;
            }

            $("#' . esc_attr($this->getHtmlId()) . '").show();
            $("#' . esc_attr($this->getHtmlId('spinner')) . '").show();

            const data = {
                "action": "getFormLogsTimelineContent",
                "form_id": formId,
                "nonce": "' . esc_js($nonce) . '"
            };

            $.post(ajaxurl, data, function(response) {
                $("#' . esc_attr($this->getHtmlId('content')) . '").find(".accordion-body").html(response);
                $("#' . esc_attr($this->getHtmlId('spinner')) . '").hide();
            }).fail(function() {
                $("#' . esc_attr($this->getHtmlId('content')) . '").find(".accordion-body").html("<div class=\"gf-notice gf-notice-danger\">' . esc_js(__('Failed to load logs. Please try again.', 'griffinforms-form-builder')) . '</div>");
                $("#' . esc_attr($this->getHtmlId('spinner')) . '").hide();
            });
        };

        document.addEventListener("gf-rightsidebar-formtab-activated", () => {
            refreshFormLogsTimeline();
        });

        document.addEventListener("form-updated", () => {
            refreshFormLogsTimeline();
        });

        document.addEventListener("form-html-received", () => {
            refreshFormLogsTimeline();
        });

        refreshFormLogsTimeline();
        ' . PHP_EOL;
    }

    public function getFormLogsTimelineContent()
    {
        $this->checkRequest('form_logs_timeline');

        $form_id = isset($_POST['form_id']) ? absint(wp_unslash($_POST['form_id'])) : 0;
        if (!$form_id) {
            echo '<div class="gf-log-empty text-muted small">' . esc_html__('Invalid form ID.', 'griffinforms-form-builder') . '</div>';
            wp_die();
        }

        $options = array(
            'limit' => 10,
            'empty_message' => __('No logs recorded for this form yet.', 'griffinforms-form-builder'),
        );

        echo '<div class="p-3 pb-4">';
        $timeline = new LogsTimeline('form', $form_id, $options);
        $timeline->html();
        echo '</div>';
        wp_die();
    }

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