<?php

namespace App\Http\Controllers\AdminAuth;

use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth; 
use Illuminate\Http\Request; 
use App\Models\Admin;
 
use Session;

class LoginController extends Controller
{
  
    public $redirectTo = '/admin/dashboard';


    public function __construct()
    {
        $this->middleware('admin.guest', ['except' => 'logout']);
    }

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        return view('admin.auth.admin_login');
    }

    


    public function login(Request $request)
    {
        $admin = Admin::where('email',$request->email)->first();

        if(!$admin){
            return redirect('/admin-login')->with('error','Unknown Email address!');
        }elseif($admin->status){

            $credentials = [
                'email' => $request->email,
                'password' => $request->password
            ];
            
            if (Auth::guard('admin')->attempt($credentials)){

                return redirect('admin/dashboard');

            }else{
                
                return redirect('/admin-login')->with('error','Wrong Email/Password combination');

            }

        }else{
            return redirect('/admin-login')->with('error', 'Account Status is not Activated!');
        }
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard('admin');
    }

    public function logout(){  
        $this->guard('admin')->logout();
        return redirect('/admin-login'); 
    }

    public function showRegisterForm()
    {
        return view('admin.auth.register');
    }

    
}
