<?php
/**
 * File AuthController.php
 *
 * @author Tuan Duong <bacduong@gmail.com>
 * @package Laravue
 * @version 1.0
 */
namespace App\Http\Controllers;

use App\Laravue\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use App\Http\Resources\UserResource;
use Illuminate\Support\Facades\DB;
use App\Laravue\Models\Users;
/**
 * Class AuthController
 *
 * @package App\Http\Controllers
 */
class AuthController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @ 管理员登入
     */
    //
    public function login(Request $request , Users $users)
    {
        $userOrAdmin= $request->input('userOrAdmin');
        $credentials = $request->only('email', 'password');
        if($userOrAdmin == 1 ){ //用户
            $BackgroundUser = $users->where([['email','=',$credentials['email']],['isadmin','=','1']])->first();
            if (!Auth::attempt($credentials)) {
                return response()->json(new JsonResponse([], 'login_error'), Response::HTTP_UNAUTHORIZED);
            }
            if($BackgroundUser != null){
                $BackgroundUser = $users->where([['email','=',$credentials['email']],['isadmin','=','1']])->first()->toArray();
                if($BackgroundUser['email'] != $credentials['email']){
                    $user = $request->user();
                    $tokenResult = $user->createToken('Personal Access Token');
                    $token = $tokenResult->token;
                    $token->save();
                    return response()->json(new UserResource($user), Response::HTTP_OK)->header('authorization', $tokenResult->accessToken);
                }else{
                    return response()->json(new JsonResponse([], '请用管理员窗口登入'), Response::HTTP_UNAUTHORIZED);
                }
            }else{
                $user = $request->user();
                $tokenResult = $user->createToken('Personal Access Token');
                $token = $tokenResult->token;
                $token->save();
                return response()->json(new UserResource($user), Response::HTTP_OK)->header('Authorization', $tokenResult->accessToken);
            }

        }else{
            $isadmin = $users->where([['email','=',$credentials['email']],['isadmin','=','2']])->first();
            if ($isadmin) {
                return response()->json(new JsonResponse([], '请用用户窗口登入'), Response::HTTP_UNAUTHORIZED);
            }
            if (!Auth::attempt($credentials)) {
                return response()->json(new JsonResponse([],'login_error'), Response::HTTP_UNAUTHORIZED);
            }
            $user = $request->user();
            $tokenResult = $user->createToken('Personal Access Token');
            $token = $tokenResult->token;
            $token->save();
            return response()->json(new UserResource($user), Response::HTTP_OK)->header('authorization', $tokenResult->accessToken);
        }
    }
    public function logout(Request $request)
    {
       $delectuser = \Session::forget('loginuid');
        if($delectuser){
            $request->user()->token()->revoke();
            return response()->json((new JsonResponse())->success([]), Response::HTTP_OK);
        }else{
            return 500;
        }
    }

    public function user()
    {
        return new UserResource(Auth::user());
    }
}
