<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class LoginController extends Controller
{
    public function list(Request  $request)
    {
        $page_index=$request->input('page_index');
        $page_size=$request->input('page_size');
        $num=$page_size*($page_index-1);
        $count=Admin::count();
        $list=Admin::orderBy('created_at')->skip($num)->take($page_size)->get();
        return $this->success_list($list,'',$count);
    }


    public function login(Request $request): \Illuminate\Http\JsonResponse
    {
        $rules=[
            'name' => 'required|max:12',
            'password' => 'required|min:6|max:18',
        ];
        $messages = [
            'name.required' => '管理员名称为必填项.',
            'name.max' => '管理员名称不能超过12个字符.',
            'password.required' => '密码为必填项.',
            'password.min' => '密码不能少于6个字符.',
            'password.max' => '密码不能超过18个字符.',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if($validator->fails()){
            return $this->error('400013',$validator->errors()->first());
        }
        $name = $request->input('name');
        $password = $request->input('password');
        if (Auth::guard('admins')->attempt(['name' => $name, 'password' => $password])) {
            $user=Admin::where('name',$request->input('name'))->first();
            $token = Auth::guard('admins')->fromUser($user);
            return $this->success([
                'access_token'=>$token,
                'token_type'=>'Bearer',
            ],'登录成功');
        }else{
            return $this->error();
        }
    }

    //注册管理员
    public function register(Request  $request): \Illuminate\Http\JsonResponse
    {
        $rules=[
            'name' => 'required|unique:admin|max:12',
            'password' => 'required|min:6|max:18',
        ];
        $messages = [
            'name.required' => '管理员名称为必填项.',
            'name.unique' => '管理员已存在.',
            'name.max' => '管理员名称不能超过12个字符.',
            'password.required' => '密码为必填项.',
            'password.min' => '密码不能少于6个字符.',
            'password.max' => '密码不能超过18个字符.',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if($validator->fails()){
            return $this->error('400013',$validator->errors()->first());
        }
        $name = $request->input('name');
        $password = $request->input('password');
        Admin::create([
            'name'=>$name,
            'password'=> bcrypt($password)
        ]);
        return $this->success([]);
    }

    //修改管理员信息
    public function edit(Request  $request): \Illuminate\Http\JsonResponse
    {
        $rules=[
            'name' => 'required|unique:admin|max:32',
            'password' => 'required|min:6|max:18',
        ];
        $messages = [
            'name.required' => '管理员名称为必填项.',
            'name.unique' => '管理员已存在.',
            'name.max' => '管理员名称不能超过32个字符.',
            'password.required' => '密码为必填项.',
            'password.min' => '密码不能少于6个字符.',
            'password.max' => '密码不能超过18个字符.',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if($validator->fails()){
            return $this->error('400013',$validator->errors()->first());
        }
        $name = $request->input('name');
        $password = $request->input('password');
        $admin_id = $request->input('admin_id');
        Admin::where('id',$admin_id)->update([
            'name'=>$name,
            'password'=> bcrypt($password)
        ]);
        return $this->success([]);
    }

    //获取用户信息
    public function getAuthUser(Request $request): \Illuminate\Http\JsonResponse
    {
//        return $this->success(Auth::guard('admins')->user());
        return $this->success(['user' => JWTAuth::parseToken()->touser()]);
    }

    //退出
    public function logout(Request $request): \Illuminate\Http\JsonResponse
    {
        try {
            JWTAuth::parseToken()->invalidate();

            return $this->success([]);
        } catch (JWTException $exception) {

            return $this->error();
        }
    }

    //删除管理员
    public function delete(Request  $request)
    {
        $admin_id=$request->input('admin_id');
        $res=Admin::where('id',$admin_id)->delete();
        if($res){
            return $this->success([]);
        }else{
            return $this->error();
        }
    }

    public function refresh()
    {
        return JWTAuth::parseToken()->refresh();
    }
}
