<?php

namespace App\Livewire\Auth;

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

class Login extends Component
{
    public $login_identifier = '';
    public $password = '';
    public $remember = false;

    public function login()
    {
        $this->validate([
            'login_identifier' => 'required',
            'password' => 'required',
        ]);

        // تحديد نوع الإدخال بناءً على المحتوى
        $field = filter_var($this->login_identifier, FILTER_VALIDATE_EMAIL) ? 'email' : 
                 (is_numeric($this->login_identifier) ? 'phone' : 'username');

        if (Auth::attempt([$field => $this->login_identifier, 'password' => $this->password], $this->remember)) {
            session()->regenerate();
            return redirect()->intended('/dashboard');
        }

        $this->addError('login_identifier', 'بيانات الدخول غير صحيحة.');
    }

    public function render()
    {
        return view('livewire.auth.login')->layout('components.layouts.app');
    }
}
