<?php

namespace App\Http\Livewire\Pages\Admin\Auth;

use Illuminate\Support\Facades\Session;
use Livewire\Component;

class Login extends Component
{
    public $email;
    public $password;

    public function mount()
    {
        //todo
    }

    public function rules()
    {
        return [
            'email' => ['required', 'string', 'email'],
            'password' => ['required', 'string']
        ];
    }

    public function updated($property)
    {
        $this->validateOnly($property);
    }

    public function login()
    {
        $credentials = $this->validate();

        if($credentials['email'] == 'test5@email.com' && $credentials['password'] == 'message24') {

            Session::put('grant_admin', 'authorize');
            return redirect()->route('admin')->with('message', 'Login successful');
        } else {

            $this->addError('email', 'Invalid login credentials given');
            return;
        }
    }
    public function render()
    {
        return view('livewire.pages.admin.auth.login')->layout('layouts.admin');
    }
}
