<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\Models\Vendor;
use Auth;
use Validator;
use App\Models\Notice;

class LoginController extends Controller
{

    public function showLoginForm()
    {
        $notices = Notice::where('created_at', '>=', now()->subDays(3))->get();
        return view('auth.vendor_login', compact('notices'));
    }

    public function vendorsignin(Request $request)
    {
        $validator = Validator::make(
            ['email' => $request->email, 'password' => $request->password],
            ['email' => 'required', 'password' => 'required']
        );
    
        if ($validator->fails()) {
            return redirect()->route('vendorlogin')->withErrors($validator)->withInput();
        } else {
            $vendor = Vendor::where('email', $request->email)->first();
    
            if ($vendor && Hash::check($request->password, $vendor->password)) {
                if ($vendor->status == 'inactive') {
                    return redirect()->route('vendorlogin')->with('error', 'Account belongs to these credentials is inactive, please contact the administrator');
                } elseif ($vendor->status == 'deleted') {
                    return redirect()->route('vendorlogin')->with('error', 'Account belongs to these credentials is deleted, please contact the administrator');
                } else {
                    Auth::login($vendor);
                    return redirect()->route('vendordashboard')->with('success', 'Login successfully');
                }
            } else {
                return redirect()->route('vendorlogin')->with('error', 'Invalid credentials, please check credentials');
            }
        }
    }
    
    

    public function logout(Request $request)
    {
        Auth::logout();
        return redirect()->route('vendorlogin')->with('success', 'Logout successfully');
    }
}

?>