<?php

namespace App\Api\V1\Controllers;
use Illuminate\Http\Request;

use Symfony\Component\HttpKernel\Exception\HttpException;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Auth;
use \App\User;
use DB;
class LoginController extends Controller
{
    /**
     * Log the user in
     *
     * @param LoginRequest $request
     * @param JWTAuth $JWTAuth
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(LoginRequest $request, JWTAuth $JWTAuth)
    {//'usertype',
        $credentials = $request->only(['Email', 'Password']);
        $token = false;
        try {
            $reflector = new \ReflectionClass('Auth');
            // $token = Auth::guard()->attempt($credentials);
            // $insert = \DB::connection('sqlsrv2')->statement('exec [ERV_SP_Insert_New_User] ?', [$credentials['username']]);
            $password=ERV_Encrypt($credentials['Password']);
            $user = \DB::connection('sqlsrv2')->select('exec [ERV_SP_User_Login_API] ?,?', [$credentials['Email'],$password]);

            // $user =User::from('tmuser as u')->where('Userid', $credentials['username'])
            //     ->where('UserPwd', ERV_Encrypt($credentials['password']))
            //     // ->whereNotIn('Active', ['T'])
            //     ->first();
            //     dd($user);
                // $resignFlag=DB::connection('sqlsrv2')
            // ->table('TMEMPLOYEE')
            // ->where('EmpCode', $credentials['username'])
            // ->whereNull('ResignDate')
            // ->get();
            // and ($resignFlag)
            $dummy = new \App\User;
            $users = $dummy->newInstance($user, true);
            $expred=0;
            $MMCode="";
            $UserType="";
          // dd($users);
            if(($user)){
                $token = $JWTAuth->fromUser($users);
                $expired = Auth::guard()->factory()->getTTL() * 60;
                $UserType= array_column($user, 'UserType');
                $MMCode= array_column($user, 'MMCode');
            }
            if(!$token) {
                $token='';
                $expired=0;
            }
         
        //     $dummy = new \App\User;
        //     $users = $dummy->newInstance($user, true);
        //     $expred=0;
        //   // dd($users);
        //     if(($user)){
        //         $token = $JWTAuth->fromUser($users);
        //         $expired = Auth::guard()->factory()->getTTL() * 60;
                
        //     }
        //     if(!$token) {
        //         $token='';
        //         $expired=0;
        //     }
            
        } catch (JWTException $e) {
            throw new HttpException(500);
        }

        return response()
            ->json([
                'status' => 'ok',
                'token' => $token,
                
                'user' => $user,
                'expires_in' => $expired
            ]);
    }

}
