@extends('layouts.login.theme')
@section("title", env("APP_NAME") . " :: Área Restrita")
@section('content')
    <form autocomplete="on" class="formLogin" id="formLogin" action="{{ route("admin.loginAuthentication") }}" method="post">
        <section class="d-flex align-items-center min-vh-100">
            <div class="container">
                <div class="row justify-content-center">
                    <div class="col-12">
                        <div class="card-admin-login p-5">
                            @include("errors.messageFlash")
                            <div class="text-center">
                                <img src="{{ asset("pmar/assets/img/logo-angra-branco.svg") }}" class="card-img-top" title="{{ __("Prefeitura Municipal de Angra dos Reis") }}" alt="{{ __("Prefeitura Municipal de Angra dos Reis") }}" class="img-fluid text-center" style="width: 100%; height: 5rem;" />
                            </div>
                            <style>
                                .form-label{ color: #FFF !important}
                            </style>
                            <div class="card-body">
                                <h5 class="text-white card-title text-center">Conta Angra</h5>
                                <br>
                                @csrf
                                {{-- Preserva redirect para pós-login (ex: cadastro pessoas trans) --}}
                                @if(!empty($redirect) || request('redirect'))
                                    <input type="hidden" name="redirect" value="{{ request('redirect', $redirect) }}">
                                @endif
                                <div class="mb-3">
                                    <input type="hidden" class="form-control" name="recaptcha" id="recaptcha">

                                    <!-- Matrícula, CPF ou E-mail -->
                                    @include('components.inputs._inputType',[
                                        "for" => null,
                                        "label" => "Matrícula, CPF ou E-mail",
                                        "name" => "login",
                                        "id" => "matriculation",
                                        "required" => true,
                                        "type" => null,
                                        "old" => old('login'),
                                        "oninput" => "",
                                        "readonly" => false,
                                        "small" => null,
                                        "autocomplete" => "username",
                                    ])

                                </div>
                                <div class="mb-3">

                                    <!-- Campo Senha -->
                                    @include('components.inputs._inputType',[
                                        "for" => null,
                                        "label" => "Senha",
                                        "name" => "password",
                                        "id" => null,
                                        "required" => true,
                                        "type" => 'password',
                                        "old" => old('password'),
                                        "oninput" => null,
                                        "readonly" => false,
                                        "small" => null,
                                        "autocomplete" => "current-password",
                                    ])

                                    <small class="text-danger text-bold small-password"></small>
                                    @error('password')
                                        <small class="text-danger text-bold">{{ $message }}</small>
                                    @enderror
                                    <a href="{{ route("admin.esqueceuSenha") }}" class="text-white">Esqueceu senha?</a>
                                </div>

                                @php
                                    $recallerName = auth()->getRecallerName();
                                    $hasRecallerCookie = request()->cookie($recallerName) ? true : false;
                                @endphp

                                <div class="form-check form-switch mb-3 fs-5 d-flex justify-content-center">
                                    <input class="form-check-input shadow" type="checkbox" id="remember" name="remember"  {{ (old('remember') === 'on' || $hasRecallerCookie) ? 'checked' : '' }} >
                                    <label class="form-check-label text-white px-2" for="remember"> <small>{{ __("Lembrar acesso") }}</small> </label>
                                </div>

                                <div class="my-3">
                                    <button class="fs-5 rounded-4 btn m-auto btn-primary  w-100" type="submit" id="submitLogin">{{ __('Entrar') }}</button>
                                </div>
                                <div class="my-3 text-center">
                                    <a class="my-auto fw-bold fs-5 rounded-4 btn m-auto bg-white text-primary w-100" href="{{ route("admin.login.govbr") }}">
                                        <i class="me-1 fa-solid fa-user-shield"></i> <span style="font-size: 14px !important;">{{ __(' Entrar com') }}</span> <strong style="font-weight: 900 !important;">
                                            <span class="text-blue">g</span><span class="text-yellow">o</span><span class="text-green">v</span><span class="text-blue">.</span><span class="text-blue">b</span><span class="text-yellow">r</span>
                                        </strong>
                                    </a>
                                </div>
                                <hr/>
                                <div class="my-3 text-center">
                                    <a href="{{ route("admin.contas.publicas.create") }}" class="text-white">Ainda não possui cadastro? <strong class="fw-bolder text-yellow fs-5">Crie sua conta</strong></a>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div id='recaptchaLogin' data-size="invisible"></div>
        </section>
    </form>

    <script>
        // Logs para debug do GOV.BR
        console.log('🔍 GOV.BR Debug - Página de login carregada');
        console.log('🔍 URL atual:', window.location.href);
        console.log('🔍 Parâmetros da URL:', new URLSearchParams(window.location.search).toString());

        // Verifica se há mensagens de erro/sucesso
        @if(session('error'))
            console.error('❌ GOV.BR Erro:', '{{ session('error') }}');
        @endif

        @if(session('success'))
            console.log('✅ GOV.BR Sucesso:', '{{ session('success') }}');
        @endif

        // Monitora cliques no botão GOV.BR
        document.addEventListener('DOMContentLoaded', function() {
            const govBrLink = document.querySelector('a[href*="login.govbr"]');
            if (govBrLink) {
                govBrLink.addEventListener('click', function(e) {
                    console.log('🔍 GOV.BR: Link clicado, redirecionando...');
                    console.log('🔍 GOV.BR: URL destino:', this.href);
                });
            }

            // Verifica se voltou do callback
            const urlParams = new URLSearchParams(window.location.search);
            if (urlParams.has('code') || urlParams.has('error')) {
                console.log('🔍 GOV.BR: Callback detectado');
                console.log('🔍 GOV.BR: Code:', urlParams.get('code'));
                console.log('🔍 GOV.BR: Error:', urlParams.get('error'));
            }
        });
    </script>
@endsection
