<?php

/**
 * @version        1.9.8
 * @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\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Language\Text;
use Joomla\String\StringHelper;
use Joomla\CMS\HTML\HTMLHelper;

$session	    = Factory::getApplication()->getSession();
if(!$config->onetime_collect){
	$session->set('name','');
	$session->set('email','');
}
$name		    = $session->get('name','');
$email		    = $session->get('email','');
$user		    = Factory::getApplication()->getIdentity();
$userId		    = $user->get('id');
$btnClass	    = $bootstrapHelper->getClassMapping('btn');
$show_category  = Factory::getApplication()->getInput()->getInt('show_category',0);

if ($config->show_detail_in_popup)
{
	$popup      = 'class="edocman-modal btn details-btn" rel="{handler: \'iframe\', size: {x: 800, y: 500}}"';
	$popupLink  = 'class="edocman-modal" rel="{handler: \'iframe\', size: {x: 800, y: 500}}"';
}
else
{
	$popup      = ' class="btn details-btn" ';
	$popupLink  = '';
}


if ($config->collect_downloader_information && !$userId && ($name == '' || $email == ''))
{
	$showDownloadForm = true;
}
else
{
	$showDownloadForm = false;
}
?>
<script type="text/javascript">
	var spanClass = '<?php echo $spanClass; ?>';
	<?php
	if(EdocmanHelper::isJoomla4())
	{
		?>
		rowFluidClass = '<?php echo $bootstrapHelper->getClassMapping('span12'); ?>';
		<?php
	}
	else
	{
		?>
		rowFluidClass = '<?php echo $bootstrapHelper->getClassMapping('row-fluid'); ?>';
		<?php
	}
	?>
</script>
<style>
	h3.edocman-document-title
	{
		font-size: <?php echo ($config->document_title_font_size == '') ? '18' : $config->document_title_font_size; ?>px !important;
	}
</style>
<div class="<?php echo $bootstrapHelper->getClassMapping('row-fluid'); ?>">
	<div id="edocman-documents" class="<?php echo $bootstrapHelper->getClassMapping('span12'); ?>">
		<div class="documents-container-blog">

			<div id="documentsblogtooltip" class="btooltip">
				<div class="btooltip-arrow"></div>
				<div class="btooltip-content"></div>
			</div>
			<?php
			if(count($items))
			{
				require_once JPATH_ROOT.'/components/com_edocman/helper/file.class.php' ;
				$activeItemid       = $Itemid;
				for ($i = 0 , $n = count($items) ;  $i < $n ; $i++)
				{
					if( !empty($category))
					{
						$catId      = $category->id;
					}
					else
					{
						$catId = 0;
					}
					$item           = $items[$i] ;
					$Itemid         = EDocmanHelperRoute::getDocumentMenuId($item->id, $catid, $activeItemid);
					$item->data     = new EDocman_File($item->id,$item->filename, $config->documents_path);
					$imgSrc         = '';
					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($config->show_default_document_thumbnail) || $config->show_default_document_thumbnail)
						{
							//$imgSrc = '<i class="'.$item->data->fileicon.'"></i>';
						}
						else
						{
							$imgSrc = '' ;
						}
					}
					if ($config->show_detail_in_popup)
					{
						$url        = Route::_('index.php?option=com_edocman&view=document&id='.$item->id.'&catid='.$catId.'&tmpl=component&Itemid='.$Itemid);
					}
					else
					{
						$url        = Route::_('index.php?option=com_edocman&view=document&id='.$item->id.'&catid='.$catId.'&Itemid='.$Itemid);
					}
					$document_details = $url;
					$downloadUrl    = Route::_('index.php?option=com_edocman&task=document.download&id='.$item->id.'&Itemid='.$Itemid) ;
					//$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    = ($item->created_user_id == $userId && $item->created_user_id > 0) || (($item->user_ids == "" || $item->user_ids == 0) && ($canDownload || $canEdit)) || ($item->user_ids != '' && $item->user_ids != '0' && in_array($userId, explode(',', $item->user_ids))) ;
					$canDownload	= EdocmanHelper::canDownload($item->id);
					$accept_license = 0;
					$show_accept_license		= 0;
					if($config->accept_license && ($item->license_id > 0 || EdocmanHelper::getDefaultLicense() > 0))
					{
						if($config->accept_license_one_time)
						{
							$alreadyAcceptLicense	= $session->get('alreadyAcceptLicense',0);
							if($alreadyAcceptLicense == 0)
							{
								$show_accept_license = 1;
							}
							else
							{
								$show_accept_license = 0;
							}
						}
						else
						{
							$show_accept_license = 1; 
						}
					}

					if($show_accept_license == 1 || $item->download_password != "")
					{
						$accept_license = 1;
					}

					$fileName               = $item->filename;
					if($fileName != "")
					{
						$fileExt            = strtolower(EdocmanHelper::getExt($fileName));
					}
					else
					{
						$fileExt            = strtolower(EdocmanHelper::getExt($item->document_url));
					}
					
					$hide_download_button	= $config->hide_download_button;
					$hide_download			= $category->hide_download;
					if((int)$hide_download == 1)
					{
						$hide_download_button = 1;
					}
					elseif((int)$hide_download == 2)
					{
						$hide_download_button = 0;
					}
					$show_view_button		= $config->show_view_button;
					$show_view				= $category->show_view;
					if((int)$show_view == 1)
					{
						$show_view_button = 1;
					}
					elseif((int)$show_view == 2)
					{
						$show_view_button = 0;
					}

					if($item->document_url != "")
					{
						if($config->external_download_link == 1)
						{
							$target = "_blank";
						}
						else
						{
							$target = "_self";
						}
					}
					else
					{
						$target = "_self";
					}
	
					$ext					= strtolower(EDocmanHelper::getExt($item)) ;
					?>
					
						
					<div class="blog-document-card" data-type="<?php echo $ext; ?>">
						<div class="blog-document-header">
							<div class="thumbnail <?php echo $ext; ?>">
								<?php
								if($imgSrc != "")
								{
								?>
									<img src="<?php echo $imgSrc; ?>" alt="<?php echo $item->title; ?>" onerror="this.style.display='none';" />	
									<span class="extension"><?php echo strtoupper($ext)?></span>
								<?php
								}	
								?>
							</div>
							<div class="document-info">
								<h2 class="document-title">
									<?php
									if ($config->use_download_link_instead_of_detail_link && $canDownload && ($accept_license == 0))
									{
										if ($showDownloadForm)
										{
											if(EdocmanHelper::isJoomla4())
											{
											?>
												<a data-toggle="modal" data-document-title="<?php echo $item->title; ?>"  id="<?php echo $item->id; ?>" class="edocmancollectdownloadbtn edocman-document-title-link" href="<?php echo Uri::root(true); ?>/index.php?option=com_edocman&view=document&layout=modal&tmpl=component&id=<?php echo $item->id;?>&direct=1" aria-label="<?php echo Text::_('EDOCMAN_DOWNLOAD_DOCUMENT'); ?>" target="<?php echo $target;?>">
											<?php
											}
											else
											{
											?>
												<a aria-label="<?php echo Text::_('EDOCMAN_DOWNLOAD_DOCUMENT'); ?>" data-toggle="modal" data-document-title="<?php echo $item->title; ?>" title="<?php echo Text::_('EDOCMAN_DOWNLOAD_DOCUMENT'); ?>"  id="<?php echo $item->id; ?>" class="email-popup edocman-document-title-link" href="#form-content" target="<?php echo $target;?>">
											<?php
											}	
											?>
												<?php
												if($config->show_icon_beside_title){
													EDocmanHelperHtml::showFileIcon($item->data);
												} ?>
												<?php 
												$title = $item->title;
												$words = explode(' ', $title);
												if (count($words) > 4) {
													$shortTitle = implode(' ', array_slice($words, 0, 4));
													echo '<span>' . htmlspecialchars($shortTitle) . '</span>';
													echo ' <span class="more-icon" onclick="toggleTooltipDocumentsBlog(event, this)" data-full-title="' . htmlspecialchars($title) . '">...</span>';
												} else {
													echo $title;
												}
												?>
											</a>
										<?php
										}
										else
										{
											if($item->document_url != "")
											{
												$downloadUrl = $item->document_url;
											}
											?>
											<a href="<?php echo $downloadUrl; ?>" title="<?php echo Text::_('EDOCMAN_DOWNLOAD_DOCUMENT'); ?>" class="edocman-document-title-link" target="<?php echo $target;?>" aria-label="<?php echo Text::_('EDOCMAN_DOWNLOAD_DOCUMENT'); ?>">
												<?php
												if($config->show_icon_beside_title){
												   EDocmanHelperHtml::showFileIcon($item->data);
												}
												?>
												<?php 
												$title = $item->title;
												$words = explode(' ', $title);
												if (count($words) > 4) {
													$shortTitle = implode(' ', array_slice($words, 0, 4));
													echo '<span>' . htmlspecialchars($shortTitle) . '</span>';
													echo ' <span class="more-icon" onclick="toggleTooltipDocumentsBlog(event, this)" data-full-title="' . htmlspecialchars($title) . '">...</span>';
												} else {
													echo $title;
												}
												?>
											</a>
										<?php
										}
									}
									else
									{
									?>
										<a href="<?php echo $url; ?>" title="<?php echo $item->title; ?>" <?php echo $popupLink; ?> class="edocman-document-title-link" aria-label="<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>">
											<?php
											if($config->show_icon_beside_title)
											{
												EDocmanHelperHtml::showFileIcon($item->data);
											} ?>
											<?php 
											$title = $item->title;
											$words = explode(' ', $title);
											if (count($words) > 4) {
												$shortTitle = implode(' ', array_slice($words, 0, 4));
												echo '<span>' . htmlspecialchars($shortTitle) . '</span>';
												echo ' <span class="more-icon" onclick="toggleTooltipDocumentsBlog(event, this)" data-full-title="' . htmlspecialchars($title) . '">...</span>';
											} else {
												echo $title;
											}
											?>
										</a>
									<?php
									}
									?>
								</h2>
								<?php
								if($item->indicators != '' || !empty($item->new_indicator) || !empty($item->update_indicator) || $item->published == 0)
								{
									$indicators = explode(',', $item->indicators);
								?>
									<div class="blog-badges">
										<?php
										if (!empty($item->new_indicator))
										{
										?>
											<span class="blog-badge new">
													<?php echo Text::_('EDOCMAN_NEW');?>
											</span>
										<?php
										}
										if(in_array('featured', $indicators))
										{
										?>
											<span class="blog-badge featured">
													<?php echo Text::_('EDOCMAN_FEATURED');?>
											</span>
										<?php
										}
										if(in_array('hot', $indicators))
										{
										?>
											<span  class="blog-badge hot">
												<?php echo Text::_('EDOCMAN_HOT');?>
											</span>
										<?php
										}
										if($item->published == 0)
										{
											?>
											<span class="blog-badge published">
												<?php echo Text::_('JUNPUBLISHED');?>
											</span>
											<?php
										}
										?>
									</div>
								<?php
								}
								?>
								
								<div class="meta-info">
									<?php
									if($item->created_time != "" && $item->created_time != "0000-00-00 00:00:00" && $config->show_creation_date == 1)
									{
										?>
										<span class="date"><i class="far fa-calendar-alt"></i> <?php echo HTMLHelper::_('date',$item->created_time, 'j F Y');?></span>
										<?php
									}
									if($item->created_user_id > 0 && $config->show_creation_user == 1){
										?>
										<span class="author"><i class="fas fa-user"></i> <?php echo Factory::getUser($item->created_user_id)->name;?>
										</span>
										<?php
									}
									?>
								</div>
								<div class="document-desc">
									<?php
									if (!$item->short_description)
									{
										$item->short_description = $item->description;
									}
									echo $item->short_description;
									?>
								</div>
								<div class="document-stats">
									<?php
									if($item->hits > 0 && $config->show_hits)
									{
									?>
										<span><i class="fas fa-eye"></i> <?php echo $item->hits?> <?php echo Text::_('EDOCMAN_VIEW');?></span>
									<?php
									}		
									?>
									<?php
									if ($config->show_number_downloads){	
									?>
										<span><i class="fas fa-download"></i> <?php echo $item->downloads; ?> <?php echo Text::_('EDOCMAN_DOWNLOAD');?></span>
									<?php } ?>
								</div>
							</div>
						</div>
						<!-- Nhóm nút chính -->
						<div class="document-actions primary-actions">
							<?php
							if ($canDownload && $show_view_button == 1 && $item->canView)
							{
								$playextension	= EdocmanHelper::returnPlayExtension();
								$showModal		= EdocmanHelper::viewable($item);
								$ext			= StringHelper::strtolower(EDocmanHelper::getExt($item)) ;
								if((in_array($ext,$playextension) || $item->view_url != "") && $showModal && !EDocmanHelper::isDropBoxTurnedOn() && !EDocmanHelper::isAmazonS3TurnedOn())
								{
									$viewUrl = Uri::root()."index.php?option=com_edocman&view=play&id=".$item->id."&tmpl=component";
									$audio_player = EdocmanHelper::generateAudioPlayer($item, $showModal);
									
									if($config->view_option == 0)
									{
									?>
										<a href="<?php echo $viewUrl; ?>" class="btn view-btn 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="edicon edicon-eye"></i>&nbsp;
											<?php echo Text::_('EDOCMAN_VIEW'); ?>
										</a>
									<?php
									}
									else
									{
									?>
										<a href="<?php echo $viewUrl; ?>" class="btn view-btn" 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="edicon edicon-eye"></i>&nbsp;
											<?php echo Text::_('EDOCMAN_VIEW'); ?>
										</a>
									<?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 );

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

											$extraViewClass = '';
											$target         = 'target="_blank"';
										}
									}
									?>
									
										<a class="btn view-btn <?php echo $extraViewClass; ?>" href="<?php echo $viewUrl; ?>"
										  <?php echo $target; ?> aria-label="<?php echo Text::_('EDOCMAN_CLICK_TO_VIEW_CONTENT_DOCUMENT'); ?>" rel="nofollow" >
											<i class="edicon edicon-eye"></i>&nbsp;
											<?php echo Text::_('EDOCMAN_VIEW'); ?>
										</a>
									
									<?php
								}
							}
							if ($canDownload && $hide_download_button !=  1  && $accept_license == 0)
							{
								if ($showDownloadForm)
								{
									
									?>
									<a data-toggle="modal" data-document-title="<?php echo $item->title; ?>"  id="<?php echo $item->id; ?>" class="edocmancollectdownloadbtn downloadbtn1 btn download-btn" href="<?php echo Uri::root(true); ?>/index.php?option=com_edocman&view=document&layout=modal&tmpl=component&id=<?php echo $item->id;?>&direct=1" aria-label="<?php echo $item->title; ?>">
										<?php if($item->document_url != ""){?>
											<i class="edicon edicon-link"></i>&nbsp;
											<?php echo Text::_('EDOCMAN_OPEN_DOCUMENT'); ?>
										<?php }else{ ?>
											<i class="edicon edicon-cloud-download"></i>&nbsp;
											<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>
										<?php } ?>
									</a>
									<?php
								}
								else
								{
									if($item->document_url != "")
									{
										$downloadUrl = $item->document_url;
									}
									?>
									<a class="downloadbtn1 btn download-btn" href="<?php echo $downloadUrl; ?>" target="<?php echo $target;?>" aria-label="<?php echo Text::_('EDOCMAN_DOWNLOAD_DOCUMENT'); ?>">

										<?php if($item->document_url != ""){?>
											<i class="edicon edicon-link"></i>&nbsp;
										<?php }else{ ?>
											<i class="edicon edicon-cloud-download"></i>&nbsp;
										<?php } ?>
										<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>
									</a>
									<?php
								}
							}
							elseif(!$canDownload && $hide_download_button != 1 && $config->login_to_download && (int)$userId == 0)
							{
								if($config->edocman_login == 1)
								{
									EdocmanHelper::generateJoomlaLogin('downloadbtn btn download-btn');
								}
								else
								{
									$return = base64_encode(Uri::getInstance()->toString());
									?>
									<a data-toggle="modal" class="logintodownload downloadbtn1 btn download-btn" 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>&nbsp;
										<?php
										echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD'); ?>
									</a>
								<?php
								}		
							}
							elseif($canDownload && $hide_download_button != 1 && ($accept_license == 1))
							{
								?>
								<a class="downloadbtn1 btn download-btn" href="<?php echo $url; ?>" aria-label="<?php echo Text::_('EDOCMAN_DOWNLOAD_DOCUMENT'); ?>">
									<?php if($item->document_url != ""){?>
										<i class="edicon edicon-link"></i>&nbsp;
									<?php }else{ ?>
										<i class="edicon edicon-cloud-download"></i>&nbsp;
									<?php } ?>
									<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>
								</a>
							<?php
							}
							
							if ($config->hide_details_button !== '1')
							{
							?>	
								<a href="<?php echo $document_details; ?>" <?php echo $popup; ?> aria-label="<?php echo Text::_('EDOCMAN_DETAILS'); ?>">
									<i class="fas fa-info-circle"></i>&nbsp;
									<?php echo Text::_('EDOCMAN_DETAILS'); ?>
								</a>
							<?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(EDocmanHelper::massDownloadShowing($canDownload, $config, $item) || (($canEdit || $canEditOwn) && $pass_lock))
							{
							?>
								<button class="btn more-btn" aria-label="<?php echo Text::_('EDOCMAN_MORE_OPTIONS');?>"><i class="fas fa-ellipsis-v"></i></button>
							<?php
							}	
							?>
						</div>
						
						<div class="document-actions extended-actions">
							<?php
							if(EDocmanHelper::massDownloadShowing($canDownload, $config, $item))
							{
								if(EDocmanHelper::isAlreadyInMassList($item->id))
								{
									$disabled = "disabled";
								}
								else
								{
									$disabled = "";
								}
								?>
								<a href="javascript:void(0);" id="btnMassDownload_<?php echo $item->id; ?>"  class="edocmandownloadlink btn mass-download-btn <?php echo $disable_download_button;?>" aria-label="<?php echo $item->title; ?>" <?php echo $disabled;?>  data-id="<?php echo $item->id; ?>" data-name="<?php echo $item->title; ?>" data-type="<?php echo $ext;?>" data-size="<?php $item->data->size; ?>">
									<i class="fas fa-plus-circle"></i>&nbsp;
									<?php echo Text::_('EDOCMAN_MASS_DOWNLOAD'); ?>
								</a>
								<?php
							}
							
							if (($canEdit || $canEditOwn) && $pass_lock)
							{
								$return = EdocmanHelper::currentUrl();
								$edit_url = Route::_('index.php?option=com_edocman&task=document.edit&id='.$item->id.'&return='.$return.'&Itemid='.$Itemid) ;
								?>
								
								<a class="btn edit-btn" href="<?php echo $edit_url; ?>" aria-label="<?php echo Text::_('EDOCMAN_EDIT'); ?>">
									<i class="edocman-icon-pencil"></i>&nbsp;
									<?php echo Text::_('EDOCMAN_EDIT'); ?>
								</a>
								<?php
							}
							if ($canDelete)
							{
							?>
								
									<a class="btn delete-btn" href="javascript:deleteConfirm(<?php echo $item->id; ?>);" aria-label="<?php echo Text::_('EDOCMAN_DELETE'); ?>">
										<i class="edocman-icon-trash"></i>&nbsp;
										<?php echo Text::_('EDOCMAN_DELETE'); ?>
									</a>
							<?php
							}
							if ($canChange)
							{
								if ($item->published)
								{
									$text = Text::_('EDOCMAN_UNPUBLISH');
									$url = "javascript:publishConfirm($item->id, 0)";
									$class = 'edocman-icon-remove';
								}
								else
								{
									$url = $url = "javascript:publishConfirm($item->id, 1)";
									$text = Text::_('EDOCMAN_PUBLISH');
									$class = 'edocman-icon-ok';
								}
								?>
									<a href="<?php echo $url; ?>" class="btn publish-btn">
										<i class="<?php echo $class; ?>"></i>&nbsp;
										<?php echo $text; ?>
									</a>
								<?php
							}
							?>
						</div>
					</div>
					<?php
				}
			}
			else
			{
				if($config->show_no_document_available_text)
				{
					echo Text::_('EDOCMAN_NO_AVAILABLE_DOCUMENTS');
				}
			}
			?>
		</div>
    </div>
</div>

<?php
if ($showDownloadForm && !EdocmanHelper::isJoomla4())
{
	echo EDocmanHelperHtml::loadCommonLayout('common/modal.php', array('bootstrapHelper' => $bootstrapHelper,'config' => $config));
}
if((!$canDownload && $hide_download_button != 1 && $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());
//echo EDocmanHelperHtml::loadCommonLayout('common/massdownload.php');
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
}
?>
<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">


// Thêm vào trong phần document.addEventListener('DOMContentLoaded', function() {...})

// Xử lý hiển thị thumbnail cho tài liệu không có ảnh
function setupDocumentThumbnails() {
    const thumbnails = document.querySelectorAll('.thumbnail');
    
    thumbnails.forEach(thumbnail => {
        const img = thumbnail.querySelector('img');
        
        // Kiểm tra nếu không có ảnh hoặc ảnh lỗi
        if (!img || !img.src || img.src === '' || img.src === 'undefined') {
            setupDefaultThumbnail(thumbnail);
        } else {
            // Xử lý khi ảnh không tải được
            img.onerror = function() {
                setupDefaultThumbnail(thumbnail);
            };
        }
    });
}

// Tạo thumbnail mặc định theo loại tài liệu
function setupDefaultThumbnail(thumbnail) {
    // Xóa ảnh nếu có
    if (thumbnail.querySelector('img')) {
        thumbnail.querySelector('img').remove();
    }
    
    // Lấy loại tài liệu từ thẻ cha
    const card = thumbnail.closest('.blog-document-card');
    const fileType = card.getAttribute('data-type') || 'unknown';
    
    // Thêm class cho thumbnail
    thumbnail.classList.add('no-image', fileType);
    
    // Tạo phần tử hiển thị phần mở rộng file
    const extensionElement = document.createElement('div');
    extensionElement.className = 'file-extension';
    extensionElement.textContent = fileType;
    
    // Thêm vào thumbnail
    thumbnail.appendChild(extensionElement);
}

// Gọi hàm thiết lập thumbnail khi trang được tải
setupDocumentThumbnails();


document.addEventListener('DOMContentLoaded', function() {
    // Filter functionality
    const documentCards = document.querySelectorAll('.blog-document-card');
	const viewButtons = document.querySelectorAll('.view-btn');
    const downloadButtons = document.querySelectorAll('.download-btn');
    const detailsButtons = document.querySelectorAll('.details-btn');
	const moreButtons = document.querySelectorAll('.more-btn');
	moreButtons.forEach(button => {
        button.addEventListener('click', function(e) {
			e.preventDefault();
            e.stopPropagation();
            
            const card = this.closest('.blog-document-card');
            const extendedActions = card.querySelector('.extended-actions');
            
            // Toggle hiển thị extended actions
            extendedActions.classList.toggle('active');
            
            // Đóng các extended actions khác
            document.querySelectorAll('.extended-actions.active').forEach(el => {
                if (el !== extendedActions) {
                    el.classList.remove('active');
                }
            });
        });
    });
    
    // Đóng extended actions khi click ra ngoài
    document.addEventListener('click', function(event) {
        if (!event.target.closest('.more-btn') && !event.target.closest('.extended-actions')) {
            document.querySelectorAll('.extended-actions.active').forEach(el => {
                el.classList.remove('active');
            });
        }
    });
});


let currentIcon = null;

function toggleTooltipDocumentsBlog(event, elem) {
    event.preventDefault();
    event.stopPropagation();

    const tooltip = document.getElementById('documentsblogtooltip');
    const content = tooltip.querySelector('.btooltip-content');

    // Toggle nếu đang mở cùng icon
    if (tooltip.style.display === 'block' && currentIcon === elem) {
        tooltip.style.display = 'none';
        currentIcon = null;
        return;
    }

    content.innerText = elem.getAttribute('data-full-title');
    tooltip.style.display = 'block';

    const rect = elem.getBoundingClientRect(); // so với viewport
	const tooltipWidth = tooltip.offsetWidth;
	const tooltipHeight = tooltip.offsetHeight;

	const left = rect.left + rect.width / 2 - tooltipWidth / 2;
	const top = rect.bottom + 10; // hiển thị ngay dưới icon

	tooltip.style.left = `${Math.max(left, 8)}px`;
	tooltip.style.top = `${top}px`;

    currentIcon = elem;

    // Click ngoài sẽ đóng tooltip
    document.addEventListener('click', function handler(e) {
        if (!tooltip.contains(e.target) && e.target !== elem) {
            tooltip.style.display = 'none';
            currentIcon = null;
            document.removeEventListener('click', handler);
        }
    });
}
</script>
