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

// no direct access
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;
HTMLHelper::_('behavior.core');
HTMLHelper::_('bootstrap.tooltip');
$app		= Factory::getApplication();
$user		= Factory::getApplication()->getIdentity();
$userId		= $user->get('id');
$function	= $this->state->function;
$listOrder	= $this->state->filter_order;
$listDirn	= $this->state->filter_order_Dir;
$defaultItemid = $app->getInput()->getInt('defaultItemid', '0');
$onclick	= $this->escape($function);
$bootstrapHelper	= $this->bootstrapHelper;
$rowFluidClass		= $bootstrapHelper->getClassMapping('row-fluid');
$span12Class		= $bootstrapHelper->getClassMapping('span12');
$controGroupClass	= $bootstrapHelper->getClassMapping('control-group');
$controLabelClass	= $bootstrapHelper->getClassMapping('control-label');
$controlsClass		= $bootstrapHelper->getClassMapping('controls');
$btnClass = $app->getInput()->getString('btnClass','edocmanxtdlink');
$isJoomla4	= EdocmanHelper::isJoomla4();
$editor		= $app->getInput()->getCmd('editor', '');
$app->getDocument()->addScriptOptions('EDEditor', $editor)
	->addScriptOptions('isJoomla4', true)
	->addScriptOptions('defaultItemid', $defaultItemid)
	->addScriptOptions('btnClass', $btnClass)
	->getWebAssetManager()
	->useScript('core');
	
