<?php

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

trait InputPhone
{
    protected $min_digits;
    protected $max_digits;
    protected $phone_format_type_options;
    protected $phone_format_type;
    protected $allow_extension;
    protected $allowed_country_codes;

    protected function setMinDigits()
    {
        $this->min_digits = $this->fixEmptyValue('min_digits', '');
    }

    protected function setMaxDigits()
    {
        $this->max_digits = $this->fixEmptyValue('max_digits', '');
    }

    protected function setPhoneFormatTypeOptions()
    {
        $this->phone_format_type_options = $this->choices->getChoices('phone_format_type_' . $this->data['field_type']);
    }

    protected function setPhoneFormatType()
    {
        $this->phone_format_type = $this->fixEmptyValue('format_type', '', $this->phone_format_type_options);
    }

    protected function setAllowExtension()
    {
        $this->allow_extension = $this->fixEmptyValue('allow_extension', 0);
    }

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

    protected function validationOptionsInputPhone()
    {
        echo '<div class="row">';

        echo '<div class="col-md-12 mb-3 gf-validation-options" id="gf-pattern-wrap">';
        $this->patternField();
        echo '</div>';

        echo '<div class="col-md-12 gf-validation-options" id="gf-custom-parameters-wrap">';
        echo '<div class="row">';

        echo '<div class="col-md-6 mb-3">';
        $this->minDigitsInputPhone();
        echo '</div>';

        echo '<div class="col-md-6 mb-3">';
        $this->maxDigitsInputPhone();
        echo '</div>';

        echo '<div class="col-md-6 mb-3">';
        $this->phoneFormatTypeInputPhone();
        echo '</div>';

        echo '<div class="col-md-6 mb-3 d-flex align-items-center">';
        $this->allowExtensionInputPhone();
        echo '</div>';

        echo '<div class="col-md-12 mb-3">';
        $this->allowedCountryCodesInputPhone();
        echo '</div>';

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

        echo '<div class="col-md-12 gf-validation-options" id="gf-db-validate-wrap">';
        $this->tableSelect();
        $this->columnSelect();
        $this->matchType();
        echo '</div>';

        echo '</div>';
    }

    protected function minDigitsInputPhone()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputNumber('min_digits', $this->min_digits, '');
        $field->setAttr('min', 1);
        echo '<label class="form-label me-2 mb-0">' . esc_html($this->lang->getText('inputphone_min_digits_label')) . '</label>';
        $field->html();
    }

    protected function maxDigitsInputPhone()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputNumber('max_digits', $this->max_digits, '');
        $field->setAttr('min', 1);
        echo '<label class="form-label me-2 mb-0">' . esc_html($this->lang->getText('inputphone_max_digits_label')) . '</label>';
        $field->html();
    }

    protected function phoneFormatTypeInputPhone()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\SelectField('phone_format_type', $this->phone_format_type);
        $field->selectOptions($this->phone_format_type_options);
        echo '<label class="form-label me-2 mb-0">' . esc_html($this->lang->getText('phone_format_type_label')) . '</label>';
        $field->html();
        echo '<p class="text-secondary small mb-0">' . esc_html($this->lang->getText('phone_format_type_description')) . '</p>';
    }

    protected function allowExtensionInputPhone()
    {
        $data = array('label' => $this->lang->getText('allow_extension_label'));
        $field = new \GriffinForms\Admin\Html\FormControls\toggleCheckbox('allow_extension', $this->allow_extension, $data);
        $field->html();
        echo '<p class="text-secondary small ms-2 mb-0">' . esc_html($this->lang->getText('allow_extension_description')) . '</p>';
    }

    protected function allowedCountryCodesInputPhone()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputText('allowed_country_codes', $this->allowed_country_codes);
        $field->setAttr('placeholder', esc_attr($this->lang->getText('allowed_country_codes_placeholder')));
        echo '<label class="form-label me-2 mb-0">' . esc_html($this->lang->getText('allowed_country_codes', 'label')) . '</label>';
        $field->html();
        echo '<p class="text-secondary small mb-0">' . esc_html($this->lang->getText('allowed_country_codes_description')) . '</p>';
    }
}
