<?php

namespace GriffinForms\Admin\Html\FormControls;

class RelativeDate extends \GriffinForms\Admin\Html\FormControls\Format
{
    protected $choices;
    protected $days;
    protected $weeks;
    protected $months;
    protected $years;
    protected $date_logic_options;
    protected $date_logic;
    protected $date_variable_options;
    protected $date_variable;
    
    protected function setDays()
    {    
        $this->days = $this->fixEmptyValue('days', 0);
    }   
    
    protected function setWeeks()
    {
        $this->weeks = $this->fixEmptyValue('weeks', 0);
    }   
    
    protected function setMonths()
    {
        $this->months = $this->fixEmptyValue('months', 0);
    }   
    
    protected function setYears()
    {
        $this->years = $this->fixEmptyValue('years', 0);
    }
    
    protected function setDateLogicOptions()
    {
        $this->date_logic_options = $this->data['choices']->getChoices('date_logic');
    }
    
    protected function setDateVariableOptions()
    {
        $this->date_variable_options = $this->data['choices']->getChoices('date_variables');
    }
    
    protected function setDateLogic()
    {
        $this->date_logic = $this->fixEmptyValue('date_logic', '', $this->date_logic_options);
    }
    
    protected function setDateVariable()
    {
        $this->date_variable = $this->fixEmptyValue('date_variable', '', $this->date_variable_options);
    }
    
    protected function html()
    {
        $logic_field = new \GriffinForms\Admin\Html\FormControls\SelectField($this->name . '_logic', $this->date_logic, '');
        $logic_field->selectOptions($this->date_logic_options);        
        
        $date_field = new \GriffinForms\Admin\Html\FormControls\SelectField($this->name . '_date_variable', $this->date_variable, '');
        $date_field->selectOptions($this->date_variable_options);
        
        echo '<div class="d-flex flex-wrap align-items-center gap-2">';

        $this->datePeriod('days');
        $this->datePeriod('weeks');
        $this->datePeriod('months');
        $this->datePeriod('years');

        echo '<div class="flex-shrink-0">';
        $logic_field->html();
        echo '</div>';

        echo '<div class="flex-grow-1">';
        $date_field->html();
        echo '</div>';

        echo '</div>';
    }
    
    protected function datePeriod($unit)
    {
        $value = $this->$unit;
            
        $html_id = 'griffinforms-field-' . $this->name . '-' . $unit;
        echo '<div class="d-flex align-items-center flex-shrink-0">';
        echo '<input id="' . esc_attr($html_id) . '" type="number" min=0 style="width:70px;" value="' . absint($value) . '">';
        echo '<div class="ms-2">' . esc_html($this->data['lang']->getText($unit, 'label')) . '</div>';
        echo '</div>';
    }
}