<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

$closure = $this->closure;

?>

<!-- START CLOSURE - Calendar -->

<?php
echo $this->formFactory->createField()
	->type('date')
	->name('start_date')
	->id('vrstartdate')
	->value($closure->start_ts)
	->required(true)
	->label(JText::translate('VRMANAGEROOMCLOSURE2'))
	->attributes([
		'onChange' => 'vrStartDateChanged()',
		'showTime' => true,
	]);
?>

<!-- END CLOSURE - Calendar -->

<?php
echo $this->formFactory->createField()
	->type('date')
	->name('end_date')
	->id('vrenddate')
	->value($closure->end_ts)
	->required(true)
	->label(JText::translate('VRMANAGEROOMCLOSURE3'))
	->attributes([
		'showTime' => true,
	]);
?>

<!-- RECURRENCE - Select -->

<?php
echo $this->formFactory->createField()
	->type('select')
	->name('repeat')
	->value($closure->repeat)
	->label(JText::translate('VRMANAGEROOMCLOSURE6'))
	->onchange('vrRecurrenceValueChanged(this.value)')
	->options([
		''        => JText::translate('VRE_RECURRENCE_NONE'),
		'daily'   => JText::translate('VRE_RECURRENCE_DAILY'),
		'weekly'  => JText::translate('VRFREQUENCYTYPE1'),
		'monthly' => JText::translate('VRFREQUENCYTYPE2'),
		'yearly'  => JText::translate('VRFREQUENCYTYPE3'),
	]);
?>

<!-- STOP - Calendar -->

<?php
echo $this->formFactory->createField()
	->type('date')
	->name('stop_date')
	->id('vrstopdate')
	->value($closure->stop > 0 ? $closure->stop : null)
	->label(JText::translate('VRMANAGEROOMCLOSURE7'))
	->attributes([
		'showTime' => true,
	])
	->control([
		'class' => 'recurrence-field',
		'visible' => (bool) $closure->repeat,
	]);
?>

<script>
	(function($, w) {
		'use strict';

		w.vrStartDateChanged = () => {
			if ($('#vrenddate').val().length == 0) {
				let date = $('#vrstartdate').val();

				$('#vrenddate').val(date).attr('data-alt-value', date);
			}
		}

		w.vrRecurrenceValueChanged = (value) => {
			if (value) {
				$('.recurrence-field').show();
			} else {
				$('.recurrence-field').hide();
			}
		}

		$(function() {
			$('select[name="repeat"]').select2({
				minimumResultsForSearch: -1,
				allowClear: false,
				width: 300,
			});
		});
	})(jQuery, window);
</script>