<?php
/**
 * @version        1.11.3
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011-2025 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
// no direct access
defined( '_JEXEC' ) or die ;
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Language\Text;
use Joomla\String\StringHelper;

/* @var JDocumentHtml $document */
$document					= Factory::getApplication()->getDocument();
$rootUri					= Uri::root();
$largeImageUri				= '';
$item						= $this->item ;

//og:title
$document->setMetaData('og:title', $item->title, 'property');

//og:image
if ($item->image && is_file(Path::clean(JPATH_ROOT.'/media/com_edocman/document/thumbs/'.$item->image)))
{
	$imgSrc					= $rootUri.'media/com_edocman/document/thumbs/'.$item->image ;
}

if ($imgSrc)
{
	$document->setMetaData('og:image', $imgSrc, 'property');
}
$document->setMetaData('og:url', Uri::getInstance()->toString(), 'property');

//og:description
$description				= !empty($item->metadesc) ? $item->metadesc : $item->metadesc;
$description				= HTMLHelper::_('string.truncate', $description, 200, true, false);
$document->setMetaData('og:description', $description, 'property');

//og:site name
$document->setMetaData('og:site_name', Factory::getApplication()->get('sitename'), 'property');


$config						= $this->config;
$session					= Factory::getApplication()->getSession();
if(!$config->onetime_collect){
	$session->set('name','');
	$session->set('email','');
}
$name						= $session->get('name','');
$email						= $session->get('email','');

$url						= Route::_(EDocmanHelperRoute::getDocumentRoute($item->id, $this->categoryId, $this->Itemid), false);
$siteUrl					= Uri::base();
$socialUrl					= Uri::getInstance()->toString();
$user						= Factory::getApplication()->getIdentity() ;
$userId						= $user->get('id');
//$canDownload				= $user->authorise('edocman.download', 'com_edocman.document.'.$item->id) ;
$canEdit					= $user->authorise('core.edit',		'com_edocman.document.'.$item->id);
$canDelete					= $user->authorise('core.delete',		'com_edocman.document.'.$item->id);
if(!$canDelete)
{
	$canDelete				= $user->authorise('edocman.deleteown',			'com_edocman.document.'.$item->id) && ($item->created_user_id == $userId);
}
$canCheckin					= $user->authorise('core.admin',        'com_checkin') || $item->checked_out == $userId || $item->checked_out == 0;
$canEditOwn					= $user->authorise('core.edit.own',		'com_edocman.document.'.$item->id) && $item->created_user_id == $userId;
$canChange					= $user->authorise('core.edit.state',	'com_edocman.document.'.$item->id) && $canCheckin;
//$canDownload				= ($user->authorise('core.admin',       'com_config') ||$item->created_user_id == $userId) || ($item->user_ids =="" && ($canDownload || $canEdit) || ($item->user_ids != '' && $item->user_ids != '0' && in_array($userId, explode(',', $item->user_ids)))) ;
$canDownload				= EdocmanHelper::canDownload($item->id);
$bootstrapHelper			= $this->bootstrapHelper;
$btnClass					= $bootstrapHelper->getClassMapping('btn');
$rowFluidClass				= $bootstrapHelper->getClassMapping('row-fluid');
$span12Class				= $bootstrapHelper->getClassMapping('span12');
require_once JPATH_ROOT.'/components/com_edocman/helper/file.class.php' ;
$accept_license				= 0;
if($config->accept_license && $this->default_license > 0)
{
	$accept_license			= 1;
}
if($config->accept_license_one_time)
{
	$alreadyAcceptLicense	= $session->get('alreadyAcceptLicense',0);
	if($alreadyAcceptLicense == 0)
	{
		$accept_license		= 1;
	}
	else
	{
		$accept_license		= 0;
	}
}
$hide_download_button		= $hide_download_button;
$hide_download = $this->category->hide_download;
if((int)$hide_download == 1){
	$hide_download_button	= 1;
}
elseif((int)$hide_download == 2)
{
	$hide_download_button	= 0;
}
$show_view_button			= $this->config->show_view_button;
$show_view					= $this->category->show_view;
if((int)$show_view == 1)
{
	$show_view_button		= 1;
}
elseif((int)$show_view == 2)
{
	$show_view_button		= 0;
}

$item->data					= new EDocman_File($item->id,$item->filename, $config->documents_path);
if($item->filename != "")
{
	$fileExt				= strtolower(EdocmanHelper::getExt($item->filename));
}
else
{
	$fileExt				= strtolower(EdocmanHelper::getExt($item->document_url));
}
if ($config->collect_downloader_information && !$userId && ($name == '' || $email == ''))
{
	$showDownloadForm		= true;
}
else
{
	$showDownloadForm		= false;
}

