<?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.lft') {
	$saveOrderingUrl = 'index.php?option=com_jrealtimeanalytics&task=categories.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->pagination->getLimitBox();
					?>
				</div>
			</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_TITLE', 's.title', @$this->orders['order_Dir'], @$this->orders['order'], 'categories.display'); ?>
			</th>
			<th class="title d-none d-md-table-cell">
				<?php echo Text::_('COM_JREALTIME_DESCRIPTION'); ?>
			</th>
			<th class="order d-none d-md-table-cell">
				<?php echo HTMLHelper::_('grid.sort', 'COM_JREALTIME_ORDER', 's.lft', 'desc', @$this->orders['order'], 'categories.display'); ?>
				<?php 
					if(isset($this->orders['order']) && $this->orders['order'] == 's.lft'):
						echo JRealtimeHelpersHtml::order($this->items, 'filesave.png', 'categories.saveOrder'); 
					endif;
				 ?>
			</th>
			<th style="width:5%">
				<?php echo HTMLHelper::_('grid.sort',   'COM_JREALTIME_PUBLISHED', 's.published', @$this->orders['order_Dir'], @$this->orders['order'], 'categories.display' ); ?>
			</th>
			<th style="width:5%">
				<?php echo HTMLHelper::_('grid.sort',   'COM_JREALTIME_ID', 's.id', @$this->orders['order_Dir'], @$this->orders['order'], 'categories.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']); ?>" <?php endif; ?> data-nested="true">
	<?php
	$k = 0;
	$originalOrders = array();
	$canCheckin = $this->user->authorise('core.manage', 'com_checkin');
	for ($i=0, $n=count( $this->items ); $i < $n; $i++) {
		$row = $this->items[$i];
		$orderkey	= array_search($row->id, $this->ordering[$row->parent_id]);
		$link =  'index.php?option=com_jrealtimeanalytics&task=categories.editEntity&cid[]='. $row->id ;
		// Access check.
		if($this->user->authorise('core.edit.state', 'com_jrealtimeanalytics')) {
			$taskPublishing	= !$row->published ? 'categories.publish' : 'categories.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" />' : 
						 '<img alt="' . $altPublishing . '" src="' . Uri::base(true) . '/components/com_jrealtimeanalytics/images/publish_x.png" width="16" height="16" />';
		}
		
		// 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, 'categories.', $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 . '"/>';
		}
		
		// Get the parents of item for sorting
		if ($row->level > 1)
		{
			$parentsStr = "";
			$_currentParentId = $row->parent_id;
			$parentsStr = " " . $_currentParentId;
			for ($i2 = 0; $i2 < $row->level; $i2++)
			{
				foreach ($this->ordering as $k => $v)
				{
					$v = implode("-", $v);
					$v = "-" . $v . "-";
					if (strpos($v, "-" . $_currentParentId . "-") !== false)
					{
						$parentsStr .= " " . $k;
						$_currentParentId = $k;
						break;
					}
				}
			}
		}
		else
		{
			$parentsStr = "";
		}
		?>
		<tr data-draggable-group="<?php echo $row->parent_id; ?>" data-item-id="<?php echo $row->id ?>" data-parents="<?php echo $parentsStr ?>" data-level="<?php echo $row->level ?>">
			<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-extralarge">
				<?php
				// Access check.
				if ( ($row->checked_out && ( $row->checked_out != $this->user->id)) || !$this->user->authorise('core.edit', 'com_jrealtimeanalytics') ) {
					echo str_repeat ( '<span class="catlevel">- </span>', $row->level - 1) . $row->title;
				} 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 str_repeat ( '<span class="catlevel">- </span>', $row->level - 1) . $row->title; ?>
					</a>
					<?php
				}
				?>
			</td>
			<td class="d-none d-md-table-cell sortable-adminlist-verylarge">
				<?php echo $row->description; ?>
			</td>
			<td class="order d-none d-md-table-cell sortable-adminlist-small">
				<?php 
				$ordering = $this->orders['order'] == 's.lft'; 
				$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, isset($this->ordering[$row->parent_id][$orderkey - 1]), 'categories.moveorder_up', 'COM_JREALTIME_MOVE_UP', $ordering); ?></span>
				<span class="movedown" aria-hidden="true"><?php echo $this->pagination->orderDownIcon( $i, $n, isset($this->ordering[$row->parent_id][$orderkey + 1]), 'categories.moveorder_down', 'COM_JREALTIME_MOVE_DOWN', $ordering); ?></span>
				<input type="text" name="order[]" size="5" value="<?php echo $orderkey + 1;?>"  <?php echo $disabled; ?>  class="ordering_input" style="text-align: center" />
				<?php $originalOrders[] = $orderkey + 1; ?>
			</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="categories.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'];?>" />
	<input type="hidden" name="original_order_values" value="<?php echo implode(',', $originalOrders); ?>" />
</form>