<?php

namespace App\Http\Controllers\API\Admin;

use App\Http\Controllers\API\His\HisController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\Admin\MenuService;
use App\Services\Admin\UserService;
use Illuminate\Support\Facades\DB;
use JWT;

class BaseInfoController extends Controller
{
    //登录后获取admin后台自身菜单列表
    public function GetBaseMenuList(Request $request){
        $userid = $request->get('userid');//中间件产生的参数
        $group = $request->get('role');//中间件产生的参数
        $s=app()->make(MenuService::class);
       // dd($s->GetBaseMenuList(['userid'=>$userid,'group'=>$group]));
        return $s->GetBaseMenuList(['userid'=>$userid,'group'=>$group]);

    }
    //获取自身基本信息
    public function GetBaseUserInfo(Request $request){
        $userid = $request->get('userid');//中间件产生的参数
        $group = $request->get('role');//中间件产生的参数
        $s=app()->make(UserService::class);
        return  $s->GetDetail(['id'=>$userid]);

    }

    //检查用户是否有某个目录的权限
    public function CheckMenuAuth(Request $request){
        $userid = $request->get('userid');//中间件产生的参数
        $group = $request->get('role');//中间件产生的参数
        $url= request('url');
        $s=app()->make(UserService::class);
        return  $s->CheckMenuAuth(['userid'=>$userid,'group'=>$group,'url'=>$url]);
    }

    //切换默认科室
    public function ChangeDefaultDept(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $deptid = request('deptid');
        $user=DB::table('users')->where(['id'=>$userid,'status'=>1])->first();
        if(!$user) return \Yz::echoError1('无效用户');
        if (!isset($deptid)) return \Yz::echoError1('请选择科室');
        $dept = DB::table('s_department')->where(['id' => $deptid])->first();
        if (!$dept) return \Yz::echoError1('科室不存在');
        $user_group=$user->group;
        if($user_group!=1){
            $send_his_data=[
                'docCode'=>$user->username
            ];
            $res = HisController::Get("查询医生有权限的科室", $send_his_data);
            if ($res['code'] == 200) {
                $res_data = $res['data'];
                if(is_array($res_data)){
                    foreach ($res_data as $k=>$v){
                        if($v['deptCode']==$dept->department_number and !empty($v['deptType']) and in_array($user_group,[2,3])){
                            if($v['deptType']=="N") $user_group=3;//护士
                            if($v['deptType']=="I") $user_group=2;//病区护士
                        }
                    }
                }
            }
        }

        $jwt= new JWT();
        $accessTimeout = $jwt -> GetGetSecretTimeOut();
        $refreshTimeout = $jwt -> GetRefreshTokenTimeOut();
        $access_token = $jwt->BuildJWT('yz','access',$user->id,$user_group,$accessTimeout);
        $refresh_token = $jwt->BuildJWT('yz','refresh',$user->id,'',$refreshTimeout);

        $u= DB::table('users')->where('id', $userid)->update([
            'token'=>md5($refresh_token),
            'department_id' => $deptid,
            'group'=>$user_group,
            'updated_at' => date("Y-m-d H:i:s")
        ]);
        if($u){
            return \Yz::Return(true,'切换成功',['token'=>$access_token,'refresh_token'=>$refresh_token]);
        }else{
            return \Yz::echoError1('切换失败');
        }
    }
}