$extralayoutCss				= ((int)$config->theme_type == 1 ? "dark_layout" : "");
$ext						= strtolower(EDocmanHelper::getExt($item)) ;
$indicators					= (array)$item->indicators;
?>
<style>
.edocman-page-heading
{
	font-size: <?php echo ($this->config->heading_font_size == '') ? '30' : $this->config->heading_font_size; ?>px !important;
}
</style>
<div id="edocman-document-page-blog" class="edocman-container edocman-document <?php echo $extralayoutCss;?>">
	<div class="document-header">
		<?php
		if ($item->image && is_file(Path::clean(JPATH_ROOT.'/media/com_edocman/document/thumbs/'.$item->image)))
		{
			$modal_url = $imgSrc = Uri::base().'media/com_edocman/document/'.$item->image ;
			$imgSrc = Uri::base().'media/com_edocman/document/thumbs/'.$item->image ;
		}
		else
		{
			$modal_url = "";
			if (!isset($this->config->show_default_document_thumbnail) || $this->config->show_default_document_thumbnail)
			{
				if (is_file(Path::clean(JPATH_ROOT.'/components/com_edocman/assets/images/icons/32x32/'.$ext.'.png')))
				{
					$imgSrc = Uri::base().'components/com_edocman/assets/images/icons/32x32/'.$ext.'.png' ;
				}
				else
				{
					$imgSrc = Uri::base().'components/com_edocman/assets/images/icons/32x32/generic.png';
				}
			}
			else
			{
				$imgSrc = '' ;
			}
		}
		if ($imgSrc)
		{
		?>
			<div class="document-thumbnail">
				<?php if (!empty($modal_url)): ?>
					<img src="<?php echo $imgSrc; ?>" alt="<?php echo $item->title; ?>" class="thumbnail-image" data-modal-url="<?php echo $modal_url; ?>">
				<?php else: ?>
					<img src="<?php echo $imgSrc; ?>" alt="<?php echo $item->title; ?>">
				<?php endif; ?>
				<div class="document-type-icon">
					<?php EDocmanHelperHtml::showFileIcon($item->data); ?>
				</div>
			</div>
			<?php if (!empty($modal_url)): ?>
			<!-- Modal container -->
			<div id="imageModal" class="imgmodal">
				<span class="imgclose">&times;</span>
				<img class="imgmodal-content" id="modalImage">
			</div>

			<script>
			document.addEventListener('DOMContentLoaded', function() {
				// Get the modal
				var modal = document.getElementById('imageModal');
				
				// Get the modal image
				var modalImg = document.getElementById('modalImage');
				
				// Get the close button
				var closeBtn = document.getElementsByClassName('imgclose')[0];
				
				// Get all thumbnail images with data-modal-url
				var thumbnails = document.getElementsByClassName('thumbnail-image');
				
				// Add click event to thumbnails
				for (var i = 0; i < thumbnails.length; i++) {
					thumbnails[i].addEventListener('click', function() {
						modal.style.display = 'block';
						modalImg.src = this.getAttribute('data-modal-url');
					});
				}
				
				// Close modal when clicking on X
				closeBtn.addEventListener('click', function() {
					modal.style.display = 'none';
				});
				
				// Close modal when clicking outside the image
				window.addEventListener('click', function(event) {
					if (event.target == modal) {
						modal.style.display = 'none';
					}
				});
			});
			</script>
			<?php endif; ?>
		<?php } ?>
		<div class="document-header-content">
			<div class="document-meta">
				<?php
				if($this->config->show_creation_date == 1 && $item->created_time != "" && $item->created_time != "0000-00-00 00:00:00")
				{
				?>
					<div class="meta-item">
						<i class="fas fa-calendar-plus"></i>
						<span><?php echo Text::_('EDOCMAN_PUBLISHED_ON');?>: <?php echo HTMLHelper::_('date',$item->created_time, 'd/m/Y');?></span>
					</div>
				<?php
				}
				?>
				<?php
				if ($this->config->show_modified_date && $item->modified_time != "" && $item->modified_time != "0000-00-00 00:00:00")
				{
				?>
					<div class="meta-item">
						<i class="fas fa-calendar-plus"></i>
						<span><?php echo Text::_('EDOCMAN_LAST_UPDATED');?>: <?php echo HTMLHelper::_('date',$item->modified_time, 'd/m/Y');?></span>
					</div>
				<?php
				}
				
				$created_user_id = $item->created_user_id;
				if ($this->config->show_creation_user == 1 && (int)$created_user_id > 0)
				{
				?>
					<div class="meta-item">
						<i class="fas fa-user"></i>
						<?php echo Text::_('EDOCMAN_CREATED_USER'); ?>:
						<?php 
						$created_user = Factory::getUser($created_user_id);
						echo $created_user->name;
						?>
					</div>
				<?php
				}
				?>
			</div>
			
			<div class="title-container">
				<?php EDocmanHelperHtml::showFileIcon($item->data); ?>
				<h1 class="document-title"><?php echo $item->title; ?></h1>
			</div>
			
			<div class="badge-container">
				<?php
				if (!empty($item->new_indicator))
				{
				?>
					<span class="edocman-badge badge-new">
							<?php echo Text::_('EDOCMAN_NEW');?>
					</span>
				<?php
				}
				if(in_array('featured', $indicators))
				{
				?>
					<span class="edocman-badge badge-featured"><?php echo Text::_('EDOCMAN_FEATURED');?></span>
				<?php
				}
				if(in_array('hot', $indicators))
				{
				?>
					<span class="edocman-badge badge-hot"><?php echo Text::_('EDOCMAN_HOT');?></span>
				<?php
				}
				?>
			</div>
			<?php
			if (count($item->tags))
			{
				$tags = $item->tags;
			?>
				<div class="blog-tags-container">
					<?php
					foreach ($tags as $tag)
					{
					?>
						<span class="blog-tag"><i class="fas fa-tag"></i> <a href="<?php echo Route::_('index.php?option=com_edocman&view=search&filter_tag='.$tag->tag.'&Itemid='.$this->Itemid); ?>" title="<?php echo $tag->tag; ?>"><?php echo $tag->tag; ?></a></span>
					<?php
					}	
					?>
				</div>
			<?php
			}	
			?>
		</div>
	</div>

	<div class="document-content">
		<div class="document-description">
			<p><?php echo $item->description ;?>
			<?php
			if($item->document_history != "")
			{
				echo "<BR />".$item->document_history;
			}
			?>
			
			</p>
			<?php
			foreach ($this->plugins as $plugin)
			{
				?>
				<h4><?php echo $plugin['title']; ?></h4>
				<?php
				echo $plugin['form'];
			}
			?>
		</div>

		<div class="document-info">
			<?php
			if($item->created_time != "" && $item->created_time != "0000-00-00 00:00:00" && $this->config->show_creation_date == 1)
			{
			?>
				<div class="info-item">
					<span class="info-label"><?php echo Text::_('EDOCMAN_PUBLISHED_ON');?></span>
					<span class="info-value"><?php echo HTMLHelper::_('date',$item->created_time, 'j F Y');?></span>
				</div>
			<?php
			}
			if ($this->config->show_modified_date && $item->modified_time != "" && $item->modified_time != "0000-00-00 00:00:00")
			{
			?>
			<div class="info-item">
				<span class="info-label"><?php echo Text::_('EDOCMAN_LAST_UPDATED'); ?></span>
				<span class="info-value"><?php echo HTMLHelper::_('date', $item->modified_time, $this->config->date_format); ?></span>
			</div>
			<?php
			}
			if ($this->config->show_category_name)
			{
			?>
			<div class="info-item">
				<span class="info-label"><?php echo Text::_('EDOCMAN_CATEGORY') ?></span>
				<span class="info-value">
					<?php
					$category = EDocmanHelper::getDocumentCategory($this->item->id);
					$link = EDocmanHelperRoute::getCategoryRoute($category->id, $this->Itemid);
					?>
					<a href="<?php echo $link?>" target="_blank" aria-label="<?php echo $category->title;?>" title="<?php echo $category->title;?>"><?php echo $category->title;?></a>
				</span>
			</div>
			<?php
			}
			if ($this->config->show_document_version && $this->item->document_version != "")
			{
			?>
				<div class="info-item">
					<span class="info-label"><?php echo Text::_('EDOCMAN_VERSION') ?></span>
					<span class="info-value"><?php echo $this->item->document_version; ?></span>
				</div>
			<?php
			}
			if ($this->config->show_filesize && $this->item->document_url == "")
			{
			?>
				<div class="info-item">
					<span class="info-label"><?php echo Text::_('EDOCMAN_FILESIZE') ?></span>
					<span class="info-value"><?php echo $this->item->data->size; ?></span>
				</div>
			<?php
			}
			if ($this->config->show_filetype)
			{
			?>
				<div class="info-item">
					<span class="info-label"><?php echo Text::_('EDOCMAN_FILE_TYPE'); ?></span>
					<span class="info-value"><?php echo $this->item->data->mime; ?></span>
				</div>
			<?php
			}
			if ($this->config->show_hits)
			{
			?>
				<div class="info-item">
					<span class="info-label"><?php echo Text::_('EDOCMAN_HITS'); ?></span>
					<span class="info-value"><?php echo $item->hits; ?>&nbsp;<?php echo Text::_('EDOCMAN_HITS'); ?></span>
				</div>
			<?php
			}
			if ($this->config->show_number_downloaded)
			{
			?>
				<div class="info-item">
					<span class="info-label"><?php echo Text::_('EDOCMAN_DOWNLOAD'); ?></span>
					<span class="info-value">
						<?php echo $item->downloads; ?> <?php echo Text::_('EDOCMAN_TIMES');?>
					</span>
				</div>
			<?php
			}			
			if (count((array)$this->item->params))
			{
				foreach($this->fields as $field)
				{
					if (!empty($this->item->params[$field->fieldname]))
					{
						
					?>
						<div class="info-item">
							<span class="info-label"><?php echo $field->title; ?></span>
							<span class="info-value">
								<?php 
								$fieldValue = $this->item->params[$field->fieldname]; 
								if(strpos($fieldValue, 'http') !== false)
								{
									?>
									<a href = "<?php echo $fieldValue?>" target = "_blank"><?php echo $fieldValue?></a>
									<?php
								}
								else
								{
									echo $fieldValue;
								}
								?>
							</span>
						</div>
					<?php
					}
				}
			}
			?>
		</div>

		<?php
		if(!$canDownload && $config->show_no_download_permission_alert)
		{
		?>
		<div class="alert alert-danger">
			<div class="alert-icon">
				<i class="fas fa-exclamation-circle"></i>
			</div>
			<div class="alert-content">
				<div class="alert-title"><?php
					echo Text::_('EDOCMAN_YOU_DO_NOT_HAVE_PERMISSION_TO_DOWNLOAD_THIS_DOCUMENT');
					?></div>
				<div class="alert-message"><?php
					echo Text::_('EDOCMAN_PLEASE_CONTACT_TO_GET_PERMISSION');
					?>.</div>
			</div>
			<button class="alert-close" onclick="closeAlert(this)" title="Đóng thông báo">
				<i class="fas fa-times"></i>
			</button>
		</div>
		<?php } 
		if ($this->default_license > 0)
		{
		?>
			<div class="license-container">
				<h3 class="license-title"><i class="fas fa-gavel"></i> <?php echo Text::_('EDOCMAN_LICENSE');?></h3>
				<div class="license-text">
					<?php
						echo $this->license->description;
					?>
				</div>
				<?php
				if($accept_license)
				{
				?>
				<div class="license-agreement">
					<input type="checkbox" name="agreeterm" id="agreeterm" value="0" onClick="javascript:agreeTerm();"/>
					<label for="agree-license"><?php echo Text::_('EDOCMAN_AGREE_TERM_AND_CONDITION');?></label>
				</div>
				<?php
				}	
				?>
			</div>

		<?php }
		
		if($pass_password)
		{
		?>
		<div class="code-unlock-section">
			<h3 class="code-unlock-title"><?php echo Text::_('EDOCMAN_ENTER_PASSWORD_FOR_DOWNLOAD');?></h3>
			<div class="code-input-container">
				<input type="text" class="code-input" name="download_password" id="download_password" placeholder="<?php echo Text::_('EDOCMAN_ENTER_DOWNLOAD_PASSWORD');?>" onkeyup="checkUnlockCode()">
				<button aria-label="<?php echo Text::_('EDOCMAN_VERIFY');?>" onClick="javascript:checkPassword('<?php echo $this->item->download_password; ?>');" class="verify-btn"><?php echo Text::_('EDOCMAN_VERIFY');?></button>
			</div>
		</div>
		<?php 
		}
		?>
		<!-- Document Actions Container -->
		<div class="document-actions-container">
			<!-- Desktop Actions -->
			<div class="desktop-actions document-actions">
				<?php
				if($accept_license || $pass_password)
				{
					$disable_download_button = "btn-disabled";
				}
				else
				{
					$disable_download_button = "";
				}
				if ($canDownload && $hide_download_button != 1)
				{
					$downloadUrl = Route::_('index.php?option=com_edocman&task=document.download&id='.$item->id.'&Itemid='.$this->Itemid) ;
					if($item->document_url != "")
					{
						$downloadUrl = $item->document_url;
					}
					if ($showDownloadForm)
					{
					?>
						<a id="download-btn-desktop" data-toggle="modal" data-document-title="<?php echo $this->item->title; ?>" title="<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>"  id="<?php echo $this->item->id; ?>" class="edocmancollectdownloadbtn edocman-document-title-link btn btn-primary <?php echo $disable_download_button;?> edocmandownloadlink" href="<?php echo Uri::root(true); ?>/index.php?option=com_edocman&view=document&layout=modal&view=document&layout=modal&tmpl=component&id=<?php echo $item->id;?>&direct=1" target="<?php echo $target;?>" aria-label="<?php echo $this->item->title; ?>">
							<?php if($this->item->document_url != "")
							{
								if($config->external_download_link == 1)
								{
									$target = "_blank";
								}
								else
								{
									$target = "";
								}
							?>
								<i class="edicon edicon-link"></i>
								<?php echo Text::_('EDOCMAN_OPEN_DOCUMENT'); ?>
							<?php }else{ ?>
								<i class="edicon edicon-cloud-download"></i>
								<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>
							<?php } ?>
						</a>
					<?php
					}
					else
					{
					?>
						<a id="download-btn-desktop" href="<?php echo $downloadUrl; ?>" class="edocmandownloadlink btn btn-primary <?php echo $disable_download_button;?>" target="<?php echo $target;?>" aria-label="<?php echo $this->item->title; ?>">
							<?php if($this->item->document_url != ""){?>
								<i class="edicon edicon-link"></i>
								<?php echo Text::_('EDOCMAN_OPEN_DOCUMENT'); ?>
							<?php }else{ ?>
								<i class="edicon edicon-cloud-download"></i>
								<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>
							<?php } ?>
						</a>
					<?php
					}
				}
				elseif(!$canDownload && $hide_download_button != 1 && $this->config->login_to_download && (int)$userId == 0)
				{
					?>
					
						<?php
						if($config->edocman_login == 1)
						{
							EdocmanHelper::generateJoomlaLogin('btn btn-primary');
						}
						else
						{
							$return = base64_encode(Uri::getInstance()->toString());
							?>
							<a id="download-btn-desktop" data-toggle="modal" class="logintodownload btn btn-primary" href="<?php echo Uri::root();?>index.php?option=com_edocman&view=document&layout=login&id=<?php echo $item->id;?>&tmpl=component&return=<?php echo $return;?>" aria-label="<?php echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD'); ?>">
								<i class="edicon edicon-user"></i>
								<?php
								echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD'); 
								?>
							</a>
						<?php
						}		
						?>
					
					<?php
				}
				if ($canDownload && $show_view_button == 1 && $item->canView)
				{
					$playextension	= EdocmanHelper::returnPlayExtension();
					$showModal		= EdocmanHelper::viewable($item);
					
					if((in_array($ext,$playextension) || $item->view_url != "") && $showModal && !EDocmanHelper::isDropBoxTurnedOn() && !EDocmanHelper::isAmazonS3TurnedOn())
					{
						if($item->view_url != "")
						{
							$viewUrl = $item->view_url;
						}
						else
						{
							$viewUrl = Uri::root()."index.php?option=com_edocman&view=play&id=".$item->id."&tmpl=component";
						}
						$audio_player = EdocmanHelper::generateAudioPlayer($item, $showModal); 
						?>
						
							<?php
							if($config->view_option == 0)
							{
							?>
								<a href="<?php echo $viewUrl; ?>" class="btn btn-secondary edocmanplayerbtn" data-toggle="modal" <?php echo $audio_player;?> aria-label="<?php echo Text::_('EDOCMAN_CLICK_TO_VIEW_CONTENT_DOCUMENT'); ?>" rel="nofollow" title="<?php echo Text::_('EDOCMAN_CLICK_TO_VIEW_CONTENT_DOCUMENT'); ?>">
									<i class="fas fa-eye"></i>
									<?php echo Text::_('EDOCMAN_VIEW'); ?>
								</a>
							<?php
							}else{
							?>
								<a href="<?php echo $viewUrl; ?>" class="btn btn-secondary" target="_blank" aria-label="<?php echo Text::_('EDOCMAN_CLICK_TO_VIEW_CONTENT_DOCUMENT'); ?>" rel="nofollow" title="<?php echo Text::_('EDOCMAN_CLICK_TO_VIEW_CONTENT_DOCUMENT'); ?>">
									<i class="fas fa-eye"></i>
									<?php echo Text::_('EDOCMAN_VIEW'); ?>
								</a>
							<?php
							}
							?>
							
						
						<?php
					}
					else 
					{
						if($item->view_url != "")
						{
							$viewUrl = $item->view_url;
						}
						else
						{
							if (in_array($ext, ['pdf']) && $config->use_googleviewer == 0 && $config->pdf_view_option == 0 && $config->pdf_view_type == 0) 
							{
								$viewUrl		= Route::_('index.php?option=com_edocman&task=document.viewdoc&tmpl=component&id=' . $item->id . '&Itemid=' . $this->Itemid);

								$extraViewClass = 'pdfviewer';
								$target         = '';
							}
							elseif($ext == 'docx' && $this->config->use_googleviewer == 0)
							{
								$viewUrl		= Route::_('index.php?option=com_edocman&task=document.viewdoc&tmpl=component&id=' . $item->id . '&Itemid=' . $this->Itemid);

								$extraViewClass = 'pdfviewer';
								$target         = '';
							}
							else
							{
								$viewUrl = Route::_('index.php?option=com_edocman&task=document.viewdoc&id=' . $item->id . '&Itemid=' . $this->Itemid);

								$extraViewClass = '';
								$target         = 'target="_blank"';
							}
						}
						?>
						
							<a href="<?php echo $viewUrl; ?>" <?php echo $target; ?> class="btn btn-secondary <?php echo  $extraViewClass; ?>" aria-label="<?php echo Text::_('EDOCMAN_CLICK_TO_VIEW_CONTENT_DOCUMENT'); ?>" rel="nofollow" title="<?php echo Text::_('EDOCMAN_CLICK_TO_VIEW_CONTENT_DOCUMENT'); ?>">
								<i class="fas fa-eye"></i>
								<?php echo Text::_('EDOCMAN_VIEW'); ?>
							</a>
						
						<?php
					}
				}

				if(EDocmanHelper::massDownloadShowing($canDownload, $config, $item))
				{
					if(EDocmanHelper::isAlreadyInMassList($item->id))
					{
						$disabled = "disabled";
					}
					else
					{
						$disabled = "";
					}
					?>
					<a href="javascript:void(0);" id="btnMassDownload" class="edocmandownloadlink mass-download-btn btn btn-light <?php echo $disable_download_button;?>" aria-label="<?php echo $this->item->title; ?>" data-id="<?php echo $this->item->id; ?>" data-name="<?php echo $this->item->title; ?>" data-type="<?php echo $ext;?>" data-size="<?php $this->item->data->size; ?>" <?php echo $disabled;?>>
						<i class="edicon edicon-box-add"></i>
						<?php echo Text::_('EDOCMAN_ADD_TO_MASS_DOWNLOAD_LIST'); ?>
					</a>
					<?php
				}
				if($this->config->show_bookmark_button)
				{
					?>
					<a href="javascript:void(0);" onclick="javascript:addBookmark('<?php echo Uri::root()?>',<?php echo $this->item->id?>);" class="btn btn-bookmark" href="#sharing-form" data-document-title="<?php echo Text::_('EDOCMAN_BOOKMARK_THIS_FILE');?>" data-original-title="<?php echo Text::_('EDOCMAN_BOOKMARK_THIS_FILE');?>"  id="<?php echo $this->item->id; ?>" aria-label="<?php echo Text::_('EDOCMAN_BOOKMARK'); ?>">
						<i class="fas fa-bookmark"></i>
						<?php
						echo Text::_('EDOCMAN_BOOKMARK');
						?>
					</a>
					<?php
				}
				if($this->config->turn_on_sharing)
				{
				?>
					
					<a class="sharing-modal-link btn btn-dark" href="<?php echo Uri::root();?>index.php?option=com_edocman&view=document&layout=sharing&id=<?php echo $item->id;?>&tmpl=component&Itemid=<?php echo $this->Itemid?>" data-document-title="<?php echo $this->item->title; ?>" data-original-title="<?php echo Text::_('EDOCMAN_SHARE_DOCUMENT'); ?>"  id="<?php echo $this->item->id; ?>" aria-label="<?php echo Text::_('EDOCMAN_SHARE_DOCUMENT'); ?>">
						<i class="fas fa-share-alt"></i>
						<?php
						echo Text::_('EDOCMAN_SHARE_DOCUMENT'); 
						?>
					</a>
				<?php
				}
				?>
			</div>
			
			<!-- Share Button with Dropdown -->
			<?php
			if ($this->config->show_social_sharing_buttons !== '0' || $canEdit || $canEditOwn)
			{
			?>
				<div class="share-container">
					<button class="share-btn" onclick="toggleShareDropdown()">
						<?php echo Text::_('EDOCMAN_MORE_OPTIONS');?>
					</button>
					<div class="share-dropdown" id="share-dropdown">
						<?php
						if ($this->config->show_social_sharing_buttons !== '0')
						{
						?>
							<div class="share-option">
								<div class="share-icon" style="background-color: var(--facebook-edocman-color);">
									<i class="fab fa-facebook-f"></i>
								</div>
								<span class="share-name">
									<?php
									$alt     = Text::sprintf('EDOCMAN_SUBMIT_ITEM_IN_SOCIAL_NETWORK', $item->title, 'FaceBook');
									echo '<a href="http://www.facebook.com/sharer.php?u=' . rawurlencode($socialUrl) . '&amp;t=' . rawurlencode($item->title) . '" title="' . $alt . '" target="blank" class="social-btn facebook">'; 
									?>
										Facebook
									<?php
									
									?>
								</span>
							</div>
							<div class="share-option">
								<div class="share-icon" style="background-color: var(--twitter-edocman-color);">
									<i class="fab fa-twitter"></i>
								</div>
								<span class="share-name">
									<?php
									$alt     = Text::sprintf('EDOCMAN_SUBMIT_ITEM_IN_SOCIAL_NETWORK', $item->title, 'Twitter');	
									echo '<a href="http://twitter.com/?status=' . rawurlencode($item->title . " " . $socialUrl) . '" title="' . $alt . '" target="blank" class="social-btn twitter">';
									?>
									Twitter
									</a>
								</span>
							</div>
							<div class="share-option">
								<div class="share-icon" style="background-color: var(--linkedin-edocman-color);">
									<i class="fab fa-linkedin-in"></i>
								</div>
								<span class="share-name"><?php
								$alt     = Text::sprintf('EDOCMAN_SUBMIT_ITEM_IN_SOCIAL_NETWORK', $item->title, 'LinkedIn');
								echo '<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=' . $socialUrl . '&amp;title=' . $item->title . '" title="' . $alt . '" target="_blank" class="social-btn linkedin">';
								?>LinkedIn</a></span>
							</div>
						<?php
						}
						
						$pass_lock = true;
						if($config->lock_function)
						{ //lock function is turned on
							if(($item->locked_by != $user->id) && ($item->is_locked == 1))
							{
								$pass_lock = false;
							}
						}

						if (($canEdit || $canEditOwn) && ($pass_lock))
						{
							$return = EdocmanHelper::currentUrl();
							$url = Route::_('index.php?option=com_edocman&task=document.edit&id='.$item->id.'&return='.$return.'&Itemid='.$this->Itemid) ;
							?>
							<div class="share-option">
								<div class="share-icon" style="background-color: #555;">
									<i class="fas fa-edit"></i>
								</div>
								<span class="share-name">
									<a href="<?php echo $url; ?>" aria-label="<?php echo Text::_('EDOCMAN_EDIT'); ?>">
										<?php echo Text::_('EDOCMAN_EDIT'); ?>
									</a>
								</span>
							</div>
							
							<?php
						}
						if ($canDelete)
						{
						?>
							<div class="share-option">
								<div class="share-icon" style="background-color: #555;">
									<i class="fas fa-trash"></i>
								</div>
								<span class="share-name">
									<a href="javascript:deleteConfirm();" aria-label="<?php echo Text::_('EDOCMAN_DELETE'); ?>">
										
										<?php echo Text::_('EDOCMAN_DELETE'); ?>
									</a>
								</span>
							</div>
						<?php
						}
						if ($canChange)
						{
							if ($item->published)
							{
								$text = Text::_('EDOCMAN_UNPUBLISH');
								$url = "javascript:publishConfirm('documents.unpublish')";
								$class = 'edocman-icon-remove';
							}
							else
							{
								$url = "javascript:publishConfirm('documents.publish')";
								$text = Text::_('EDOCMAN_PUBLISH');
								$class = 'edocman-icon-ok';
							}
							?>
							<div class="share-option">
								<div class="share-icon" style="background-color: #555;">
									<i class="fas fa-toggle-on"></i>
								</div>
								<span class="share-name">
									<a href="<?php echo $url; ?>">
										<?php echo $text; ?>
									</a>
								</span>
							</div>
						<?php
						}	
						?>
					</div>
				</div>
				<?php
					}					
				?>
		</div>
		
		<!-- Mobile Actions -->
		<div class="mobile-actions">
			<div class="mobile-primary-actions">
				<?php
				if ($canDownload && $hide_download_button != 1)
				{
					$downloadUrl = Route::_('index.php?option=com_edocman&task=document.download&id='.$item->id.'&Itemid='.$this->Itemid) ;
					if($item->document_url != "")
					{
						$downloadUrl = $item->document_url;
					}
					if ($showDownloadForm)
					{
					?>
						<a data-toggle="modal" id="download-btn-mobile" data-document-title="<?php echo $this->item->title; ?>" title="<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>"  id="<?php echo $this->item->id; ?>" class="edocmancollectdownloadbtn edocman-document-title-link btn btn-primary <?php echo $disable_download_button;?> edocmandownloadlink" href="<?php echo Uri::root(true); ?>/index.php?option=com_edocman&view=document&layout=modal&view=document&layout=modal&tmpl=component&id=<?php echo $item->id;?>&direct=1" target="<?php echo $target;?>" aria-label="<?php echo $this->item->title; ?>">
							<?php if($this->item->document_url != "")
							{
								if($config->external_download_link == 1)
								{
									$target = "_blank";
								}
								else
								{
									$target = "";
								}
							?>
								<i class="edicon edicon-link"></i>
								<?php echo Text::_('EDOCMAN_OPEN_DOCUMENT'); ?>
							<?php }else{ ?>
								<i class="edicon edicon-cloud-download"></i>
								<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>
							<?php } ?>
						</a>
					<?php
					}
					else
					{
					?>
						<a href="<?php echo $downloadUrl; ?>" id="download-btn-mobile" class="edocmandownloadlink btn btn-primary <?php echo $disable_download_button;?>" target="<?php echo $target;?>" aria-label="<?php echo $this->item->title; ?>">
							<?php if($this->item->document_url != ""){?>
								<i class="edicon edicon-link"></i>
								<?php echo Text::_('EDOCMAN_OPEN_DOCUMENT'); ?>
							<?php }else{ ?>
								<i class="edicon edicon-cloud-download"></i>
								<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>
							<?php } ?>
						</a>
					<?php
					}
				}
				elseif(!$canDownload && $hide_download_button != 1 && $this->config->login_to_download && (int)$userId == 0)
				{
					?>
					
						<?php
						if($config->edocman_login == 1)
						{
							EdocmanHelper::generateJoomlaLogin('btn btn-primary');
						}
						else
						{
							$return = base64_encode(Uri::getInstance()->toString());
							?>
							<a data-toggle="modal" id="download-btn-mobile" class="logintodownload btn btn-primary" href="<?php echo Uri::root();?>index.php?option=com_edocman&view=document&layout=login&id=<?php echo $item->id;?>&tmpl=component&return=<?php echo $return;?>" aria-label="<?php echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD'); ?>">
								<i class="edicon edicon-user"></i>
								<?php
								echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD'); 
								?>
							</a>
						<?php
						}		
						?>
					
					<?php
				}
				if ($canDownload && $show_view_button == 1 && $item->canView)
				{
					$playextension	= EdocmanHelper::returnPlayExtension();
					$showModal		= EdocmanHelper::viewable($item);
					
					if((in_array($ext,$playextension) || $item->view_url != "") && $showModal && !EDocmanHelper::isDropBoxTurnedOn() && !EDocmanHelper::isAmazonS3TurnedOn())
					{
						if($item->view_url != "")
						{
							$viewUrl = $item->view_url;
						}
						else
						{
							$viewUrl = Uri::root()."index.php?option=com_edocman&view=play&id=".$item->id."&tmpl=component";
						}
						$audio_player = EdocmanHelper::generateAudioPlayer($item, $showModal);
						?>
						
							<?php
							if($config->view_option == 0)
							{
							?>
								<a href="<?php echo $viewUrl; ?>" class="btn btn-secondary edocmanplayerbtn" data-toggle="modal" <?php echo $audio_player;?> aria-label="<?php echo Text::_('EDOCMAN_VIEW'); ?>" rel="nofollow" title="<?php echo Text::_('EDOCMAN_VIEW'); ?>">
									<i class="fas fa-eye"></i>
									<?php echo Text::_('EDOCMAN_VIEW'); ?>
								</a>
							<?php
							}else{
							?>
								<a href="<?php echo $viewUrl; ?>" class="btn btn-secondary" target="_blank" aria-label="<?php echo Text::_('EDOCMAN_VIEW'); ?>" rel="nofollow" title="<?php echo Text::_('EDOCMAN_VIEW'); ?>">
									<i class="fas fa-eye"></i>
									<?php echo Text::_('EDOCMAN_VIEW'); ?>
								</a>
							<?php
							}
							?>
							
						
						<?php
					}
					else 
					{
						if($item->view_url != "")
						{
							$viewUrl = $item->view_url;
						}
						else
						{
							if($ext == 'pdf' && EdocmanHelper::isPDFViewerEnabled())
							{
								$viewUrl		= Route::_('index.php?option=com_edocman&task=document.viewdoc&tmpl=component&id=' . $item->id . '&Itemid=' . $this->Itemid);

								$extraViewClass = 'pdfviewer';
								$target         = '';
							}
							else
							{
								$viewUrl = Route::_('index.php?option=com_edocman&task=document.viewdoc&id=' . $item->id . '&Itemid=' . $this->Itemid);

								$extraViewClass = '';
								$target         = 'target="_blank"';
							}
						}
						?>
						
							<a href="<?php echo $viewUrl; ?>" <?php echo $target; ?> class="btn btn-secondary <?php echo  $extraViewClass; ?>" aria-label="<?php echo Text::_('EDOCMAN_VIEW'); ?>" rel="nofollow" title="<?php echo Text::_('EDOCMAN_VIEW'); ?>">
								<i class="fas fa-eye"></i>
								<?php echo Text::_('EDOCMAN_VIEW'); ?>
							</a>
						
						<?php
					}
				}
				?>
			</div>
			<div class="blog-dropdown">
				<button class="blog-dropdown-btn" onclick="toggleDropdown()">
					<i class="fas fa-ellipsis-v"></i>
				</button>
				<div class="blog-dropdown-content" id="blog-dropdown-menu">
					<?php
					if($this->config->show_bookmark_button)
					{
					?>
						<button onclick="javascript:addBookmark('<?php echo Uri::root()?>',<?php echo $this->item->id?>);" class="document-details-dropdown-item" href="#sharing-form" data-document-title="<?php echo Text::_('EDOCMAN_BOOKMARK_THIS_FILE');?>" data-original-title="<?php echo Text::_('EDOCMAN_BOOKMARK_THIS_FILE');?>"  id="<?php echo $this->item->id; ?>" aria-label="<?php echo Text::_('EDOCMAN_BOOKMARK'); ?>">

							<i class="fas fa-bookmark"></i> <?php echo Text::_('EDOCMAN_BOOKMARK'); ?>
						</button>
					<?php
					}
					
					if(EDocmanHelper::massDownloadShowing($canDownload, $config, $item))
					{
						if(EDocmanHelper::isAlreadyInMassList($item->id))
						{
							$disabled = "disabled";
						}
						else
						{
							$disabled = "";
							?>
							<button id="btnMassDownload1" class="edocmandownloadlink document-details-dropdown-item <?php echo $disable_download_button;?>" aria-label="<?php echo $this->item->title; ?>" <?php echo $disabled;?>>
								<i class="fas fa-plus"></i> <?php echo Text::_('EDOCMAN_ADD_TO_MASS_DOWNLOAD_LIST'); ?>
							</button>
							<?php
						}
					}	
					?>
				</div>
			</div>
		</div>
		<?php
		if($this->config->enable_rating)
		{
			echo $this->loadTemplate('rating');
		}

		if($this->config->show_related_documents && count($this->related_items) > 0)
		{
			echo $this->loadTemplate('relates');
		}

		if (@$this->config->jcomment_integration && file_exists(JPATH_ROOT.'/components/com_jcomments/jcomments.php'))
		{
			require_once JPATH_ROOT.'/components/com_jcomments/jcomments.php';
			?>
			<div class="edocman-comments clearfix">
				<?php  echo JComments::showComments($item->id, 'com_edocman', $item->title); ?>
			</div>
			<?php
		}
		?>
	</div>
