<?php

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

trait InputDate
{
    protected $date_type_options;
    protected $date_type;
    protected $min_date;
    protected $max_date;
    protected $relative_min_date;
    protected $relative_max_date;
    protected $day_type_options;
    protected $day_type;
    
    protected function setDateTypeOptions()
    {
        $this->date_type_options = $this->choices->getChoices('validation_datetype_inputdate');
    }
    
    protected function setDateType()
    {
        $this->date_type = $this->fixEmptyValue('date_type', '', $this->date_type_options);
    }
    
    protected function setDayTypeOptions()
    {
        $this->day_type_options = $this->choices->getChoices('daytype');
    }
    
    protected function setDayType()
    {
        $this->day_type = $this->fixEmptyValue('day_type', '', $this->day_type_options);
    }
    
    protected function setMinDate()
    {
        $this->min_date = $this->fixEmptyValue('min', '');
    }
    
    protected function setMaxDate()
    {
        $this->max_date = $this->fixEmptyValue('max', '');
    }
    
    protected function setRelativeMinDate()
    {
        $this->relative_min_date = $this->fixEmptyValue('relative_min', '');
    }
    
    protected function setRelativeMaxDate()
    {
        $this->relative_max_date = $this->fixEmptyValue('relative_max', '');
    }
    
    protected function validationOptionsInputDate()
    {       
        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">';

        $this->dateType();

        echo '<div id="griffinforms-inputdate-validations-fixeddate-wrap" class="griffinforms-inputdate-validations-datetype-wrap">';

        echo '<div class="mb-3 w-50">';
        $this->minDate();
        echo '</div>';

        echo '<div class="mb-3 w-50">';
        $this->maxDate();
        echo '</div>';

        echo '</div>';

        echo '<div id="griffinforms-inputdate-validations-relativedate-wrap" class="griffinforms-inputdate-validations-datetype-wrap">';

        echo '<div class="p-3 rounded bg-white border mb-3">';
        $this->relativeMinDate();
        echo '</div>';

        echo '<div class="p-3 rounded bg-white border">';
        $this->relativeMaxDate();
        echo '</div>';

        echo '</div>';

        $this->dayType();

        echo '</div>';
        
        echo '</div>';
    }
    
    protected function dateType()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\SelectField('validation_datetype', $this->date_type, '');
        $field->selectOptions($this->date_type_options);
        
