<?php 
/** 
 * @package JREALTIMEANALYTICS::EVENTSTATS::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage eventstats
 * @subpackage tmpl
 * @author Joomla! Extensions Store
 * @copyright (C) 2014 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html  
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Uri\Uri;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Helpers\Html as JRealtimeHelpersHtml;

// Ordering drag'n'drop management
$saveOrderingUrl = null;
if ($this->orders['order'] == 's.ordering') {
	$saveOrderingUrl = 'index.php?option=com_jrealtimeanalytics&task=eventstats.saveOrder&format=json&ajax=1';
	HTMLHelper::_('draggablelist.draggable');
}
?>
<form action="index.php" method="post" name="adminForm" id="adminForm">
	<table class="full headerlist">
		<tr>
			<td class="left">
				<div class="input-group">
					<span class="input-group-text" aria-label="<?php echo Text::_('COM_JREALTIME_FILTER');?>"><span class="icon-filter" aria-hidden="true"></span> <?php echo Text::_('COM_JREALTIME_FILTER' ); ?>:</span>
					<input type="text" name="search" id="search" value="<?php echo htmlspecialchars($this->searchword, ENT_COMPAT, 'UTF-8');?>" class="text_area"/>
				</div>
				<button class="btn btn-primary btn-sm" onclick="this.form.submit();"><?php echo Text::_('COM_JREALTIME_GO' ); ?></button>
				<button class="btn btn-primary btn-sm" onclick="document.getElementById('search').value='';this.form.submit();"><?php echo Text::_( 'COM_JREALTIME_RESET' ); ?></button>
			</td>
			<td class="right d-flex justify-content-end">
				<div class="input-group d-none d-md-flex flex-end">
					<span class="input-group-text" aria-label="<?php echo Text::_('COM_JREALTIME_STATE');?>"><span class="icon-filter" aria-hidden="true"></span> <?php echo Text::_('COM_JREALTIME_STATE' ); ?></span>
					<?php
						echo $this->lists['state'];
						echo $this->lists['categories'];
						echo $this->pagination->getLimitBox();
					?>
				</div>
			</td>
		</tr>
		<tr>
			<td class="left">
				<div class="input-group input-group-date active">
					<span class="input-group-text" aria-label="<?php echo Text::_('COM_JREALTIME_FILTER_BY_DATE_FROM');?>"><span class="icon-calendar" aria-hidden="true"></span> <?php echo Text::_('COM_JREALTIME_FILTER_BY_DATE_FROM' ); ?>:</span>
					<input type="text" name="fromperiod" id="fromPeriod" data-role="calendar" autocomplete="off" value="<?php echo $this->dates['start'];?>" class="text_area"/>
				</div>
				
				<div class="input-group input-group-date active">
					<span class="input-group-text" aria-label="<?php echo Text::_('COM_JREALTIME_FILTER_BY_DATE_TO');?>"><span class="icon-calendar" aria-hidden="true"></span> <?php echo Text::_('COM_JREALTIME_FILTER_BY_DATE_TO' ); ?>:</span>
					<input type="text" name="toperiod" id="toPeriod" data-role="calendar" autocomplete="off" value="<?php echo $this->dates['to'];?>" class="text_area"/>
				</div>
				<button class="btn btn-primary btn-sm" onclick="this.form.submit();"><?php echo Text::_('COM_JREALTIME_GO' ); ?></button>
				<button class="btn btn-primary btn-sm" onclick="document.getElementById('fromPeriod').value='';document.getElementById('toPeriod').value='';this.form.submit();"><?php echo Text::_('COM_JREALTIME_RESET' ); ?></button>
			</td>
		</tr>
	</table>

	<table id="adminList" class="adminlist table table-striped table-hover">
	<thead>
		<tr>
			<th style="width:1%">
				<?php echo Text::_('COM_JREALTIME_NUM' ); ?>
			</th>
			<th style="width:1%">
				<input type="checkbox" name="checkall-toggle" value="" class="form-check-input" onclick="Joomla.checkAll(this);" />
			</th>
			<th class="title">
				<?php echo HTMLHelper::_('grid.sort',  'COM_JREALTIME_NAME', 's.name', @$this->orders['order_Dir'], @$this->orders['order'], 'eventstats.display'); ?>
			</th>
			<th class="title">
				<?php echo HTMLHelper::_('grid.sort',  'COM_JREALTIME_EVENT_TYPE', 's.type', @$this->orders['order_Dir'], @$this->orders['order'], 'eventstats.display'); ?>
			</th>
			<th class="title">
				<?php echo HTMLHelper::_('grid.sort',  'COM_JREALTIME_CAT', 'cat.title', @$this->orders['order_Dir'], @$this->orders['order'], 'eventstats.display'); ?>
			</th>
			<th class="title d-none d-md-table-cell">
				<?php echo Text::_('COM_JREALTIME_DESCRIPTION'); ?>
			</th>
			<th class="title d-none d-md-table-cell">
				<?php echo HTMLHelper::_('grid.sort',  'COM_JREALTIME_NUM_OCCURRENCES', 'event_occurrences', @$this->orders['order_Dir'], @$this->orders['order'], 'eventstats.display'); ?>
			</th>
			<th class="title d-none d-md-table-cell">
				<?php echo HTMLHelper::_('grid.sort',  'COM_JREALTIME_GOAL_EXPECTATION', 's.hasgoal', @$this->orders['order_Dir'], @$this->orders['order'], 'eventstats.display'); ?>
			</th>
			<th class="order d-none d-md-table-cell">
				<?php echo HTMLHelper::_('grid.sort',   'COM_JREALTIME_ORDER', 's.ordering', @$this->orders['order_Dir'], @$this->orders['order'], 'eventstats.display'); ?>
				<?php 
					if(isset($this->orders['order']) && $this->orders['order'] == 's.ordering'):
						echo JRealtimeHelpersHtml::order($this->items, 'filesave.png', 'eventstats.saveOrder'); 
					endif;
				 ?>
			</th>
			<th style="width:5%">
				<?php echo HTMLHelper::_('grid.sort',   'COM_JREALTIME_PUBLISHED', 's.published', @$this->orders['order_Dir'], @$this->orders['order'], 'eventstats.display' ); ?>
			</th>
			<th style="width:5%">
				<?php echo HTMLHelper::_('grid.sort',   'COM_JREALTIME_ID', 's.id', @$this->orders['order_Dir'], @$this->orders['order'], 'eventstats.display' ); ?>
			</th>
		</tr>
	</thead>
	<tbody <?php if ($saveOrderingUrl) :?> class="js-draggable" data-url="<?php echo $saveOrderingUrl; ?>" data-direction="<?php echo strtolower($this->orders['order_Dir']); ?>" data-nested="true" <?php endif; ?>>
	<?php
	$k = 0;
	$canCheckin = $this->user->authorise('core.manage', 'com_checkin');
	for ($i=0, $n=count( $this->items ); $i < $n; $i++) {
		$row = $this->items[$i];
		$link =  'index.php?option=com_jrealtimeanalytics&task=eventstats.editEntity&cid[]='. $row->id ;
		$labelOccurencesClass = $row->event_occurrences ? 'primary' : 'danger';
		// Access check.
		if($this->user->authorise('core.edit.state', 'com_jrealtimeanalytics')) {
			$taskPublishing	= !$row->published ? 'eventstats.publish' : 'eventstats.unpublish';
			$altPublishing 	= !$row->published ? Text::_( 'Publish' ) : Text::_( 'Unpublish' );
			$published = '<a href="javascript:void(0);" onclick="return Joomla.listItemTask(\'cb' . $i . '\',\'' . $taskPublishing . '\')">';
			$published .= $row->published ? 
						  '<img alt="' . $altPublishing . '" src="' . Uri::base(true) . '/components/com_jrealtimeanalytics/images/icon-16-tick.png" width="16" height="16" />' : 
						  '<img alt="' . $altPublishing . '" src="' . Uri::base(true) . '/components/com_jrealtimeanalytics/images/publish_x.png" width="16" height="16" />';
			$published .= '</a>';
		} else {
			$altPublishing 	= $row->published ? Text::_( 'Published' ) : Text::_( 'Unpublished' );
			$published = $row->published ? 
						'<img alt="' . $altPublishing . '" src="' . Uri::base(true) . '/components/com_jrealtimeanalytics/images/icon-16-tick.png" width="16" height="16" border="0" alt="unpublish" />' :
						'<img alt="' . $altPublishing . '" src="' . Uri::base(true) . '/components/com_jrealtimeanalytics/images/publish_x.png" width="16" height="16" border="0" alt="unpublish" />';
		}
		
		// Access check.
		$checked = null;
		if($this->user->authorise('core.edit', 'com_jrealtimeanalytics')) {
			$checked = $row->checked_out && $row->checked_out != $this->user->id ?
						HTMLHelper::_('jgrid.checkedout', $i, Factory::getContainer()->get(\Joomla\CMS\User\UserFactoryInterface::class)->loadUserById($row->checked_out)->name, $row->checked_out_time, 'eventstats.', $canCheckin) . '<input type="checkbox" style="display:none" data-enabled="false" id="cb' . $i . '" name="cid[]" value="' . $row->id . '"/>' :
						HTMLHelper::_('grid.id', $i, $row->id);
		} else {
			$checked = '<input type="checkbox" style="display:none" data-enabled="false" id="cb' . $i . '" name="cid[]" value="' . $row->id . '"/>';
		}
		?>
		<tr data-draggable-group="<?php echo $row->catid; ?>">
			<td style="width:1%" align="center">
				<?php echo $this->pagination->getRowOffset($i); ?>
			</td>
			<td style="width:1%">
				<?php echo $checked; ?>
			</td>
			<td class="sortable-adminlist-medium">
				<?php
				// Access check.
				if ( ($row->checked_out && ( $row->checked_out != $this->user->id)) || !$this->user->authorise('core.edit', 'com_jrealtimeanalytics') ) {
					echo $row->name;
				} else {
					?>
					<a href="<?php echo $link; ?>" title="<?php echo Text::_('COM_JREALTIME_EDIT_EVENT' ); ?>">
						<span class='fas fa-pen-square' aria-hidden='true'></span> 
						<?php echo $row->name; ?>
					</a>
					<?php
				}
				?>
			</td>
			<td class="sortable-adminlist-medium">
				<span class="badge bg-warning"><?php echo Text::_('COM_JREALTIME_' . $row->type); ?></span>
			</td>
			<td class="sortable-adminlist-small">
				<?php echo $row->cat_title ? $row->cat_title : '-'; ?>
			</td>
			<td class="d-none d-md-table-cell sortable-adminlist-large">
				<?php echo $row->description; ?>
			</td>
			<td class="d-none d-md-table-cell sortable-adminlist-small">
				<span class="badge bg-<?php echo $labelOccurencesClass;?>"><?php echo $row->event_occurrences; ?></span>
			</td>
			<td class="d-none d-md-table-cell sortable-adminlist-medium">
				<?php if($row->hasgoal && $row->goal_expectation): ?>
					<?php if($row->event_occurrences >= $row->goal_expectation):?>
						<?php $className = 'success large'; $translation = 'COM_JREALTIME_GOAL_REACHED'; ?>
					<?php else:?>
						<?php $className = 'info'; $translation = 'COM_JREALTIME_GOAL_INPROGRESS'; ?>
					<?php endif;?>
					<span class="badge bg-<?php echo $className;?> hasTooltip" title="<?php echo Text::_($translation); ?>">
						<?php echo $row->event_occurrences; ?>/<?php echo $row->goal_expectation; ?>
					</span>
				<?php else: ?>
					<span class="badge bg-primary"><?php echo Text::_('COM_JREALTIME_NOGOAL'); ?></span>
				<?php endif; ?>
			</td>
			<td class="order d-none d-md-table-cell sortable-adminlist-small">
				<?php 
				$ordering = $this->orders['order'] == 's.ordering'; 
				$disabled = $ordering ?  '' : 'disabled="disabled"';
				
				$iconClass = '';
				if (!$this->user->authorise('core.edit', 'com_jrealtimeanalytics')) {
					$iconClass = ' inactive';
				}
				elseif (!$ordering) {
					$iconClass = ' inactive tip-top hasTooltip" title="' . HTMLHelper::tooltipText('JORDERINGDISABLED');
				}
				?>
				<div style="display:inline-block" class="sortable-handler<?php echo $iconClass ?>" >
					<span class="icon-menu" aria-hidden="true"></span>
				</div>
				
				<span class="moveup" aria-hidden="true"><?php echo $this->pagination->orderUpIcon( $i, ($row->catid == @$this->items[$i-1]->catid), 'eventstats.moveorder_up', 'COM_JREALTIME_MOVE_UP', $ordering); ?></span>
				<span class="movedown" aria-hidden="true"><?php echo $this->pagination->orderDownIcon( $i, $n, ($row->catid == @$this->items[$i+1]->catid), 'eventstats.moveorder_down', 'COM_JREALTIME_MOVE_DOWN', $ordering); ?></span>
				<input type="text" name="order[]" size="5" value="<?php echo $row->ordering; ?>"  <?php echo $disabled; ?>  class="ordering_input" style="text-align: center" />
			</td>
					
			<td>
				<?php echo $published;?>
			</td>
			<td>
				<?php echo $row->id; ?>
			</td>
		</tr>
		<?php
	}
	?>
	</tbody>
	<tfoot>
		<td colspan="100%">
			<?php echo $this->pagination->getListFooter(); ?>
		</td>
	</tfoot>
	</table>

	<input type="hidden" name="section" value="view" />
	<input type="hidden" name="option" value="<?php echo $this->option;?>" />
	<input type="hidden" name="task" value="eventstats.display" />
	<input type="hidden" name="boxchecked" value="0" />
	<input type="hidden" name="filter_order" value="<?php echo @$this->orders['order'];?>" />
	<input type="hidden" name="filter_order_Dir" value="<?php echo @$this->orders['order_Dir'];?>" />
</form>