@extends('front.include.app')
@section('content')
    <section class="container">
        <style>
            .error {
                color: red;
            }
        </style>
        <div class="row">
            <div class="col-md-12 col-sm-12" align="center">
                <h3 class="blue paddin-20">Submit New entry</h3>
                @error('customer.*')
                    <div class="alert alert-danger">{{ $message }}</div>
                @enderror
                @if((10 - count($data) > 0))
                <form id="createEntry" method="post" action="{{ route('store-customer') }}" enctype="multipart/form-data">
                    @csrf
                    <div class="row">
                        <div class="col-lg-12">
                            <div id="inputFormRow">
                                <div class="input-group ">
                                    <input type="text"
                                        class="form-control blueinput @error('customer') is-invalid @enderror"
                                        name="customer[]" placeholder="Customer First Name">
                                    @error('customer')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                    <div class="input-group-append">
                                        @if((10 - count($data) > 1))
                                        <button id="addRow" type="button"><img src="{{ asset('assets/images/4.png') }}"
                                                width="35px">
                                        </button>
                                        @endif
                                    </div>
                                </div>
                            </div>
                            <div id="newRow"></div>
                        </div>
                    </div>
                    <input type="submit" class="form-control redbutton mt-4" name="" value="Submit">
                </form>
                @else
                    <h3>Not Allowed</h3>
                @endif
                <p class="blue mt-3">{{ isset($data) ? 10 - count($data) : 10 }} Entries are left </p>
                <span class="leftEn" style="display: none;">{{ isset($data) ? 10 - count($data) : 10 }}</span>

            </div>


        </div>

    </section>
@endsection
@section('script')
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
        integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous">
    </script>
    <!-- JQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js"></script>

    <script type="text/javascript">
        $("#addRow").click(function() {
            let leftEntry = $(".leftEn").text();
            let allowLengh = $("#newRow").find("#inputFormRow").length + 1;

            // alert(allowLengh);
            if((leftEntry - allowLengh) > 0){
                var html = '';
                html += '<div id="inputFormRow">';
                html += '<div class="input-group mb-1">';
                html +=
                    '<input type="text" class="form-control blueinput "name="customer[]"  placeholder="Customer First Name" required="true">';
                html += '<div class="input-group-append">';
                html += `<button id="removeRow" type="button" ><img src="{{ asset('assets/images/3.png') }}" width="35px"></button>`;
                html += '</div>';
                html += '</div>';

                $('#newRow').append(html);
            }else{
                alert("No more entries are allowed to submit!");
            }
        });

        // remove row
        $(document).on('click', '#removeRow', function() {
            $(this).closest('#inputFormRow').remove();
        });
    </script>
    <script>
        $(function() {
            $(document).ready(function() {
                // alert();
                $.validator.methods.isJSON = function(value, element) {
                    if (typeof(value) !== 'string') {
                        return false;
                    }
                    try {
                        JSON.parse(value);
                        return true;
                    } catch (e) {
                        return false;
                    }
                }

                // Add a custom validation method to check for unique names
                $.validator.addMethod("uniqueNames", function(value, element) {
                    var names = [];
                    var valid = true;
                    $('input[name="customer[]"]').each(function() {
                        var name = $(this).val();
                        if (names.indexOf(name) !== -1) {
                            valid = false;
                            return false; // Exit the loop early
                        }
                        names.push(name);
                    });
                    return valid;
                }, "Names must be unique.");

                $("#createEntry").validate({
                    rules: {
                        "customer[]": {
                            required: true,
                            minlength: 3,
                            uniqueNames: true

                        },

                    },
                    messages: {
                        "customer[]": {
                            required: "Please Enter Customer name",
                            uniqueNames: "Names must be unique."
                        },

                    },
                    errorElement: "em",
                    errorPlacement: function(error, element) {
                        // Add the `help-block` class to the error element
                        error.addClass("help-block");

                        if (element.prop("type") === "checkbox") {
                            error.insertAfter(element.parent("label"));
                        } else {
                            error.insertAfter(element);
                        }
                    },
                    highlight: function(element, errorClass, validClass) {
                        $(element).parents(".col-sm-5").addClass("has-error").removeClass(
                            "has-success");
                    },
                    unhighlight: function(element, errorClass, validClass) {
                        $(element).parents(".col-sm-5").addClass("has-success").removeClass(
                            "has-error");
                    }
                });
            });
        });
    </script>
@endsection
