<?php

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

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

    public bool $showPassword = false;

    public function togglePasswordVisibility(): void
    {
        $this->showPassword = !$this->showPassword;
    }

    public function login(): void
    {
        $this->validate();

        $this->form->authenticate();

        Session::regenerate();

        $this->redirectIntended(default: RouteServiceProvider::HOME, navigate: true);
    }
}; ?>

<div class="px-4 sm:px-6 md:px-8">
    <!-- Session Status -->
    <x-auth-session-status class="mb-4" :status="session('status')" />

    <form wire:submit="login">
        <!-- Email Address -->
        <div>
            <x-input-label for="email" :value="__('Email')" class="text-xs md:text-sm" />
            <x-text-input wire:model="form.email" id="email" class="block mt-1 w-full py-2.5 text-xs md:text-sm"
                type="email" name="email" autofocus autocomplete="username" />
            <x-input-error :messages="$errors->get('form.email')" class="mt-2 text-xs md:text-sm" />
        </div>

        <!-- Password -->
        <div class="mt-6 relative">
            <x-input-label for="password" :value="__('Kata Sandi')" class="text-xs md:text-sm" />
            <div class="relative">
                <x-text-input wire:model="form.password" id="password"
                    class="block mt-1 w-full py-2.5 pr-10 text-xs md:text-sm"
                    :type="$showPassword ? 'text' : 'password'" name="password" autocomplete="current-password" />

                <button type="button" wire:click="togglePasswordVisibility"
                    class="absolute inset-y-0 right-0 flex items-center px-3 text-gray-600">
                    @if($showPassword)
                        <!-- Icon for showing password -->
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24"
                            stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                        </svg>
                    @else
                        <!-- Icon for hiding password -->
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24"
                            stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.542-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.542 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21" />
                        </svg>
                    @endif
                </button>
            </div>
            <x-input-error :messages="$errors->get('form.password')" class="mt-2 text-xs md:text-sm" />
        </div>


        <!-- Remember Me -->
        <div class="flex justify-between mt-4 mb-6">
            <label for="remember" class="inline-flex items-center">
                <input wire:model="form.remember" id="remember" type="checkbox"
                    class="rounded border-gray-300 text-gray-600 shadow-sm focus:ring-gray-500" name="remember">
                <span class="ms-2 text-xs md:text-sm text-gray-600">{{ __('Ingatkan saya') }}</span>
            </label>
            @if(Route::has('password.request'))
                <a class="underline text-xs md:text-sm text-gray-600 hover:text-gray-900 rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500"
                    href="{{ route('password.request') }}" wire:navigate>
                    {{ __('lupa kata sandi?') }}
                </a>
            @endif
        </div>

        <div class="flex justify-center mt-4 mb-2">
            <a class="underline text-xs md:text-sm text-gray-600 hover:text-gray-900 rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                href="{{ route('register') }}" wire:navigate>
                {{ __('Belum punya akun?') }}
            </a>
        </div>

        <div class="flex items-center">
            <x-primary-button
                class="ring-2 ring-gray-700 shadow-[4px_4px_0px_2px_#374151] text-white hover:shadow-[2px_2px_0px_2px_#374151] hover:translate-y-0.5 hover:translate-x-0.5 w-full text-center bg-orangeCrayola py-2.5 text-xs md:text-sm">
                {{ __('Masuk') }}
            </x-primary-button>
        </div>
    </form>
</div>
