<?php

namespace App\Http\Livewire\Pages\Auth;

use Illuminate\Support\Facades\Auth;
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();

        // ddd($credentials);

        if(Auth::attempt($credentials, true)) {
            return redirect()->route('orders')->with('message', 'Login successful');
        } else {
            $this->addError('email', 'Invalid login credentials given');
            return;
        }
    }

    public function render()
    {
        return view('livewire.pages.auth.login');
    }
}
