<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\common\api\MenuApi;

/**
 * Description of Menu
 *
 * @author sgq
 */
class Menu extends AdminController {

    /**
     * @auth {{/menu}}
     * @return type
     */
    function index() {
        return view();
    }

    /**
     * @auth {{/menu/list}}
     * @return type
     */
    function list() {
        $menus = MenuApi::getList($this->request->param());
        return json($menus);
    }

    /**
     * @auth {{/menu/add}}
     * @return type
     */
    function add() {
        if ($this->request->isPost()) {
            try {
                $params = $this->request->param();
                validate(\app\admin\validate\Menu::class)->check($params);
                if (MenuApi::save($this->request->param())) {
                    return json(["msg" => "添加成功"]);
                }
                return json(["msg" => "添加失败"]);
            } catch (\think\Exception $e) {
                return json(["msg" => $e->getMessage()]);
            }
        }
        return view();
    }

    /**
     * @auth {{/menu/edit}}
     * @return type
     */
    function edit() {
        $params = $this->request->param();
        $id = $params["id"];
        if ($this->request->isPost()) {
            try {
                if (!$id)
                    return json(["msg" => "没有选择菜单"]);
                validate(\app\admin\validate\Menu::class)->check($params);
                if (MenuApi::save($this->request->param())) {
                    return json(["msg" => "修改成功"]);
                }
                return json(["msg" => "修改失败"]);
            } catch (\think\Exception $e) {
                return json(["msg" => $e->getMessage()]);
            }
        }
        $menu = MenuApi::getOne($id);
        return view("", ["menu" => $menu]);
    }

    /**
     * @auth {{/menu/remove}}
     * @return type
     */
    function delete() {
        if ($this->request->isPost()) {
            $id = $this->request->param("id");
            if (MenuApi::delete($id))
                return json(["msg" => "删除成功"]);
            return json(["msg" => "删除失败"]);
        }
    }

    function selectMenuTreeList() {
        $list = \app\common\api\MenuApi::getAllMenus("id,code,pcode,name,num,levels,ismenu");
        foreach ($list as $key => $item) {
            if ($item["pcode"] == "0") {
                $list[$key]["open"] = true;
            }
        }
        $format_list[] = ["code" => 0, "pcode" => "", "name" => "顶级", "open" => true, "children" => $list];
        return $format_list;
    }

    /**
     * 树形
     * @return type
     */
    function treelist() {
        $id = $this->request->param("id");
        $list = getTreeList(\app\common\api\MenuApi::getPrivilagesByRoleid($id));
        $format_list = [];
        foreach ($list as $item) {
            $format_list[] = [
                "checked" => $item["checked"],
                "id" => $item["id"],
                "isOpen" => true,
                "name" => $item["name"],
                "open" => $item["pid"] == 0 ? true : false,
                "pId" => $item["pid"]
            ];
        }
        return $format_list;
    }

}
