<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Models\AdminUserModel;
use App\Services\Admin\AdminUser;
use App\Http\Controllers\Admin\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Validator, Auth;

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 = '/admin';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->_service = new AdminUser();
        $this->middleware('guest', ['except' => 'logout']);
    }
    /**
     * 重写登录视图页面
     * @author 晚黎
     * @date   2016-09-05T23:06:16+0800
     * @return [type]                   [description]
     */
    public function showLoginForm()
    {
        session()->put('is_we7','true');
        return view('admin.auth.login');
    }
    /**
     * 自定义认证驱动
     * @author 晚黎
     * @date   2016-09-05T23:53:07+0800
     * @return [type]                   [description]
     */
    protected function guard()
    {
        return auth()->guard('admin');
    }

    public function username()
    {
        return 'name';
    }

    public function wxLogin()
    {
        $store_id = session()->get('store_id');
        // dd($store_id);die;
        $wUser = session()->get('wUser');
        $wAccount = session()->get('wAccount');
        if ($wUser['groupid'] == 0) {
            $res = AdminUserModel::where([['name', 'album' . $wUser['name']],['is_root', 1]])->first();
        } else {
            $res = AdminUserModel::where([['name', 'album' . $wUser['name']],['store_id', $store_id]])->first();
        }
        session()->put('store_id', $store_id);
        if (!$res) {
            $createUser['name'] = 'album' . $wUser['name'];
            $createUser['real_name'] = 'album' . $wUser['username'];
            $createUser['password'] = bcrypt('qweqwe');
            $createUser['type'] = 0;
            $createUser['status'] = 1;
            $createUser['is_root'] = $wUser['groupid'] == 0 ? 1 : null;
            $createUser['store_id'] = $store_id;
            $createUser['admin_role_id'] = 7;
            $res = AdminUserModel::create($createUser);
        }

        if (Auth::guard('admin')->loginUsingId($res->id)) {

            $userAuth = Auth::guard('admin')->user();
            session()->put('is_we7','true');
            return redirect('?route=Admin/Base/Index/index');
        }
        //dd('success');
    }

    public function login(\Illuminate\Http\Request $request)
    {

        $validator = Validator::make($data = $request->all(),
            [
                'name' => 'required', 'password' => 'required',
            ],
            [
                'name.required'=>'请输入用户名',
                'password.required'=>'请输入密码'
            ]
        );

        if ($validator->fails()) {
            $msg = $validator->messages()->first();
            return $this->showWarning($msg);
        }
        $res = $this->_service->login($request->name, $request->password);
        if($res) {
            session()->put('is_we7','true');
            return redirect('?route=Admin/Base/Index/index');

        }else{
            $msg = $this->_service->getMsg();
            $this->showWarning($msg);
        }

        return redirect('?route=Admin/Auth/Login/showLoginForm');
    }
    public function logout()
    {
        Auth::guard('admin')->logout();
        return redirect('?route=Admin/Auth/Login/showLoginForm');

    }
}