</div>

<form method="post" name="edocman_form" id="edocman_form" action="index.php">
	<input type="hidden" name="cid[]" value="<?php echo $item->id; ?>" id="document_id" />
	<input type="hidden" name="category_id" value="<?php echo $this->categoryId ; ?>" />
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="Itemid" value="<?php echo $this->Itemid; ?>" />
	<input type="hidden" name="option" value="com_edocman" />
	<?php echo HTMLHelper::_('form.token'); ?>

	<script type="text/javascript">
		function deleteConfirm()
		{
			var msg = "<?php echo Text::_('EDOCMAN_DELETE_CONFIRM'); ?>";
			if (confirm(msg))
			{
				var form = document.edocman_form ;
				form.task.value = 'documents.delete';
				form.submit();
			}
		}
		function publishConfirm(task) {
			var msg;
			if (task == 'documents.publish')
			{
				msg = "<?php echo Text::_('EDOCMAN_PUBLISH_CONFIRM'); ?>";
			}
			else
			{
				msg = "<?php echo Text::_('EDOCMAN_UNPUBLISH_CONFIRM'); ?>";
			}
			if (confirm(msg))
			{
				var form = document.edocman_form ;
				form.task.value = task;
				form.submit();
			}
		}
		function agreeTerm() {
			var agreeTerm = document.getElementById('agreeterm');
			if(agreeTerm.value == "0") {
				agreeTerm.value = "1";
				<?php
				if($this->config->accept_license_one_time) {
				?>
					var xhr = new XMLHttpRequest();
					xhr.open('POST', 'index.php?option=com_edocman', true);
					xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					
					xhr.onload = function() {
						if (xhr.status === 200) {
							//do nothing
						}
					};
					
					xhr.onerror = function(e) {
						alert('Error');
					};
					
					xhr.send('task=document.store_license_session');
				<?php
				}
				?>
				
				// Remove downloaddisabled class from download links
				document.querySelectorAll(".edocmandownloadlink").forEach(function(link) {
					link.classList.remove("downloaddisabled");
				});
			} else {
				agreeTerm.value = "0";
				<?php
				if($this->config->accept_license_one_time) {
				?>
					var xhr = new XMLHttpRequest();
					xhr.open('POST', 'index.php?option=com_edocman', true);
					xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					
					xhr.onload = function() {
						if (xhr.status === 200) {
							//do nothing
						}
					};
					
					xhr.onerror = function(e) {
						alert('Error');
					};
					
					xhr.send('task=document.disable_license_session');
				<?php
				}
				?>
				
				// Add downloaddisabled class to download links
				document.querySelectorAll(".edocmandownloadlink").forEach(function(link) {
					link.classList.add("downloaddisabled");
				});
			}
		}

		function addBookmark(live_site, id) {
			const xhr = new XMLHttpRequest();
			xhr.open('POST', live_site + 'index.php?option=com_edocman', true);
			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xhr.responseType = 'json';
			
			xhr.onload = function() {
				if (xhr.status === 200) {
					const response = xhr.response;
					alert(response.result);
				}
			};
			
			xhr.onerror = function() {
				console.error('Error in bookmark request');
			};
			
			xhr.send('task=document.bookmark&id=' + id + '&tmpl=component');
		}



	</script>
