<?php

namespace App\Livewire;

use Livewire\Component;
use Livewire\Attributes\Layout;

class Login extends Component
{

    public $email;
    public $password;

    protected $rules = [
        'email' => 'required',
        'password' => 'required',
    ];

    protected $messages = [
        'email.required' => 'La mail è obbligatoria',
        'password.required' => 'La password è obbligatoria',
    ];

    public $error = '';

    #[Layout('layouts.frontend')]
    public function render()
    {
        return view('livewire.login');
    }

    public function login()
    {

        $this->validate();
        
        if (\Auth::attempt(['email' => $this->email, 'password' => $this->password])) 
        {
        
            $user = \Auth::user();

            /*if (!$user->first_login_completed) 
            {
                $user->first_login_at = now();
                $user->save();

                return redirect('/first-login');
            }*/

            return redirect()->route('dashboard');
        } 
        else 
        {
            $this->error = 'ERRORE';
        }
    }
}
