<?php

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

trait InputTime
{
    protected $min_time;
    protected $max_time;

    protected function setMinTime()
    {
        $this->min_time = $this->fixEmptyValue('min', '');
    }

    protected function setMaxTime()
    {
        $this->max_time = $this->fixEmptyValue('max', '');
    }

    protected function validationOptionsInputTime()
    {
        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 w-50">';
        $this->minTime();
        echo '</div>';

        echo '<div class="mb-3 w-50">';
        $this->maxTime();
        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 minTime()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputTime('min_time', $this->min_time, '');
        echo '<div class="mb-1">' . esc_html($this->lang->getText('min_time', 'label')) . '</div>';
        $field->html();
        echo '<div class="form-text text-muted small">' . esc_html($this->lang->getText('min_time', 'description')) . '</div>';
    }

    protected function maxTime()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputTime('max_time', $this->max_time, '');
        echo '<div class="mb-1">' . esc_html($this->lang->getText('max_time', 'label')) . '</div>';
        $field->html();
        echo '<div class="form-text text-muted small">' . esc_html($this->lang->getText('max_time', 'description')) . '</div>';
    }

    protected function inputTimeJs()
    {
        echo 'jQuery(document).ready(function($) {' . PHP_EOL;
        echo '    // Bind change events to update time constraints when users manually type values' . PHP_EOL;
        echo '    $("#griffinforms-mintime-inputtime").on("change", setMinTime);' . PHP_EOL;
        echo '    $("#griffinforms-maxtime-inputtime").on("change", setMaxTime);' . PHP_EOL;
        echo '    setMinTime();' . PHP_EOL;
        echo '    setMaxTime();' . PHP_EOL;
        echo '    // Function to update maxTime constraint on the min time picker' . PHP_EOL;
        echo '    function setMaxTime() {' . PHP_EOL;
        echo '        var maxVal = $("#griffinforms-maxtime-inputtime").val();' . PHP_EOL;
        echo '        var minVal = $("#griffinforms-mintime-inputtime").val();' . PHP_EOL;
        echo '        if (maxVal && minVal && minVal > maxVal) {' . PHP_EOL;
        echo '            $("#griffinforms-mintime-inputtime").attr("max", maxVal);' . PHP_EOL;
        echo '        } else if (maxVal) {' . PHP_EOL;
        echo '            $("#griffinforms-mintime-inputtime").attr("max", maxVal);' . PHP_EOL;
        echo '        }' . PHP_EOL;
        echo '    }' . PHP_EOL;
        echo '    // Function to update minTime constraint on the max time picker' . PHP_EOL;
        echo '    function setMinTime() {' . PHP_EOL;
        echo '        var minVal = $("#griffinforms-mintime-inputtime").val();' . PHP_EOL;
        echo '        var maxVal = $("#griffinforms-maxtime-inputtime").val();' . PHP_EOL;
        echo '        if (minVal && maxVal && maxVal < minVal) {' . PHP_EOL;
        echo '            $("#griffinforms-maxtime-inputtime").attr("min", minVal);' . PHP_EOL;
        echo '        } else if (minVal) {' . PHP_EOL;
        echo '            $("#griffinforms-maxtime-inputtime").attr("min", minVal);' . PHP_EOL;
        echo '        }' . PHP_EOL;
        echo '    }' . PHP_EOL;
        echo '});' . PHP_EOL;
    }
}
