<?php
/**
 * @package DJ-Catalog2
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 *
 */
// no direct access
defined('_JEXEC') or die;

use Joomla\CMS\Factory;

//Joomla\CMS\HTML\HTMLHelper::_('bootstrap.tooltip');

$user = Factory::getUser();
$listOrder = strtolower($this->state->get('list.ordering'));
$listDirn = strtolower($this->state->get('list.direction'));
$saveOrder = ($listOrder == 'a.ordering' && $listDirn == 'asc');

if ($saveOrder && !empty($this->items))
{
	$saveOrderingUrl = 'index.php?option=com_djcatalog2&task=items.saveOrderAjax&tmpl=component&' . Joomla\CMS\Session\Session::getFormToken() . '=1';
	Joomla\CMS\HTML\HTMLHelper::_('draggablelist.draggable');
}
$assoc = JLanguageAssociations::isEnabled();
JLoader::register('JHtmlDJCatalog2', JPATH_ADMINISTRATOR . '/components/com_djcatalog2/helpers/html/djcatalog2.php');
?>
<form action="<?php echo JRoute::_('index.php?option=com_djcatalog2&view=items');?>" method="post" name="adminForm" id="adminForm">
<div class="row">
	<?php if (!empty($this->sidebar) /*&& !JHtmlDJCatalog2::isJoomla4()*/): ?>
	<div id="j-sidebar-container" class="col-md-2">
		<?php echo $this->sidebar; ?>
	</div>
	<div id="j-main-container" class="col-md-10">
	<?php else: ?>
		<div id="j-main-container">
	<?php endif;?>
	
	<?php if ($this->state->get('filter.parent') > 0 ) {?>
		<?php if (!empty($this->parent_item->id)) {?>
			<div class="alert alert-info">
				<?php echo JText::sprintf('COM_DJCATALOG2_NOTICE_CHILD_ITEMS_LIST', $this->escape($this->parent_item->name)); ?>
				<a class="btn btn-primary  btn-mini" href="<?php echo JRoute::_('index.php?option=com_djcatalog2&view=items&filter_parent=0'); ?>"><?php echo JText::_('COM_DJCATALOG2_CHILD_ITEMS_GO_BACK'); ?></a>
			</div>
		<?php } else {?>
			<div class="alert alert-error">
				<?php echo JText::_('COM_DJCATALOG2_NOTICE_CHILD_ITEMS_LIST_MISSING_PARENT'); ?>
				<a class="btn btn-primary  btn-mini" href="<?php echo JRoute::_('index.php?option=com_djcatalog2&view=items&filter_parent=0'); ?>"><?php echo JText::_('COM_DJCATALOG2_CHILD_ITEMS_GO_BACK'); ?></a>
			</div>
		<?php } ?>
		
	<?php } ?>
	
	<?php echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this)); ?>
	
	<div class="clearfix"> 

	<table class="table table-striped">
		<thead>
			<tr>
				<th width="1%" class="hidden-phone">
					<input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
				</th>
				<th scope="col" class="w-1 text-center d-none d-md-table-cell">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', '', 'a.ordering', $listDirn, $listOrder, null, 'asc', 'JGRID_HEADING_ORDERING', 'icon-sort'); ?>
				</th>
				<th width="75" align="center">
					<?php echo JText::_('COM_DJCATALOG2_IMAGE'); ?>
				</th>
				<th width="10%">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'COM_DJCATALOG2_NAME', 'a.name', $listDirn, $listOrder); ?>
				</th>
				<th width="5%" class="nowrap hidden-phone">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'JGRID_HEADING_ACCESS', 'a.access', $listDirn, $listOrder); ?>
				</th>
                <?php if ($assoc) : ?>
                    <th width="15%">
                    	<?php echo JText::_('COM_DJCATALOG2_ASSOCIATIONS'); ?>
                    </th>
                <?php endif; ?>
                <th width="10%">
                	<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'COM_DJCATALOG2_LANGUAGE', 'a.language', $listDirn, $listOrder); ?>
                </th>
				<th width="15%"  class="title">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort',  'COM_DJCATALOG2_CATEGORY', 'category_name', $listDirn, $listOrder ); ?>
				</th>
				<th width="15%"  class="title">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort',  'COM_DJCATALOG2_PRODUCER', 'producer_name', $listDirn, $listOrder ); ?>
				</th>
				<th width="1%" class="nowrap">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'COM_DJCATALOG2_PRICE', 'a.price', $listDirn, $listOrder); ?>
				</th>
				<th width="1%" class="nowrap">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'COM_DJCATALOG2_SPECIAL_PRICE', 'a.special_price', $listDirn, $listOrder); ?>
				</th>
				<th width="5%" colspan="3">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'JSTATUS', 'a.published', $listDirn, $listOrder); ?>
				</th>
				<?php /*?>
				<th width="5%">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'COM_DJCATALOG2_FEATURED', 'a.featured', $listDirn, $listOrder); ?>
				</th>
				<th width="5%">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'COM_DJCATALOG2_AVAILABLE', 'a.available', $listDirn, $listOrder); ?>
				</th>
				<?php */ ?>
				<th width="1%" class="nowrap">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'JGLOBAL_HITS', 'a.hits', $listDirn, $listOrder); ?>
				</th>
				<th width="1%" class="nowrap">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'JGRID_HEADING_ID', 'a.id', $listDirn, $listOrder); ?>
				</th>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="15">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody<?php if ($saveOrder) : ?> class="js-draggable" data-url="<?php echo $saveOrderingUrl; ?>" data-direction="<?php echo $listDirn; ?>" data-nested="true"<?php endif; ?>>
		<?php 
		$j=0;
		foreach ($this->items as $i => $item) :
			$item->max_ordering = 0; //??
			$canCheckin	= $user->authorise('core.manage', 'com_checkin') || $item->checked_out==$user->get('id') || $item->checked_out==0;
			$canChange  = $user->authorise('core.edit.state', 'com_djcatalog2') && $canCheckin;
			?>
			<tr class="row<?php echo $j++ % 2; ?>" data-draggable-group="<?php echo $item->cat_id.':'.$item->parent_id; ?>">
				<td class="center">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('grid.id', $i, $item->id); ?>
				</td>
				<td class="text-center d-none d-md-table-cell">
							<?php
				$iconClass = '';
				if (!$canChange)
				{
					$iconClass = ' inactive';
				}
				elseif (!$saveOrder)
				{
					$iconClass = ' inactive" title="' . JText::_('JORDERINGDISABLED');
				}
				?>
				<span class="sortable-handler<?php echo $iconClass ?>">
					<span class="icon-ellipsis-v" aria-hidden="true"></span>
				</span>
				<?php if ($canChange && $saveOrder) : ?>
					<input type="text" name="order[]" size="5" value="<?php echo $item->ordering; ?>" class="width-20 text-area-order hidden">
				<?php endif; ?>
			</td>
				<td align="center">
					<?php 
					if ($item->item_image) { ?><img alt="<?php echo $item->image_caption; ?>" src="<?php echo DJCatalog2ImageHelper::getImageUrl($item->image_fullpath,'thumb', $item->image_path); ?>"/><?php }
					else { ?><img src="<?php echo str_replace('/administrator', '', JURI::base()).'components/com_djcatalog2/assets/images/noimage.jpg'; ?>" alt="" /><?php }?>
				</td>
				<td>
					<?php if ($item->checked_out) { ?>
						<?php echo Joomla\CMS\HTML\HTMLHelper::_('jgrid.checkedout', $i, $item->editor, $item->checked_out_time, 'items.', $canCheckin); ?>
					<?php } ?>
					<?php if (!$canCheckin) :?>
						<?php echo $this->escape($item->name); ?>
					<?php else: ?>
						<span class="editlinktip hasTip" title="<?php echo JText::_( 'COM_DJCATALOG2_EDIT_ITEM_TOOLTIP' );?>::<?php echo $this->escape($item->name); ?>">
						<a href="<?php echo JRoute::_('index.php?option=com_djcatalog2&task=item.edit&id='.$item->id);?>">
							<?php echo $this->escape($item->name); ?></a>
						</span>
					<?php endif; ?>
						
					<div class="small"><?php echo JText::sprintf('JGLOBAL_LIST_ALIAS', $this->escape($item->alias));?></div>
					
					<?php if ($item->sku) {?>
						<div class="small"><?php echo JText::_('COM_DJCATALOG2_SKU').': '.$item->sku; ?></div>
					<?php } ?>
				</td>
				<td class="small hidden-phone">
					<?php echo $this->escape($item->access_level); ?>
				</td>
                <?php if ($assoc) : ?>
                    <td align="center">
                        <?php if ($item->association) : ?>
                            <?php echo Joomla\CMS\HTML\HTMLHelper::_('DJCatalog2.associationItem', $item->id); ?>
                        <?php endif; ?>
                    </td>
                <?php endif; ?>
                <td align="center">
                    <?php echo JLayoutHelper::render('joomla.content.language', $item); ?>
                </td>
				<td>
					<span class="editlinktip hasTip" title="<?php echo JText::_( 'COM_DJCATALOG2_EDIT_CATEGORY_TOOLTIP' );?>::<?php echo $this->escape($item->category_name); ?>">
					<a href="<?php echo JRoute::_('index.php?option=com_djcatalog2&task=category.edit&id='.$item->cat_id);?>" >
					<?php echo $this->escape($item->category_name); ?></a></span>
				</td>
				<td>
					<span class="editlinktip hasTip" title="<?php echo JText::_( 'COM_DJCATALOG2_EDIT_PRODUCER_TOOLTIP' );?>::<?php echo $this->escape($item->producer_name); ?>">
					<a href="<?php echo JRoute::_('index.php?option=com_djcatalog2&task=producer.edit&id='.$item->producer_id);?>" >
					<?php echo $this->escape($item->producer_name); ?></a></span>
				</td>
				<td class="center">
					<?php echo number_format($item->price, 2); ?>
				</td>
				<td class="center">
					<?php echo number_format($item->special_price, 2); ?>
				</td>
				<td class="text-center d-none d-md-table-cell">
					<?php
						$options = [
									'task_prefix' => 'items.',
									'disabled' => !$canChange,
									'id' => 'published-' . $item->id
									];
						echo (new Joomla\CMS\Button\PublishedButton)
						->addState(3, 'publish', 'unpublish', JText::_('JLIB_HTML_PUBLISH_ITEM'), ['tip_title' => JText::_('COM_DJCATALOG2_PUBLISHED_EXCLUDED')])
						->render((int) $item->published, $i, $options, $item->publish_up, $item->publish_down);
					?>
				</td>
				<td class="text-center d-none d-md-table-cell">
					<?php
						$options = [
							'task_prefix' => 'items.',
							'disabled' => !$canChange,
							'id' => 'featured-' . $item->id
						];
	
						echo (new Joomla\CMS\Button\FeaturedButton)
							->render((int) $item->featured, $i, $options);
					?>
				</td>
				<td class="text-center d-none d-md-table-cell">
					<?php
						$options = [
							'task_prefix' => 'items.',
							'disabled' => !$canChange,
							'id' => 'available-' . $item->id
						];
	
						echo (new Joomla\CMS\Button\ActionButton)
							->addState(0, 'available', 'unpublish', JText::_('COM_DJCATALOG2_AVAILABLE'), ['tip_title' => JText::_('COM_DJCATALOG2_MAKE_AVAILABLE')])
							->addState(1, 'inavailable', 'publish', JText::_('COM_DJCATALOG2_AVAILABLE'), ['tip_title' => JText::_('COM_DJCATALOG2_MAKE_NOT_AVAILABLE')])
							->render((int) $item->available, $i, $options);
					?>
				</td>
				<?php /*?>
				<td class="text-center d-none d-md-table-cell">			
				
					<?php // echo Joomla\CMS\HTML\HTMLHelper::_('jgrid.published', $item->published, $i, 'items.', true, 'cb'	); ?>
					<div class="btn-group">
						<?php
						Joomla\CMS\HTML\HTMLHelper::_('actionsdropdown.' . 'archive', 'cb' . $i, 'items');
						Joomla\CMS\HTML\HTMLHelper::_('actionsdropdown.' . 'unarchive', 'cb' . $i, 'items');
						Joomla\CMS\HTML\HTMLHelper::_('actionsdropdown.' . 'trash', 'cb' . $i, 'items');
						Joomla\CMS\HTML\HTMLHelper::_('actionsdropdown.' . 'untrash', 'cb' . $i, 'items');
						//JHtmlActionsDropdown::addCustomItem(JText::_('COM_DJCATALOG2_PUBLISHED_UNEXCLUDE'), 'publish', 'cb'.$i, 'items.Listing');
						JHtmlActionsDropdown::addCustomItem(JText::_('COM_DJCATALOG2_PUBLISHED_EXCLUDE'), 'unpublish', 'cb'.$i, 'items.unlisting');
						
						echo Joomla\CMS\HTML\HTMLHelper::_('actionsdropdown.render', $this->escape($item->name));
						?>
					</div>
				</td>
				<?php */ ?>
				<td class="center">
					<?php echo (int) $item->hits; ?>
				</td>
				<td class="center">
					<?php echo $item->id; ?>
					<?php if ($item->parent_id == 0) {?>
						<a class="btn btn-primary btn-mini" href="<?php echo JRoute::_('index.php?option=com_djcatalog2&view=items&filter_parent='.(int)$item->id); ?>&filter_search="><?php echo JText::_('COM_DJCATALOG2_SHOW_CHILDREN'); ?></a>
						<br />
					<?php } ?>
						<a class="btn btn-primary btn-mini" href="<?php echo JRoute::_('index.php?option=com_djcatalog2&view=reviews&filter_search=id:'.(int)$item->id); ?>"><?php echo JText::_('COM_DJCATALOG2_REVIEWS'); ?></a>
				</td>
			</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
	</div>
</div>
	<div>


        <?php echo Joomla\CMS\HTML\HTMLHelper::_(
            'bootstrap.renderModal',
            'collapseModal',
            array(
                'title'  => JText::_('COM_DJCATALOG2_BATCH_TITLE'),
                'footer' => $this->loadTemplate('batch_footer'),
            ),
            $this->loadTemplate('batch')
        ); ?>


        <input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<?php echo Joomla\CMS\HTML\HTMLHelper::_('form.token'); ?>
	</div>
</div>
</form>


