<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Modules\Admin\Services\FilterService;
use Modules\Admin\Services\SettingService;
use DB;
class LoginController extends Controller
{
    use AuthenticatesUsers;

    /**
     * 登陆后的跳转页面
     *
     * @var string
     */
    protected $redirectTo = '/admin';

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

    //登录页面
    public function loginView(Request $request)
    {
        $filter = FilterService::check_ip();
        if (Auth::guard('web')->check()) {
            return redirect('/admin');
        }

        return view('admin::login');
    }

    //登录动作
    public function doLogin(Request $request)
    {
        $request['password'] = base64_decode($request->input('password'));
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);
            return $this->sendLockoutResponse($request);
        }

        $valid = validPass($request['password']);
        if (is_string($valid)) {
            $this->guard()->logout();
            $request->session()->invalidate();
            return [
                'code'    => 1,
                'message' => '密码限制：' . $valid . '，请找管理员修改',
            ];
        }

        if ($this->attemptLogin($request)) {
//            $roles = $this->guard()->user()->getRoleNames()->toArray();
//            if (count($roles) == 0 || (count($roles) == 1 && $roles[0] == 'PuTongZhiYuan')) {
//                $this->guard()->logout();
//                $request->session()->invalidate();
//                return [
//                    'code'    => 1,
//                    'message' => '没有登录权限',
//                ];
//            }
            $token = $request->input('staff_num') . "," . $request->input('password') . "," . $request->getClientIp();
            $token = base64_encode($token);
            setcookie("token", $token, time() + 7200, "/", ".nxjiewei.com");
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);
        return $this->sendFailedLoginResponse($request);
    }

    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();
        $this->clearLoginAttempts($request);
        return [
            'code'    => 0,
            'message' => '登录成功',
        ];
    }

    /**
     * 用户名验证字段
     * @return string
     */
    public function username()
    {
        return 'staff_num';
    }
}