<?php

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

HTMLHelper::_('bootstrap.dropdown');
$user = Factory::getApplication()->getIdentity();
if (count($categories))
{
	$return = base64_encode(Uri::getInstance()->toString());
	if ($categoryId && $config->show_subcategory_text)
	{
	?>
		<h2 class="edocman-heading"><i class="edicon edicon-folder-open"></i>&nbsp;<?php echo Text::_('EDOCMAN_SUB_CATEGORIES'); ?></h2>
	<?php
	}
	if((int) $number_categories > 0)
    {
        $numberColumns = $number_categories;
    }
	elseif (isset($config->number_subcategories))
	{
		$numberColumns = $config->number_subcategories ;
	}
	else
	{
		$numberColumns = 1 ;
	}
	$span = intval(12 / $numberColumns);
	if ($span != 12)
	{
		$spanClass = ' '.$bootstrapHelper->getClassMapping('span'.$span);
	}
	else
	{
		$spanClass = '';
	}
	$rowFluidClass = $bootstrapHelper->getClassMapping('row-fluid');
	?>
	<style>
	h3.edocman-category-title
	{
		font-size: <?php echo ($config->category_title_font_size == '') ? '25' : $config->category_title_font_size; ?>px !important;
	}
	</style>
	<div id="edocman-categories" class="edgallery-categories-grid-items categories-gallery">
		<div class="<?php echo $rowFluidClass; ?> clearfix">
		<?php
		$j = 0;
		for ($i = 0 , $n = count($categories) ; $i < $n ; $i++)
		{
			$category = $categories[$i];
			if (!$config->show_empty_cat && !$category->total_documents)
			{
				continue ;
			}
			$imgUrl = Uri::base() . 'components/com_edocman/assets/images/icons/thumbs/noimage.png';
			if ($category->image && is_file(Path::clean(JPATH_ROOT.'/media/com_edocman/category/thumbs/'.$category->image)))
			{
				$imgUrl = Uri::base().'media/com_edocman/category/thumbs/'.$category->image;
			}
			else
			{
				if (!isset($config->show_default_category_thumbnail) || $config->show_default_category_thumbnail)
				{
					$imgUrl = Uri::base().'components/com_edocman/assets/images/icons/32x32/folder.png' ;
				}
			}
			
			$j++;
			?>
			<div class="<?php echo $spanClass; ?>">
				<div class="edocman-category-gallery">
					<div class="edocman-category-gallery-title">
						<div class="edocman-category-gallery-title-icon">
							<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-folder-fill" viewBox="0 0 16 16">
							  <path d="M9.828 3h3.982a2 2 0 0 1 1.992 2.181l-.637 7A2 2 0 0 1 13.174 14H2.825a2 2 0 0 1-1.991-1.819l-.637-7a2 2 0 0 1 .342-1.31L.5 3a2 2 0 0 1 2-2h3.672a2 2 0 0 1 1.414.586l.828.828A2 2 0 0 0 9.828 3m-8.322.12q.322-.119.684-.12h5.396l-.707-.707A1 1 0 0 0 6.172 2H2.5a1 1 0 0 0-1 .981z"/>
							</svg>
						</div>
						<div class="edocman-category-gallery-title-details">
							<a href="<?php echo Route::_(EDocmanHelperRoute::getCategoryRoute($category->id, $Itemid)); ?>" class="edocman-category-gallery-title-link" title="<?php echo $category->title;?>">
								<?php echo $category->title;?>
							</a>
							<?php
							if ($config->show_number_documents)
							{
							?>
								<div class="clearfix"></div>
								<span class="edocman-category-gallery-title-ndocuments">(<?php echo $category->total_documents ;?> <?php echo $category->total_documents != 1 ? Text::_('EDOCMAN_DOCUMENTS') :  Text::_('EDOCMAN_DOCUMENT') ; ?>)</span>
							<?php
							}
							?>
						</div>
						<div class="edocman-category-gallery-title-dropdown dropdown-<?php echo $category->id;?>">
							<div class="edocman-category-gallery-title-dropdown-menu-icon" onclick="toggleMenu(<?php echo $category->id;?>)">
								<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-three-dots-vertical" viewBox="0 0 16 16">
								  <path d="M9.5 13a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0m0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0m0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0"/>
								</svg>
							</div>
							<div class="edocman-category-gallery-title-dropdown-menu-popup" id="menuPopup_<?php echo $category->id;?>">
								<a href="<?php echo Route::_(EDocmanHelperRoute::getCategoryRoute($category->id, $Itemid)); ?>" class="edocman-category-gallery-title-link" target="_blank" title="<?php echo $category->title;?>"><?php echo Text::_('EDOCMAN_OPEN_CATEGORY_IN_NEW_TAB')?></a>
								<?php
								if ($user->authorise('edocman.manage_categories', 'com_edocman') || $user->authorise('edocman.manage_subcategories', 'com_edocman.category.'.$category->id))
								{

									$url = Route::_('index.php?option=com_edocman&task=editcategory&cid[]='.$category->id.'&return='.$return.'&Itemid='.$Itemid) ;
								?>
									<a href="<?php echo $url;?>" title="<?php echo Text::_('EDOCMAN_EDIT');?>"><?php echo Text::_('EDOCMAN_EDIT');?></a>
								<?php
								}	
								?>
							</div>
						 </div>
					</div>
					<div class="edgallery-category-item edgallery-category-item-grid-overlay">
						<a href="<?php echo Route::_(EDocmanHelperRoute::getCategoryRoute($category->id, $Itemid)); ?>" class="edgallery-category-thumb-link" title="<?php echo $category->title;?>">
							<img class="edgallery-category-thumb" src="<?php echo $imgUrl; ?>" alt="<?php echo $category->title;?>">
						</a>
					</div>
				</div>
			</div>
			<script>
			// Close menu when clicking outside
			document.addEventListener("click", function(event) {
				var menuContainer = document.querySelector(".dropdown-<?php echo $category->id;?>");
				if (!menuContainer.contains(event.target)) {
					document.getElementById("menuPopup_<?php echo $category->id?>").style.display = "none";
				}
			});
			</script>
		<?php
			if($j == $numberColumns)
			{
				?>
				</div><div class="<?php echo $rowFluidClass; ?> clearfix">
				<?php
				$j = 0;
			}
		}
		?>
		</div>
	</div>
<?php
}
?>
<script type="text/javascript">
    function deleteCategoryConfirm(id) {
        var msg = "<?php echo Text::_('EDOCMAN_DELETE_CONFIRM'); ?>";
        if (confirm(msg))
        {
            location.href = "<?php echo Uri::root()?>index.php?option=com_edocman&task=deletecategory&cid[]=" + id + "&parentId=<?php echo $categoryId?>&return=<?php echo $return;?>";
        }
    }

	function toggleMenu(catId) 
	{
		var menu = document.getElementById("menuPopup_" + catId);
		menu.style.display = (menu.style.display === "block") ? "none" : "block";
	}
</script>
