<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use App\Models\Employee;
use Session;
use Illuminate\Support\Facades\Auth;


class AuthController extends Controller
{

    protected $redirectTo = '/';
    protected $guard = 'employee';

    public function home(){
        if(Auth::guard("employee")->check()){
            return redirect()->route('steps');
        }
        return view('front.home', ['data' => array()]);
    }
    // Employee Login
    public function employee_login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'employee_id' => 'required',
            'password' => 'required',
        ]);
        
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }
        $credentials = array(
            'employee_id' => $request->employee_id,
            'password'=> $request->password, 
        );
        $user = Employee::where($credentials)->first();
        // dd($user);
        if (empty($user)) {
            return redirect()->back()->withErrors(array('erroors'=>'Information you provide is invalid'));
        }
        if($user->status != 1){
            return redirect()->back()->withErrors(array('erroors'=>'Employee Currentlly blocked'));
        }
        
        // Auth::login($user);
        Auth::guard('employee')->login($user);
        // dd(auth()->guard('employee')->user());
        // Auth::attempt($credentials);
        if (Auth::guard('employee')->check()) {
            if(empty(auth()->guard('employee')->user()->password)){
                return redirect()->route('employee-info')->withSuccess('Signed in');
            }else{
                return redirect()->route('steps')->withSuccess('Signed in');
            }
            
        }else{
            return redirect()->back()->withErrors(array('erroors'=>'Authentication failed!'));
        }
                
    }
   
    public function logout()
    {
        session()->flush();
        Auth::logout(); // logging out user
        return redirect()->route("home"); // redirection to login screen
    }


    
}
