<?php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    public function showLoginForm()
    {
        return view('auth.login');
    }

    public function login(Request $request)
{
    // Validasi input
    $request->validate([
        'email' => 'required|email',
        'password' => 'required'
    ]);

    // Cek kredensial login
    if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
        // Setelah login berhasil, redirect ke dashboard admin
        $request->session()->flash('login_success', 'Login berhasil!');
        return redirect()->route('admin.dashboard');  // Redirect ke dashboard admin
    }    

    // Jika login gagal
    return back()->withErrors(['email' => 'Login gagal, cek kembali email dan password!']);
}

public function showDashboard()
{
    // Flash session untuk notifikasi pop-up setelah masuk ke dashboard
    session()->flash('welcome_message', 'Selamat datang di Dashboard Admin!');
    
    return view('admin.dashboard');
}

    public function logout(Request $request)
    {
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return redirect('/');
    }
    
}