<?php

use App\Livewire\Forms\LoginForm;
use Illuminate\Support\Facades\Session;
use Livewire\Attributes\Layout;
use Livewire\Volt\Component;

new #[Layout('layouts.guest')] class extends Component
{
    public LoginForm $form;

    /**
     * Handle an incoming authentication request.
     */
    public function login(): void
    {
        $this->validate();

        $this->form->authenticate();

        Session::regenerate();

        $this->redirectIntended(default: route('dashboard', absolute: false), navigate: true);
    }
}; ?>

<div>
    <x-auth.page-heading :title="__('Welcome back')" :subtitle="__('Sign in to pick up where you left off.')" />

    <x-auth-session-status class="mb-6" :status="session('status')" />

    <form wire:submit="login" class="space-y-5">
        <div>
            <x-input-label for="email" :value="__('Email')" />
            <x-text-input wire:model="form.email" id="email" class="mt-1.5 block w-full" type="email" name="email" required autofocus autocomplete="username" />
            <x-input-error :messages="$errors->get('form.email')" class="mt-2" />
        </div>

        <div>
            <div class="flex items-end justify-between gap-3">
                <x-input-label for="password" :value="__('Password')" class="mb-0" />
                @if (Route::has('password.request'))
                    <a
                        class="text-xs font-semibold text-accent hover:text-accent/90 focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background rounded-md"
                        href="{{ route('password.request') }}"
                        wire:navigate
                    >
                        {{ __('Forgot password?') }}
                    </a>
                @endif
            </div>
            <x-text-input
                wire:model="form.password"
                id="password"
                class="mt-1.5 block w-full"
                type="password"
                name="password"
                required
                autocomplete="current-password"
            />
            <x-input-error :messages="$errors->get('form.password')" class="mt-2" />
        </div>

        <div class="flex items-center gap-2">
            <input
                wire:model="form.remember"
                id="remember"
                type="checkbox"
                class="size-4 rounded-md border-border text-accent shadow-sm focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:ring-offset-background"
                name="remember"
            >
            <label for="remember" class="text-sm text-muted-foreground cursor-pointer select-none">
                {{ __('Keep me signed in') }}
            </label>
        </div>

        <div class="space-y-4 pt-1">
            <x-primary-button class="w-full justify-center py-3 text-base" wire:loading.attr="disabled" wire:target="login">
                <span wire:loading.remove wire:target="login">{{ __('Log in') }}</span>
                <span wire:loading wire:target="login">{{ __('Signing in…') }}</span>
            </x-primary-button>

            @if (Route::has('register'))
                <p class="text-center text-sm text-muted-foreground">
                    {{ __('No account yet?') }}
                    <a
                        href="{{ route('register') }}"
                        wire:navigate
                        class="font-semibold text-foreground underline-offset-4 hover:underline focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background rounded-sm"
                    >
                        {{ __('Create one') }}
                    </a>
                </p>
            @endif
        </div>
    </form>
</div>
