@extends('templates.' . site('template') . '.layouts.auth')

@section('contents')
    <section class="min-h-screen flex justify-center items-center px-6 lg:px-40">
        {{-- verification form --}}
        <div class="bg-gray-800/50 p-6 rounded-lg space-y-6 w-full md:2/3 lg:w-1/3 mx-auto mb-8">

            <p class="text-2xl text-white mb-6">
                <span
                    class="text-transparent bg-clip-text bg-gradient-to-r from-[#FF3BFF] via-[#ECBFBF] to-[#5C24FF] lg:to-[#D94FD5]">2FA
                </span>
            </p>

            <form action="{{ route('user.g2fa.g2fa') }}" method="POST" class="px-4 lg:px-10 mt-6 space-y-6" id="verifyForm">
                @csrf

                <p class="text-white text-sm mb-6">2 Factor Authentication</p>

                <div class="w-full flex items-center justify-center mb-6">

                    <div class="flex lg:gap-10 gap-6 text-white">
                        <input type="number" maxlength="1" name="otp[]" autofocus
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:outline-none" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:outline-none" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:outline-none" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:outline-none" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:outline-none" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:outline-none" />
                    </div>
                    <input type="hidden" name="otp" id="otp" value="" required maxlength="6">

                    <span>
                        @error('otp')
                            {{ $message }}
                        @enderror
                    </span>

                </div>

                <div class="grid grid-cols-1 mt-3">
                    <button type="submit" id="verifyBtn"
                        class="bg-sky-800 w-full text-white px-4 py-2 rounded-2xl hover:bg-gray-600 transition-colors duration-200">Verify</button>
                </div>

            </form>
        </div>
    </section>

    <img src="{{ asset('/assets/templates/pandel/images/abstract-bg.svg') }}" alt=""
        class="absolute w-full md:flex top-80 lg:top-20 -z-10" />
@endsection

@section('scripts')
    {{-- <script>
        $(document).ready(function() {
            const $inputs = $(".otp-input");

            $inputs.on("input", function(e) {
                const $this = $(this);
                const value = $this.val();

                if (value.length === 1) {
                    $this.next(".otp-input").focus();
                }
            });

            $inputs.on("keydown", function(e) {
                const $this = $(this);

                if (e.key === "Backspace" && !$this.val()) {
                    $this.prev(".otp-input").focus();
                }
            });

            $inputs.on("paste", function(e) {
                const pasteData = e.originalEvent.clipboardData.getData('text').slice(0, 4);

                $inputs.each(function(index) {
                    $(this).val(pasteData[index] || "");
                });
                $inputs.eq(pasteData.length).focus();
                e.preventDefault();
            });
        });


        //////////////////////////////// parse
        $(document).ready(function() {
            $('.otp-input').on('input', function() {
                let otpValue = '';

                // Concatenate all input values
                $('.otp-input').each(function() {
                    otpValue += $(this).val();
                });

                // Set the hidden input value
                $('#otp').val(otpValue);
            });
        });
    </script> --}}

    <script>
        ////// Concatenate all input values
        $(document).ready(function() {
            $('.otp-input').on('input', function() {
                let otpValue = '';

                // Concatenate all input values
                $('.otp-input').each(function() {
                    otpValue += $(this).val();
                });

                // Set the hidden input value
                $('#otp').val(otpValue);
            });
        });


        $(document).ready(function() {
            const $inputs = $(".otp-input");

            $inputs.on("input", function(e) {
                const $this = $(this);
                const value = $this.val();

                // Allow only 1 character
                if (value.length > 1) {
                    $this.val(value.charAt(0)); // Keep only the first character
                }

                // Move focus to the next input if a character is entered
                if (value.length === 1) {
                    $this.next(".otp-input").focus();
                }
            });

            $inputs.on("keydown", function(e) {
                const $this = $(this);

                if (e.key === "Backspace" && !$this.val()) {
                    $this.prev(".otp-input").focus();
                }
            });

            $inputs.on("paste", function(e) {
                const pasteData = e.originalEvent.clipboardData.getData('text').slice(0, $inputs.length);

                // Fill inputs with pasted data, ensuring only one character per input
                $inputs.each(function(index) {
                    $(this).val(pasteData[index] || "");
                });

                // Focus on the next input after the last pasted character
                $inputs.eq(pasteData.length).focus();
                e.preventDefault();
            });
        });
    </script>


    <script>
        $(document).ready(function() {

            //otp form
            $('#verifyForm').submit(function(e) {
                e.preventDefault();

                var form = $(this);
                var formData = form.serialize();
                var clicked = $('#verifyBtn');

                //disable the submit button
                clicked.addClass('relative disabled');
                clicked.append('<span class="button-spinner"></span>');
                clicked.prop('disabled', true);

                clicked.addClass('bg-gray-600');
                clicked.removeClass('bg-sky-800 hover:bg-gray-600');

                $.ajax({
                    url: form.attr('action'),
                    type: 'POST',
                    data: formData,
                    dataType: 'json',
                    success: function(response) {
                        var verifyText = response.message;
                        $('#noticeMsg').html(verifyText).show();
                        var url = "{{ route('user.dashboard') }}";
                        window.location.href = url;

                    },
                    error: function(xhr, status, error) {
                        $('#verifyBtn').show();
                        var errors = xhr.responseJSON.errors;

                        if (errors) {
                            $.each(errors, function(field, messages) {
                                var fieldErrors = '';
                                $.each(messages, function(index, message) {
                                    fieldErrors += message + '<br>';
                                });


                                Swal.fire({
                                    icon: 'error',
                                    html: fieldErrors,
                                    toast: true,
                                    position: 'top-end',
                                    showConfirmButton: false,
                                    timer: 3000,
                                    timerProgressBar: true,
                                    didOpen: (toast) => {
                                        toast.addEventListener('mouseenter',
                                            Swal.stopTimer);
                                        toast.addEventListener('mouseleave',
                                            Swal.resumeTimer);
                                    }
                                });
                            });
                        } else {
                            Swal.fire({
                                icon: 'error',
                                text: 'An error occured, please try again later',
                                toast: true,
                                position: 'top-end',
                                showConfirmButton: false,
                                timer: 3000,
                                timerProgressBar: true,
                                didOpen: (toast) => {
                                    toast.addEventListener('mouseenter',
                                        Swal.stopTimer);
                                    toast.addEventListener('mouseleave',
                                        Swal.resumeTimer);
                                }
                            });
                        }


                    },
                    complete: function() {
                        clicked.removeClass('disabled');
                        clicked.find('.button-spinner').remove();
                        clicked.prop('disabled', false);

                        clicked.removeClass('bg-gray-600');
                        clicked.addClass('bg-sky-800 hover:bg-gray-600');

                    }

                });
            });

        });
    </script>
@endsection
