<?php

namespace app\controller\admin\system;

use app\BaseController;
use app\entity\SysMenu;
use app\service\CurdService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

class MenuController extends BaseController
{
    /**
     * 查询菜单列表
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function list(): Json
    {
        $model = SysMenu::withSearch(['title', 'path', 'authority'], [
            'title' => $this->request->get('title/s', ''),
            'path' => $this->request->get('path/s', ''),
            'authority' => $this->request->get('authority/s', ''),
        ]);

        $lists = CurdService::getList($this->request, $model, ['sort_number' => 'asc']);

        return $this->writeSuccess('ok', $lists);
    }
    public function update()
    {
        $data = $this->request->put([
            'menuId'   => 0,
            'parentId' => 0,
            'title' => '',
            'menuType' => '',
            'openType' => '',
            'icon' => '',
            'path' => '',
            'component' => '',
            'authority' => '',
            'sortNumber' => 0,
            'hide' => 0,
            'meta' => '',
        ]);

        $user = SysMenu::findOrFail($data['menuId']);
        $user->save([
            'parent_id' => $data['parentId'],
            'title' => $data['title'],
            'menu_type' => $data['menuType'],
            'open_type' => $data['openType'],
            'icon' => $data['icon'],
            'path' => $data['path'],
            'component' => $data['component'],
            'authority' => $data['authority'],
            'sortNumber' => $data['sortNumber'],
            'hide' => $data['hide'],
            'meta' => $data['meta'],
        ]);
        return $this->writeSuccess('添加成功');
    }
    /**
     * 添加菜单列表
     * @return Json
     */
    public function add(): Json
    {
        $data = $this->request->post([
            'parentId' => 0,
            'title' => '',
            'menuType' => '',
            'openType' => '',
            'icon' => '',
            'path' => '',
            'component' => '',
            'authority' => '',
            'sortNumber' => 0,
            'hide' => 0,
            'meta' => '',
        ]);

        $user = new SysMenu();
        $user->save([
            'parent_id' => $data['parentId'],
            'title' => $data['title'],
            'menu_type' => $data['menuType'],
            'open_type' => $data['openType'],
            'icon' => $data['icon'],
            'path' => $data['path'],
            'component' => $data['component'],
            'authority' => $data['authority'],
            'sortNumber' => $data['sortNumber'],
            'hide' => $data['hide'],
            'meta' => $data['meta'],

        ]);
        return $this->writeSuccess('添加成功');
    }

    /**
     * 删除菜单
     * @param SysMenu $model
     * @return Json
     */
    public function remove(SysMenu $model)
    {
        $model->delete();

        return $this->writeSuccess('删除成功');
    }
}