<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\system\controller\v1;


use app\BaseController;
use app\model\system\SystemMenu;
use app\model\system\SystemRole;
use app\Request;
use library\services\UtilService;
use library\utils\Dtree;

class SysMenu extends  BaseController
{

    /**
     * 基本设置
     */
    public function list(){
        $menuMenu = new SystemMenu();
        $menus = $menuMenu->getArMenu();
        return app('json')->success($menus);
    }

    /**
     * 系统分类显示 | 关闭
     */
    public function status(Request $request) {
        [$id,$is_show] = UtilService::getMore([
            ['id','0','empty','参数错误'],
            ['is_show','0']
        ],$request,true);
        $bool = (new SystemMenu())->setStatus($id,$is_show);
        if($bool) {
            return app('json')->success('操作成功');
        } else {
            return app('json')->fail('提交失败');
        }
    }


    /**
     * 获取树级分类
     */
    public function treeList() {
        $menuAr = (new SystemMenu())->order("seq","desc")->select()->toArray();
        $data = sort_list_tier($menuAr, '顶级', 'pid', 'id');
        return app('json')->success($data);
    }

    /**
     * 获取详情栏目数据
     */
    public function info(Request $request){
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $data = (new SystemMenu())->field("title,id,icon,path,is_show,pid,code,seq")->find(compact('id'))->toArray();
        return app('json')->success($data);
    }

    /**
     * 保存数据
     */
    public function save(Request $request){
        $post = UtilService::getMore([
            ['title',''],
            ['pid','0'],
            ['seq','0'],
            ['path',''],
            ['icon',''],
            ['is_show','0'],
            ['code',''],
            ['id','0']
        ],$request);
        (new SystemMenu())->saveMenu($post);
        return app('json')->success("数据保存成功");
    }

    /**
     * 栏目删除
     * @param Request $request
     */
    public function del(Request $request) {
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $bool =   (new SystemMenu())->delMenu($id);
        return app('json')->success("栏目删除成功");
    }

}