<?php

namespace App\Controller\admin;


use App\Controller\AbstractController;
use App\Helpers\TreeHelper;
use App\Model\AppAdminMenu;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\View\RenderInterface;

#[Controller(prefix: 'admin')]
class IndexController extends AbstractController
{
    #[RequestMapping(path: '', methods: 'get')]
    public function index(RenderInterface $render)
    {
        return $render->render('admin');
    }

    #[RequestMapping(path: 'menu-config', methods: 'get')]
    public function menuConfig()
    {
        $items = AppAdminMenu::orderBy('weight', 'DESC')->get()->toArray();

        $formatted_items = [];
        foreach ($items as $item) {
            $item['pid'] = (int)$item['pid'];
            $item['name'] = $item['title'];
            $item['value'] = $item['id'];
            $item['icon'] = $item['icon'] ? "layui-icon {$item['icon']}" : '';
            $formatted_items[] = $item;
        }
        return [
            'code' => 0,
            'data' => TreeHelper::getTree($formatted_items)
        ];

        return $data;
    }
}