<?php

namespace GriffinForms\Admin\Js\Local;

class Payments extends \GriffinForms\Admin\Js\Local\Settings
{
    protected function getOptionsDataJs()
    {
        $js = '
        function getOptionsData() {
            getPaymentCurrencyOption();
            getActiveGatewayOption();
            getTaxOptions();
        }
        ';

        $js .= $this->getPaymentCurrencyOptionJs();
        $js .= $this->getActiveGatewayOptionJs();
        $js .= $this->getTaxOptionsJs();

        $js .= $this->disableToggleJs();
        $js .= $this->toggleTaxFieldsJs();

        return $js;
    }

    protected function getPaymentCurrencyOptionJs()
    {
        return '
        function getPaymentCurrencyOption() {
            var currency = $("#griffinforms-settings-payments-payment_currency").val() || "USD";
            optionsData["payment_currency"] = currency;
        }
        ';
    }

    protected function getTaxOptionsJs()
    {
        return '
        function getTaxOptions() {
            var taxEnabledField = jQuery("#griffinforms-settings-payments-taxenabled");
            optionsData["tax_enabled"] = taxEnabledField.length && taxEnabledField.is(":checked") ? 1 : 0;

            var taxTypeField = jQuery("#griffinforms-settings-payments-taxratetype");
            optionsData["tax_rate_type"] = (taxTypeField.length ? taxTypeField.val() : "") || "percentage";

            var rateField = jQuery("#griffinforms-settings-payments-taxratevalue");
            var rateValue = rateField.length ? parseFloat(rateField.val()) : 0;
            optionsData["tax_rate_value"] = isNaN(rateValue) ? 0 : rateValue;

            var labelField = jQuery("#griffinforms-settings-payments-taxdisplaylabel");
            optionsData["tax_display_label"] = labelField.length ? jQuery.trim(labelField.val()) : "";

            var regLabelField = jQuery("#griffinforms-settings-payments-taxregistrationlabel");
            optionsData["tax_registration_label"] = regLabelField.length ? jQuery.trim(regLabelField.val()) : "";

            var regNumberField = jQuery("#griffinforms-settings-payments-taxregistrationnumber");
            optionsData["tax_registration_number"] = regNumberField.length ? jQuery.trim(regNumberField.val()) : "";
        }
        ';
    }

    protected function getActiveGatewayOptionJs()
    {
        return '
        function getActiveGatewayOption() {
            var gateways = [];
            $("input[name=\'allowed_payment_gateways[]\']:checked").each(function() {
                var value = $(this).val();
                if (value && value !== "none") {
                    gateways.push(value);
                }
            });

            if (gateways.length === 0) {
                optionsData["allowed_payment_gateways"] = [];
            } else {
                optionsData["allowed_payment_gateways"] = gateways;
            }
        }
        ';
    }

    protected function disableToggleJs()
    {
        return '
        jQuery(document).on("change", "input[name=\'allowed_payment_gateways[]\'][value=\'none\']", function() {
            if (this.checked) {
                jQuery("input[name=\'allowed_payment_gateways[]\']").not(this).prop("checked", false);
            }
        });

        jQuery(document).on("change", "input[name=\'allowed_payment_gateways[]\']:not([value=\'none\'])", function() {
            if (this.checked) {
                jQuery("input[name=\'allowed_payment_gateways[]\'][value=\'none\']").prop("checked", false);
            }
        });
        ';
    }

    protected function toggleTaxFieldsJs()
    {
        return '
        jQuery(document).ready(function() {
            const wrapper = jQuery("#griffinforms-settings-payments-tax-wrapper");
            const toggle = jQuery("#griffinforms-settings-payments-taxenabled");
            const updateVisibility = function() {
                if (toggle.is(":checked")) {
                    wrapper.stop(true, true).fadeIn(200);
                } else {
                    wrapper.stop(true, true).fadeOut(200);
                }
            };
            updateVisibility();
            toggle.on("change", updateVisibility);
        });
        ';
    }
}