</form>
<?php
if( $this->config->turn_on_sharing && !EdocmanHelper::isJoomla4())
{
	echo EDocmanHelperHtml::loadCommonLayout('common/sharing.php', array('bootstrapHelper' => $bootstrapHelper));
}
if ($showDownloadForm && !EdocmanHelper::isJoomla4())
{
	echo EDocmanHelperHtml::loadCommonLayout('common/modal.php', array('bootstrapHelper' => $bootstrapHelper,'config' => $config));
}
if((!$canDownload && $hide_download_button != 1 && $this->config->login_to_download && (int)$userId == 0) && !EdocmanHelper::isJoomla4())
{
	echo EDocmanHelperHtml::loadCommonLayout('common/login.php', array('bootstrapHelper' => $bootstrapHelper));
}
echo EDocmanHelperHtml::loadCommonLayout('common/player.php', array());
echo EDocmanHelperHtml::loadCommonLayout('common/download.php', array());
if($config->login_to_download && (int)$userId == 0)
{
	echo EDocmanHelperHtml::loadCommonLayout('common/modallogin.php', array());
	?>
	<script src="<?php echo Uri::base(); ?>components/com_edocman/assets/js/login.js" type="text/javascript"></script>
	<?php
}
if($this->config->turn_on_sharing)
{
	echo EDocmanHelperHtml::loadCommonLayout('common/modalsharing.php', array());
	?>
	<script src="<?php echo Uri::base(); ?>components/com_edocman/assets/js/sharing.js" type="text/javascript"></script>
	<?php
}
?>
<script src="<?php echo Uri::base(); ?>components/com_edocman/assets/js/player.js" type="text/javascript"></script>
<script src="<?php echo Uri::base(); ?>components/com_edocman/assets/js/download.js" type="text/javascript"></script>
<script type="text/javascript">
	function toggleShareDropdown() {
		document.getElementById("share-dropdown").classList.toggle("show");
		
		// Đóng dropdown menu nếu đang mở
		const dropdownMenu = document.getElementById("blog-dropdown-menu");
		if (dropdownMenu.classList.contains('show')) {
			dropdownMenu.classList.remove('show');
		}
	}

	function toggleDropdown() {
		document.getElementById("blog-dropdown-menu").classList.toggle("show");
		
		// Đóng share dropdown nếu đang mở
		const shareDropdown = document.getElementById("share-dropdown");
		if (shareDropdown.classList.contains('show')) {
			shareDropdown.classList.remove('show');
		}
	}
</script>
<?php
echo EDocmanHelperHtml::loadCommonLayout('common/massdownload.php');
?>
