<?php

/**
 * @version        1.9.7
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

// no direct access
defined('_JEXEC') or die;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Language\Text;

if (version_compare(JVERSION, '4.0.0-dev', 'lt'))
{
	HTMLHelper::_('behavior.formvalidation');
	HTMLHelper::_('behavior.tooltip');
}
else
{
	HTMLHelper::_('bootstrap.tooltip', '.hasTooltip', ['placement' => 'left']);
}
$customOptions = array(
	'filtersHidden'       => true,
	'defaultLimit'        => Factory::getApplication()->get('list_limit', 20),
	'searchFieldSelector' => '#filter_search'
);
HTMLHelper::_('searchtools.form', '#adminForm', $customOptions);
HTMLHelper::_('script','system/multiselect.js',false,true);
$listOrder	= $this->state->filter_order;
$listDirn	= $this->state->filter_order_Dir;
$config     = EDocmanHelper::loadConfig();

if(EdocmanHelper::isJoomla4())
{
	$searchBtnClass = "btn-primary";
	$cancelSearchBtnClass = "btn-secondary";
}
else
{
	$searchBtnClass = "";
	$cancelSearchBtnClass = "";
}
$bootstrapHelper	= new EDocmanHelperBootstrap(EDocmanHelperHtml::getAdminBootstrapHelper());
$rowFluidClass		= $bootstrapHelper->getClassMapping('row-fluid');
$span12Class		= $bootstrapHelper->getClassMapping('span12');

$db = $db = Factory::getContainer()->get('db');;

if(Factory::getApplication()->getInput()->getString('start_date','') != '' || Factory::getApplication()->getInput()->getString('end_date','') != '' )
{
	$showFilterForm = "js-stools-container-filters-visible";
}
else
{
	$showFilterForm = "";
}
?>

<form action="<?php echo Route::_('index.php?option=com_edocman&view=terms'); ?>" method="post" name="adminForm" id="adminForm">
	<div class="<?php echo $rowFluidClass;?>">
		<div class="<?php echo $span12Class;?>">
			<?php
			if(EdocmanHelper::isJoomla4())
			{
			?>
				<div class="js-stools" role="search">
					<div class="js-stools-container-bar">
						<div id="filter-bar" class="btn-toolbar">
							<div class="btn-group">
								<div class="input-group input-append">
									<input type="text" name="filter_search" id="filter_search" placeholder="<?php echo Text::_('JSEARCH_FILTER'); ?>" value="<?php echo $this->escape($this->state->filter_search); ?>" class="hasTooltip input-medium form-control" title="<?php echo HTMLHelper::tooltipText('EDOCMAN_FILTER_SEARCH_DOCUMENTS_DESC'); ?>" inputmode="search" aria-describedby="filter_search-desc"/>
									
									<button type="submit" class="btn <?php echo $searchBtnClass;?> hasTooltip" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_SUBMIT'); ?>"><span class="icon-search"></span></button>
								</div>
							</div>
							<div class="btn-group">
								<button type="button" class="btn btn-primary js-stools-btn-filter">
									Filter Options	<span class="icon-angle-down" aria-hidden="true"></span>
								</button>
								<button type="button" class="btn <?php echo $cancelSearchBtnClass; ?> hasTooltip js-stools-btn-clear" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_CLEAR'); ?>" onclick="document.getElementById('filter_search').value='';this.form.submit();"><span class="icon-remove"></span></button>
							</div>
							<div class="ordering-select">
								<div class="js-stools-field-list">
									<?php
									echo $this->pagination->getLimitBox();
									?>
								</div>
							</div>
						</div>
					</div>
					<div class="js-stools-container-filters clearfix <?php echo $showFilterForm;?>">
						<div class="js-stools-field-filter">
							<?php echo HTMLHelper::_('calendar', Factory::getApplication()->getInput()->getString('start_date',''), 'start_date', 'start_date','%Y-%m-%d', array('class' => 'input-small form-control ishort','placeholder' => Text::_('EDOCMAN_START'))) ; ?>
						</div>
						<div class="js-stools-field-filter">
							<?php echo HTMLHelper::_('calendar', Factory::getApplication()->getInput()->getString('end_date',''), 'end_date', 'end_date','%Y-%m-%d', array('class' => 'input-small form-control ishort', 'placeholder' => Text::_('EDOCMAN_END'))) ; ?>
						</div>
					</div>
				</div>
			<?php
			}
			else
			{
			?>
				<div class="js-stools clearfix" role="search">
					<div class="clearfix">
						<div class="js-stools-container-bar">
							<div id="filter-bar" class="js-stools-container-bar">
								<div class="btn-wrapper input-append">
									<input type="text" name="filter_search" id="filter_search" placeholder="<?php echo Text::_('JSEARCH_FILTER'); ?>" value="<?php echo $this->escape($this->state->filter_search); ?>" class="hasTooltip input-medium form-control" title="<?php echo HTMLHelper::tooltipText('EDOCMAN_FILTER_SEARCH_DOCUMENTS_DESC'); ?>" inputmode="search" aria-describedby="filter_search-desc"/>
									
									<button type="submit" class="btn <?php echo $searchBtnClass;?> hasTooltip" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_SUBMIT'); ?>"><span class="icon-search"></span></button>
									
								</div>
								<div class="btn-wrapper">
									<button type="button" class="btn hasTooltip js-stools-btn-filter">
										Filter Options	<span class="caret" aria-hidden="true"></span>
									</button>
									<button type="button" class="btn <?php echo $cancelSearchBtnClass; ?> hasTooltip js-stools-btn-clear" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_CLEAR'); ?>" onclick="document.getElementById('filter_search').value='';this.form.submit();"><span class="icon-remove"></span></button>
								</div>
							</div>
						</div>
						<div class="js-stools-container-list hidden-phone hidden-tablet shown">
							<div class="js-stools-field-list">
								<?php
								echo $this->pagination->getLimitBox();
								?>
							</div>
						</div>
					</div>
					<div class="js-stools-container-filters clearfix <?php echo $showFilterForm;?>">
						<div class="js-stools-field-filter">
							<?php echo HTMLHelper::_('calendar', Factory::getApplication()->getInput()->getString('start_date',''), 'start_date', 'start_date','%Y-%m-%d', array('class' => 'input-small form-control ishort','placeholder' => Text::_('EDOCMAN_START'))) ; ?>
						</div>
						<div class="js-stools-field-filter">
							<?php echo HTMLHelper::_('calendar', Factory::getApplication()->getInput()->getString('end_date',''), 'end_date', 'end_date','%Y-%m-%d', array('class' => 'input-small form-control ishort', 'placeholder' => Text::_('EDOCMAN_END'))) ; ?>
						</div>
					</div>
				</div>
			<?php
			}
			?>
		</div>
	</div>
	<div class="<?php echo $rowFluidClass;?>">
		<div class="<?php echo $span12Class;?>">
			<table class="table table-striped">
				<thead>
					<tr>
						<th width="1%">
							<input type="checkbox" name="checkall-toggle" value="" onclick="Joomla.checkAll(this);" />
						</th>
						<th class="title" style="text-align: left;">
							<?php echo HTMLHelper::_('grid.sort',  Text::_('EDOCMAN_TERM'), 'tbl.keyword', $listDirn, $listOrder); ?>
						</th>
						<th width="15%" style="text-align: center;">
							<?php echo HTMLHelper::_('grid.sort',  Text::_('EDOCMAN_CREATED_ON'), 'tbl.searched_on', $listDirn, $listOrder); ?>
						</th>
						<th width="10%" style="text-align: center;">
							<?php echo Text::_('IP Address'); ?>
						</th>
						<th width="30%" style="text-align: left;">
							<?php echo Text::_('EDOCMAN_LOCATION'); ?>
						</th>    			
						<th width="10%" style="text-align: center;">
							<?php echo Text::_('EDOCMAN_IS_EXISTING_TAG'); ?>
						</th> 														               
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="6">
							<?php echo $this->pagination->getListFooter(); ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
				<?php foreach ($this->items as $i => $item) :
				
					$db->setQuery("Select count(id) from #__edocman_tags where tag = ".$db->quote($item->keyword));
					$count = $db->loadResult();
					?>
					<tr class="row<?php echo $i % 2; ?>">
						<td class="center">
							<?php echo HTMLHelper::_('grid.id', $i, $item->id); ?>
						</td>		       
						<td>
							<?php echo $item->keyword ; ?>
						</td>
						<td class="center">
							<?php echo $item->searched_on; ?>
						</td>                               
						<td class="center">
							<?php echo $item->ip_address ; ?>
						</td>               
						<td style="text-align: left;">
							<?php
							if($item->city != "")
							{
								echo $item->city;
							}
							if($item->postcode != "")
							{
								echo ", ".$item->postcode;
							}
							if($item->country != "")
							{
								echo ", ".$item->country;
							}
							?>
						</td>
						<td class="center">
							<?php
							if((int)$count > 0)
							{
								?>
								<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="green" class="bi bi-check2-circle" viewBox="0 0 16 16">
								  <path d="M2.5 8a5.5 5.5 0 0 1 8.25-4.764.5.5 0 0 0 .5-.866A6.5 6.5 0 1 0 14.5 8a.5.5 0 0 0-1 0 5.5 5.5 0 1 1-11 0z"/>
								  <path d="M15.354 3.354a.5.5 0 0 0-.708-.708L8 9.293 5.354 6.646a.5.5 0 1 0-.708.708l3 3a.5.5 0 0 0 .708 0l7-7z"/>
								</svg>
								<?php
							}
							else
							{
								?>
								<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="red" class="bi bi-x-circle" viewBox="0 0 16 16">
								  <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
								  <path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"/>
								</svg>
								<?php
							}
							?>
						</td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<input type="hidden" name="task" value="" />
			<input type="hidden" name="boxchecked" value="0" />
			<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
			<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
			<?php echo HTMLHelper::_('form.token'); ?>
		</div>
	</div>
</form>
