<?php

namespace App\Filament\Auth\Pages;

use Filament\Auth\Pages\Login as BaseLogin;
use Filament\Forms\Components\TextInput;
use Illuminate\Contracts\Support\Htmlable;

class CustomLogin extends BaseLogin
{
    public function mount(): void
    {
        parent::mount();
    }

    protected function getEmailFormComponent(): TextInput
    {
        return TextInput::make('email')
            ->label('手机号')
            ->placeholder('请输入11位手机号')
            ->required()
            ->autocomplete()
            ->autofocus()
            ->maxLength(11)
            ->regex('/^1[3-9]\d{9}$/')
            ->helperText('请输入11位手机号码（以1开头）')
            ->extraInputAttributes(['tabindex' => 1])
            ->prefixIcon('heroicon-m-phone')
            ->telRegex('/^1[3-9]\d{9}$/');
    }

    protected function getCredentialsFromFormData(array $data): array
    {
        return [
            'username' => $data['email'],
            'password' => $data['password'],
        ];
    }

    public function getTitle(): string | Htmlable
    {
        return '登录到数学知识图谱管理系统';
    }

    public function getHeading(): string | Htmlable | null
    {
        return null;
    }

    protected function throwFailureValidationException(): never
    {
        throw \Illuminate\Validation\ValidationException::withMessages([
            'data.email' => '手机号或密码错误',
            'data.password' => '手机号或密码错误',
        ]);
    }
}
