<?php

namespace GriffinForms\Admin\Html\FormControls\Field\ValidationTraits;

trait InputAddress
{
    protected $country_filter_mode_options = array();
    protected $country_filter_mode = '';
    protected $allowed_countries = '';
    protected $excluded_countries = '';

    protected function setCountryFilterModeOptions()
    {
        $this->country_filter_mode_options = $this->choices->getChoices('country_filter_mode_inputaddress');
    }

    protected function setCountryFilterMode()
    {
        $this->country_filter_mode = $this->fixEmptyValue('country_filter_mode', '', $this->country_filter_mode_options);
    }

    protected function setAllowedCountries()
    {
        if (isset($this->value['allowed_countries']) && is_array($this->value['allowed_countries'])) {
            $this->allowed_countries = implode(', ', $this->value['allowed_countries']);
        } else {
            $this->allowed_countries = '';
        }
    }

    protected function setExcludedCountries()
    {
        if (isset($this->value['excluded_countries']) && is_array($this->value['excluded_countries'])) {
            $this->excluded_countries = implode(', ', $this->value['excluded_countries']);
        } else {
            $this->excluded_countries = '';
        }
    }

    protected function validationOptionsInputAddress()
    {
        echo '<div class="row">';
        echo '<div class="col-md-12 gf-validation-options" id="gf-custom-parameters-wrap">';
        echo '<div class="mb-3">';
        $this->countryFilterModeInputAddress();
        echo '</div>';

        echo '<div class="mb-3" id="griffinforms-address-allowed-wrap">';
        $this->allowedCountriesInputAddress();
        echo '</div>';

        echo '<div class="mb-3" id="griffinforms-address-excluded-wrap">';
        $this->excludedCountriesInputAddress();
        echo '</div>';

        echo '</div>';
        echo '</div>';
    }

    protected function countryFilterModeInputAddress()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\SelectField('country_filter_mode', $this->country_filter_mode);
        $field->selectOptions($this->country_filter_mode_options);
        echo '<label class="form-label mb-1" for="' . esc_attr($field->elementId()) . '">';
        echo esc_html($this->lang->getText('country_filter_mode_label'));
        echo '</label>';
        $field->html();
        echo '<p class="text-secondary small mb-0">' . esc_html($this->lang->getText('country_filter_mode_description')) . '</p>';
    }

    protected function allowedCountriesInputAddress()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\TextArea('address_allowed_countries', $this->allowed_countries, array('rows' => 4));
        echo '<label class="form-label mb-1" for="' . esc_attr($field->elementId()) . '">';
        echo esc_html($this->lang->getText('allowed_countries_label'));
        echo '</label>';
        $field->html();
        echo '<p class="text-secondary small mb-0">' . esc_html($this->lang->getText('allowed_countries_description')) . '</p>';
    }

    protected function excludedCountriesInputAddress()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\TextArea('address_excluded_countries', $this->excluded_countries, array('rows' => 4));
        echo '<label class="form-label mb-1" for="' . esc_attr($field->elementId()) . '">';
        echo esc_html($this->lang->getText('excluded_countries_label'));
        echo '</label>';
        $field->html();
        echo '<p class="text-secondary small mb-0">' . esc_html($this->lang->getText('excluded_countries_description')) . '</p>';
    }

    protected function inputAddressJs()
    {
        ?>
        jQuery(function($) {
            const modeSelect = $("#griffinforms-countryfiltermode-selectfield");
            const allowedWrap = $("#griffinforms-address-allowed-wrap");
            const excludedWrap = $("#griffinforms-address-excluded-wrap");

            function toggleCountryInputs() {
                const mode = modeSelect.val();
                if (mode === "limit_to_countries") {
                    allowedWrap.show();
                    excludedWrap.hide();
                } else if (mode === "exclude_countries") {
                    allowedWrap.hide();
                    excludedWrap.show();
                } else {
                    allowedWrap.hide();
                    excludedWrap.hide();
                }
            }

            modeSelect.on("change", toggleCountryInputs);
            toggleCountryInputs();
        });
        <?php
    }
}
