<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;

class AuthController extends Controller
{
    //
    public function LoginView()
    {
        return view('admin.pages.login');
    }
    public function ValidateLogin(Request $request)
    {
        // dd($request->all());
        $user = User::where(['email' => $request->email , 'password' => sha1($request->password)])->first();
        // dd(sha1($request->password));
        if($user)
        {
            $session = session()->put([
                'username' => $user->username,
                'email' => $user->email,
                'role' => $user->role,
                'id' => $user->id,
            ]);
            
            return redirect()->route('dashboard.index')->with('success','Login Successfully!');
        }
        elseif($request->email == "admin@gmail.com" && $request->password == "admin123")
        {
            $session = session()->put([
                'username' => 'admin',
                'email' => 'admin@gmail.com',
                'role' => 'admin',
                'id' => 001,
            ]);
            
            return redirect()->route('dashboard.index')->with('success','Login Successfully!');
        }
        else{
            return back()->with('error','Something went wrong!');
        }
    }
    
    

    public function Logout()
    {
        \Session::flush();
        return redirect()->route('admin.login.view');
    }
}
