<?php

namespace GriffinForms\Admin\App\Html\Modals\Accounts;

trait Requests
{
    protected function setRegisterAjaxHandlers()
    {
        add_action('wp_ajax_getAccountRegistrationConfig', array($this, 'getAccountRegistrationConfig'));
        add_action('wp_ajax_getAccountRuleEditorData', array($this, 'getAccountRuleEditorData'));
        add_action('wp_ajax_getAccountRulesListHtml', array($this, 'getAccountRulesListHtml'));
        add_action('wp_ajax_getAccountRuleEditorHtml', array($this, 'getAccountRuleEditorHtml'));
        add_action('wp_ajax_saveAccountRegistrationConfig', array($this, 'saveAccountRegistrationConfig'));
        add_action('wp_ajax_deleteAccountRegistrationRule', array($this, 'deleteAccountRegistrationRule'));
    }

    protected function getRequestsJs()
    {
        $this->getAccountRegistrationConfigJs();
        $this->getAccountRuleEditorDataJs();
        $this->getAccountRulesListHtmlJs();
        $this->getAccountRuleEditorHtmlJs();
        $this->saveAccountRegistrationConfigJs();
        $this->deleteAccountRegistrationRuleJs();
    }

    protected function getAccountRegistrationConfigJs()
    {
        $nonce = wp_create_nonce('get_account_registration_config');
        $form_id = (int) $this->utility->getFormId();

        echo '
        function griffinformsGetAccountRegistrationConfig() {
            return jQuery.post(ajaxurl, {
                action: "getAccountRegistrationConfig",
                form_id: ' . $form_id . ',
                nonce: "' . esc_js($nonce) . '"
            });
        }
        ';
    }

    protected function getAccountRuleEditorDataJs()
    {
        $nonce = wp_create_nonce('get_account_rule_editor_data');
        $form_id = (int) $this->utility->getFormId();

        echo '
        function griffinformsGetAccountRuleEditorData() {
            return jQuery.post(ajaxurl, {
                action: "getAccountRuleEditorData",
                form_id: ' . $form_id . ',
                nonce: "' . esc_js($nonce) . '"
            });
        }
        ';
    }

    protected function getAccountRulesListHtmlJs()
    {
        $nonce = wp_create_nonce('get_account_rules_list_html');
        $form_id = (int) $this->utility->getFormId();

        echo '
        function griffinformsGetAccountRulesListHtml(settingsPayload) {
            return jQuery.post(ajaxurl, {
                action: "getAccountRulesListHtml",
                form_id: ' . $form_id . ',
                settings: JSON.stringify(settingsPayload || {}),
                nonce: "' . esc_js($nonce) . '"
            });
        }
        ';
    }

    protected function getAccountRuleEditorHtmlJs()
    {
        $nonce = wp_create_nonce('get_account_rule_editor_html');
        $form_id = (int) $this->utility->getFormId();

        echo '
        function griffinformsGetAccountRuleEditorHtml(rulePayload, settingsPayload, isPersistedRule) {
            return jQuery.post(ajaxurl, {
                action: "getAccountRuleEditorHtml",
                form_id: ' . $form_id . ',
                rule: JSON.stringify(rulePayload || {}),
                settings: JSON.stringify(settingsPayload || {}),
                is_persisted_rule: isPersistedRule ? "1" : "0",
                nonce: "' . esc_js($nonce) . '"
            });
        }
        ';
    }

    protected function saveAccountRegistrationConfigJs()
    {
        $nonce = wp_create_nonce('save_account_registration_config');
        $form_id = (int) $this->utility->getFormId();

        echo '
        function griffinformsSaveAccountRegistrationConfig(payload) {
            return jQuery.post(ajaxurl, {
                action: "saveAccountRegistrationConfig",
                form_id: ' . $form_id . ',
                payload: JSON.stringify(payload || {}),
                nonce: "' . esc_js($nonce) . '"
            });
        }
        ';
    }

    protected function deleteAccountRegistrationRuleJs()
    {
        $nonce = wp_create_nonce('delete_account_registration_rule');
        $form_id = (int) $this->utility->getFormId();

        echo '
        function griffinformsDeleteAccountRegistrationRule(ruleId) {
            return jQuery.post(ajaxurl, {
                action: "deleteAccountRegistrationRule",
                form_id: ' . $form_id . ',
                rule_id: ruleId || "",
                nonce: "' . esc_js($nonce) . '"
            });
        }
        ';
    }
}
