<?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!');

$cron = $this->cron;

$vik = VREApplication::getInstance();

?>

<!-- NAME - Text -->

<?php
echo $this->formFactory->createField()
	->type('text')
	->name('name')
	->required(true)
	->value($cron->name)
	->label(JText::translate('VRE_WIDGET_NAME'));
?>

<!-- DRIVER - Select -->

<?php
/**
 * Disable type selection if we are editing an existing cron job
 * in order to avoid unexpected behaviors due to the created DB column(s).
 *
 * A description is shown to explain why the type cannot be changed.
 */
echo $this->formFactory->createField()
	->type('select')
	->name($cron->id ? '' : 'class')
	->value($cron->class)
	->id('vre-cron-class')
	->label(JText::translate('VRMANAGEPAYMENT2'))
	->description($cron->id ? JText::translate('VRECRONJOBTYPEDISABLED') : '')
	->required($cron->id ? false : true)
	->disabled($cron->id)
	->options($this->cronjobs);

if ($cron->id)
{
	// use hidden field because disabled dropdowns will not send the value set
	echo $this->formFactory->createField()
		->type('hidden')
		->name('class')
		->value($cron->class);
}
?>

<!-- PUBLISHED - Checkbox -->

<?php
echo $this->formFactory->createField()
	->type('checkbox')
	->name('published')
	->checked($cron->published)
	->label(JText::translate('VRMANAGEPAYMENT3'));
?>

<!-- RESUME NOTIFICATIONS - Checkbox -->

<?php
// In case the notifications are currently paused, display a checkbox to immediately resume them.
if (!E4J\VikRestaurants\Helpers\DateHelper::isNull($cron->resume_notif) && JFactory::getDate($cron->resume_notif) > JFactory::getDate())
{
	echo $this->formFactory->createField()
		->type('checkbox')
		->name('resume_notif')
		->label(JText::translate('VRECRONJOBRESUMENOTIF'))
		->description(JText::sprintf('VRECRONJOBRESUMENOTIF_DESC', JHtml::fetch('date', $cron->resume_notif, JText::translate('DATE_FORMAT_LC2'))));
}
?>

<?php
JText::script('VRE_FILTER_SELECT_DRIVER');
?>

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

		$(function() {
			$('#vre-cron-class').select2({
				placeholder: Joomla.JText._('VRE_FILTER_SELECT_DRIVER'),
				allowClear: false,
				width: '90%',
			}).on('change', function() {
				Joomla.submitform('cronjob.add', document.adminForm);
			});
		});
	})(jQuery);
</script>