<script>

    $(document).ready(function () {

//  ROOM EVENT
        let amount = $('#amount').val();

        let nod =  $('#number_of_nights').val(1);

        $('.rooms').on('change', function () {
//            let amount = $(this).attr('data-amount');
            let amount = $('option:selected', this).attr('data-amount');

            $('#amount').val(amount);
            $('#total_amount').val(amount);
        });

        $('#amount').on('change', function () {
//            let amount = $(this).attr('data-amount');
            let nod =  $('#number_of_nights').val();
            let amount = $(this).val();
            let added_amount = amount * nod;
            $('#total_amount').val(added_amount);
        });



        $('#number_of_nights').on('input, change', function () {

            let nod =  $(this).val();
            let amount = $('#amount').val();

            if(nod > 1)
            {
                $('#amount').removeAttr('readonly');
            }else{
                $('#amount').prop('readonly', 'readonly');
            }
            setTimeout(
                function()
                {
                    let total_amount = amount * nod;
//                    alert(total_amount);
                    $('#total_amount').val(total_amount);


                }, 500);

        });



        $('#product_id').on('change', function () {
            let quantity = $('#quantity').val();
           update_price(quantity);
        });

        $('#quantity').on('change', function()
        {
//            alert($(this).val());
            update_price($(this).val());
        });

        function update_price(quantity) {
            let price = $('#product_id').find('option:selected').attr('data-price');
            let amount = price * quantity;

            $("#amount").val(amount);
        }
//        $("#available_container").css('display', 'block');
        $("#arrival").on('input', function () {

             let arrival_date = $(this).val();

//            alert(arrival_date);

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                type: "POST",
                url: "{!! route('get_reservations') !!}",
                dataType: "html",
                data: {'arrival_date': arrival_date,},

                success: function (data) {
//                    alert(data);
                    var result = data;
                    if (result !== null) {
                        $("#available_container").show();
                        $("#available_container").html(data);

                        $("#next_btn").show();

                    } else {
                        $("#available_container").show();
                        $("#available_container").html("<span class='span alert alert-danger'> No Roooms available for the selected date</span>");
                    }
                }
            });

          $("#available_container").show();
        });

        $("#sales_date").on('change', function () {
            let search_date = $(this).val();
            $(".sales").LoadingOverlay('show');
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                type: "POST",
                url: "{!! route('get_sales_by_date') !!}",
                dataType: "html",
                data: {'search_date': search_date},

                success: function (data) {
//                    alert(data);
                    if (data !== null) {
                        $(".sales").html(data);
                        $(".sales").LoadingOverlay('hide', true);

                    } else {
                        alert('no record for selected date');
                    }
                }
            });
        });

        $("#checkins_date").on('change', function () {
            let search_date = $(this).val();
            $(".checkins").LoadingOverlay('show');
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                type: "POST",
                url: "{!! route('get_checkins_by_date') !!}",
                dataType: "html",
                data: {'search_date': search_date},

                success: function (data) {
//                    alert(data);
                    if (data !== null) {
                        $(".checkins").html(data);
                        $(".checkins").LoadingOverlay('hide', true);

                    } else {
                        alert('no record for selected date');
                    }
                }
            });
        });

        $("#pay_channel").on('change', function () {
//            alert($(this).val());
                let channel = $(this).val();

                if (channel == "PENDING")
                {
                    $("#review_wrapper").show(500);
                }else {
                    $("#review_wrapper").hide(500);
                }

        });


//end document.ready
    });



    {{$language_id = 2}}
    SHOULD_AUTO_GEN_SLUG = false;

    /* Generate the slug field, if it was not touched by the user (or if it was an empty string) */
    function populate_slug_field() {

//        alert("A");
        var cat_slug = document.getElementById('category_slug');

        if (cat_slug.value.length < 1) {
            // if the slug field is empty, make sure it auto generates
            SHOULD_AUTO_GEN_SLUG = true;
        }

        if (SHOULD_AUTO_GEN_SLUG) {
            // the slug hasn't been manually changed (or it was set above), so we should generate the slug
            // This is done in two stages - one to remove non words/spaces etc, the another to replace white space (and underscore) with a -
            cat_slug.value = document.getElementById("category_name").value.toLowerCase()
                .replace(/[^\w-_ ]+/g, '') // replace with nothing
                .replace(/[_ ]+/g, '-') // replace _ and spaces with -
                .substring(0, 99); // limit str length

        }

    }

    if (document.getElementById("category_slug").value.length < 1) {
        SHOULD_AUTO_GEN_SLUG = true;
    } else {
        SHOULD_AUTO_GEN_SLUG = false; // there is already a value in #category_slug, so lets pretend it was changed already.
    }


    //multi-language data
    var defalutLangId = {{$language_id}};
    var preLangId = defalutLangId;
    var languageList = {};
    $("#language_list > option").each(function () {
        languageList[this.value] = {
            lang_id: -1,
            category_name: "",
            slug: "",
            category_description: "",
            lang_name: this.text
        }
    });

    $('#language_list').on('change', function () {
        fillLanguageList(this.value);
        preLangId = this.value;
    });

    function fillLanguageList(langId) {
        languageList[preLangId].lang_id = preLangId;
        languageList[preLangId].category_name = $('#category_name').val();
        languageList[preLangId].slug = $('#category_slug').val();
        languageList[preLangId].category_description = $('#category_description').val();

        $('#category_name').val(languageList[langId].category_name);
        $('#category_slug').val(languageList[langId].slug);
        $('#category_description').val(languageList[langId].category_description);
    }

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $('#submit-btn').click(function () {
//        alert('i got here');
        if (languageList[$('#language_list').val()].lang_id == -1) {
            if (!$('#category_name').val()) {
                alert("Category name must not be empty");
                return;
            } else if (!$('#category_slug').val()) {
                alert("Category slug must not be empty");
                return;
            }
            languageList[preLangId].lang_id = $('#language_list').val();
            languageList[preLangId].category_name = $('#category_name').val();
            languageList[preLangId].slug = $('#category_slug').val();
            languageList[preLangId].category_description = $('#category_description').val();
        }
        //        alert('i got here');

        $.post('{{route("binshopsblog.admin.categories.store_category")}}',
            {
                data: languageList,
                parent_id: parseInt($('#parent_id').val())
            },
            function (data, status) {
                if (data.code === 403) {
                    alert("Slug is already taken: " + languageList[data.data].lang_name);
                } else if (data.code === 200) {
                }
                window.location.replace("{{route('binshopsblog.admin.categories.index')}}");
            });
    });






</script>
