<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/control/panel/menu';
//    protected $redirectTo = '/rejected/index/general';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    public function username(): string
    {
        return 'name';
    }

    /**
     * Handle a login request to the application.
     *
     * @param Request $request
     * @return array|JsonResponse|RedirectResponse|Response|\Symfony\Component\HttpFoundation\Response|void
     *
     * @throws ValidationException
     */
    public function login(Request $request)
    {
        $rule = [
            $this->username() => 'required|string',
            'password' => 'required|string',
        ];
        if($request['is_json']){
            $errors=Validator::make($request->all(),$rule)->errors();
            if($errors->count()>0){
                return ['success'=>false,'errors'=>$errors];
            }
        }else{
            $request->validate($rule);
        }

        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            if($request['is_json']){
                return ['success'=>false,'message'=>'登录请求过于频繁'];
            }
            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            if(env('DB_USERNAME')!='developer')
                app('LogService')->log(__METHOD__,__FUNCTION__,'',Auth::user()['id']);
            if($request['is_json']){
                return ['success'=>true,'url'=>url($this->redirectTo)];
            }
            return $this->sendLoginResponse($request)->header('Cache-Control','no-store');
        }

        $this->incrementLoginAttempts($request);
        if(env('DB_USERNAME')!='developer')
            app('LogService')->log(__METHOD__,__FUNCTION__,'',Auth::user()['id']);
        if($request['is_json']){
            return ['success'=>false,'errors'=>['name'=>['登录信息验证失败']]];
        }
        return $this->sendFailedLoginResponse($request);
    }
}
