<?php

namespace App\Controller\admin\api;

use App\Controller\AbstractController;
use App\Helpers\AppHelper;
use App\Helpers\TreeHelper;
use App\Model\AppAdminMenu;
use App\Model\AppArticle;
use App\Model\AppBrand;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: 'admin/api/menu')]
class MenuController extends AbstractController
{
    #[RequestMapping(path:'', methods: 'get')]
    public function index(): array
    {
        $request = $this->request;
        $query = AppAdminMenu::query()->orderBy('id');
        $pagination = $query->paginate($request->input('limit', 10), page: $request->input('page'));
        $data = $query->get()->toArray();
//        $data['cover'] = AppHelper::setImageUrl($query['cover']);

//        foreach ($data as &$v) {
//            $v['cover'] = AppHelper::setImageUrl($v['cover']);
//        }
        return ['code' => 0, 'msg' => 'ok', 'count' => $pagination->total(), 'data' => $data];
    }

    #[RequestMapping(path:'list', methods: 'get')]
    public function list(): array
    {
        $items = AppAdminMenu::query()->whereIn('type', [0, 1])->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)
        ];
    }

    /**
     * 新增菜单
     * @url /admin/api/menu/insert
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'insert', methods: 'post')]
    public function insert(): \Psr\Http\Message\ResponseInterface
    {
        $model = new AppAdminMenu();
        $model->setRawAttributes($this->request->post());
        $model->pid = $model->pid ?: 0;
        $model->save();
        return $this->response->json([
            'code' => 0,
            'msg' => 'ok'
        ]);
    }

    /**
     * 编辑菜单数据
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'update', methods: 'post')]
    public function update(): \Psr\Http\Message\ResponseInterface
    {
        $model = AppAdminMenu::find($this->request->post('id'));
        $model->setRawAttributes($this->request->post());
        $model->save();
        return $this->response->json([
            'code' => 0,
            'msg' => 'ok'
        ]);
    }

    /**
     * 预览菜单数据
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'view', methods: 'get')]
    public function view(): ResponseInterface
    {
        $id = $this->request->input('id');
        $model = AppAdminMenu::find($id)->toArray();
        return $this->response->json([
            'code' => 0,
            'data' => $model,
            'msg' => 'ok'
        ]);
    }
}