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

@section('css')
@endsection

@section('contents')
    <section class="px-8 lg:px-40 mt-8 lg:mt-20 relative w-full"
        style=" background-image: url('{{ asset('/assets/templates/cavit/images/bot-hand.svg') }}');  background-size: contain; background-position: right; background-repeat: no-repeat; ">
        <img src="{{ asset('/assets/templates/cavit/images/circles.svg') }}" alt=""
            class="absolute md:top-40 md:left-40 hidden lg:block" />

        <img src="{{ asset('/assets/templates/cavit/images/circles.svg') }}" alt=""
            class="absolute md:-bottom-40 right-4 hidden lg:block" />



        <div
            class="@if (user()) hidden @endif shadow-lg overflow-hidden border border-transparent bg-clip-border md:self-center w-full lg:w-1/2 mx-auto p-8 md:p-12 lg:p-16 bg-pink-400/5 relative rounded-2xl backdrop-blur-sm  backdrop-custom">
            <!-- Pseudo-element for gradient border -->
            <div class="absolute inset-0 rounded-2xl"
                style=" z-index: -1; background: linear-gradient(to bottom, #ff3bff, #5c24ff); padding: 1px; /* Adjust border width */ mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0); -webkit-mask-composite: xor; mask-composite: exclude; ">
            </div>

            <a href="{{ route('home') }}" class="text-white flex gap-2  py-2 rounded-md w-32 mb-10">
                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
                    style="fill: rgb(255, 255, 255);transform: ;msFilter:;">
                    <path
                        d="m21.743 12.331-9-10c-.379-.422-1.107-.422-1.486 0l-9 10a.998.998 0 0 0-.17 1.076c.16.361.518.593.913.593h2v7a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-4h4v4a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-7h2a.998.998 0 0 0 .743-1.669z">
                    </path>
                </svg>
                <span class="text-white">Home</span>
            </a>

            <!-- Content -->
            <h3 class="text-pink-400 font-semibold text-center mb-4 text-xl md:text-3xl">
                Get Started with <span class="font-bold">{{ site('name') }}</span>
            </h3>

            {{-- notification --}}
            <div class="px-4 lg:px-10 mt-6 space-y-6">
                <p id="noticeMsgOtp" class="hidden noticeMsg"
                    style="position: relative; background-color: #f8f9fa; color:#3b0b3b; padding: 16px; border-left: 4px solid #fb64b6; border-right: 4px solid #fb64b6; border-radius: 6px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); max-width: 400px; margin: 20px auto; ">
                </p>
            </div>



            <form action="{{ route('user.register-validate') }}" method="POST" class="" id="registerForm">
                @csrf

                <p class="text-center text-sm text-gray-200 mb-8 md:mb-16">
                    Please enter your details
                </p>

                <div class="block">
                    <label for="" class="text-white">Email Address</label>
                    <input type="email" id="email" name="email" placeholder="Enter Email Address"
                        class="border border-gray-400 rounded-lg px-2 py-3 w-full mb-4 text-white mt-1" />
                </div>
                <span>
                    @error('email')
                        {{ $message }}
                    @enderror
                </span>

                <div class="block">
                    <label for="" class="text-white">Password</label>
                    <input type="password" name="password" id="password" placeholder="Enter Password"
                        class="border border-gray-400 rounded-lg px-2 py-3 w-full mb-4 text-white" />
                </div>
                <span>
                    @error('password')
                        {{ $message }}
                    @enderror
                </span>

                <div class="block">
                    <label for="" class="text-white">Confirm Password</label>
                    <input type="password" name="password_confirmation" id="confirm-password"
                        placeholder="Re-enter Password"
                        class="border border-gray-400 rounded-lg px-2 py-3 w-full mb-8 text-white" />
                </div>
                <span>
                    @error('password-confirmation')
                        {{ $message }}
                    @enderror
                </span>

                <button type="submit" id="registerBtn"
                    class="gradient-btn-filled-submit w-full rounded-3xl text-white flex items-center justify-center">
                    Sign Up
                </button>

                <p class="text-center text-gray-400 mt-4">
                    Already have an account?
                    <a href="{{ route('user.login') }}" class="text-white font-semibold">Login</a>
                </p>
            </form>

            <form action="{{ route('user.register-verify') }}" method="POST" class="hidden" id="verifyForm">
                @csrf

                <p class="text-white text-sm mb-6 text-center">Enter the OTP Sent to your Email</p>

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

                    <div class="grid lg:grid-cols-6 grid-cols-3 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="w-full mb-6">
                    <div class="text-center">
                        <button type="submit" id="verifyBtn"
                            class="gradient-btn-filled-submit w-[90%] mx-auto rounded-3xl text-white flex items justify-center">Send
                            Code</button>
                    </div>
                </div>

                <div class="text-center text-white py-5">
                    <a href="{{ route('user.register') }}" class="hover:text-blue-400" id="resendBtn">Go Back</a>
                </div>

            </form>



        </div>


    </section>
@endsection

@section('scripts')
    <script>
        $(document).ready(function() {
            $('#registerForm').submit(function(e) {
                e.preventDefault(); // Prevent default form submission

                var form = $(this);
                var formData = form.serialize(); // Serialize form data as JSON

                var clicked = $('#registerBtn');

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

                //custom styling
                clicked.addClass('disabled-button');
                clicked.removeClass(
                    'gradient-btn-filled-submit'
                );

                $.ajax({
                    url: form.attr('action'),
                    type: 'POST',
                    data: formData,
                    dataType: 'json',
                    success: function(response) {
                        var verifyText = response.message;
                        var verify = response.verify;

                        toastNotify('success', verifyText);
                        $('.noticeMsg').html(verifyText).show();

                        if (verify == 1) {
                            //hide register form and display verification form
                            $('#registerForm').hide();
                            $('#verifyForm').show();

                            //update page title
                            $('#page-title').html('Verify Email');
                        } else {
                            var url = '{{ route('user.dashboard') }}';
                            window.location.href = url;
                        }




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

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


                                toastNotify('error', fieldErrors);

                                $('.noticeMsg').html(fieldErrors).show();


                            });
                        } else {
                            toastNotify('error', 'An error occured, please try again later');


                        }


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

                        //custom styling
                        clicked.removeClass('disabled-button');
                        clicked.addClass(
                            'gradient-btn-filled-submit'
                        );

                    }

                });
            });


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

                var form = $(this);
                var formData = form.serialize(); // Serialize form data as JSON

                var clicked = $('#verifyBtn');

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

                //custom styling
                clicked.addClass('disabled-button');
                clicked.removeClass(
                    'gradient-btn-filled-submit'
                );

                $.ajax({
                    url: form.attr('action'),
                    type: 'POST',
                    data: formData,
                    dataType: 'json',
                    success: function(response) {
                        var verifyText = response.message;
                        $('#noticeMsg').html(verifyText).show();

                        toastNotify('success', verifyText);



                        var url = '{{ route('user.dashboard') }}';
                        window.location.href = url;

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

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

                                toastNotify('error', fieldErrors);

                            });
                        } else {
                            toastNotify('error', 'An error occured, please try again later');

                        }


                    },

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

                        //custom styling
                        clicked.removeClass('disabled-button');
                        clicked.addClass(
                            'gradient-btn-filled-submit'
                        );

                    }

                });
            });
        });
    </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>
@endsection