EdocmanHelper::addScript(Uri::root() . 'components/com_edocman/assets/js/admin-documents-modal.js');
$dItemid  = $app->getInput()->getInt('Itemid', $defaultItemid);
//$this->canUpload = false;
if ($this->canUpload)
{
?>
	<div class="<?php echo $rowFluidClass;?>" id="modalHeading">
		<form class="form form-horizontal">
			<div class="<?php echo $controGroupClass;?>">
				<label class="<?php echo $controLabelClass;?>" for="choose_document_option">
					<?php echo Text::_('EDOCMAN_INSERT_DOCUMENT_OPTION'); ?>
				</label>
				<?php
				if (version_compare(JVERSION, '3.0', 'lt'))
				{
					?>
					<div class="<?php echo $controlsClass;?>">
						<?php echo $this->lists['choose_document_option']; ?>
					</div>
					<?php
				}
				elseif(EdocmanHelper::isJoomla4())
				{
					?>
					<div class="<?php echo $controlsClass;?>">
						<?php echo $this->lists['choose_document_option']; ?>
					</div>
					<?php
				}
				else
				{
					echo $this->lists['choose_document_option'];
				}
				?>
			</div>
		</form>
	</div>
<?php
}
?>
<form action="<?php echo Route::_('index.php?option=com_edocman&view=documents&layout=modal&tmpl=component&function='.$function.'&'.Session::getFormToken().'=1'); ?>" method="post" name="adminForm" id="adminForm" onsubmit="storeInsertOption();">
	<fieldset id="filter-bar">
		<div class="btn-wrapper input-append btn-group">
			<div class="input-group">
				<input type="text" name="filter_search" placeholder="<?php echo Text::_('JSEARCH_FILTER_LABEL'); ?>" id="filter_search" value="<?php echo $this->escape($this->state->get('filter_search')); ?>" title="<?php echo Text::_('Search'); ?>" class="input-medium form-control" />
				<button class="btn btn-primary" type="submit"><?php echo Text::_('JSEARCH_FILTER_SUBMIT'); ?></button>
				<button class="btn btn-secondary hasTooltip js-stools-btn-clear" type="button" onclick="document.id('filter_search').value='';this.form.submit();"><?php echo Text::_('JSEARCH_FILTER_CLEAR'); ?></button>
			</div>
		</div>
		<div class="filter-select fltrt pull-right">
			<?php echo EdocmanHelper::getChoicesJsSelect($this->lists['filter_category_id']); ?>
			<?php
			if ($this->config->activate_multilingual_feature) {
				?>
				<select name="filter_language" class="inputbox form-select" onchange="this.form.submit()">
					<option value=""><?php echo Text::_('JOPTION_SELECT_LANGUAGE');?></option>
					<?php echo HTMLHelper::_('select.options', HTMLHelper::_('contentlanguage.existing', true, true), 'value', 'text', $this->state->get('filter.language'));?>
				</select>
			<?php
			}
			?>
			<select name="filter_published" class="inputbox form-select" onchange="this.form.submit()">
				<option value=""><?php echo Text::_('JOPTION_SELECT_PUBLISHED');?></option>
				<?php echo HTMLHelper::_('select.options', HTMLHelper::_('jgrid.publishedOptions'), "value", "text", $this->state->filter_published, true);?>
			</select>
		</div>
	</fieldset>
	<div class="clearfix"></div>
	<table class="adminlist table table-striped table-bordered  table-condensed">
		<thead>
			<tr>
				<th width="1%">
					<input type="checkbox" name="checkall-toggle" value="" onclick="checkAll(this)" />
				</th>      
				<th style="text-align: left;">
					<?php echo Text::_('EDOCMAN_TITLE'); ?>					
				</th>			
				<th width="15%" style="text-align: left;">
					<?php echo Text::_('EDOCMAN_CATEGORY'); ?>
				</th>	        				                               
				<th width="5%">
					<?php echo Text::_('EDOCMAN_HITS'); ?>					
				</th>
				<th width="5%">
					<?php echo Text::_('EDOCMAN_DOWNLOADS'); ?>					
				</th>         
				<th width="10%">
					<?php echo Text::_('JGRID_HEADING_ACCESS'); ?>					
				</th>                       
                <th width="1%" class="nowrap">
                	<?php echo Text::_('JGRID_HEADING_ID'); ?>                    
                </th>                
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="7">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody>
		<?php foreach ($this->items as $i => $item) :

			$needles = array (
			'document'  => array((int) $item->id)
			);
			$cat = EdocmanHelper::getDocumentCategory($item->id);
			$catId = $cat->id;
			$itemid         = EDocmanHelperRoute::getDocumentMenuId($item->id, $catid, $defaultItemid);
			if((int) $itemid == 0)
			{
				$itemid = $defaultItemid;
			}
			?>
			<tr class="row<?php echo $i % 2; ?>">
				<td class="center">
					<?php echo HTMLHelper::_('grid.id', $i, $item->id); ?>
				</td>    
				<td>
					<!--
    				<a class="pointer" onclick="if (window.parent) window.parent.<?php echo $this->escape($function);?>('<?php echo $item->id; ?>', '<?php echo $this->escape(addslashes($item->title)); ?>');">
					-->
					<a class="pointer select-link" href="javascript:void(0)" data-function="<?php echo $this->escape($onclick); ?>" data-id="<?php echo $item->id; ?>" data-itemid="<?php echo $itemid;?>" data-title="<?php echo $item->title;?>" data-btnclass="<?php echo $btnClass ;?>">
    						<?php echo $item->title; ?></a>    			   				
    				<p class="smallsub">					
				</td> 					
				<td>
					<?php echo $item->category_title ; ?>
				</td>			          			              
			    <td class="center">
			    	<?php echo (int)$item->hits ; ?>
			    </td>
			    <td class="center">
			    	<?php echo (int)$item->downloads ; ?>
			    </td>
			    <td class="center">
				    <?php echo $item->access_level ; ?>
				</td>                        
				<td class="center">
					<?php echo (int) $item->id; ?>
				</td>                
			</tr>
			<?php endforeach; ?>
		</tbody>
	</table>

	<div>
		<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; ?>" />
		<input type="hidden" name="choose_document_option" value="0" />
		<input type="hidden" name="editor" id="editor" value="<?php echo $editor; ?>" />
		<input type="hidden" name="Itemid" value="<?php echo $dItemid; ?>" />
		<input type="hidden" name="defaultItemid" value="<?php echo $defaultItemid; ?>" />
		<input type="hidden" name="btnClass" id="btnclass" value="<?php echo $btnClass; ?>" />
		<?php echo HTMLHelper::_('form.token'); ?>
	</div>
</form>

