<?php

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

trait InputEmail
{
    protected $is_restricted_domains;
    protected $restricted_domains;
    protected $excluded_domains;

    protected function setIsRestrictedDomains()
    {
        $this->is_restricted_domains = $this->fixEmptyValue('is_restricted_domains', 0);
    }
    
    protected function setRestrictedDomains()
    {
        if (isset($this->value['restricted_domains']) and is_array($this->value['restricted_domains'])) {
            $this->restricted_domains = implode(', ', $this->value['restricted_domains']);
        }
    }
    
    protected function setExcludedDomains()
    {
        if (isset($this->value['excluded_domains']) and is_array($this->value['excluded_domains'])) {
            $this->excluded_domains = implode(', ', $this->value['excluded_domains']);
        }    
    }

    protected function validationOptionsInputEmail()
    {
        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->restrictDomains();
        echo '</div>';      
        
        echo '<div class="mb-3">';
        $this->excludeDomains();
        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 restrictDomains()
    {
        $data = array('label' => $this->lang->getText('restrict_domains'));
        $toggle_field = new \GriffinForms\Admin\Html\FormControls\toggleCheckbox('restrict_domains', $this->is_restricted_domains, $data);
        $domains_field = new \GriffinForms\Admin\Html\FormControls\TextArea('restricted_domain_names', $this->restricted_domains);
        
        echo '<div class="mb-3">';
        $toggle_field->html();
        echo '</div>';
        
        echo '<div id="griffinforms-field-restricted-domain-names-wrap">';
        echo '<label class="form-label">' . esc_html($this->lang->getText('restricted_domain_names', 'label')) . '</label>';
        echo '<div>';
        $domains_field->html();
        echo '</div>';
        echo '</div>';
    }
    
    protected function excludeDomains()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\TextArea('excluded_domain_names', $this->excluded_domains);
        
        echo '<div class="griffinforms-field-excluded-domain-names-wrap">';
        echo '<label class="form-label">' . esc_html($this->lang->getText('excluded_domain_names', 'label')) . '</label>';
        echo '<div>';
        $field->html(); 
        echo '</div>';
        echo '</div>';
    }
}