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

JHtml::fetch('vrehtml.assets.select2');
JHtml::fetch('vrehtml.assets.fontawesome');

$cron = $this->cron;

$vik = VREApplication::getInstance();

/**
 * Trigger event to display custom HTML.
 * In case it is needed to include any additional fields,
 * it is possible to create a plugin and attach it to an event
 * called "onDisplayViewCronjobs".
 * It is also possible to use "onDisplayViewCronjobsSidebar"
 * to include any additional fieldsets within the right sidebar.
 * The event method receives the view instance as argument.
 *
 * @since 1.10
 */
$detailsForms = $this->onDisplayView();
$sidebarForms = $this->onDisplayView('Sidebar');

?>

<form name="adminForm" id="adminForm" action="index.php" method="post">
	
	<?php echo $vik->openCard(); ?>

		<!-- CONTENT -->

		<div class="span5 full-width">

			<!-- DETAILS -->

			<div class="row-fluid">
				<div class="span12">
					<?php
					echo $vik->openFieldset('Cron Job');
					echo $this->loadTemplate('cronjob');
					?>
					
					<!-- Define role to detect the supported hook -->
					<!-- {"rule":"customizer","event":"onDisplayViewCronjob","key":"details","type":"field"} -->

					<?php	
					/**
					 * Look for any additional fields to be pushed within
					 * the "Details" fieldset (left-side).
					 *
					 * @since 1.10
					 */
					if (isset($detailsForms['details']))
					{
						echo $detailsForms['details'];

						// unset details form to avoid displaying it twice
						unset($detailsForms['details']);
					}

					// display description of the selected driver
					foreach ($this->cronjobs as $opt)
					{
						if (!empty($opt->description))
						{
							if ($opt->value == $cron->class)
							{
								// display cron job description
								echo $this->formFactory->createField()
									->type('alert')
									->style('info')
									->hiddenLabel(true)
									->text($opt->description);
							}
						}
					}
						
					echo $vik->closeFieldset();
					?>
				</div>
			</div>

			<!-- Define role to detect the supported hook -->
			<!-- {"rule":"customizer","event":"onDisplayViewCronjob","type":"fieldset"} -->

			<?php
			/**
			 * Iterate remaining forms to be displayed within
			 * the main content.
			 *
			 * @since 1.10
			 */
			foreach ($detailsForms as $formName => $formHtml)
			{
				$title = JText::translate($formName);
				?>
				<div class="row-fluid">
					<div class="span12">
						<?php
						echo $vik->openFieldset($title);
						echo $formHtml;
						echo $vik->closeFieldset();
						?>
					</div>
				</div>
				<?php
			}
			?>

		</div>

		<!-- SIDEBAR -->

		<div class="span7 full-width">

			<!-- PARAMS -->

			<div class="row-fluid">
				<div class="span12">
					<?php
					echo $vik->openFieldset(JText::translate('VRE_SETTINGS_FIELDSET'), 'form-vertical');
					echo $this->loadTemplate('params');
					echo $vik->closeFieldset();
					?>
				</div>
			</div>

			<!-- Define role to detect the supported hook -->
			<!-- {"rule":"customizer","event":"onDisplayViewCronjobSidebar","type":"fieldset"} -->

			<?php
			/**
			 * Iterate remaining forms to be displayed within
			 * the sidebar.
			 *
			 * @since 1.10
			 */
			foreach ($sidebarForms as $formName => $formHtml)
			{
				$title = JText::translate($formName);
				?>
				<div class="row-fluid">
					<div class="span12">
						<?php
						echo $vik->openFieldset($title);
						echo $formHtml;
						echo $vik->closeFieldset();
						?>
					</div>
				</div>
				<?php
			}
			?>

		</div>

	<?php echo $vik->closeCard(); ?>

	<?php echo JHtml::fetch('form.token'); ?>
	
	<input type="hidden" name="id" value="<?php echo (int) $cron->id; ?>" />
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="view" value="managecronjob" />
	<input type="hidden" name="option" value="com_vikrestaurants" />

</form>

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

		$(function() {
			w.validator = new VikFormValidator('#adminForm');

			Joomla.submitbutton = (task) => {
				if (task.indexOf('save') === -1 || w.validator.validate()) {
					Joomla.submitform(task, document.adminForm);
				}
			}
		});
	})(jQuery, window);
</script>