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

$rows = $this->rows;

$filters = $this->filters;

$vik = VREApplication::getInstance();

$config = VREFactory::getConfig();

$user = JFactory::getUser();

$canEdit      = $user->authorise('core.edit', 'com_vikrestaurants');
$canEditState = $user->authorise('core.edit.state', 'com_vikrestaurants');

$is_searching = $this->hasFilters();

/**
 * 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 "onDisplayViewCronjobsList". The event method receives the
 * view instance as argument.
 *
 * @since 1.10
 */
$forms = $this->onDisplayListView($is_searching);

// generate secure key hash
$securekey = md5($config->get('cron_secure_key'));
?>

<form action="index.php?option=com_vikrestaurants" method="post" name="adminForm" id="adminForm">

	<div class="btn-toolbar" style="height: 32px;">
		<div class="btn-group pull-left input-append">
			<input type="text" name="search" id="vrkeysearch" size="32" 
				value="<?php echo $this->escape($filters['search']); ?>" placeholder="<?php echo $this->escape(JText::translate('JSEARCH_FILTER_SUBMIT')); ?>" />

			<button type="submit" class="btn">
				<i class="fas fa-search"></i>
			</button>
		</div>

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

		<?php
		// plugins can use the "search" key to introduce custom
		// filters within the search bar
		if (isset($forms['search']))
		{
			echo $forms['search'];
		}
		?>

		<div class="btn-group pull-left">
			<button type="button" class="btn <?php echo ($is_searching ? 'btn-primary' : ''); ?>" onclick="vrToggleSearchToolsButton(this);">
				<?php echo JText::translate('JSEARCH_TOOLS'); ?>&nbsp;<i class="fas fa-caret-<?php echo ($is_searching ? 'up' : 'down'); ?>" id="vr-tools-caret"></i>
			</button>
		</div>
		
		<div class="btn-group pull-left">
			<button type="button" class="btn" onclick="clearFilters();">
				<?php echo JText::translate('JSEARCH_FILTER_CLEAR'); ?>
			</button>
		</div>
	</div>

	<div class="btn-toolbar" id="vr-search-tools" style="height: 32px;<?php echo ($is_searching ? '' : 'display: none;'); ?>">

		<?php
		$options = JHtml::fetch('vrehtml.admin.crondrivers', true);
		?>
		<div class="btn-group pull-left">
			<select name="class" id="vre-class-sel" class="<?php echo (!empty($filters['class']) ? 'active' : ''); ?>" onchange="document.adminForm.submit();">
				<?php echo JHtml::fetch('select.options', $options, 'value', 'text', $filters['class']); ?>
			</select>
		</div>

		<?php
		$options = [
			JHtml::fetch('select.option', '', JText::translate('VRFILTERSELECTSTATUS')),
			JHtml::fetch('select.option', 1, JText::translate('JPUBLISHED')),
			JHtml::fetch('select.option', 0, JText::translate('JUNPUBLISHED')),
		];
		?>
		<div class="btn-group pull-left">
			<select name="status" id="vre-status-sel" class="<?php echo (strlen($filters['status']) ? 'active' : ''); ?>" onchange="document.adminForm.submit();">
				<?php echo JHtml::fetch('select.options', $options, 'value', 'text', $filters['status']); ?>
			</select>
		</div>

		<!-- Define role to detect the supported hook -->
		<!-- {"rule":"customizer","event":"onDisplayViewCronjobsList","type":"search","key":"filters"} -->

		<?php
		// plugins can use the "filters" key to introduce custom
		// filters within the search bar
		if (isset($forms['filters']))
		{
			echo $forms['filters'];
		}
		?>

	</div>

