<?php

/**
 * @version        1.9.4
 * @package        Joomla
 * @subpackage     Edocman
 * @author         Dang Thuc Dam
 * @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\Language\Text;
use Joomla\CMS\Router\Route;

// Load the tooltip behavior.
HTMLHelper::_('behavior.keepalive');
$document = Factory::getApplication()->getDocument();
HTMLHelper::_('bootstrap.tooltip', '.hasTooltip', ['placement' => 'left']);
$extralayoutCss     = ((int)$this->config->theme_type == 1 ? "dark_layout" : "");

$bootstrapHelper	= $this->bootstrapHelper;
$rowFluidClass   	= $bootstrapHelper->getClassMapping('row-fluid');
$span12Class		= $bootstrapHelper->getClassMapping('span12');
$span3Class		    = $bootstrapHelper->getClassMapping('span3');
$span6Class		    = $bootstrapHelper->getClassMapping('span6');
$controlLabelClass 	= $bootstrapHelper->getClassMapping('control-label');
$controlsClass     	= $bootstrapHelper->getClassMapping('controls');
$controlGroupClass 	= $bootstrapHelper->getClassMapping('control-group');
?>
<?php
$document->getWebAssetManager()->addInlineScript("
	function submitDocument() {
		Joomla.submitform('document.save', document.getElementById('item-form'));
	}
");
?>
<script type="text/javascript">
	function cancelSubmit() {
		var form = document.getElementById('item-form') ;
		form.task.value = 'document.cancel' ;
		form.submit() ;
	}
</script>
<style>
.edocman-page-heading
{
	font-size: <?php echo ($this->config->heading_font_size == '') ? '30' : $this->config->heading_font_size; ?>px !important;
}
</style>
<form action="<?php echo Route::_('index.php?option=com_edocman&task=upload.upload&id='.(int) $this->item->id .'&Itemid='.$this->Itemid); ?>" method="post" name="adminForm" id="item-form" class="form-validate <?php echo $extralayoutCss; ?>" enctype="multipart/form-data">
	<?php
	if ($this->params->get('page_heading'))
	{
		$heading = $this->params->get('page_heading');
	}
	else
	{
		$heading = Text::_('EDOCMAN_UPLOAD_DOCUMENT');
	}
	?>
	<h1 class="edocman-page-heading"><?php echo $heading; ?></h1>
	<?php
	if($this->header_text != ""){
		?>
		<strong><?php echo $this->header_text;?></strong>
		<?php
	}
	?>
	<div id="edit-document-form" class="<?php echo $rowFluidClass;?> <?php echo $extraClass;?>">
		<div class="<?php echo $span12Class?>">
			<div class="<?php echo $controlGroupClass;?>">
				<label class="<?php echo $controlLabelClass;?>">
					<?php echo $this->form->getLabel('title'); ?>
				</label>
				<div class="<?php echo $controlsClass;?>">
					<?php echo $this->form->getInput('title'); ?>
				</div>
			</div>
			<?php
				if ($this->catId)
				{
				?>
					<div class="<?php echo $controlGroupClass;?>">
						<label class="<?php echo $controlLabelClass;?>">
							<?php echo $this->form->getLabel('category_id'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<input type="hidden" name="jform[category_id]" value="<?php echo $this->catId; ?>" />
							<?php echo $this->categoryTitle; ?>
						</div>
					</div>
				<?php
				}
				else
				{
				?>
					<div class="<?php echo $controlGroupClass;?>">
						<label class="<?php echo $controlLabelClass;?>">
							<?php echo $this->form->getLabel('category_id'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<?php //
								echo EdocmanHelper::getChoicesJsSelect($this->form->getInput('category_id')); 
							?>
						</div>
					</div>
				<?php
				}
				?>

				<div class="<?php echo $controlGroupClass;?>">
					<label class="<?php echo $controlLabelClass;?>">
						<?php echo $this->form->getLabel('filename'); ?>
					</label>
					<div class="<?php echo $controlsClass;?>">
						<?php echo $this->form->getInput('filename'); ?>
						<?php
						if ($this->item->id) {
							?>
								<span style="padding-top: 3px; display: block;">
								<?php
								if ($this->item->original_filename) {
									echo Text::_('EDOCMAN_FILE').": ";                                    
								?>
								<a href="<?php echo 'index.php?option=com_edocman&task=document.download&id='.$this->item->id; ?>&Itemid=<?php echo EdocmanHelper::getItemid(); ?>"><?php echo $this->item->original_filename ; ?></a>
							<?php        
							}      
							?>
								</span>
							<?php
						}
						?>
					</div>
				</div>
				
				<div class="<?php echo $controlGroupClass;?>">
					<label class="<?php echo $controlLabelClass;?>">
						<?php echo $this->form->getLabel('short_description'); ?>
					</label>
					<div class="<?php echo $controlsClass;?>">
						<?php echo $this->form->getInput('short_description'); ?>
					</div>
				</div>
				<div class="<?php echo $controlGroupClass;?>">
					<label class="<?php echo $controlLabelClass;?>">
						<?php echo $this->form->getLabel('description'); ?>
					</label>
					<div class="<?php echo $controlsClass;?>">
						<?php echo $this->form->getInput('description'); ?>
					</div>
				</div>
			</div>
			<div class="<?php echo $rowFluidClass;?>">
				<div class="<?php echo $span12Class?>">
					<input type="button" class="btn btn-warning" onclick="cancelSubmit();" value="<?php echo Text::_('EDOCMAN_CANCEL'); ?>" />
					<input type="button" class="btn btn-success" onclick="submitDocument();" value="<?php echo Text::_('EDOCMAN_SUBMIT'); ?>"  />
				</div>
			</div>
		</div>
	<input type="hidden" name="task" value="" />
	<?php
	if((int)$this->item->id > 0){
		?>
		<input type="hidden" name="jform[modified_time]" id="modified_time" value="<?php echo date("Y-m-d H:i:s");?>" />
		<?php
	}
	?>
	<input type="hidden" name="return" id="return" value="<?php echo Factory::getApplication()->getInput()->getString('return',''); ?>" />
	<input type="hidden" name="currentpage" id="currentpage" value="<?php echo EdocmanHelper::currentUrl(); ?>" />
	<?php echo HTMLHelper::_('form.token'); ?>
</form>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
    document.getElementById('jform_filename').addEventListener('change', function(e) {
        var fileName = e.target.files[0].name;
        fileName = fileName.substring(0, fileName.lastIndexOf('.'));
        
        if(document.getElementById('jform_title').value == "") {
            document.getElementById('jform_title').value = fileName;
        }
    });
});

</script>
