<?php

namespace app\adminapi\controller;

use app\adminapi\model\AuthRule;
use app\common\controller\AdminApi;
use think\Cookie;
use think\Hook;


/**
 * api权限菜单管理
 *
 *
 */
class Rule extends AdminApi
{
    protected $model = null;


    /**
     * 初始化操作
     * @access protected
     */
    public function _initialize()
    {
        $this->model = new AuthRule;
        parent::_initialize();
    }



    /**
     * 所有权限列表
     *
     * @ApiMethod (GET)
     * @ApiParams (name="is_tree", type="string", required=true, description="是否是树形结构")
     */
    public function index()
    {
        $admin_id = $this->auth->id;
        $is_tree = $this->request->get('is_tree');

        $menulist = $this->model->getMenulist($admin_id,[],$is_tree,true);

        $this->success('查询成功', $menulist);
    }


    /**
     * 添加权限节点（接口或菜单）
     *
     * @ApiMethod (POST)
     * @ApiParams (name="pid", type="int", required=true, description="父ID")
     * @ApiParams (name="ismenu", type="int", required=true, description="是否为菜单 0接口 1菜单")
     * @ApiParams (name="name", type="string", required=true, description="节点URL（节点URL和外链选填其一）")
     * @ApiParams (name="url", type="string", required=true, description="外链（节点URL和外链选填其一）")
     * @ApiParams (name="rule_name", type="string", required=true, description="权限标识（菜单才需要）")
     * @ApiParams (name="title", type="string", required=true, description="节点中文名")
     * @ApiParams (name="icon", type="string", required=true, description="图标（菜单才需要）")
     * @ApiParams (name="weigh", type="int", required=true, description="权重")
     * @ApiParams (name="menutype", type="string", required=true, description="菜单类型：'addtabs','blank','dialog','ajax'")
     * @ApiParams (name="extend", type="string", required=true, description="额外扩展属性（比如加类名做特特殊回调逻辑）")
     * @ApiParams (name="status", type="string", required=true, description="状态（normal=正常，hidden=隐藏）")
     *
     */
    public function add()
    {
        $admin_id = $this->auth->id;
        $params = $this->request->post();
        if(empty($params["status"]))$params["status"] = "normal";
        if(empty($params["weigh"]))$params["weigh"] = 0;
        if(empty($params["menutype"]))$params["menutype"] = "addtabs";

        try{
            $menulist = $this->model->add($params,true);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }


        $this->success('添加成功', $menulist);
    }




    /**
     * 编辑权限节点（接口或菜单）
     *
     * @ApiMethod (POST)
     * @ApiParams (name="pid", type="int", required=true, description="父ID")
     * @ApiParams (name="ismenu", type="int", required=true, description="是否为菜单 0接口 1菜单")
     * @ApiParams (name="name", type="string", required=true, description="节点URL（节点URL和外链选填其一）")
     * @ApiParams (name="url", type="string", required=true, description="外链（节点URL和外链选填其一）")
     * @ApiParams (name="rule_name", type="string", required=true, description="权限标识（菜单才需要）")
     * @ApiParams (name="title", type="string", required=true, description="节点中文名")
     * @ApiParams (name="icon", type="string", required=true, description="图标（菜单才需要）")
     * @ApiParams (name="weigh", type="int", required=true, description="权重")
     * @ApiParams (name="menutype", type="string", required=true, description="菜单类型：'addtabs','blank','dialog','ajax'")
     * @ApiParams (name="extend", type="string", required=true, description="额外扩展属性（比如加类名做特特殊回调逻辑）")
     * @ApiParams (name="status", type="string", required=true, description="状态（normal=正常，hidden=隐藏）")
     *
     */
    public function edit($id=null)
    {
        $admin_id = $this->auth->id;
        $params = $this->request->post();
        if(empty($params["status"]))$params["status"] = "normal";
        if(empty($params["weigh"]))$params["weigh"] = 0;
        if(empty($params["menutype"]))$params["menutype"] = "addtabs";
        try{
            $menulist = $this->model->edit($id,$params,true);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }




        $this->success('编辑成功', $menulist);
    }




    /**
     * 查看详情
     *
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="string", required=true, description="规则详情ID")
     */
    public function detail()
    {
        $admin_id = $this->auth->id;
        $id = $this->request->get('id/d');

        try{
            $menulist = $this->model->detail($id,$show_field=[],$except_field=[]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }



        $this->success('查询成功', $menulist);
    }



    /**
     * 删除规则
     *
     * @ApiMethod (GET)
     * @ApiParams (name="ids", type="string", required=true, description="规则详情ID,多值逗号分割")
     */
    public function del()
    {
        $admin_id = $this->auth->id;
        $ids = $this->request->get('ids/s');

//        var_dump($ids);
        try{
            $menulist = $this->model->del($ids,true);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }



        $this->success('执行成功', ["count"=>$menulist]);
    }









}