<?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']],['password','=',sha1($credentials['password'])],['state','=','2']])->first();
            if (Auth::attempt($credentials)) {
                return response()->json(new JsonResponse([], 'login_error'), Response::HTTP_UNAUTHORIZED);
            }
            if($BackgroundUser){
                $BackgroundUser = $request->user();
                print_r($BackgroundUser);
                die();
                $tokenResult = $BackgroundUser->createToken('Personal Access Token');
                $token = $tokenResult->token;
                $token->save();
                return response()->json(new UserResource($BackgroundUser), Response::HTTP_OK)->header('Authorization', $tokenResult->accessToken);
            }

        }else{
            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)
    {
        $request->user()->token()->revoke();
        return response()->json((new JsonResponse())->success([]), Response::HTTP_OK);
    }

    public function user()
    {
        return new UserResource(Auth::user());
    }
    // 用户登入接口
    public function userslogin(Request $request , Users $users){
        $email = $request->input('email');
        $password = $request->input('password');
//        if (!Auth::attempt($credentials)) {
//            return response()->json(new JsonResponse([], 'login_error'), Response::HTTP_UNAUTHORIZED);
//        }

        if(is_null($email) || is_null($password)){
           return  $this->jsonErrorData(105,'用户名或密码不能为空');
        }
        $BackgroundUser = $users->where([['email','=',$email],['password','=',sha1($password)],['state','=','2']])->first();
        if ($BackgroundUser){
            $tokenResult = $BackgroundUser->createToken('Personal Access Token');
            $token = $tokenResult->token;
            $token->save();
            return response()->json(new UserResource($BackgroundUser), Response::HTTP_OK)->header('Authorization', $tokenResult->accessToken);

        }else{
            return response()->json(new JsonResponse([], 'login_error'), Response::HTTP_UNAUTHORIZED);
        }
    }
}
