<?php
namespace App\Services\Admin;
use Illuminate\Support\Facades\DB;
class MenuService
{
    public function GetBaseMenuList($arr){
        $result=array();
        if($arr['userid']=='search'){
            $menulist=DB::select("select b.id,b.pid,b.order,b.icon, b.name,b.url from group_menu as a left join menu as b on a.menu_id=b.id where a.group_id =? and b.status=1 order by `order` ",[$arr['group']]);
            $result['list']=$menulist;
            $result['status']='ok';
        }else{
            $query=DB::select("select `group` from users   where id =? ",[$arr['userid']]);
            if($query[0]->group==$arr['group']){
                $menulist=DB::select("select b.id,b.pid,b.order,b.icon, b.name,b.url from group_menu as a left join menu as b on a.menu_id=b.id where a.group_id =? and b.status=1  order by `order`",[$arr['group']]);

                $result['list']=$menulist;
                $result['status']='ok';

            }else{
                $result['status']='no';
                $result['msg']='权限不匹配';
            }
        }

        return $result;
    }

    public function GetList($arr){
        $result=array();
        $sql='';
        if( $arr['type']=='enable'){
            $sql=" and  status=1";
        }
        $query_p = DB::select("select * from menu where pid is null".$sql);
        $result['list'] = [];
        $i=0;
        foreach ($query_p as $item) {
            $query = DB::select("select * from menu where pid = ?".$sql, [$item->id]);
            $child_items = [];
            $result['list'][$i] = [
                'id' => $item->id,
                'pid' => $item->pid,
                'name' => $item->name,
                'url' => $item->url,
                'icon' => $item->icon,
                'created_at' => $item->created_at,
                'updated_at' => $item->updated_at,
                'children' => [] // 子级节点先为空数组，稍后再填充
            ];
            $j=0;
            foreach ($query as $child) {
                $child_items[$j] = [
                    'id' => $child->id,
                    'pid' => $child->pid,
                    'name' => $child->name,
                    'url' => $child->url,
                    'icon' => $child->icon,
                    'created_at' => $child->created_at,
                    'updated_at' => $child->updated_at,
                 //   'children' => [] // 子级节点先为空数组，稍后再填充
                ];
                $result['list'][$i]['children'][$j]=$child_items[$j];
                $j++;
            }

            $i++;
        }

      return $result;
    }
    public function GetFatherMenuList(){
        $result=array();
        $list=DB::table('menu')->where(['status'=>1,'pid'=>null])->get();
        if(count($list)){
            $result['status']='ok';
            $result['msg']='获取成功';
            $result['list']=$list;
        }else{
            $result['status']='no';
            $result['msg']='未找到有效一级菜单';
        }
        return $result;
    }
    public function AddMenu($arr){
        $result=array();
        //dd($arr);
        $i=DB::table('menu')->insert([
            'pid' => $arr['info']['pid'],
            'name' => $arr['info']['name'],
            'url' => $arr['info']['url'],
            'status'=>1
        ]);
        if($i){
            $result['status']='ok';
            $result['msg']='插入成功';
        }else{
            $result['status']='no';
            $result['msg']='操作失败';
        }
        return $result;
    }
    public function EditMenu($arr){
        $result=array();
        $U=DB::table('menu')
            ->where('id', $arr['info']['id'])
            ->update([
                'pid' => $arr['info']['pid'],
                'name' => $arr['info']['name'],
                'url' => $arr['info']['url'],
                'icon' => $arr['info']['icon']
            ]);
        if($U){
            $result['status']='ok';
            $result['msg']='更新成功';
        }else{
            $result['status']='no';
            $result['msg']='操作失败';
        }
        return $result;
    }
}
