<?php

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

trait InputNumber
{
    protected $allow_number_type_options;
    protected $allow_number_type;
    protected $allow_decimal;
    protected $multiples;

    protected function setAllowNumberTypeOptions()
    {
        $this->allow_number_type_options = $this->choices->getChoices('allow_number_type_' . $this->data['field_type']);
    }
    
    protected function setAllowNumberType()
    {
        $this->allow_number_type = $this->fixEmptyValue('number_type', '', $this->allow_number_type_options);
    }
    
    protected function setAllowDecimal()
    {
        $this->allow_decimal = $this->fixEmptyValue('allow_decimal', '');
    }
    
    protected function setMultiples()
    {
        if (isset($this->value['multiples']) and is_array($this->value['multiples'])) {
            $this->multiples = implode(',', $this->fixEmptyValue('multiples', ''));
        }
    }

    protected function validationOptionsInputNumber()
    {
        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="mb-3">';
        $this->minCharsInputNumber();
        $this->maxCharsInputNumber();
        echo '</div>';        
        echo '<div class="mb-3 d-flex align-items-center">';
        $this->allowNumberType();
        echo '</div>';        
        echo '<div class="mb-3" id="gf-allow-decimal-value-wrap">';
        $this->allowDecimalValue();
        echo '</div>';
        echo '</div>';
        echo '<div class="col-md-12 gf-validation-options" id="gf-multiples-wrap">';
        $this->multiplesOf();
        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 minCharsInputNumber()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputNumber('min_chars', $this->min_chars, '');
        echo '<span class="me-2">' . esc_html($this->lang->getText('inputnumber_minchars_label')) . '</span>';
        echo '<span class="me-3">';
        $field->html();
        echo '</span>'; 
    }
    
    protected function maxCharsInputNumber()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputNumber('max_chars', $this->max_chars, '');
        echo '<span class="me-2">' . esc_html($this->lang->getText('inputnumber_maxchars_label')) . '</span>';
        echo '<span>';
        $field->html();
        echo '</span>';
    }
    
        protected function allowNumberType()
    {   
        $field = new \GriffinForms\Admin\Html\FormControls\SelectField('allow_number_type', $this->allow_number_type);
        $field->selectOptions($this->allow_number_type_options);
        echo '<label class="me-2">' . esc_html($this->lang->getText('allow_number_type', 'label')) . '</label>';
        $field->html();
    }       
    
    protected function allowDecimalValue()
    {   
        $data = array('label' => $this->lang->getText('allow_decimal'));
        $field = new \GriffinForms\Admin\Html\FormControls\toggleCheckbox('allow_decimal_value', $this->allow_decimal, $data);
        echo '<span class="me-3">';
        $field->html();
        echo '</span>';
    }
    
    protected function multiplesOf()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputText('multiples', $this->multiples);
        echo '<label class="me-2">' . esc_html($this->lang->getText('multiples_of', 'label')) . '</label>';
        $field->html();
    }
}