<?php

/**
 * @version		   1.9.8
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Dang Thuc Dam
 * @copyright	   Copyright (C) 2022 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */ 
defined('_JEXEC') or die ;
use Joomla\Filesystem\Folder;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;

$bootstrapHelper 	= $this->bootstrapHelper;
$rowFluidClass   	= $bootstrapHelper->getClassMapping('row-fluid');
$span12Class		= $bootstrapHelper->getClassMapping('span12');
$controlsClass		= $bootstrapHelper->getClassMapping('controls');
$extralayoutCss = ((int)$this->config->theme_type == 1 ? "dark_layout" : "");

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
if(!is_dir(Path::clean(JPATH_ROOT.'/media/com_edocman/document/medium'))){
    Folder::create(JPATH_ROOT.'/media/com_edocman/document/medium');
    File::copy(JPATH_ROOT.'/media/com_edocman/index.html',JPATH_ROOT.'/media/com_edocman/document/medium/index.html');
}
$user           = Factory::getApplication()->getIdentity();
$userId         = $user->get('id');
?>
<form method="post" name="adminForm" id="edocman_form" action="<?php echo Uri::root();?>index.php?option=com_edocman&Itemid=<?php echo $this->Itemid; ?>">
<div class="<?php echo $rowFluidClass." ".$extralayoutCss; ?>" id="edocmanSearchDiv">
	<div class="<?php echo $span12Class; ?>">
		<div class="<?php echo $rowFluidClass; ?>">
			<div class="<?php echo $span12Class; ?>">
				<?php
				$category = '';
				$keyword  = '';
				if($this->state->filter_search != "" || $this->state->filter_category_id > 0)
				{
					if($this->state->filter_category_id > 0)
					{
						$cat = EdocmanHelper::getCategory($this->state->filter_category_id);
					}
					if($this->state->filter_search != "")
					{
						$text1 = Text::_('EDOCMAN_HERE_ARE_SEARCH_RESULT_FOR');
						if($this->state->filter_category_id > 0)
						{
							$text2 = Text::_('EDOCMAN_IN_CATEGORY');

							$category = '<strong class="category">'.$cat->title.'</strong>';
						}

						$keyword = ' <strong class="keyword">'.$this->state->filter_search.'</strong>';
					}
					elseif($this->state->filter_category_id > 0)
					{
						$text1 = Text::_('EDOCMAN_HERE_ARE_DOCUMENTS_IN_CATEGORY');
						$text2 = "";

						$category = ' <strong class="category">'.$cat->title.'</strong>';
					}
					?>
					<div class="search_notification">
						<p><?php echo $text1. $keyword. $text2. $category?></p>
					</div>
					<?php
				}
				if($this->show_search_form)
				{
				?>
					<div class="<?php echo $rowFluidClass; ?>" id="edocmanSearchForm">
						<div class="btn-toolbar">
							<div class="btn-group pull-left">
								<label for="search-searchword" class="element-invisible">
									<?php echo Text::_('EDOCMAN_KEYWORD');?>:			
								</label>
								<input type="text" name="filter_search" title="<?php echo Text::_('EDOCMAN_KEYWORD');?>" placeholder="<?php echo Text::_('EDOCMAN_KEYWORD');?>" id="search-searchword" size="30" maxlength="200" value="<?php echo htmlspecialchars($this->state->filter_search, ENT_QUOTES, 'UTF-8'); ?>" class="input-large ilarge form-control" />
							</div>
							<div class="btn-group pull-left">
								<?php echo $this->categoryList; ?>
							</div>
							<div class="btn-group pull-left">
								<button name="Search" onclick="this.form.submit()" class="btn btn-primary hasTooltip" title="Search">
									<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
									  <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
									</svg>
									<?php echo Text::_('EDOCMAN_SEARCH');?>			
								</button>
							</div>
							<div class="clearfix"></div>
						</div>
						<div class="searchintro">
						</div>
					</div>
				<?php
				}						
				?>
			</div>
		</div>
		<div class="<?php echo $rowFluidClass; ?>">
			<div class="<?php echo $span12Class; ?>">
				<?php
				if($this->state->filter_category_id || $this->state->filter_search )
				{
					?>
					<h2 class="edocman_heading_title"><?php echo Text::_('EDOCMAN_SEARCH_RESULT'); ?></h2>
					<?php
					if($this->searchValue != "")
					{
						?>
						<h5>
							<?php echo Text::_('EDOCMAN_SEARCH_FOR');?>
							<?php echo $this->searchValue; ?>
						</h5>
					<?php
					}		
					?>
					<?php
				}	
				if(count($this->items)) 
				{
					echo EDocmanHelperHtml::loadCommonLayout('common/documents_columns.php', array('items' => $this->items, 'config' => $this->config, 'Itemid' => $this->Itemid, 'category' => $this->category ,'categoryId' => $this->categoryId,  'bootstrapHelper' => $this->bootstrapHelper ,'show_category' => $this->show_category));
					if ($this->pagination->total > $this->pagination->limit)
					{
					?>
						<div class="pagination">
							<?php echo $this->pagination->getPagesLinks(); ?>
						</div>
					<?php
					}
				}
				else
				{
					if($this->state->filter_category_id || $this->state->filter_search || $this->state->filter_tag)
					{
					?>
						<p><?php echo Text::_('EDOCMAN_NO_DOCS_FOUND'); ?></p>
					<?php
					}
				}
				?>
			</div>
		</div>
	</div>
</div>
<input type="hidden" name="view" value="search" />	
<input type="hidden" name="Itemid" value="<?php echo $this->Itemid ; ?>" />
<input type="hidden" name="option" value="com_edocman" />
<input type="hidden" name="cid[]" value="0" id="document_id" />
<input type="hidden" id="task" name="task" value="" />
<input type="hidden" name="document_id" id="document_id" value="" />
<input type="hidden" name="layout" value="columns" />
<input type="hidden" name="<?php echo EdocmanHelper::getHashedFieldName(); ?>" value="<?php echo time(); ?>" />
<input type="hidden" name="show_searchform" value="<?php echo Factory::getApplication()->getInput()->getInt('show_searchform', 0); ?>" />
<input type="hidden" name="return" id="current_url" value="<?php echo base64_encode(Uri::getInstance()->toString());?>" />
<input type="hidden" name="show_category" id="show_category" value="<?php echo $this->show_category; ?>" />
<?php echo HTMLHelper::_('form.token'); ?>
</form>
<script type="text/javascript">
function changeDirection(newDirection) {
	var form = document.getElementById('edocman_form') ;
	form.direction.value = newDirection ;
	form.submit();
}	

function changeDirection(newDirection) {
	var form = document.getElementById('edocman_form') ;
	form.direction.value = newDirection ;
	form.submit();
}
function deleteConfirm(id) {
	var msg = "<?php echo Text::_('EDOCMAN_DELETE_CONFIRM'); ?>";
	if (confirm(msg)) {
		var form = document.getElementById('edocman_form') ;
		form.task.value = 'document.delete';
		document.getElementById('document_id').value = id;
		form.submit();
	}
}

function publishConfirm(id, published) {
	var msg, task ;
	if (published) {
		msg = "<?php echo Text::_("EDOCMAN_PUBLISH_CONFIRM"); ?>";
		task = "document.publish" ;
	} else {
		msg = "<?php echo Text::_("EDOCMAN_UNPUBLISH_CONFIRM"); ?>";
		task = "document.unpublish" ;
	}
	if (confirm(msg)) {
		var form = document.getElementById('edocman_form') ;
		form.task.value = task ;
		document.getElementById('document_id').value = id;
		form.submit();
	}
}
</script>