<?php
if (count($rows) == 0)
{
	echo $vik->alert(JText::translate('JGLOBAL_NO_MATCHING_RESULTS'));
}
else
{
	/**
	 * Trigger event to display custom columns.
	 *
	 * @since 1.10
	 */
	$columns = $this->onDisplayTableColumns();
	?>

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

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

	<table cellpadding="4" cellspacing="0" border="0" width="100%" class="<?php echo $vik->getAdminTableClass(); ?>">
		<?php echo $vik->openTableHead(); ?>
			<tr>

				<th width="1%">
					<?php echo $vik->getAdminToggle(count($rows)); ?>
				</th>

				<!-- ID -->
				
				<th class="<?php echo $vik->getAdminThClass('left hidden-phone nowrap'); ?>" width="1%" style="text-align: left;">
					<?php echo JHtml::fetch('vrehtml.admin.sort', 'JGRID_HEADING_ID', 'j.id', $this->orderDir, $this->ordering); ?>
				</th>

				<!-- NAME -->
				
				<th class="<?php echo $vik->getAdminThClass('left'); ?>" width="30%" style="text-align: left;">
					<?php echo JHtml::fetch('vrehtml.admin.sort', 'VRE_WIDGET_NAME', 'j.name', $this->orderDir, $this->ordering); ?>
				</th>

				<!-- CUSTOM -->

				<?php foreach ($columns as $k => $col): ?>
					<th data-id="<?php echo $this->escape($k); ?>" class="<?php echo $vik->getAdminThClass('left hidden-phone'); ?>">
						<?php echo $col->th; ?>
					</th>
				<?php endforeach; ?>

				<!-- PUBLISHED -->
				
				<th class="<?php echo $vik->getAdminThClass('hidden-phone'); ?>" width="8%" style="text-align: center;">
					<?php echo JText::translate('VRMANAGEPAYMENT3'); ?>
				</th>

				<!-- STATUS -->
				
				<th class="<?php echo $vik->getAdminThClass('left'); ?>" width="12%" style="text-align: left;">
					<?php echo JText::translate('VRMANAGEAPIUSER13'); ?>
				</th>

				<!-- LOGS -->
				
				<th class="<?php echo $vik->getAdminThClass(); ?>" width="8%" style="text-align: center;">
					<?php echo JText::translate('VRMANAGEAPIUSER11'); ?>
				</th>

				<!-- LAUNCH -->
				
				<th class="<?php echo $vik->getAdminThClass('hidden-phone'); ?>" width="8%" style="text-align: center;">
					<?php echo JText::translate('VRECRONMANUALLAUNCH'); ?>
				</th>

				<!-- DATE -->
				
				<th class="<?php echo $vik->getAdminThClass('left hidden-phone'); ?>" width="10%" style="text-align: left;">
					<?php echo JHtml::fetch('vrehtml.admin.sort', 'VRCREATEDON', 'j.createdon', $this->orderDir, $this->ordering); ?>
				</th>
			
			</tr>
		<?php echo $vik->closeTableHead(); ?>
		
		<?php
		for ($i = 0, $n = count($rows); $i < $n; $i++)
		{
			$row = $rows[$i];
			?>
			<tr class="row<?php echo ($i % 2); ?>">

				<td>
					<input type="checkbox" id="cb<?php echo (int) $i;?>" name="cid[]" value="<?php echo (int) $row['id']; ?>" onClick="<?php echo $vik->checkboxOnClick(); ?>">
				</td>
				
				<!-- ID -->

				<td class="hidden-phone">
					<?php echo $row['id']; ?>
				</td>

				<!-- NAME -->

				<td>
					<div class="td-primary">
						<?php
						if ($canEdit)
						{
							?>
							<a href="index.php?option=com_vikrestaurants&amp;task=cronjob.edit&amp;cid[]=<?php echo (int) $row['id']; ?>">
								<?php echo $row['name']; ?>
							</a>
							<?php
						}
						else
						{
							echo $row['name'];
						}
						?>
					</div>

					<div class="td-secondary">
						<?php
						try
						{
							/**
							 * Make driver class human-readable.
							 * 
							 * @since 1.11
							 */
							echo $this->cronDispatcher->getCron($row['class'])->getTitle();
						}
						catch (Exception $notFound)
						{
							// cron not found...
							echo $row['class'];
						}
						?>
					</div>
				</td>

				<!-- CUSTOM -->

				<?php foreach ($columns as $k => $col): ?>
					<td data-id="<?php echo $this->escape($k); ?>" class="hidden-phone">
						<?php echo isset($col->td[$i]) ? $col->td[$i] : ''; ?>
					</td>
				<?php endforeach; ?>

				<!-- PUBLISHED -->
				
				<td style="text-align: center;" class="hidden-phone">
					<?php echo JHtml::fetch('vrehtml.admin.stateaction', $row['published'], $row['id'], 'cronjob.publish', $canEditState); ?>
				</td>
				
				<!-- STATUS -->
				
				<td>
					<?php
					if (!empty($row['lastlog']['id']))
					{
						?>
						<div class="vrreservationstatus<?php echo $row['lastlog']['status'] ? 'confirmed' : 'removed'; ?>">
							<?php echo JText::translate('VR' . ($row['lastlog']['status'] ? 'OK' : 'ERROR')); ?>
						</div>

						<div class="td-secondary">
							<?php echo JHtml::fetch('date.relative', $row['lastlog']['createdon'], null, null, JText::translate('DATE_FORMAT_LC3') . ' ' . $config->get('timeformat')); ?>
						</div>
						<?php
					}
					else
					{
						echo '/';
					}
					?>
				</td>

				<!-- LOGS -->
				
				<td style="text-align: center;">
					<a href="index.php?option=com_vikrestaurants&view=cronjoblogs&id_cron=<?php echo (int) $row['id']; ?>">
						<i class="fas fa-sticky-note big"></i>
					</a>
				</td>

				<!-- LAUNCH -->
				
				<td style="text-align: center;" class="hidden-phone">
					<?php
					if ($row['published'])
					{
						$url = 'index.php?option=com_vikrestaurants&task=cronjob_listener_rq&id_cron=' . $row['id'] . '&secure_key=' . $securekey;
						$url = VREFactory::getPlatform()->getUri()->route($url);
						?>
						<a href="<?php echo $url; ?>" target="_blank">
							<i class="fas fa-rocket big"></i>
						</a>
						<?php
					}
					else
					{
						?>
						<a href="javascript:void(0)" class="disabled">
							<i class="fas fa-rocket big"></i>
						</a>
						<?php
					}
					?>
				</td>

				<!-- DATE -->
				
				<td class="hidden-phone">
					<?php echo JHtml::fetch('date', $row['createdon'], JText::translate('DATE_FORMAT_LC3') . ' ' . $config->get('timeformat')); ?>
				</td>

			</tr>
			<?php
		}
		?>
	</table>
	<?php
}
?>

	<input type="hidden" name="boxchecked" value="0" />
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="view" value="cronjobs" />

	<input type="hidden" name="filter_order" value="<?php echo $this->escape($this->ordering); ?>" />
	<input type="hidden" name="filter_order_Dir" value="<?php echo $this->escape($this->orderDir); ?>" />

	<?php echo JHtml::fetch('form.token'); ?>
	<?php echo $this->navbut; ?>
</form>

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

		w.clearFilters = () => {
			$('#vrkeysearch').val('');
			$('#vre-class-sel').updateChosen('');
			$('#vre-status-sel').updateChosen('');
			
			document.adminForm.submit();
		}

		$(function() {
			VikRenderer.chosen('.btn-toolbar');
		});
	})(jQuery, window);
</script>