<?php
	if ($this->canUpload)
	{
	?>
		<div class="<?php echo $rowFluidClass;?>" id="modalUploadDocumentForm">
			<form  action="<?php echo Route::_('index.php?option=com_edocman&task=document.saveDocument'); ?>" method="post" name="upload-document-form" id="item-form" class="form form-horizontal upload-new-document" enctype="multipart/form-data">
				<div class="<?php echo $controGroupClass;?>">
					<label class="<?php echo $controLabelClass;?>" for="title">
						<?php echo Text::_('JGLOBAL_TITLE'); ?>
					</label>
					<div class="<?php echo $controlsClass;?>">
						<input type="text" required="required" size="40" class="input-large form-control" value="" id="title" name="title" />
					</div>
				</div>
				<div class="<?php echo $controGroupClass;?>">
					<label class="<?php echo $controLabelClass;?>" for="category_id">
						<?php echo Text::_('EDOCMAN_CATEGORY'); ?>
					</label>
					<div class="<?php echo $controlsClass;?>">
						<?php echo $this->lists['category_id']; ?>
					</div>
				</div>
				<div class="<?php echo $controGroupClass;?>">
					<label class="<?php echo $controLabelClass;?>" for="access">
						<?php echo Text::_('JFIELD_ACCESS_LABEL'); ?>
					</label>
					<div class="<?php echo $controlsClass;?>">
						<?php echo $this->lists['access']; ?>
					</div>
				</div>
				<div class="<?php echo $controGroupClass;?>">
					<label class="<?php echo $controLabelClass;?>" for="published">
						<?php echo Text::_('JSTATUS'); ?>
					</label>
					<?php
					if (version_compare(JVERSION, '3.0', 'lt'))
					{
						?>
						<div class="<?php echo $controlsClass;?>">
							<?php echo $this->lists['published']; ?>
						</div>
					<?php
					}
					else
					{
						echo $this->lists['published'];
					}
					?>
				</div>
				<div class="<?php echo $controGroupClass;?>">
					<label class="<?php echo $controLabelClass;?>" for="filename">
						<?php echo Text::_('EDOCMAN_FILE'); ?>
					</label>
					<div class="<?php echo $controlsClass;?>">
						<input type="file" size="60" class="inputbox form-control" value="" id="filename" name="filename" />
					</div>
				</div>
				<div class="<?php echo $controGroupClass;?>">
					<label class="<?php echo $controLabelClass;?>" for="description">
						<?php echo Text::_('JGLOBAL_DESCRIPTION'); ?>
					</label>
					<div class="<?php echo $controlsClass;?>">
						<textarea id="description" name="description" class="input-xlarge form-control" cols="60" rows="5"><?php echo $this->input->get('description', '', 'none'); ?></textarea>
					</div>
				</div>
				<div class="<?php echo $controGroupClass;?>">
					<button type="button" class="btn btn-small btn-primary" onclick="saveDocument();"><span class="icon-save icon-white"></span><?php echo Text::_('EDOCMAN_UPLOAD'); ?></button>
				</div>
				<input type="hidden" name="id" value="0" />
				<input type="hidden" name="defaultItemId" id="defaultItemId" value="<?php echo $defaultItemid; ?>" />
				<input type="hidden" name="onclick" id="onclick" value="<?php echo $onclick; ?>" />
				<input type="hidden" name="editor" id="editor" value="<?php echo $editor; ?>" />
				<input type="hidden" name="Itemid" value="<?php echo $dItemid; ?>" />
				<input type="hidden" name="btnClass" id="btnclass" value="<?php echo $btnClass; ?>" />
				<?php echo HTMLHelper::_('form.token'); ?>
			</form>
		</div>

		<script type="text/javascript">
			(function($){
				$(document).ready(function(){
					changeOption = (function(value){
						if(value == 1)
						{
							$('#adminForm').hide();
							$('#item-form').show();
						}
						else
						{
							$('#item-form').hide();
							$('#adminForm').show();
						}
					})

					saveDocument = (function(){
						if($( "#title" ).val() == '')
						{
							$( "#title" ).css("border","1px solid #f11");
							$( "#title" ).focus();
						}
						else if($("#category_id").val() == 0)
						{
							$("#category_id").css("border","1px solid #f11");
							$("#category_id").val().focus();
						}
						else
						{
							$('#item-form').submit();
						}
					})

					storeInsertOption = (function(){
						document.adminForm.choose_document_option.value = $('input:radio[name=choose_document_option]:checked').val();
						return true;

					})

					var selectedOption = $('input:radio[name=choose_document_option]:checked').val();
					changeOption(selectedOption);

				})
			})(jQuery)
		</script>
	<?php
	}
?>