<?php
/**
 * User: nickbai
 * Date: 2017/10/23 13:33
 * Email: 1902822973@qq.com
 */
namespace app\admin\controller;

class Menu extends Base
{
    // 菜单列表
    public function index()
    {
//        $token = 'NzgyNTUwIyRAJSFeKi8xNTYxNTQwMzQzLzE=';
//        $res = model('Admins')->checktoken($token);
//        if($res == -1){
//            return $res;
//        }
//        $admin_id = explode('/',base64_decode($token))['2'];
//        $adminInfo = db('admins')->where('id', $admin_id)->find();

        if (request()->isAjax()) {

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $result = db('menu')->limit($offset, $limit)->select();
            //一级菜单
            $pmenu = db('menu')->where('p_id',0)->select();
            foreach ($result as $key => $vo) {
                //$vo['p_menu'] == '11';
                for($i=0;$i<count($pmenu);$i++){
                    if($result[$key]['p_id'] == $pmenu[$i]['id']){
                        $result[$key]['p_menu'] = $pmenu[$i]['name_cn'];
                    }
                }
                // 生成操作按钮
                if (0 != $vo['id']) {
                    $result[$key]['operate'] = $this->makeBtn($vo['id']);
                }
            }

            $return['total'] = db('menu')->count();  //总数据
            $return['rows'] = $result;

            return json($return);

        }
        return $this->fetch();
    }

    // 添加菜单
    public function add()
    {
        if (request()->isPost()) {

            $param = input('post.');
            $has = db('menu')->where('name_cn', $param['name'])->find();
            if (!empty($has)) {
                return json(['code' => -1, 'data' => '', 'msg' => '该菜单已经存在']);
            }
            if($param['p_id'] == -1){
                return json(['code' => -2, 'data' => '', 'msg' => '请选择父级菜单']);
            }
            $info = array();
            $powerinfo = array();
            $info['name_cn'] = $param['name'];
            $info['p_id'] = $param['p_id'];
            $info['sort'] = $param['sort'];
            if($param['p_id'] == 0){
                $info['ico'] = $param['ico'];
            }else{
                $info['url'] = $param['url'];
            }
            $admingroup = db('admingroup')->where('group_name', '超级管理员')->find();
            try {
                $menu_id = db('menu')->insertGetId($info);
                $powerinfo['group_id'] = $admingroup['group_id'];
                $powerinfo['menu_id'] = $menu_id;
                db('admingrouppower')->insert($powerinfo);
            } catch (\Exception $e) {
                return json(['code' => -3, 'data' => '', 'msg' => $e->getMessage()]);
            }
            return json(['code' => 1, 'data' => '', 'msg' => '添加菜单成功']);
        }
        $pmenu = db('menu')->where('p_id',0)->select();
        $this->assign([
            'pmenu' => $pmenu,
            'status' => config('kf_status')
        ]);
        return $this->fetch();
    }

    // 编辑菜单
    public function edit()
    {
        // 菜单id
        $id = input('param.id/d');
        if (request()->isAjax()) {
            $param = input('post.');
            $param['name_cn'] = $param['name'];
            $info = array();
            $info['name_cn'] = $param['name'];
            if(!empty($param['p_id'])){
                $info['p_id'] = $param['p_id'];
            }
            if(!empty($param['url'])){
                $info['url'] = $param['url'];
            }
            if(!empty($param['ico'])){
                $info['ico'] = $param['ico'];
            }
            $info['sort'] = $param['sort'];
            try {
                db('menu')->where('id', $param['id'])->update($info);
            } catch (\Exception $e) {
                return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
            }
            return json(['code' => 1, 'data' => '', 'msg' => '编辑菜单成功']);
        }
        $menu = db('menu')->where('id',$id)->find();
        $pmenu = db('menu')->where('p_id',0)->select();

        $this->assign([
            'menu' => $menu,
            'pmenu' => $pmenu,
            'id' => $id,
            'status' => config('kf_status')
        ]);
        return $this->fetch();
    }

    // 删除菜单
    public function delMenu()
    {
        if (request()->isAjax()) {
            //菜单id
            $id = input('param.id/d');
            $menu = db('menu')->where('id', $id)->find();

            try {
                if($menu['p_id'] == 0){
                    //父级菜单下的子集菜单id
                    $menus = db('menu')->where('p_id', $id)->select();
                    for($i=0;$i<count($menus);$i++){
                        $ids[] = $menus[$i]['id'];
                    }
                    $ids = implode(",",$ids) ;
                    //删除父级菜单下的子集菜单
                    db('menu')->where('p_id', $id)->delete();
                    db('admingrouppower')->where('menu_id', $ids)->delete();
                }
                db('menu')->where('id', $id)->delete();
                db('admingrouppower')->where('menu_id', $id)->delete();
            } catch (\Exception $e) {
                return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
            }
            return json(['code' => 1, 'data' => '', 'msg' => '删除菜单成功']);
        }
    }

    // 生成菜单操作按钮
    private function makeBtn($id)
    {
        $operate = '<a href="' . url('menu/edit', ['id' => $id]) . '">';
        $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';

        $operate .= '<a href="javascript:menuDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
        $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';

        return $operate;
    }
}
