<?php

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

$user = Factory::getApplication()->getIdentity();

if (count($categories))
{
	if($open_documents == 1)
	{
		$style = "display:block;";
	}
	else
	{
		$style = "display:none;";
	}
	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
	}
	$rowFluidClass = $bootstrapHelper->getClassMapping('row-fluid');
	?>
    <table class="table-condensed">
		<thead>
            <tr>
                <th class="sr-only">
                    Category title 
                </th>
            </tr>
        </thead>
        <tbody>
            <?php
            for ($i = 0 , $n = count($categories) ; $i < $n ; $i++)
            {
                $category = $categories[$i];
                ?>
                <tr>
                    <td class="edocman-category-title-td" data-label="">
						<a title="<?php echo $category->title; ?>" aria-label="<?php echo $category->title; ?>" href="javascript:openDocumentGroups(<?php echo $category->id;?>);" style="text-decoration:none;">
							<i class="edicon edicon-folder" id="a_category_visible_<?php echo $category->id; ?>"></i>
						</a>
                        <a href="javascript:openDocumentGroups(<?php echo $category->id;?>);" class="edocman-category-title-link" style="display: inline-block;text-decoration:none;" >
                            <?php
                            echo $category->title;
                            if ($config->show_number_documents)
                            {
                                ?>
                                <span class="number_documents">(<?php echo $category->total_documents ;?> <?php echo $category->total_documents > 1 ? Text::_('EDOCMAN_DOCUMENTS') :  Text::_('EDOCMAN_DOCUMENT') ; ?>)</span>
                                <?php
                            }
                            ?>
                        </a>
						
                        <?php
                        if ($config->enable_rss)
                        {
                            ?>
                            <span class="edocman-rss-icon">
								<a href="<?php echo Route::_('index.php?option=com_edocman&view=category&id='.$category->id.'&format=feed&type=rss'); ?>"><img src="<?php echo Uri::root().'/components/com_edocman/assets/images/rss.png' ?>" /></a>
							</span>
                            <?php
                        }
                        if ($categoryId > 0 && $user->authorise('edocman.manage_categories', 'com_edocman') || $user->authorise('edocman.manage_subcategories', 'com_edocman.category.'.$categoryId))
                        {
                            $url = Route::_('index.php?option=com_edocman&task=editcategory&cid[]='.$category->id.'&Itemid='.$Itemid) ;
                            ?>
                            <a class="documents_table_edit" href="<?php echo $url; ?>" title="<?php echo Text::_('EDOCMAN_EDIT'); ?>">
                                <i class="edocman-icon-pencil"></i>
                            </a>
                            <?php
                        }
                        if ($categoryId > 0 && $user->authorise('edocman.manage_categories', 'com_edocman') || $user->authorise('edocman.manage_subcategories', 'com_edocman.category.'.$categoryId))
                        {
                            ?>
                            <a class="documents_table_delete" href="javascript:deleteCategoryConfirm(<?php echo $category->id; ?>);" title="<?php echo Text::_('EDOCMAN_DELETE'); ?>">
                                <i class="edocman-icon-trash"></i>
                            </a>
                            <?php
                        }
                        ?>
                    </td>
                </tr>
                <?php
                if(count($category->documents))
                {
					if ($config->show_view_button && count($category->documents))
					{
						$extsForView = explode(',', $config->exts_for_view);
						for ($j = 0, $n1 = count($extsForView); $j < $n1; $j++)
						{
							$extsForView[$j] = strtolower(trim($extsForView[$j]));
						}
						foreach ($category->documents as $item)
						{
							$fileName = $item->filename;
							$fileExt  = strtolower(EdocmanHelper::getExt($fileName));
							if (in_array($fileExt, $extsForView) || ($item->view_url != ''))
							{
								$item->canView = 1;
							}
							else
							{
								$item->canView = 0;
							}
						}
					}
                    ?>
                    <tr>
                        <td width="100%">
							<div id="category<?php echo $category->id?>" style="<?php echo $style;?>">
								<?php
								echo EDocmanHelperHtml::loadCommonLayout('common/documents_table.php', array('items' => $category->documents, 'config' => $config, 'Itemid' => $Itemid, 'category' => $category, 'categoryId' => (int) $category->id, 'bootstrapHelper' => $bootstrapHelper));
								?>
								<input type="hidden" name="category_visible_<?php echo $category->id; ?>" id="category_visible_<?php echo $category->id; ?>" value="0" />
							</div>
                        </td>
                    </tr>
                    <?php
                }
            }
            ?>
        </tbody>
    </table>
<?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?>";
        }
    }
	function openDocumentGroups(id) {
		var category_visible = document.getElementById("category_visible_" + id).value;
		var categoryElement = document.getElementById("category" + id);
		var visibilityElement = document.getElementById("category_visible_" + id);
		var iconElement = document.getElementById("a_category_visible_" + id);
		
		if(category_visible == "0") {
			// Show the category with slide down animation
			slideDown(categoryElement);
			visibilityElement.value = "1";
			iconElement.classList.remove("edicon-folder");
			iconElement.classList.add("edicon-folder-open");
		} else {
			// Hide the category with slide up animation
			slideUp(categoryElement);
			visibilityElement.value = "0";
			iconElement.classList.remove("edicon-folder-open");
			iconElement.classList.add("edicon-folder");
		}
	}

	// Helper function to replicate jQuery's slideDown
	function slideDown(element) {
		// Store the element's height
		element.style.display = 'block';
		var height = element.scrollHeight;
		
		// Set initial properties
		element.style.overflow = 'hidden';
		element.style.height = 0;
		element.style.transition = 'height 0.3s ease';
		
		// Trigger reflow
		element.offsetHeight;
		
		// Set target height
		element.style.height = height + 'px';
		
		// Clean up after animation completes
		element.addEventListener('transitionend', function transitionEnd() {
			element.style.height = '';
			element.style.overflow = '';
			element.style.transition = '';
			element.removeEventListener('transitionend', transitionEnd);
		});
	}

	// Helper function to replicate jQuery's slideUp
	function slideUp(element) {
		// Store the element's height
		var height = element.scrollHeight;
		
		// Set initial properties
		element.style.overflow = 'hidden';
		element.style.height = height + 'px';
		element.style.transition = 'height 0.3s ease';
		
		// Trigger reflow
		element.offsetHeight;
		
		// Set target height to 0
		element.style.height = 0;
		
		// Clean up and hide after animation completes
		element.addEventListener('transitionend', function transitionEnd() {
			element.style.display = 'none';
			element.style.height = '';
			element.style.overflow = '';
			element.style.transition = '';
			element.removeEventListener('transitionend', transitionEnd);
		});
	}

</script>
