<?php

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

trait InputUrl
{
    protected $allow_protocol_type_options;
    protected $allow_protocol_type;
    protected $restricted_tld;
    protected $excluded_tld;

    protected function setAllowProtocolTypeOptions()
    {
        $this->allow_protocol_type_options = $this->choices->getChoices('allow_protocol_type_' . $this->data['field_type']);
    }
    
    protected function setAllowProtocolType()
    {
        $this->allow_protocol_type = $this->fixEmptyValue('protocols', '');
    }
    
    protected function setRestrictedTld()
    {
        if (isset($this->value['restricted_tld']) and is_array($this->value['restricted_tld'])) {
            $this->restricted_tld = implode(', ', $this->value['restricted_tld']);
        } else {
            $this->restricted_tld = '';
        }
    }
    
    protected function setExcludedTld()
    {
        if (isset($this->value['excluded_tld']) and is_array($this->value['excluded_tld'])) {
            $this->excluded_tld = implode(', ', $this->value['excluded_tld']);
        } else {
            $this->excluded_tld = '';
        }
    }

    protected function validationOptionsInputUrl()
    {
        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->allowProtocolType();
        echo '</div>';        
        
        echo '<div class="mb-3">';
        $this->restrictTld();
        echo '</div>';      
        
        echo '<div class="mb-3">';
        $this->excludeTld();
        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 allowProtocolType()
    {
        $data = array('lang' => $this->lang);
        $field = new \GriffinForms\Admin\Html\FormControls\inputCheckbox('allow_protocol_type', $this->allow_protocol_type, $data);
        $field->checkboxOptions(array_keys($this->allow_protocol_type_options));
        echo '<span class="mb-3 me-3">';
        $field->html();
        echo '</span>';
    }
    
    protected function restrictTld()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\TextArea('restricted_tld_names', $this->restricted_tld);
        
        echo '<div id="griffinforms-field-restricted-tld-names-wrap">';
        echo '<label class="form-label">' . esc_html($this->lang->getText('restricted_tld_names', 'label')) . '</label>';
        echo '<div>';
        $field->html();
        echo '</div>';
        echo '</div>';
    }
    
    protected function excludeTld()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\TextArea('excluded_tld_names', $this->excluded_tld);
        
        echo '<div id="griffinforms-field-restricted-tld-names-wrap">';
        echo '<label class="form-label">' . esc_html($this->lang->getText('restricted_exclude_names', 'label')) . '</label>';
        echo '<div>';
        $field->html();
        echo '</div>';
        echo '</div>';
    }
}