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

$rows = $this->rows;

$filters = $this->filters;

$vik = VREApplication::getInstance();

$config = VREFactory::getConfig();

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

?>

<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":"onDisplayViewCronjoblogsList","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'];
		}

		// in case a plugin needs to use the filter bar, display the button
		if (isset($forms['filters']))
		{
			?>
			<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>
			<?php
		}
		?>
		
		<div class="btn-group pull-left">
			<button type="button" class="btn" onclick="clearFilters();">
				<?php echo JText::translate('JSEARCH_FILTER_CLEAR'); ?>
			</button>
		</div>

		<div class="btn-group pull-right">
			<?php echo $vik->calendar($filters['date'], 'date', 'vr-date-filter', null, array('onChange' => 'document.adminForm.submit();')); ?>
		</div>		

	</div>

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

		<!-- Define role to detect the supported hook -->
		<!-- {"rule":"customizer","event":"onDisplayViewCronjoblogsList","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":"onDisplayCronjoblogsTableTH","type":"th"} -->

	<!-- Define role to detect the supported hook -->
	<!-- {"rule":"customizer","event":"onDisplayCronjoblogsTableTD","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 nowrap hidden-phone'); ?>" width="1%" style="text-align: left;">
					<?php echo JText::translate('JGRID_HEADING_ID'); ?>
				</th>

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

				<!-- CONTENT -->
				
				<th class="<?php echo $vik->getAdminThClass('left'); ?>" width="30%" style="text-align: left;">
					<?php echo JText::translate('VRMANAGEAPIUSER14'); ?>
				</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; ?>

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

				<!-- MAILED -->
				
				<th class="<?php echo $vik->getAdminThClass('hidden-phone'); ?>" width="10%" style="text-align: center;">
					<?php echo JText::translate('VRENOTIFIED'); ?>
				</th>
			
			</tr>
		<?php echo $vik->closeTableHead(); ?>
		
		<?php
		for ($i = 0, $n = count($rows); $i < $n; $i++)
		{
			$row = $rows[$i];

			$content = strip_tags((string) $row['content']);
			
			if (strlen($content) > 300)
			{
				$content = rtrim(mb_substr($content, 0, 256, 'UTF-8'), '. ') . '...';
			}
			?>
			<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>

				<!-- DATE -->
				
				<td>
					<a href="javascript:void(0)" onClick="openLogInfoModal(<?php echo (int) $row['id']; ?>);">
						<?php echo JHtml::fetch('date.relative', $row['createdon'], null, null, JText::translate('DATE_FORMAT_LC3') . ' ' . $config->get('timeformat')); ?>
					</a>
				</td>
				
				<!-- CONTENT -->

				<td style="word-break: break-word;">
					<?php echo $content; ?>
				</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; ?>

				<!-- STATUS -->
				
				<td style="text-align: center;">
					<span class="vrreservationstatus<?php echo $row['status'] ? 'confirmed' : 'removed'; ?>">
						<?php echo JText::translate('VR' . ($row['status'] ? 'OK' : 'ERROR')); ?>
					</span>
				</td>

				<!-- MAILED -->
				
				<td style="text-align: center;" class="hidden-phone">
					<?php
					if ($row['mailed'] == 2)
					{
						// paused notification
						?><i class="fas fa-bell-slash warn big"></i><?php
					}
					else
					{
						echo JHtml::fetch('vrehtml.admin.stateaction', $row['mailed']);
					}
					?>
				</td>

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

	<input type="hidden" name="boxchecked" value="0" />
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="view" value="cronjoblogs" />
	
	<input type="hidden" name="id_cron" value="<?php echo (int) $filters['id_cron']; ?>" />

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

<?php
// log info modal
echo JHtml::fetch(
	'bootstrap.renderModal',
	'jmodal-loginfo',
	[
		'title'       => JText::translate('VREMAINTITLEVIEWCRONJOBLOGINFO'),
		'closeButton' => true,
		'keyboard'    => false, 
		'bodyHeight'  => 80,
		'url'		  => '',
		'footer'      => '<button type="button" class="btn btn-danger" data-role="delete" style="float:right;">' . JText::translate('VRDELETE') . '</button>',
	]
);

JText::script('VRSYSTEMCONFIRMATIONMSG');
?>

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

		const vreOpenJModal = (id, url, jqmodal) => {
			<?php echo $vik->bootOpenModalJS(); ?>
		}

		w.clearFilters = () => {
			$('#vrkeysearch').val('');
			$('#vr-date-filter').val('');
			
			document.adminForm.submit();
		}

		w.openLogInfoModal = (id) => {
			let url = 'index.php?option=com_vikrestaurants&tmpl=component&view=cronjobloginfo&id=' + id;

			// register log ID
			$('button[data-role="delete"]').attr('data-id', id);

			vreOpenJModal('loginfo', url, true);
		}

		$(function() {
			$('button[data-role="delete"]').on('click', function(event) {
				// confirm before delete
				let r = confirm(Joomla.JText._('VRSYSTEMCONFIRMATIONMSG'));

				if (!r) {
					event.preventDefault();
					event.stopPropagation();
					return false;
				}

				// uncheck all records
				$('input[name="cid[]"]').prop('checked', false);
				// check selected record
				$('input[name="cid[]"][value="' + $(this).attr('data-id') + '"]').prop('checked', true);

				Joomla.submitbutton('cronjoblog.delete');
			});
		});

		Joomla.submitbutton = (task) => {
			if (task == 'cronjoblog.truncate' && !confirm(Joomla.JText._('VRSYSTEMCONFIRMATIONMSG'))) {
				return false;
			}
			
			Joomla.submitform(task, document.adminForm);
		}
	})(jQuery, window);
</script>