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

@section('css')
    <style>
        input {
            background-color: transparent;
            color: inherit;
            /* Keeps text color consistent */
            border: none;
            outline: none;
        }
    </style>
@endsection

@section('contents')
    <div class="h-screen w-full flex items-center justify-center relative">
        <div class="w-full max-w-screen-sm lg:p-0 p-4">
            <div
                class="bg-slate-800/50 backdrop-blur-md border border-white/15 rounded-2xl p-6 md:p-10 relative overflow-hidden">
                <a href="{{ route('home') }}">
                    <p class="text-white flex gap-2 items-center">
                        <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="M12.707 17.293 8.414 13H18v-2H8.414l4.293-4.293-1.414-1.414L4.586 12l6.707 6.707z">
                            </path>
                        </svg>
                        <span>Back</span>
                    </p>
                </a>

                <div class="px-4 lg:px-10 mt-6 space-y-6">
                    <p class="bg-green-500 text-gray-300 p-1 rounded-lg text-xs text-center hidden noticeMsg" id="noticeMsg"
                        style="background: #1ce01c; color:#fff; margin:10px 0px;"></p>
                </div>

                <div class="text-white mb-10 text-center">
                    <p
                        class="max-w-lg mx-auto text-4xl mb-4 font-semibold text-transparent bg-clip-text bg-gradient-to-r from-[#5823c1] via-[#ff8ac7] to-[#feba7f]">
                        Get Started with {{ site('name') }}
                    </p>
                    <p class="">Please enter your details</p>
                </div>

                <form action="{{ route('user.register-validate') }}" method="POST" class="" id="registerForm">
                    @csrf
                    <div class="w-full mb-10">
                        <fieldset class="border py-1 px-3 rounded-2xl">
                            <legend class="px-3 mx-8 text-white"><label for="amount"
                                    class="text-white text-sm">Email</label></legend>
                            <div class="w-full text-white ">

                                <div class="w-full">
                                    <input type="email" id="email" name="email"
                                        class="w-full p-4 bg-transparent text-xl text-white border-0 focus:outline-none flex-1"
                                        placeholder="Enter Email">
                                </div>
                            </div>
                        </fieldset>

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

                    <div class="w-full mb-4">
                        <fieldset class="border py-1 px-3 rounded-2xl">
                            <legend class="px-3 mx-8 text-white"><label for="amount"
                                    class="text-white text-sm">Password</label></legend>
                            <div class="w-full text-white ">

                                <div class="w-full">
                                    <input type="password" name="password" placeholder="Enter Password" id="password"
                                        class="w-full p-4 bg-transparent text-xl text-white border-0 focus:outline-none flex-1"
                                        placeholder="Enter Password">
                                </div>
                            </div>
                        </fieldset>

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

                    <div class="w-full mb-20">
                        <fieldset class="border py-1 px-3 rounded-2xl">
                            <legend class="px-3 mx-8 text-white"><label for="amount" class="text-white text-sm">Confirm
                                    Password</label></legend>
                            <div class="w-full text-white ">

                                <div class="w-full">
                                    <input type="password" name="password_confirmation" placeholder="Re-enter Password"
                                        id="confirm-password"
                                        class="w-full p-4 bg-transparent text-xl text-white border-0 focus:outline-none flex-1">
                                </div>
                            </div>
                        </fieldset>

                        <span>
                            @error('password-confirmation')
                                {{ $message }}
                            @enderror
                        </span>
                    </div>


                    <div class="w-full mb-4">
                        <div class="text-center">
                            <button type="submit" id="registerBtn"
                                class="border border-[#f272d5] bg-linear-to-r from-[#f272d5]/35 to-[#f272d5]/5  hover:bg-linear-to-l from- text-white  rounded-full px-10 py-4 w-full  transition delay-100 duration-200 ease-in-out">Sign
                                Up</button>
                        </div>
                    </div>

                    <div class="w-full mb-4">
                        <p class="text-center text-[#B4B1B1]"> Already have an account? <a href="{{ route('user.login') }}"
                                class="text-white"> Login</a></p>
                    </div>

                </form>


                {{-- verification form --}}

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

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

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

                            <input type="hidden" name="otp" placeholder="OTP" id="otp"
                                class="theme1-text-input" required maxlength="6">
                            <span>
                                @error('email')
                                    {{ $message }}
                                @enderror
                            </span>
                        </div>
                    </div>

                    <div class="w-full mb-4 pt-10">
                        <div class="text-center">
                            <button type="submit" id="verifyBtn"
                                class="border border-[#f272d5] bg-linear-to-r from-[#f272d5]/35 to-[#f272d5]/5  hover:bg-linear-to-l from- text-white  rounded-full px-10 py-4 w-full  transition delay-100 duration-200 ease-in-out">Verify</button>
                        </div>

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


                </form>
            </div>
        </div>
    </div>
@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);

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

                    }

                });
            });


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

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

                    }

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

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