<?php

namespace GriffinForms\Admin\Html\FormControls\Field;

class Iterations extends \GriffinForms\Admin\Html\FormControls\Field\Format
{
    protected $lang;
	protected $secure;
	protected $value;
    protected $allow_iterations;
    protected $min_iterations;
    protected $max_iterations;
    protected $add_button_label;
    protected $is_unique_value;
    
    protected function setValue()
    {
    	$this->value = $this->secure->data('iterations', $this->value);
    }
    
    protected function setAllowIterations()
    {
        $this->allow_iterations = $this->fixEmptyValue('allow', 0);
    }
    
    protected function setMinIterations()
    {
        $this->min_iterations = $this->fixEmptyValue('min', 0);
    }
    
    protected function setMaxIterations()
    {
        $this->max_iterations = $this->fixEmptyValue('max', '');
    }
    
    protected function setAddButtonLabel()
    {
        $default_label = $this->lang->getText('add_iteration_button_label');
        $this->add_button_label = $this->fixEmptyValue('add_button_label', $default_label);
    }
    
    
    protected function setIsUniqueValue()
    {
        $this->is_unique_value = $this->fixEmptyValue('unique', 0);
    }
    
    protected function iterationToggle()
    {
        $checked = '';
        if($this->allow_iterations === 1) {
            $checked = ' checked';
        }
        
        echo '<div class="col-md-12 mb-3">';
        echo '<label for="allow-multiple-iterations">';
        esc_html($this->lang->getText('allow_multiple_iterations'));
        echo '</label> ';
        echo '<input type="checkbox" id="allow-multiple-iterations" class="ms-2"';
        echo esc_attr($checked) . '>';
        echo '</div>';
        
    }
    
    protected function iterationsCount()
    {
        echo '<div class="mb-3" id="iterations-count-wrap">';
		echo '<span>' . esc_html($this->lang->getText('iteration_count_1')) . '</span>';
        echo '<input type="number" min="0" id="min-iterations-allowed" class="mx-2" style="min-width:100px !important; width: 100px;" value="' . absint($this->min_iterations) . '">';
		echo '<span>' . esc_html($this->lang->getText('iteration_count_2')) . '</span>';
		echo '<input type="number" min="0" id="max-iterations-allowed" class="mx-2" style="min-width:100px !important; width: 100px;" value="' . absint($this->max_iterations) . '">';
        echo '<span>' . esc_html($this->lang->getText('iteration_count_3')) . '</span>';
        echo '<div id="min-iteration-suggestion" class="griffinforms-formcontrol-inline-error text-danger small">' . esc_html($this->lang->getText("min_iteration_suggestion")) . '</div>';
        echo '<div id="max-iteration-suggestion" class="griffinforms-formcontrol-inline-error text-danger small">' . esc_html($this->lang->getText("max_iteration_suggestion")) . '</div>';
        echo '</div>';
        
    }
    
    protected function isUniqueValue()
    {
        $checked = '';
        
        if($this->is_unique_value === 1) {
            $checked = ' checked';
        }
        
        echo '<div class="w-50 my-3" id="unique-values-wrap">';
        echo '<label for="only-unique-values">';
        echo esc_html($this->lang->getText('only_unique_values'));
        echo '</label> ';
        echo '<input type="checkbox" id="only-unique-values" name="only_unique_values" class="ms-2"' . esc_attr($checked ). '>';
        echo '</div>';
        
    }
    
    protected function addButtonLabel()
    {
    	echo '<div>';
    	echo '<span class="me-2">' . esc_html($this->lang->getText('add_field_iteration_button_label')) . '</span>';
    	echo '<input id="add-iteration-label" type="text" style="min-width:50px !important;" ';
    	echo 'value="' . esc_attr($this->add_button_label) . '">';
    	echo '</div>';
    }
    
    public function html()
    {
        echo '<div class="row">';
        $this->iterationToggle();
        echo '<div class="col-md-12 mb-3" id="iterations-options-wrap">';
        $this->iterationsCount();
        $this->isUniqueValue();
        $this->addButtonLabel();
        echo '</div></div>';
        
    }
}