timeout_range_slider = null

function filters_func_find_route_main (el,owner_type=null,win_loc_replace=null,weight=null){

    console.log("asd")

    if (win_loc_replace){
        event.preventDefault()
        let data_d = {}
        let get_url = ""
        let list = forloop_func_form(data_d,"get",get_url)
        data_d = list[0]
        get_url = list[1]
        // if (owner_type !== null) {
        //     data_d['owner_type'] = owner_type
        // }
        // get_url = get_url + `owner_type=${owner_type}`
        window.location.href = `${win_loc_replace}?${get_url}`

        // history.pushState({'data':data_d,'str_data':get_url}, "state_filters", `?${get_url}`);

    } else {
        if (el !== document.querySelector(".range_slider_form_filters")){
            event.preventDefault()
        }

            // let form = el.form
            // let formData = new FormData(form)
            let data_d = {}
            let get_url = ""
            let list = forloop_func_form(data_d,"get",get_url)
            data_d = list[0]
            get_url = list[1]
            // if (owner_type !== null) {
            //     data_d['owner_type'] = owner_type
            //     get_url = get_url + `owner_type=${owner_type}`
            // }

            if (weight){

                if (timeout_range_slider) {
                    clearTimeout(timeout_range_slider)
                }
                timeout_range_slider = setTimeout(
                    function () {
                        ajax_for_filter(data_d, get_url)
                    }
                ,100)

            } else {
                ajax_for_filter(data_d, get_url)
            }
    }
}

function ajax_for_filter (data_d,get_url){
    if (document.querySelector(".container_loader_filters_routes")){
        document.querySelector(".container_loader_filters_routes").classList.toggle("show")
    }
    $.ajax({
        headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        url: '/routes/find_routes/',
        type: "POST",
        // async: true,
        cache: false,
        processData: false,
        contentType: false,
        // enctype: 'json',
        data: JSON.stringify(data_d),
        success: function(data){
            let old_page_iterator = page_iterator
            page_iterator++
            history.pushState({'data':data_d,'str_data':get_url}, "state_filters", `?${get_url}`);
            // let element = document.createElement("img")
            // element.classList.add("loader_filters_routes")
            // element.src = "/static/img/svg/loader.svg"

            // let div = document.createElement("div")
            // div.classList.add("width-100")
            // div.classList.add("text-align-center")
            // div.classList.add("mb-10")
            //
            // div.innerHTML = "<img src='/static/img/svg/loader.svg' class='loader_filters_routes'/>"
            // let ins_bef_el = document.querySelector(".block-finded-routes").firstChild
            //
            // $(div).insertBefore(ins_bef_el)
            page_iterator = 1
            standart_page_iterator = 1
            separator_iterator = 1
            iterator_f_check = 1

            let new_page_routes = document.createElement("div")
            new_page_routes.classList.add("page_paging_elements_" + page_iterator)
            let insert_place_main = document.querySelector(".block-finded-routes")
            if (insert_place_main){
                insert_place_main.innerHTML = ''
                insert_place_main.appendChild(new_page_routes)
            }

            // $(new_page_routes).insertAfter(".page_paging_elements_" + old_page_iterator);
            // let line_separator = document.createElement("div")
            // line_separator.classList.add("line_separator_page_w_el")
            // $(line_separator).insertAfter(".page_paging_elements_" + separator_iterator);
            // document.querySelector(".info_profile").innerHTML = data.html;
            if (data.last_block){
                let block_last_block_routes = document.querySelector(".button-find-more-routes")
                if (block_last_block_routes){
                    block_last_block_routes.classList.add("hidden")
                }
            } else {
                let block_last_block_routes = document.querySelector(".button-find-more-routes")
                if (block_last_block_routes) {
                    if (block_last_block_routes.classList.contains('hidden')) {
                        block_last_block_routes.classList.remove("hidden")
                    }
                }
            }
            if (data.html === "\n\n\n\n\n"){
                // document.querySelector(".block-finded-routes").innerHTML = "<span style='color: #ff0000;font-weight: 800;font-size: 18px;padding: 10px;'>Нечего не найдено!</span>"
                document.querySelector(".block-finded-routes").innerHTML = "<div class=\"not_found_routes show\">\n" +
                    "            <div class=\"text-align-center fw-700 font-large c-txt-b2 m-a w-80 m-t-8p\">\n" +
                    "                Упс... <span class=\"orange-text\">Ничего не найдено</span>, попробуйте\n" +
                    "                изменить параметры поиска или оставьте заявку\n" +
                    "                на перевозку посылки\n" +
                    "            </div>\n" +
                    "            <img class=\"boxes_not_fond_routes left\" src=\"/static/img/boxes_for_not_found_routes/b_1.svg\"\">\n" +
                    "            <img class=\"boxes_not_fond_routes right\" src=\"/static/img/boxes_for_not_found_routes/b_2.svg\"\">\n" +
                    "        </div>"


            } else {
                let block_not_found_routes = document.querySelector(".not_found_routes")
                if (block_not_found_routes){
                    if (block_not_found_routes.classList.contains('show')){
                        block_not_found_routes.classList.remove("show")
                    }

                }
                let insert_place = document.querySelector(`.page_paging_elements_${page_iterator}`)
                if (insert_place){
                    document.querySelector(`.page_paging_elements_${page_iterator}`).innerHTML = data.html
                } else {
                    old_page_iterator = 1
                    let new_search = document.querySelector(`.page_paging_elements_${page_iterator}`)
                    if (new_search){
                        new_search.innerHTML = data.html
                    } else {
                        let new_page_routes1 = document.createElement("div")
                        new_page_routes1.classList.add("page_paging_elements_" + page_iterator)
                        new_page_routes1.innerHTML = data.html
                        let new_insert_place = document.querySelector('.block-finded-routes')
                        if (new_insert_place){
                            new_insert_place.appendChild(new_page_routes1)
                        } else {
                            console.log(`error! || you cannot find block for routes on the host ${window.location.host} in the time ${moment()}`)
                            alert(`error! || you cannot find block for routes on the host ${window.location.host} in the time ${moment()}`)
                        }
                    }
                }
                let loader = document.querySelector(".container_loader_filters_routes")
                if (loader){
                    loader.classList.toggle("show")

                } else{
                    let new_loader = document.createElement("div")
                    new_loader.classList.value = `width-100 text-align-center container_loader_filters_routes mb-10`
                    new_loader.innerHTML = `<img class="loader_filters_routes" src="/static/img/svg/loader.svg">`
                    $(new_loader).insertBefore(document.querySelector(".block-finded-routes").firstChild)
                }


                // if (document.querySelector(".loader_filters_routes")){
                //     document.querySelector(".loader_filters_routes").classList.remove("show")
                // }

            }


            // window.location.href = window.location.href + "?" + get_url
            // window.location.search = "?" + get_url


            // window.history.pushState({data:get_url
            // },{})
            // clearTimeout(t)
        },
        errors: function (data){
            // clearTimeout(t)
        }
    });
    return null
}