$(document).ready(function () {

    $('.select2-search').select2({
        dropdownCssClass: "dropdown-register-mlk"
    });

    setTimeout(function () {
        $('#fix_dwapp').addClass('show');
    }, 500);

    $('#close_app').click(function () {

        $('#fix_dwapp').removeClass('show');
    });

    $('.list-area-range .form-rdo[type="checkbox"]').change(function () {

        var idrngarea = $(this).attr('id');
        var rngareaname = $(this).parent('.item-rdo').find('.lbl-rdo').text();

        if ($(this).prop('checked')) {
            $(this).parent('.item-rdo').addClass('active');
            $('.selected-areas-range .placeholder-slc').css('display', 'none');
            $('.modal-mlktab .btn-confirm').removeClass('disable');
            $('.list-section-cities .btn-confirm').removeClass('disable');

            $('.selected-areas-range .mCSB_container').append('<span class="city-slc" data-id="' + idrngarea + '">' + rngareaname + '<i class="icon-close"></i></span>');

            $('.selected-areas-range .city-slc .icon-close').click(function () {

                var datarngarea = $(this).parent('.city-slc').data('id');
                $(this).parent('.city-slc').remove();
                $('.list-area-range .form-rdo[id="' + datarngarea + '"]').parent('.item-rdo').removeClass('active');
                $('.list-area-range .form-rdo[id="' + datarngarea + '"]').prop('checked', false);

                if ($('.selected-areas-range .city-slc').length == 0) {
                    $('.selected-areas-range .placeholder-slc').css('display', 'block');
                    $('.modal-mlktab .btn-confirm').addClass('disable');
                }

            });

            if ($(this).hasClass('rdo-all-area')) {
                $(this).parents('div').find('.item-rdo').addClass('active');
                $('.list-area-range .form-rdo[type="checkbox"]').prop('checked', true);
            }

        } else {
            $(this).parent('.item-rdo').removeClass('active');
            $('.selected-areas-range .city-slc[data-id="' + idrngarea + '"]').remove();

            if ($('.selected-areas-range .city-slc').length == 0) {
                $('.selected-areas-range .placeholder-slc').css('display', 'block');
                $('.modal-mlktab .btn-confirm').addClass('disable');
            }

            if ($(this).hasClass('rdo-all-area')) {
                $(this).parents('div').find('.item-rdo').removeClass('active');
                $('.list-area-range .form-rdo[type="checkbox"]').prop('checked', false);
            }
        }
    });

    $(document).on('keyup', ".input-search-modal .form-control", function () {

        if ($(this).val() == ' ' || $(this).val() == '  ') {
            $(this).parents('.modal-content').find('.list-modal-content .item-rdo').css({
                "display": "none"
            });
        } else {
            let input, filter, ul, li, a, b, i, txtValue, txtValueb;
            input = $(this);
            filter = input.val().toUpperCase();
            ul = $(this).parents('.modal-content').find('.list-modal-content');

            if (!ul.length) {
                ul = $(this).parents('.list-section').find('.list-modal-content');
            }

            li = ul.children(".item-rdo");
            for (i = 0; i < li.length; i++) {
                a = li[i].getElementsByTagName("label")[0];
                txtValue = a.textContent || a.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    li[i].style.display = "";
                } else if (
                    ('منظزیه'.toUpperCase().indexOf(filter) > -1) && txtValue === 'ابوریحان'
                ) {
                    li[i].style.display = "";
                } else {
                    li[i].style.display = "none";
                }
            }
        }

    });

    $(document).on('keyup', ".input-search-modal .input-area-list", function () {

        if ($(this).val() == ' ' || $(this).val() == '  ') {
            $(this).parents('.modal-content').find('.list-modal-content .item-rdo').css({
                "display": "none"
            });
        } else {
            let input, filter, ul, li, a, b, i, txtValue, txtValueb;
            input = $(this);
            filter = input.val().toUpperCase();
            ul = $(this).parents('.modal-content').find('.list-modal-content');

            if (!ul.length) {
                ul = $(this).parents('.list-section').find('.list-modal-content');
            }

            li = ul.children(".item-rdo");
            for (i = 0; i < li.length; i++) {
                a = li[i].getElementsByTagName("label")[0];
                txtValue = a.textContent || a.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    li[i].style.display = "";
                } else if (
                    ('منظزیه'.toUpperCase().indexOf(filter) > -1) && txtValue === 'ابوریحان'
                ) {
                    li[i].style.display = "";
                }
                else {
                    li[i].style.display = "none";
                }
            }
        }

    });
    $(document).on('keyup', ".input-search-modal .input-line-list", function () {

        if ($(this).val() == ' ' || $(this).val() == '  ') {
            $(this).parents('.modal-content').find('.list-modal-content .item-rdo').css({
                "display": "none"
            });
        } else {
            let input, filter, ul, li, a, b, i, txtValue, txtValueb;
            input = $(this);
            filter = input.val().toUpperCase();
            ul = $(this).parents('.modal-content').find('.list-modal-content');

            if (!ul.length) {
                ul = $(this).parents('.list-section').find('.list-modal-content');
            }

            li = ul.children(".item-rdo");
            for (i = 0; i < li.length; i++) {
                a = li[i].getElementsByTagName("label")[0];
                txtValue = a.textContent || a.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    li[i].style.display = "";
                } else {
                    li[i].style.display = "none";
                }
            }
        }

    });

    $('#modal_report_next input[type="radio"]').change(function () {
        if ($(this).prop('checked')) {
            $('#confirm_report_next').removeClass('disable');
        }
    });

    $(document).on('click', '#back_register', function () {
        $('#register_code').modal('hide');
        setTimeout(function () {
            $('#register_site').modal('show');
        }, 1000);
    });

    /*--------------------- Pages ---------------------*/

    $(".chbox-all").click(function () {
        $(this).parents('.body-filter').find("input[type=checkbox]").prop("checked", $(this).prop("checked"));
    });

    $("input[type=checkbox]").click(function () {
        if (!$(this).prop("checked")) {
            $(this).parents('.body-filter').find(".chbox-all").prop("checked", false);
        }
    });

    $('#multiple_cons').click(function () {
        $('#single_cons').removeClass('active');
        $(this).addClass('active');
        $('.list-cons .item-cons').removeClass('single-row');
    });
    $('#single_cons').click(function () {
        $('#multiple_cons').removeClass('active');
        $(this).addClass('active');
        $('.list-cons .item-cons').addClass('single-row');
    });

    $('#filter_resp').on('click', function () {
        if ($('.aside-search').hasClass('show'))
        {
            $('.aside-search').removeClass('show');
            $('.header-main').removeClass('z-index-zero');

        }
        else
        {
            $('.aside-search').addClass('show');
            $('.header-main').addClass('z-index-zero');
        }
    });

    $('#close_filtering').on('click', function () {
        $('.aside-search').removeClass('show');
        $('.header-main').removeClass('z-index-zero');
    });

    if ($('#form_request_mlk').length) {
        $("#form_request_mlk").validate();
    }

    $('.select2').on('change', function () {
        var dataselect = $(this).find("option:selected").val();

        if (dataselect != 0) {
            $(this).parents('.form-group').find('input.hidden-select').val(dataselect);
            $(this).parents('.form-group').find('input.hidden-select').removeClass('error');
            $(this).parents('.form-group').find('input.hidden-select').addClass('valid');
            $(this).parents('.form-group').find('label.error').hide();
        } else {
            $(this).parents('.form-group').find('input.hidden-select').val('');
            $(this).parents('.form-group').find('label.error').show();
        }
    });

    $('#upload_profile').on('click', function () {
        $(this).parents(".image-profile").find(".file-upload").click();
    });

    $(".image-profile .file-upload").on('change', function (input) {
        $(this).parents('.image-profile').find('.view-profile').attr('src', URL.createObjectURL(event.target.files[0]));
    });

    $('[data-toggle="popover"]').popover();

    $('.expert-details .btn-view-cons').click(function () {
        $(this).toggleClass('active');
    });

    $('.expert-details .btn-like').click(function () {
        $(this).toggleClass('active');
    });

    $('.expert-details .btn-archive-sec').click(function () {
        $('.body-box-cm').addClass('show');
    });

    // $("#rating_expert").starRating({
    //     totalStars: 5,
    //     starShape: 'rounded',
    //     starSize: 25,
    //     emptyColor: '#eceff1',
    //     hoverColor: '#ffdb70',
    //     activeColor: '#ffca28',
    //     ratedColor: '#ffca28',
    //     useGradient: false,
    //     disableAfterRate: false,
    //     onHover: function (currentIndex, currentRating, $el) {
    //         $('.live-rating').text(currentIndex);
    //     },
    //     callback: function (currentIndex, currentRating, $el) {
    //         $('.live-rating').text(currentIndex);
    //     },
    //     onLeave: function (currentIndex, currentRating, $el) {
    //         $('.live-rating').text(currentRating);
    //     }
    // });


    $(window).scroll(function () {

        var scroll = $(window).scrollTop();

        if (scroll >= 300) {
            $(".chat-seller").addClass("hide-text");
        } else {

            $(".chat-seller").removeClass("hide-text");
        }
    });

    if ($('#send_message').length) {

        $('#send_message').on('change keyup keydown keypress paste cut', function () {

            autosize($('#send_message'));

            if ($(this).height() > 20) {

                $(this).addClass('change');
            } else {

                $(this).removeClass('change');
            }
        });
    }


    $('#modal_video_mlk').on('hidden.bs.modal', function () {
        $('#show_video').trigger('pause');
    });

    /*--------------------- /Pages ---------------------*/

});