        echo '<div class="mb-3">';
        $field->html();
        echo '</div>';
    }
    
    protected function minDate()
    {                           
        $field = new \GriffinForms\Admin\Html\FormControls\InputDate('min_date', $this->min_date, '');
        echo '<div class="mb-1">' . esc_html($this->lang->getText('min_date', 'label')) . '</div>';
        $field->html();
        echo '<div class="form-text text-muted small">' . esc_html($this->lang->getText('date_tip_yearrange', 'description')) . '</div>';
    }
    
    protected function maxDate()
    {        
        $field = new \GriffinForms\Admin\Html\FormControls\InputDate('max_date', $this->max_date, '');
        echo '<div class="mb-1">' . esc_html($this->lang->getText('max_date', 'label')) . '</div>';
        $field->html();
        echo '<div class="form-text text-muted small">' . esc_html($this->lang->getText('date_tip_yearrange', 'description')) . '</div>';
    }
    
    protected function relativeMinDate()
    {
        $data = array('choices' => $this->choices, 'lang' => $this->lang);
        $field = new \GriffinForms\Admin\Html\FormControls\RelativeDate('relative_min', $this->relative_min_date, $data);
        echo '<div class="mb-1">' . esc_html($this->lang->getText('min_date', 'label')) . '</div>';
        $field->html();
    }
    
    protected function relativeMaxDate()
    {
        $data = array('choices' => $this->choices, 'lang' => $this->lang);
        $field = new \GriffinForms\Admin\Html\FormControls\RelativeDate('relative_max', $this->relative_max_date, $data);
        echo '<div class="mb-1">' . esc_html($this->lang->getText('max_date', 'label')) . '</div>';
        $field->html();
    }
    
    protected function dayType()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\SelectField('validation_daytype', $this->day_type, '');
        $field->selectOptions($this->day_type_options);

        echo '<div class="mt-3" id="griffinforms-inputdate-daytype-wrap">';
        echo '<div class="mb-1">' . esc_html($this->lang->getText('date_type', 'label')) . '</div>';
        $field->html();
        echo '</div>';
    }
    
    protected function inputdateJs()
    {
        ?>
        jQuery(document).ready(function($) {
            var dateTypeSelect = $("#griffinforms-validationdatetype-selectfield");

            function showDateType() {
                var dateType = dateTypeSelect.val();
                $(".griffinforms-inputdate-validations-datetype-wrap").hide();
                if (dateType === "fixed_date") {
                    $("#griffinforms-inputdate-validations-fixeddate-wrap").fadeIn();
                } else if (dateType === "relative_date") {
                    $("#griffinforms-inputdate-validations-relativedate-wrap").fadeIn();
                }
            }

            function showDayType() {
                var dateType = dateTypeSelect.val();
                if (dateType === "fixed_date" || dateType === "relative_date") {
                    $("#griffinforms-inputdate-daytype-wrap").show();
                } else {
                    $("#griffinforms-inputdate-daytype-wrap").hide();
                }
            }

            dateTypeSelect.on("change", function() {
                showDateType();
                showDayType();
            });

            showDateType();
            showDayType();
        });

        function reinitializeInputDatepicker(selector) {
            jQuery(selector).datepicker("destroy").datepicker({
                changeMonth: true,
                changeYear: true,
                yearRange: "1900:2100",
                showButtonPanel: true,
                prevText: "←",
                nextText: "→",
                dateFormat: "mm/dd/yy"
            });
        }

        jQuery(document).ready(function($) {
            reinitializeInputDatepicker("#griffinforms-mindate-inputdate");
            reinitializeInputDatepicker("#griffinforms-maxdate-inputdate");

            $("#griffinforms-mindate-inputdate").datepicker("option", "onSelect", function() { setMinDate(); });
            $("#griffinforms-maxdate-inputdate").datepicker("option", "onSelect", function() { setMaxDate(); });
            $("#griffinforms-mindate-inputdate").on("change", setMinDate);
            $("#griffinforms-maxdate-inputdate").on("change", setMaxDate);
            setMinDate();
            setMaxDate();

            function setMaxDate() {
                var maxDate = new Date($("#griffinforms-maxdate-inputdate").datepicker("getDate"));
                var currentMin = new Date($("#griffinforms-mindate-inputdate").datepicker("getDate"));
                if (isNaN(maxDate.getTime()) || maxDate.getFullYear() === 1970) {
                    $("#griffinforms-mindate-inputdate").datepicker("option", "maxDate", "");
                    return;
                }
                if (currentMin && !isNaN(currentMin.getTime()) && currentMin > maxDate) { return; }
                $("#griffinforms-mindate-inputdate").datepicker("option", "maxDate", maxDate);
                $("#griffinforms-mindate-inputdate").datepicker("refresh");
            }

            function setMinDate() {
                var minDate = new Date($("#griffinforms-mindate-inputdate").datepicker("getDate"));
                var currentMax = new Date($("#griffinforms-maxdate-inputdate").datepicker("getDate"));
                if (isNaN(minDate.getTime())) {
                    $("#griffinforms-maxdate-inputdate").datepicker("option", "minDate", "");
                    return;
                }
                if (currentMax && !isNaN(currentMax.getTime()) && minDate > currentMax) { return; }
                $("#griffinforms-maxdate-inputdate").datepicker("option", "minDate", minDate);
                $("#griffinforms-maxdate-inputdate").datepicker("refresh");
            }
        });
        <?php
    }

}