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

@section('css')
@endsection

@section('contents')
    <!-- login form  -->
    <section class="px-8 lg:px-40 mt-8 lg:mt-20 relative w-full">
        <div class="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 relative rounded-2xl backdrop-blur-sm"
            style="
            background: linear-gradient(
              to right,
              #14f19510,
              #e39dff10,
              #80ecff10
            );
          ">
            <!-- Pseudo-element for gradient border -->
            <div class="absolute inset-0 rounded-2xl"
                style="
              z-index: -1;
              background: linear-gradient(to right, #14f195, #80ecff, #e39dff);
              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>

            <!-- Content -->

            <a href="{{ route('home') }}"
                class="bg-[#a3dc2f] flex items-center justify-center gap-2  py-2 rounded-xl w-24 mb-10">
                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
                    style="fill: rgb(0, 0, 0);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-black">Home</span>
            </a>


            {{-- 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 #a3dc2f;  border-radius: 6px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); max-width: 400px; margin: 20px auto; ">
                </p>
            </div>


            <h3
                class="text-transparent bg-clip-text bg-gradient-to-r from-[#a3dc2f] via-[#78f6c3] to-[#b96a9c] font-semibold text-center mb-4 text-xl md:text-3xl">
                Get Started with
                <span
                    class="font-bold text-transparent bg-clip-text bg-gradient-to-r from-[#f087fe] to-[#b96a9c]">cbrilliance</span>
            </h3>

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

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


                <label for="" class="text-black">Email Address</label>
                <input type="email" id="email" name="email" placeholder="Enter Email Address"
                    class="border border-[#a3db2f] rounded-lg px-2 py-4 w-full mb-4 md:mb-10 text-gray-700 mt-1 text-sm focus:outline-none" />
                <p>
                    <span>
                        @error('email')
                            {{ $message }}
                        @enderror
                    </span>
                </p>

                <label for="" class="text-black">Password</label>
                <input type="password" name="password" id="password" placeholder="Enter Password"
                    class="border border-[#a3db2f] rounded-lg px-2 py-4 w-full mb-8 md:mb-10 mt-1 text-gray-700 text-sm focus:outline-none" />
                <p>
                    <span>
                        @error('password')
                            {{ $message }}
                        @enderror
                    </span>
                </p>

                <label for="" class="text-black">Confirm Password</label>
                <input type="password" name="password_confirmation" id="confirm-password" placeholder="Re-enter Password"
                    class="border border-[#a3db2f] rounded-lg px-2 py-4 w-full mb-8 md:mb-10 mt-1 text-gray-700 text-sm focus:outline-none" />
                <p>
                    <span>
                        @error('password-confirmation')
                            {{ $message }}
                        @enderror
                    </span>
                </p>

                <button type="submit" id="registerBtn"
                    class="primaryBtn w-full rounded-3xl text-black py-2 cursor-pointer hover:transform hover:scale-105 transition-transform flex items-center justify-center">
                    Sign Up
                </button>

                <p class="text-center text-black mt-4">
                    Already have an account?
                    <a href="{{ route('user.login') }}" class="text-black font-semibold hover:text-[#a3db2f]">Log In</a>
                </p>
            </form>

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

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

                    <div class="grid lg:grid-cols-6 grid-cols-3 md:gap-10 gap-6 text-black">
                        <input type="number" maxlength="1" name="otp[]" autofocus
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-[#a3db2f] rounded" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-[#a3db2f] rounded" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-[#a3db2f] rounded" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-[#a3db2f] rounded" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-[#a3db2f] rounded" />
                        <input type="number" maxlength="1" name="otp[]"
                            class="otp-input w-12 h-10 text-center text-xl bg-transparent border border-[#a3db2f] rounded" />
                    </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="primaryBtn w-full rounded-3xl text-black py-2 cursor-pointer hover:transform hover:scale-105 transition-transform flex items-center justify-center">Send
                            Code</button>
                    </div>
                </div>

                <div class="text-center text-black py-5">
                    <a href="{{ route('user.register') }}" class="hover:text-[#a3db2f]" 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(
                    'primaryBtn'
                );

                $.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(
                            'primaryBtn'
                        );

                    }

                });
            });


            //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(
                    'primaryBtn'
                );

                $.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(
                            'primaryBtn'
                        );

                    }

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