<?php
/**
 * @version        1.9.4
 * @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\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;

$extralayoutCss     = ((int)$this->config->theme_type == 1 ? "dark_layout" : "");
// Load the tooltip behavior.
$document = Factory::getApplication()->getDocument();
if (version_compare(JVERSION, '4.0.0-dev', 'lt'))
{
	JHtml::_('behavior.formvalidation');
}

if(EdocmanHelper::isJoomla4())
{
	$extraClass		= "joomla4";
}

$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');

HTMLHelper::_('behavior.keepalive');
$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&view=document&layout=edit&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 $rowFluidClass;?>">
				<div class="<?php echo $span12Class?>" style="text-align:right;">
					<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 class="<?php echo $rowFluidClass;?>" style="padding-top:15px;">
				<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->config->show_alias_form)
					{
					?>
						<div class="<?php echo $controlGroupClass;?>">
							<label class="<?php echo $controlLabelClass;?>">
								<?php echo $this->form->getLabel('alias'); ?>
							</label>
							<div class="<?php echo $controlsClass;?>">
								<?php echo $this->form->getInput('alias'); ?>
							</div>
						</div>
					<?php } ?>
					<?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('extra_category_ids'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<?php echo $this->form->getInput('extra_category_ids'); ?>
						</div>
					</div>
					<div class="<?php echo $controlGroupClass;?>">
						<label class="<?php echo $controlLabelClass;?>">
							<?php echo $this->form->getLabel('url'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<?php echo $this->form->getInput('url'); ?>
						</div>
					</div>

					<?php
					if($this->config->show_thumb_form){
					?>
					<div class="<?php echo $controlGroupClass;?>">
						<label class="<?php echo $controlLabelClass;?>">
							<?php echo $this->form->getLabel('image'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<?php echo $this->form->getInput('image'); ?>
						</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('document_url'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<?php echo $this->form->getInput('document_url'); ?>
						</div>
					</div>
					<?php
					if($this->config->view_url){
					?>
					<div class="<?php echo $controlGroupClass;?>">
						<label class="<?php echo $controlLabelClass;?>">
							<?php echo $this->form->getLabel('view_url'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<?php echo $this->form->getInput('view_url'); ?>
						</div>
					</div>
					<?php } ?>

					<?php
					if (!isset($this->config->access_level_inheritance) || $this->config->access_level_inheritance !== '1')
					{
						?>
							<div class="<?php echo $controlGroupClass;?>">
								<label class="<?php echo $controlLabelClass;?>">
									<?php echo $this->form->getLabel('accesspicker'); ?>
								</label>
								<div class="<?php echo $controlsClass;?>">
									<?php
									EDocmanHelper::showCheckboxfield('accesspicker', $this->item->accesspicker, Text::_('EDOCMAN_PRESETS'), Text::_('EDOCMAN_GROUPS'));
									?>
									<div class="clr"></div>
									<?php
									if($this->item->accesspicker == 0)
									{
										$style1 = "display:block";
										$style2 = "display:none";
									}
									else
									{
										$style2 = "display:block";
										$style1 = "display:none";
									}
									?>
									<div class="control-group" id="presetsdiv" style="<?php echo $style1;?>" data-showon='<?php echo EDocmanHelper::renderShowon(array('jform[accesspicker]' => '0')); ?>'>
										<?php echo $this->form->getInput('access'); ?>
									</div>
									<div class="control-group" id="groupsdiv" style="<?php echo $style2;?>" data-showon='<?php echo EDocmanHelper::renderShowon(array('jform[accesspicker]' => '1')); ?>'>
										<?php 
										echo $this->form->getLabel('groups'); 
										if(EdocmanHelper::isJoomla4())
										{
											echo HTMLHelper::_('access.usergroup', 'groups[]', $this->item->groups, 'multiple class="form-select"');
										}
										else
										{
											echo $this->form->getInput('groups'); 
										}
										?>
									</div>
								</div>
							</div>
						<?php
						}
						$user = Factory::getApplication()->getIdentity() ;
						if ($user->authorise('edocman.assign_documents_to_users', 'com_edocman'))
						{
						?>
							<div class="<?php echo $controlGroupClass;?>">
								<label id="jform_user_ids-lbl" for="jform_user_ids" class="hasTip <?php echo $controlLabelClass;?>" title="<?php echo Text::_('EDOCMAN_USER_IDS_DESC'); ?>" aria-invalid="false">
									<?php echo Text::_('EDOCMAN_USER_IDS');?>
								</label>
								<div class="<?php echo $controlsClass;?>">

									<?php
									if($this->config->owner_inheritance)
									{
										if($this->item->id == 0)
										{
											echo Text::_('EDOCMAN_INHERIT_FROM_MAIN_CATEGORIES');
										}
										else
										{
											if($this->item->user_ids != "")
											{
												echo $this->item->user_ids;
												echo " (".Text::_('EDOCMAN_INHERIT_FROM_MAIN_CATEGORIES').")";
											}
											else
											{
												echo Text::_('EDOCMAN_INHERIT_FROM_MAIN_CATEGORIES');
											}
										}
										?>

										<?php
									}
									else
									{
										?>
										<?php echo EdocmanHelper::getUserInputBackend($this->item->user_ids, true); ?>
										<?php
									}
									?>
								</div>
							</div>
							<?php
							if($this->config->user_group_ids)
							{
							?>
							<div class="<?php echo $controlGroupClass;?>">
								<label id="jform_user_ids-lbl" for="jform_user_ids" class="hasTip <?php echo $controlLabelClass;?>" title="<?php echo Text::_('EDOCMAN_GROUP_IDS_DESC'); ?>" aria-invalid="false">
									<?php echo $this->form->getLabel('group_ids'); ?>
								</label>
								<div class="<?php echo $controlsClass;?>">

									<?php
									if($this->config->owner_inheritance)
									{
										if($this->item->id == 0)
										{
											echo Text::_('EDOCMAN_INHERIT_FROM_MAIN_CATEGORIES');
										}
										else
										{
											if($this->item->owner_group_ids != "")
											{
												echo $this->item->owner_group_ids;
												echo " (".Text::_('EDOCMAN_INHERIT_FROM_MAIN_CATEGORIES').")";
											}
											else
											{
												echo Text::_('EDOCMAN_INHERIT_FROM_MAIN_CATEGORIES');
											}
										}
										?>

										<?php
									}
									else
									{
										if(EdocmanHelper::isJoomla4())
										{
											echo EdocmanHelper::getChoicesJsSelect(HTMLHelper::_('access.usergroup', 'jform[group_ids][]', $this->item->group_ids, 'multiple class="form-select"'));
										}
										else
										{
											echo $this->form->getInput('group_ids'); 
										}
									}
									?>
								</div>
							</div>
						<?php
							}
						}
					?>
					<?php
					if($this->config->show_license_form)
					{
					?>
						<div class="<?php echo $controlGroupClass;?>">
							<label class="<?php echo $controlLabelClass;?>">
								<?php echo $this->form->getLabel('license'); ?>
							</label>
							<div class="<?php echo $controlsClass;?>">
								<?php echo $this->form->getInput('license'); ?>
							</div>
						</div>
					<?php 
					}
					

					if($this->config->show_published_form)
					{
					?>
					<div class="<?php echo $controlGroupClass;?>">
						<label class="<?php echo $controlLabelClass;?>">
						<?php echo $this->form->getLabel('published'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<?php //echo $this->form->getInput('published'); 
							EDocmanHelperHtml::showStateField($this->item->published);
							?>
						</div>
					</div>
					<?php } ?>
					<?php
					if(($this->config->lock_function) && ($this->config->show_lock_form)){
					?>
					<div class="<?php echo $controlGroupClass;?>">
						<label class="<?php echo $controlLabelClass;?>">
							<?php echo $this->form->getLabel('is_locked'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<?php echo $this->form->getInput('is_locked'); ?>
						</div>
					</div>
					<input type="hidden" name="old_locked_status" value="<?php echo $this->item->is_locked; ?>" />
					<?php } ?>
					<?php
					if($this->config->show_meta_form){
					?>
						<div class="<?php echo $controlGroupClass;?>">
							<label class="<?php echo $controlLabelClass;?>">
								<?php echo $this->form->getLabel('metadesc'); ?>
							</label>
							<div class="<?php echo $controlsClass;?>">
								<?php echo $this->form->getInput('metadesc'); ?>
							</div>
						</div>

						<div class="<?php echo $controlGroupClass;?>">
							<label class="<?php echo $controlLabelClass;?>">
								<?php echo $this->form->getLabel('metakey'); ?>
							</label>
							<div class="<?php echo $controlsClass;?>">
								<?php echo $this->form->getInput('metakey'); ?>
							</div>
						</div>
					<?php } ?>
					<?php
					if($this->config->show_tag_form){
					?>
					<div class="<?php echo $controlGroupClass;?>">
						<label class="<?php echo $controlLabelClass;?>">
							<?php echo $this->form->getLabel('tags'); ?>
						</label>
						<div class="<?php echo $controlsClass;?>">
							<ul class="tags">
								<?php if(isset($this->tags) && count($this->tags)): ?>
								<?php foreach($this->tags as $tag): ?>
								<li class="tagAdded">
									<?php echo $tag->tag; ?>
									<span title="<?php echo Text::_('EDOCMAN_CLICK_TO_REMOVE_TAG'); ?>" class="tagRemove">&times;</span>
									<input type="hidden" name="tags[]" value="<?php echo $tag->tag; ?>" />
								</li>
								<?php endforeach; ?>
								<?php endif; ?>
								<li class="tagAdd">
									<input type="text" id="search-field" />
								</li>
								<li class="clr"></li>
							</ul>
						</div>
					</div>
					<?php } ?>
					
					<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>
			<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>
	</div>
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="license_id" id="license_id" value="<?php echo $this->default_license; ?>" />
	<?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">
jQuery('.tagRemove').click(function(event) {
	event.preventDefault();
	jQuery(this).parent().remove();
});
jQuery('ul.tags').click(function() {
	jQuery('#search-field').focus();
});
jQuery('#search-field').keypress(function(event) {
	if (event.which == '13') {
		if (jQuery(this).val() != '') {
			jQuery('<li class="tagAdded">' + jQuery(this).val() + '<span class="tagRemove" onclick="jQuery(this).parent().remove();">&times;</span><input type="hidden" value="' + jQuery(this).val() + '" name="tags[]"></li>').insertBefore('.tags .tagAdd');
			jQuery(this).val('');
			event.preventDefault();
		}
	}
});
var tagsUrl = '<?php echo Uri::root(); ?>';
tagsUrl += 'index.php?option=com_edocman&task=tags';
jQuery('#search-field').autocomplete({
	source: function(request, response) {
		jQuery.ajax({
			type: 'post',
			url: tagsUrl,
			data: 'q=' + request.term,
			dataType: 'json',
			success: function(data) {
				jQuery('#search-field').removeClass('tagsLoading');
				response(jQuery.map(data, function(item) {
					return item;
				}));
			}
		});
	},
	minLength: 3,
	select: function(event, ui) {
		jQuery('<li class="tagAdded">' + ui.item.label + '<span class="tagRemove" onclick="jQuery(this).parent().remove();">&times;</span><input type="hidden" value="' + ui.item.value + '" name="tags[]"></li>').insertBefore('.tags .tagAdd');
		this.value = '';
		return false;
	},
	search: function(event, ui) {
		jQuery('#search-field').addClass('tagsLoading');
	}
});
function updateRadioButton(select_item) {
    const presetsDiv = document.getElementById('presetsdiv');
    const groupsDiv = document.getElementById('groupsdiv');
    
    if (select_item == '0') {
        slideDown(presetsDiv);
        slideUp(groupsDiv);
    } else {
        slideUp(presetsDiv);
        slideDown(groupsDiv);
    }
}

// Helper function to replicate jQuery's slideDown
function slideDown(element) {
    // Save the element's current display state
    const originalDisplay = window.getComputedStyle(element).display === 'none' ? 'block' : window.getComputedStyle(element).display;
    
    // Set initial state
    element.style.overflow = 'hidden';
    element.style.display = originalDisplay;
    element.style.height = '0px';
    
    // Get the target height
    const targetHeight = element.scrollHeight;
    
    // Set transition
    element.style.transition = 'height 300ms ease';
    
    // Trigger animation
    setTimeout(() => {
        element.style.height = targetHeight + 'px';
    }, 0);
    
    // Clean up after animation completes
    setTimeout(() => {
        element.style.height = '';
        element.style.overflow = '';
        element.style.transition = '';
    }, 300);
}

// Helper function to replicate jQuery's slideUp
function slideUp(element) {
    // Set initial state
    element.style.overflow = 'hidden';
    element.style.height = element.scrollHeight + 'px';
    
    // Set transition
    element.style.transition = 'height 300ms ease';
    
    // Trigger animation
    setTimeout(() => {
        element.style.height = '0px';
    }, 0);
    
    // Hide after animation completes
    setTimeout(() => {
        element.style.display = 'none';
        element.style.height = '';
        element.style.overflow = '';
        element.style.transition = '';
    }, 300);
}

</script>
