<?php

use Hura8\Components\Product\AdminController\AProductCategoryController;

$objAProductCategoryController = new AProductCategoryController();

$category_collection = $objAProductCategoryController->getAllParent();

return [
    'category_list' => get_category_list(0, getRequest("id"), $level=1, $prefix="", $category_collection )
];


function get_category_list($parentId=0, $currentCat="",$level=1, $prefix="", $category_collection = array()){

    $categoryTree = "";
    $extra_space = "";
    for($i = 1; $i < $level; $i++){
        $extra_space .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    }

    $stt = 0;
    if(isset($category_collection[$parentId])) {

        foreach($category_collection[$parentId] as $index => $cat_info){
            $cat_id = $cat_info['id'];

            $stt ++;
            $imgUrl = (strlen($cat_info["thumbnail"]) > 2) ? " <img src=\"".$cat_info["thumbnail"]."\" style=\"max-width:30px; max-height:30px;\" />" : "";

            if($cat_info["status"]) $status = "<a href=\"javascript:update_status(".$cat_id.",'off')\" class='btn btn-soft btn-xs btn-outline btn-square'><i data-lucide='eye-off' class='size-3'></i></a>";
            else $status = "<a href=\"javascript:update_status(".$cat_id.",'on')\" class='btn btn-soft btn-xs btn-outline btn-square'><i data-lucide='eye' class='size-3'></i></a>";

            $edit_link ="/admin/product/category-form?id=".$cat_id;

            $hide_this = ($parentId > 0) ? "style='display: none;'" : '';

            $show_category_name = $cat_info["title"];

            if(!IS_DEFAULT_LANGUAGE && isset($cat_info["not_translated"]) && $cat_info["not_translated"]) {
                $show_category_name = "<span style='color: red'>[Chưa dịch]</span> ".$cat_info["title"];
            }

            if($cat_info['is_parent']) {
                $show_category_name = "<a href=\"javascript:;\" class='link-active' onclick=\"RowExpand.open_child('parent_".$cat_id."')\">".$show_category_name."</a>";
            }

            $categoryTree .= "
                <tr id='row_".$cat_id."' class='parent_".$parentId." row'  ". $hide_this ."
                    onmouseover=\"this.className='row-hover parent_".$parentId."'\" onmouseout=\"this.className='parent_".$parentId."'\">
                   
                    <td>
                        <a class='link-active' name='cat_".$cat_id."'></a>
                        ". $extra_space . $prefix . $stt.". ".  $show_category_name . $imgUrl."
                    </td> 

                     <td>
                        <a href='".$cat_info['request_path']."' class='link-active' title='Mở tại website' target='_blank'>Xem trang</a>
                    </td> 

                    <td>".$cat_id."</td>
                    <td class='whitespace-nowrap'>413787</td>
                    <td><a class='link-active' href='/admin/product?category=".$cat_id."'>1292</a></td>
                    <td>
                        <input class='input' type=text id=order_".$cat_id." value='".$cat_info["ordering"]."' size=2 onchange=\"update_order(".$cat_id.",this.value)\" /> 
                        <span class='status-ordering-".$cat_id."'></span>
                    </td> 
                    <td class='whitespace-nowrap'>
                        SP + Danh mục con
                    </td>
                    
            ";

            if(IS_DEFAULT_LANGUAGE) {
                $categoryTree .= "  
                     <td>
                        <a href='/admin/product/category-attribute?id=".$cat_id."&popup=1' class='pop-up'>Tổng</a> (".$cat_info['attribute_count'].")
                    </td>
                    <td>
                      <div class='list-btn flex items-center gap-3'>
                        <a href=\"".$edit_link."\" class='btn btn-soft btn-xs btn-square btn-outline  btn-view'><i data-lucide='pencil' class='size-3'></i></a>
                        <a class='btn btn-soft btn-xs btn-square btn-outline  btn-view' href='javascript:set_category_feature(.$cat_id.,'off')'><i data-lucide='crown' class='size-3'></i></a>
                        <span id=status-".$cat_id.">".$status."</span> 
                        <span class='status-delete-".$cat_id."'><a href=\"javascript:deleteThis(".$cat_id.")\" class='btn btn-soft btn-xs btn-square btn-error btn-outline '><i data-lucide='trash-2' class='size-3'></i></a></span>
                      </div>
                    </td> 
            ";

            }else{
                $categoryTree .= " 
                        <td> 
                            <a href=\"".$edit_link."\"><i data-lucide='pencil' class='size-3'></i></a>
                        </td> 
                ";
            }

            $categoryTree .= "  
                </tr>
            ";

            if($cat_info["is_parent"]) $categoryTree .= get_category_list($cat_id, $currentCat, $level + 1, $prefix . $stt.".", $category_collection);

        }
    }

    return $categoryTree;
}