$(window).on("load", function () {

    if ($('#horizontal_areas_range').length) {
        $("#horizontal_areas_range").mCustomScrollbar({
            axis: "x",
            advanced: {
                autoExpandHorizontalScroll: true
            }
        });
    }

});

/*--------------------- Responsive ---------------------*/
$(function () {

    var width = $(window).width(),
        height = $(window).height();

    if ((width >= 1200) && (height >= 0)) {

        /*--------------------- Menu Hover ---------------------*/
        $(".mega-dropdown").hover(function () {

            $(this).find('.mega-dropdown-menu').toggleClass('show');
        });
        /*--------------------- /Menu Hover ---------------------*/

    } else {

        $('.navbar-toggler').click(function () {

            $(this).toggleClass('mm-wrapper_opened');
        });
        $(document).click(function (event) {
            if (!$(event.target).closest(".navbar-toggler,#navbarNavAltMarkup").length) {
                $(".navbar-toggler").removeClass('mm-wrapper_opened');
                $('#navbarNavAltMarkup').removeClass('show');
            }
        });
        $(document).click(function (event) {
            if (!$(event.target).closest(".navbar-toggler,#navbarNavAltMarkup").length) {
                $(".navbar-toggler").removeClass('mm-wrapper_opened');
                $('#navbarNavAltMarkup').removeClass('show');
            }
        });

        var swiper = new Swiper(".swiper-filter", {
            slidesPerView: 4,
            spaceBetween: 0,
            autoplay: {
                delay: 4000,
            },
            navigation: {
                nextEl: ".arrow-mlk.flt-next",
                prevEl: ".arrow-mlk.flt-prev",
            },
            breakpoints: {
                640: {
                    slidesPerView: 4,
                },
                768: {
                    slidesPerView: 5,
                },
                1024: {
                    slidesPerView: 6,
                },
                1200: {
                    slidesPerView: 10,
                },
            },
        });
    }
});
/*--------------------- /Responsive ---------------------*/

$("#compare_mlk .form-control").keyup(function () {

    if ($(this).val() == ' ' || $(this).val() == '  ') {
        $(this).parents('.modal-content').find('.list-mlk-estate .item-mlk').css({
            "display": "none"
        });
    } else {
        let input, filter, ul, li, a, b, i, txtValue, txtValueb;
        input = $(this);
        filter = input.val().toUpperCase();
        ul = $(this).parents('.modal-content').find('.list-mlk-estate .row');
        li = ul.children(".item-mlk");
        for (i = 0; i < li.length; i++) {
            a = li[i].getElementsByClassName("name-mlk")[0];
            b = li[i].getElementsByClassName("code-mlk")[0];
            txtValue = a.textContent || a.innerText;
            txtValueb = b.textContent || b.innerText;
            if (txtValue.toUpperCase().indexOf(filter) > -1 || txtValueb.toUpperCase().indexOf(filter) > -1) {
                li[i].style.display = "";
            } else {
                li[i].style.display = "none";

            }
        }
    }

    if ($('#form_call_mlk').length) {
        $("#form_call_mlk").validate();
    }
});
