<?php


namespace app\Admin\controller;
use think\Db;

class Menu extends Base
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     *获取用户菜单
     */
    public function MenuList(){
//        if($this->userinfo['role_id']==0){
//            return app_show(0,"获取成功",[]);
//        }
//        $action = Db::name("role")->where(["id"=>$this->userinfo['role_id'],"status"=>1])->find();
//        if(empty($action)){
//            return error_show(1004,"未找到角色权限");
//        }
        $data= Db::name("menu_action")->where(['status'=>1,"cstatus"=>1,"is_show"=>1])->order("weight desc,id asc,cweight desc,cid asc")->select();
        $list=[];
        $act=[];
        foreach ($data as $value){
            $list[$value["id"]]['menu_name']=$value['menu_name'];
            $list[$value["id"]]['menu_img']=$value['menu_img'];
            $list[$value["id"]]['menu_route']=$value['menu_route'];
            $list[$value["id"]]['status']=$value['status'];
            $temp = [];
            $temp['menu_name']=$value['cname'];
            $temp['menu_img']=$value['cmenu_img'];
            $temp['menu_route']=$value['cmenu_route'];
            $temp['menu_url']=$value['cmenu_url'];
            $temp['menu_url']=$value['cmenu_url'];
            $temp['status']=$value['cstatus'];
            $temp['is_private']=$value['cprivate'];
            $list[$value["id"]]['child'][$value['cid']]=$temp;
            $act[$value['id']][$value['cid']][]=$value['acode'];
            $list[$value["id"]]['child'][$value['cid']]['action']= $act[$value['id']][$value['cid']];

        }
        array_walk($list,function (&$value){
            $value['child']= array_values($value['child']);
        });
        return app_show(0,"获取成功",array_values($list));
     }

    /**
     *获取所有菜单列表
     */
     public function MenuAll(){
         $data = Db::name("admin_menu")->where(['pid'=>0,"is_show"=>1])->order("weight desc,id asc")->select();
         $list=[];
         foreach ($data as $key=>$value){
             $temp=[];
             $temp =  Db::name("admin_menu")->where(['pid'=>$value['id'],"is_show"=>1])->order("weight desc,id asc")
                 ->select();
             $value['child']=$temp;
             $list[]=$value;
         }
         return app_show(0,"获取成功",$list);
     }

     public function ActionMenu(){
         $pageid = isset($this->post['id']) ? intval($this->post['id']) : "";
         if($pageid==""){
             return error_show(1001,'页面id不能为空');
         }
         $menu = Db::name("admin_menu")->where(["id"=>$pageid])->find();
         if(empty($menu)){
             return error_show(1003,'页面数据未找到');
         }
         if($menu['is_show']==0){
             return error_show(1003,'页面数据已删除');
         }
         $condition = ['menuid'=>$pageid,];
         $data=Db::name('action')->alias("a")->Join("fc_action_list l","a.action_code=l.action_code","left")->field
         ("a.*,action_name")->where($condition)->select();
         return app_show(0,"获取成功",$data);
     }

    /**
     *
     */
    public function ActionSave(){

        $actid = isset($this->post['id']) ? intval($this->post['id']) : "";
        if($actid==""){
            return error_show(1001,'功能id不能为空');
        }
        $menuid = isset($this->post['menuid']) ? intval($this->post['menuid']) : "";
        if($menuid==""){
            return error_show(1001,'页面menuid不能为空');
        }
        $menu = Db::name("admin_menu")->where(["id"=>$menuid])->find();
        if(empty($menu)){
            return error_show(1003,'页面数据未找到');
        }
        if($menu['is_show']==0){
            return error_show(1003,'页面数据已删除');
        }
        $code = isset($this->post['action_code']) ? trim($this->post['action_code']) : "";
        $status = isset($this->post['status']) ? intval($this->post['status']) : 1;
        if($code==""){
            return error_show(1002,'功能code不能为空');
        }
        $action_info = Db::name("action_list")->where(["action_code"=>$code])->find();
        if(empty($action_info)){
            return error_show(1003,'功能数据未找到');
        }
        if($action_info['is_show']==0){
            return error_show(1003,'功能数据已删除');
        }
        $istrue =Db::name("action")->where(['menuid'=>$menuid,"action_code"=>$code])->find();
        if($istrue && $istrue['id']!=$actid){
            return error_show(1005,'此功能已存在');
        }
        try{
            $data = ['action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
            $result=Db::name("action")->where(["id"=>$actid])->update($data);
            if($result){
                write_log("菜单{$menu['menu_name']}功能{$action_info['action_name']}编辑成功",$this->userinfo,"orderaction",
                    "edit","0");
                return app_show(0,"更新成功");
            }else{
                return error_show(1004,"更新失败");
            }

        }catch (\Exception $e){
            return error_show(1003,$e->getMessage());
        }
    }

    /**
     * 功能状态修改
     */
    public function ActionStatus(){
        $actid = isset($this->post['id']) ? intval($this->post['id']) : "";
        if($actid==""){
            return error_show(1001,'页面功id不能为空');
        }
        $action =Db::name("action")->where(["id"=>$actid])->find();
        if(empty($action)){
            return error_show(1001,'页面功能未找到数据');
        }
        $action_info = Db::name("action_list")->where(["action_code"=>$action['action_code']])->find();
        if(empty($action_info)){
            return error_show(1003,'功能数据未找到');
        }
        if($action_info['is_show']==0){
            return error_show(1003,'功能数据已删除');
        }
        $menu = Db::name("admin_menu")->where(["id"=>$action['menuid']])->find();
        if(empty($menu)){
            return error_show(1003,'页面数据未找到');
        }
        if($menu['is_show']==0){
            return error_show(1003,'页面数据已删除');
        }
        $status = isset($this->post['status']) ? intval($this->post['status']) : 1;
        try{
            $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
            $result=Db::name("action")->where(["id"=>$actid])->update($data);
            $msg= $status==1?"启用":"禁用";
            if($result){
                write_log("菜单{$menu['menu_name']}功能{$action_info['action_name']}{$msg}成功",$this->userinfo,
                    "orderaction","status","0");
                return app_show(0,"更新成功");
            }else{
                return error_show(1004,"更新失败");
            }
        }catch (\Exception $e){
            return error_show(1003,$e->getMessage());
        }
    }

    public function ActionDel(){
        $actid = isset($this->post['id']) ? intval($this->post['id']) : "";
        if($actid==""){
            return error_show(1001,'页面功id不能为空');
        }
        $action =Db::name("action")->where(["id"=>$actid])->find();
        if(empty($action)){
            return error_show(1001,'页面功能未找到数据');
        }
        $action_info = Db::name("action_list")->where(["action_code"=>$action['action_code']])->find();
        if(empty($action_info)){
            return error_show(1003,'功能数据未找到');
        }
        if($action_info['is_show']==0){
            return error_show(1003,'功能数据已删除');
        }
        $menu = Db::name("admin_menu")->where(["id"=>$action['menuid']])->find();
        if(empty($menu)){
            return error_show(1003,'页面数据未找到');
        }
        if($menu['is_show']==0){
            return error_show(1003,'页面数据已删除');
        }
        $result=Db::name("action")->where(["id"=>$actid])->delete();
        if($result){
            write_log("菜单{$menu['menu_name']}功能{$action_info['action_name']}删除成功",$this->userinfo,"orderaction",
                "del","0");
            return app_show(0,"更新成功");
        }else{
            return error_show(1004,"更新失败");
        }
    }

    /**
     * @return \think\response\Json|void
     * @throws \think\exception\DbException
     */
    public function ActionAdd(){
        $pageid = isset($this->post['menuid']) ? intval($this->post['menuid']) : "";
        if($pageid==""){
            return error_show(1001,'页面id不能为空');
        }
        $code = isset($this->post['action_code']) ? trim($this->post['action_code']) : "";

        $status = isset($this->post['status']) ? intval($this->post['status']) : 1;
        if($code==""){
            return error_show(1002,'功能code不能为空');
        }
        $action_info = Db::name("action_list")->where(["action_code"=>$code])->find();
        if(empty($action_info)){
            return error_show(1003,'功能数据未找到');
        }
        if($action_info['is_show']==0){
            return error_show(1003,'功能数据已删除');
        }
        $menu = Db::name("admin_menu")->where(["id"=>$pageid])->find();
        if(empty($menu)){
            return error_show(1003,'页面数据未找到');
        }
        if($menu['is_show']==0){
            return error_show(1003,'页面数据已删除');
        }
        try{
            $where = ['menuid'=>$pageid,'action_code'=>$code];
            $true =Db::name("action")->where($where)->find();
            $data = ['menuid'=>$pageid,'action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s"),"addtime"=>date("Y-m-d H:i:s")];
            if($true){
                return error_show(1003,'此功能已存在');
            }else{
                Db::name("action")->insert($data);
                write_log("菜单{$menu['menu_name']}功能{$action_info['action_name']}新建成功",$this->userinfo,"orderaction",
                    "add","0");
                return app_show(0,"添加成功");
            }
        }catch (\Exception $e){
            return error_show(1005,$e->getMessage());
        }
    }

    public function MenuAdd(){

        $name = isset($this->post['menu_name']) ?trim($this->post['menu_name']) :"";
        if($name==""){
            return error_show(1002,"菜单名称不能为空");
        }
        $url = isset($this->post['menu_url']) ?trim($this->post['menu_url']) :"";
        $route = isset($this->post['menu_route']) ?trim($this->post['menu_route']) :"";
        $code = isset($this->post['menu_code']) ?trim($this->post['menu_code']) :"";
        $img = isset($this->post['menu_img']) ?trim($this->post['menu_img']) :"";
        $pid = isset($this->post['pid']) ?intval($this->post['pid']) :0;
        $private = isset($this->post['private']) ?intval($this->post['private']) :0;
        $weight = isset($this->post['weight']) ?floatval($this->post['weight']) :1;
        if($pid!=0 && $route==""){
            return error_show(1002,"子级菜单路由不能为空");
        }
        $data=[
            "menu_name"=>$name,
            "menu_url"=>$url,
            "menu_route"=>$route,
            "menu_code"=>$code,
            "menu_img"=>$img,
            "pid"=>$pid,
            "weight"=>$weight,
            "is_show"=>1,
            "is_private"=>$private,
            "status"=>1,
            "addtime"=>date("Y-m-d H:i:s"),
            "updatetime"=>date("Y-m-d H:i:s"),
        ];

        $result = Db::name("admin_menu")->insert($data);
        if($result){
            write_log("菜单{$data['menu_name']}新建成功",$this->userinfo,"order","add","0");
            return app_show(0,"添加成功");
        }else{
            return error_show(1003,"添加失败");
        }
    }

    /**
     * @return \think\response\Json|void
     * @throws \think\Exception
     */
    public function MenuEdit(){

        $id = isset($this->post['id']) ?intval($this->post['id']) :"";
        if($id!=""){
            $menu = Db::name("admin_menu")->where(["id"=>$id])->find();
            if($menu==false){
                return error_show(1003,"菜单不信息不存在");
            }
        }
        $name = isset($this->post['name']) ?trim($this->post['name']) :"";
        if($name==""){
            return error_show(1002,"菜单名称不能为空");
        }
        $url = isset($this->post['url']) ?trim($this->post['url']) :"";
        $route = isset($this->post['route']) ?trim($this->post['route']) :"";
        $code = isset($this->post['menu_code']) ?trim($this->post['menu_code']) :"";
        $img = isset($this->post['img']) ?trim($this->post['img']) :"";
        $pid = isset($this->post['pid']) ?intval($this->post['pid']) :0;
        $weight = isset($this->post['weight']) ?floatval($this->post['weight']) :1;
        $is_show = isset($this->post['is_show']) ? intval($this->post['is_show']) : 0;
        $private = isset($this->post['private']) ?intval($this->post['private']) :(isset($menu['is_private']) ?
            $menu['is_private'] : 0);
        if($pid!=0 && $route==""){
            return error_show(1002,"子级菜单路由不能为空");
        }
        $data=[
            "menu_name"=>$name,
            "menu_url"=>$url,
            "menu_route"=>$route,
            "menu_code"=>$code,
            "menu_img"=>$img,
            "pid"=>$pid,
            'is_show'=>$is_show,
            "is_private"=>$private,
            'status'=>1,
            "weight"=>$weight,
            "updatetime"=>date("Y-m-d H:i:s")
        ];
        if($id!=""){
            $result = Db::name("admin_menu")->where(["id"=>$id])->update($data);
            $msh ="编辑";
        }else{
            $result = Db::name("admin_menu")->insert($data); $msh ="新建";
        }
        if($result){
            write_log("菜单{$data['menu_name']}{$msh}成功",$this->userinfo,"order",$id==""?"add":"edit","0");
            return app_show(0,"{$msh}成功");
        }else{
            return error_show(1003,"{$msh}失败");
        }
    }
    public function MenuDel(){

        $id = isset($this->post['id']) ?intval($this->post['id']) :"";
        $menu =  Db::name("admin_menu")->where(["id"=>$id])->find();
        if($menu==false){
            return error_show(1003,"菜单不信息不存在");
        }
        $menu['is_show']=0;
        $menu['status']=0;
        $menu['updatetime']=date("Y-m-d H:i:s");
        $result =  Db::name("admin_menu")->update($menu);
        if($result){
            write_log("菜单{$menu['menu_name']}删除成功",$this->userinfo,"order","del","0");
            return app_show(0,"删除成功");
        }else{
            return error_show(1003,"删除失败");
        }
    }

    public function MenuStatus(){

        $id = isset($this->post['id']) ?intval($this->post['id']) :"";
        $menu = Db::name("admin_menu")->where(["id"=>$id])->find();
        if($menu==false){
            return error_show(1003,"菜单信息不存在");
        }
        $statu = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']) :"";
        if($statu===""){
            return error_show(1003,"菜单状态不能为空");
        }
        $menu['status']=$statu;
        $menu['updatetime']=date("Y-m-d H:i:s");
        $result = Db::name("admin_menu")->update($menu);
        $msg=$statu==1?"启用":"禁用";
        if($result){
            write_log("菜单{$menu['menu_name']}{$msg}成功",$this->userinfo,"order","status","0");
            return app_show(0,"状态更新成功");
        }else{
            return error_show(1003,"状态更新失败");
        }
    }
}