profile_tabs_f_static_map = new Map([
    ['chats','chat'],
    ['support_tickets','support'],
    ['show_cur_subscribe','my_subscribe'],
    ['change_profile','change_profile'],
    ['new_route_view_customer','create_route_for_customer'],
    ['new_route_view_mover','create_route_for_mover'],
    ['get_routes','my_routes'],
    ['dashboard','dashboard']
])

function select_tab_profile (el,url,owner_type=null) {
    let data = {}
    let confirm_url = `/user_account/${url}/`
    if (url.includes('subscribe')){
        confirm_url = `/subscribes/${url}/`
    } else if (url.includes('new_route')){
        data = {
            'owner_type': owner_type
        }
    } else if (url.includes('get_routes')){
        confirm_url = `/routes/${url}/`
    }
    if (window.location.href.includes("profile")){
        document.querySelector(".info_profile").innerHTML = '<img src="/static/img/svg/loader.svg" style="height: 30px;position: absolute;top: 47%;left: 45%;"/>'
    }
    $.ajax({
        // headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        url: confirm_url,
        type: "POST",
        // async: true,
        cache: false,
        processData: false,
        contentType: false,
        // enctype: 'json',
        data: JSON.stringify(data),
        success: function(data){
            let scroll_top = window.scrollY
            document.querySelector(".info_profile").innerHTML = data.html;
            let list_div = document.querySelectorAll('.menu_profile div');
            list_div.forEach(el=>{ el.classList.remove('selected'); });
            el.classList.add('selected')
            // let body = document.querySelector("body")
            // body.style.overflow = "hidden"
            let confirm_url_f_lang_ru = ''
            let confirm_url_f_lang_en = ''
            if (owner_type){
                window.history.pushState(null, null, `/${document.documentElement.lang}/profile/page/${profile_tabs_f_static_map.get(`${url}_${owner_type}`)}`)
                confirm_url_f_lang_ru = `/ru/profile/page/${profile_tabs_f_static_map.get(`${url}_${owner_type}`)}/`
                confirm_url_f_lang_en = `/en/profile/page/${profile_tabs_f_static_map.get(`${url}_${owner_type}`)}/`
            } else {
                window.history.pushState(null, null, `/${document.documentElement.lang}/profile/page/${profile_tabs_f_static_map.get(url)}/`)
                confirm_url_f_lang_ru = `/ru/profile/page/${profile_tabs_f_static_map.get(url)}/`
                confirm_url_f_lang_en = `/en/profile/page/${profile_tabs_f_static_map.get(url)}/`
            }
            document.querySelector("#ru_lang").href = confirm_url_f_lang_ru
            document.querySelector("#en_lang").href = confirm_url_f_lang_en
            // let header = document.querySelector("header")
            // header.scrollIntoView({
            //     behavior: "smooth",
            //     block: "end",
            //     inline:'nearest'
            // })
            window.scrollTo({
              top: 0,
              behavior: "smooth",
            });
            middleWareJS()
            let user_type = getInfoAboutUser()
            if (user_type === 'mobile') {
                open_curtain_w_btn_profile()

            } else {
                if (!window.location.href.includes('profile')){
                    open_curtain_w_btn_profile()
                }
            }
            if (window.location.href.includes("profile")){
                setStandartSettingsToBlockOverlay()
            }

            if (window.location.href.includes("profile")){
                selectTabProfileIfHisNotSelected(url)
            }
            deleteMarkerMessages(el)
        },
        error: function (data){
            console.log(data)

        }
    });
}

function deleteMarkerMessages (el) {
    let marker = document.querySelectorAll(".icon_unread_messages")
    if (marker.length > 0){
        marker[0].classList.remove("showed")
        if (marker[1]){
            marker[1].classList.remove("showed")
        }
    }
}

function selectTabProfileIfHisNotSelected (url) {
    if (url !== 'new_route_view') {
        let el_f_select = document.querySelector(`[data-ajax-url=${url}]`)
        el_f_select.classList.add('selected')
    }
}

function clickONTHEAPROfileBTN (el) {
    let el_f_click = el.querySelector(".text_btn_profile")
    el_f_click.click()
}

function changeHrefCl (el) {
    let dataset = el.dataset
    let old_href = dataset['href']
    let new_href = old_href
    el.setAttribute('href',new_href)
}


function createTicketShow () {
    $.ajax({
        headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        url: '/messages/support_create_ticket_form/',
        type: "POST",
        // async: true,
        cache: false,
        processData: false,
        contentType: false,
        // enctype: 'json',
        // data: formData,
        success: function(data){

            document.querySelector(".insert-tech-place").innerHTML = data.html;
            middleWareJS()
        },
    });
}

function countLetters () {
    event.preventDefault()
    let area = document.querySelector(".el-form-create-ticket-textarea")
    let count = area.value.length
    let ins_area = document.querySelector(".letrs-count-span")
    // let spans = document.querySelectorAll(".ins-span")
    let count_div = ins_area.closest("div")
    if (count < 500) {
        let ins_area = document.querySelector(".letrs-count-span")
        ins_area.innerHTML = count
        if (count_div.classList.contains("disabled")){
            count_div.classList.remove("disabled")
        }
    }
    else {
        ins_area.innerHTML = '500'
        if (count_div.classList.contains("disabled")){
        //
        }
        else {
            count_div.classList.add("disabled")
        }


    }

}

// var input;


// function inputQuest (){
//     event.preventDefault()
//     // let input = document.querySelector(".create-ticket-file")
//     let input = document.createElement("input");
//     input.setAttribute("type", "file")
//     input.style.display = "none"
//     input.click()
//     // console.log("asd")
//
//     return input
// }
//
//
// function attachFileCreateTicket () {
//     let input = inputQuest()
//     let file = input.files[0]
//     // let file_url = URL.createObjectURL(file)
//
//
//
//     let data = file
//
// }
//
// function attachFilemeassge (e,el,id_ticket=null,sender,receiver) {
//     let input = inputQuest()
//     let file = input.files[0]
//     let int = setInterval(function (){
//         file = input.files[0]
//         if (file === undefined || file === null){
//         //
//         } else {
//
//             // let file_1 = URL.createObjectURL(file)
//             // let file_1 = URL.createObjectURL(file)
//             // var reader = new FileReader();
//             // var file = new ArrayBuffer(file);
//             // let file_1 = reader.readAsArrayBuffer(file)
//             // let file_1 = new ImageBitmap([file],{'type':file.type})
//             let file_1 = new Blob ([file],{'type':file.type})
//             // let url_ = URL.toDataURL(file_1)
//             // let file_2 = file_1.text()
//             // let reader = new ArrayBuffer(file_1);
//             // reader.Base64.encode(file_1)
//             let file_3 = URL.createObjectURL(file_1)
//             let file_4 = $(file_1).toDataURL
//             var reader = new FileReader();
//             reader.readAsBinaryString(file_1);
//             // let file_8 = new ImageBitmap(file,{'type':file.type})
//             // var fs = require('fs'),
//             data_1 = readFileSync(file);
//             console.log(data.toString('base64'));
//
//             // let file_url = URL.createObjectURL(file)
//
//             let data = reader
//             el.dataset['file_img'] = data
//             input.remove()
//             clearInterval(int)
//             sendMessage(id_ticket,sender,receiver,'pst_img')
//         }
//
//     },1000)
//
//
// }


// function getBase64(file,el) {
//     const reader = new FileReader()
//     let file_new = null
//     let file_new_p = new Promise(function () {
//         reader.readAsDataURL(file)
//         reader.onload = function () {
//             file_new = JSON.stringify({
//                 'file': reader.result,
//                 'file_type': el.type,
//                 'file_name': el.name
//             })
//
//         }
//
//     })
//     file_new_p.then(function (){
//         return file_new
//     })
// }
//
// async function fileListToBase64(fileList) {
//   const files_for_data = []
//
//   for (let i = 0; i < fileList.length; i++) {
//     let el = fileList[i]
//     files_for_data.push(getBase64(fileList[i],el))
//   }
//
//   return await Promise.all(files_for_data)
// }
function getBase64(file,el) {
    const reader = new FileReader()
    return new Promise(resolve => {
      reader.onload = ev => {
            resolve(file_new = JSON.stringify({
                'file': reader.result,
                'file_type': el.type,
                'file_name': el.name,
                'file_size': el.size
               })
            )
      }
      reader.readAsDataURL(file)

    })
}

async function fileListToBase64(fileList=null,file=null) {
  // create function which return resolved promise
  // with data:base64 string

  // here will be array of promisified functions
  const promises = []
  const promise = {}
  let return_ = null
  if (fileList) {
      return_ = promises
      // loop through fileList with for loop
      for (let i = 0; i < fileList.length; i++) {
         let el = fileList[i]
         promises.push(getBase64(fileList[i], el))
      }
      return await Promise.all(return_)
  } else if (file){
      const promise = {}
      return_ = promise
      promise[file.name] = getBase64(file, file)
      return await return_[file.name]
  }

      // array with base64 strings
  // return await Promise.all(return_)
}

async function attachFilemeassge (el,id_ticket=null,sender=null,receiver=null,change_avatar=null){
    // let file = el.files[0];
    let fileList = el.files;
    if (!change_avatar) {
        if (el.files.length > 0) {
            const files_for_data = await fileListToBase64(fileList)
            let data = files_for_data
            sendMessage(id_ticket, sender, receiver, data)
        }
    } else {
        if (el.files.length > 0) {
            let file = fileList[0]
            const files_for_data = await fileListToBase64(null,file)
            let data = files_for_data
            return data
        }
    }
    // let reader = new FileReader();
    // let mas_for_for_each = []

    // for (let i = 0;i < el.files.length;i++){
    //     let file_for_data_url_opert = el.files[i]
    //
    //     reader.readAsDataURL(file_for_data_url_opert);
    //     reader.onload = function() {
    //         // console.log(reader.result);
    //         let file = JSON.stringify({
    //             'file': reader.result,
    //             'file_type': el.files[i].type,
    //             'file_name': el.files[i].name
    //         })
    //         files_for_data.push(file)
    //     };
    //
    //     reader.onerror = function() {
    //         console.log(reader.error);
    //     };
    //
    // }










}

function change_profile_confirm (el){
    event.preventDefault()
    let form = el.form
    let formData = new FormData(form);
    // let changed_elements = {}
    // for (let i = 0;i < form.length;i++){
    //     let cur_el = form[i]
    //     if (cur_el.localName !== 'button'){
    //         let new_val = cur_el.value
    //         let dataset = cur_el.dataset
    //         let old_val = ''
    //         if (dataset){
    //             old_val = dataset['initialValue']
    //         }
    //         // if (old_val){
    //             if (old_val === new_val){
    //             //
    //             } else {
    //                 changed_elements[cur_el.name] = cur_el.value
    //             }
    //     }
    //     // }
    // }


    $.ajax({
        headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        url: '/user_account/change_profile_confirm/',
        type: "POST",
        // async: true,
        cache: false,
        processData: false,
        contentType: false,
        // enctype: 'json',
        // data: JSON.stringify(changed_elements),
        data: formData,
        success: function(data){
            middleWareJS()

            document.querySelector(".info_profile").innerHTML = data.html;

             document.getElementById('save_changes_txt').classList.add('hide')
             document.getElementById('changes_saved_txt').classList.add('show')



        },
        error: function (data){

            // document.querySelector(".avatar_user_profile").innerHTML = data.responseJSON.html;
            document.querySelector(".info_profile").innerHTML = data.responseJSON.html;

        }
    });
}



async function upload_photo_f_profile (el,files) {
    // if (!files){
    const data = await attachFilemeassge(el, null, null, null, 'avatar')
    // }
    // if (files){
    $.ajax({
        headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        url: '/user_account/change_avatar_confirm/',
        type: "POST",
        // async: true,
        cache: false,
        processData: false,
        contentType: false,
        // enctype: 'json',
        data: data,
        success: function(data){
            middleWareJS()

            document.querySelector(".avatar_user_profile").src = data.url;

        },
        error: function (data){

            document.querySelector(".avatar_user_profile").innerHTML = data.responseJSON.html;

        }
    });
    // }
}




function createTicket (el) {
    event.preventDefault()
    let form = el.form
    var formData = new FormData(form);
    $.ajax({
        headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        url: '/messages/create_ticket/',
        type: "POST",
        // async: true,
        cache: false,
        processData: false,
        contentType: false,
        // enctype: 'json',
        data: formData,
        success: function(data){
            middleWareJS()

            document.querySelector(".insert-tech-place").innerHTML = data.html;

        },
        error: function (data){

            document.querySelector(".insert-tech-place").innerHTML = data.responseJSON.html;

        }
    });
}



function selectedUserMessenger (ticket_id=null,user_id=null,el){
    let loader = document.querySelector(".loader_chat_f_sw_chats")
    let block_chat = document.querySelector(".block-chat")
    if (loader){
        loader.classList.toggle("show")
        if (block_chat) {
            block_chat.innerHTML = ''
        }
    } else if (block_chat){
        block_chat.innerHTML = `<img class="loader_chat_f_sw_chats show" src="/static/img/svg/loader.svg" alt="loader">`
    }
    let data = null
    let url = null
    if (ticket_id !== null){
        data = {
            'ticket_id': ticket_id
        }
        url = 'support_show_chat_by_ticket/'
    } else if (user_id !== null){
        data = {
            'user_id': user_id
        }
        url = 'show_chat_w_user/'
    }
    let user_type = getInfoAboutUser()
    if (user_type === 'mobile' || user_type === 'laptop') {
        open_curtain_w_contacts()
    }
    data['mobile'] = getInfoAboutUser() === 'mobile' || getInfoAboutUser() === 'laptop'
    $.ajax({
        headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        url: '/messages/' + url,
        type: "POST",
        // async: true,
        cache: false,
        processData: false,
        contentType: false,
        // enctype: 'json',
        data: JSON.stringify(data),
        success: function(data){
            middleWareJS()
            if (loader){
                loader.classList.toggle("show")
            }
            // let left_curtain = document.querySelector('.curtain.left')
            document.querySelector(".info_profile").innerHTML = data.html;
            document.querySelector(".enter-message-inp").focus()

            // document.querySelector(".tab_user_messanger.select").scrollIntoView({behavior: "smooth",block:'center',inline: 'start'});

            if (document.querySelector(".menu_buttons.curtain.left.open")){
                let top = document.querySelector(".tab_user_messanger.select").offsetTop
                document.querySelector(".menu_buttons.curtain.left.open").scrollTo({top:top})
            }
            // document.querySelector(".handler_curtain_left").classList.toggle("open")
            // document.querySelector(".handler_curtain_left").classList.toggle("close")
            // if (getInfoAboutUser() === 'mobile' || getInfoAboutUser() === 'laptop'){
            //     window.scrollTo({
            //       top: 0,
            //       left: 0
            //   // behavior: "smooth",
            //     });
            // }
            // if (window.location.host.includes('support') && user_type !== 'mobile' && user_type !== 'laptop'){
            //     let menu = document.querySelector(".menu_buttons.curtain.left")
            //     let container_user_messenger = document.querySelector(".container-messenger")
            //     if (!container_user_messenger.classList.contains('margin') || !menu.classList.contains('margin')){
            //         container_user_messenger.classList.add('margin')
            //         menu.classList.add('margin')
            //     }
            // }





        },
        error: function (data){

            document.querySelector(".info_profile").innerHTML = data.responseJSON.html;

        }
    });
}


function sendMessage(id_ticket=null,sender,receiver,files=null){
    // import {sendMessageSocket} from "./chat_sockets";
    if (!files){
        event.preventDefault()
    }
    let text = document.querySelector(".enter-message-inp").value
    document.querySelector(".enter-message-inp").value = null
    let img = document.querySelector(".attach-file-btn-message").dataset["file_img"]


    if (files){

        let data = {}
            if (id_ticket === null){
                data = {
                    'sender': sender,
                    'receiver': receiver,
                    'text': '',
                    'files': files
                }
            } else {
                 data = {
                    'ticket_id': id_ticket,
                    'sender': sender,
                    'receiver': receiver,
                    'text': '',
                    'files': files
                }
            }




        sendMessageSocket(data,img)

    } else {

        if (text.length === 0){
        //
        }
        else {
            let data = {}
            if (id_ticket === null){
                data = {
                    'sender': sender,
                    'receiver': receiver,
                    'text': text,
                    'img': img
                }
            } else {
                 data = {
                    'ticket_id': id_ticket,
                    'sender': sender,
                    'receiver': receiver,
                    'text': text,
                    'img': img
                }
            }



            // $.ajax({
            //     headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
            //     url: '/ru/messages/send_msg/',
            //     type: "POST",
            //     // async: true,
            //     cache: false,
            //     processData: false,
            //     contentType: false,
            //     // enctype: 'json',
            //     data: JSON.stringify(data),
            //     success: function(data){
            //         middleWareJS()
            //
            //         document.querySelector(".container-messages").innerHTML = data.html;
            //         document.querySelector(".enter-message-inp").focus()
            //
            //     },
            //     error: function (data){
            //
            //         document.querySelector(".container-messages").innerHTML = data.responseJSON.error;
            //         document.querySelector(".enter-message-inp").focus()
            //     }
            // });

            sendMessageSocket(data)
        }
    }
}

function openTicket (ticket_id){
    let data = {
        'ticket_id': ticket_id,
        'mobile': getInfoAboutUser() === 'mobile' || getInfoAboutUser() === 'laptop'
    }

    $.ajax({
        headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        url: '/messages/support_show_chat_by_ticket/',
        type: "POST",
        // async: true,
        cache: false,
        processData: false,
        contentType: false,
        // enctype: 'json',
        data: JSON.stringify(data),
        success: function(data){
            middleWareJS()

            document.querySelector(".info_profile").innerHTML = data.html;

        },
        error: function (data){

            document.querySelector(".info_profile").innerHTML = data.responseJSON.html;

        }
    });
}

function sendMessageEnter (e,id_ticket,sender,receiver){
    if (e.keyCode === 13){
        sendMessage(id_ticket,sender,receiver)
    }
    else {
    //
    }
}

function send_subscribe (id){
    let data  = {
        'subscribe_id':id
    }
    $.ajax({
        headers: { "X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val() },
        url: '/subscribes/subscribe_now/',
        type: "POST",
        // async: true,
        cache: false,
        processData: false,
        contentType: false,
        // enctype: 'json',
        data: JSON.stringify(data),
        success: function(data){

            document.querySelector(".info_profile").innerHTML = data.html;

        },
        error: function (data){

            document.querySelector(".info_profile").innerHTML = data.responseJSON.html;

        }
    });
}

var last_open_curtain = null

function open_curtain_w_btn_profile () {
    let curtain = document.querySelector(".menu_buttons.right")
    let user_type = getInfoAboutUser()
    if (user_type === 'mobile' && window.location.href.includes("profile")){
        curtain.classList.toggle("open")
        curtain.classList.toggle("close")
        toggle_cut_width_curtain(curtain)


        open_overlay(curtain)
        last_open_curtain = curtain
        set_curtain_z_index(curtain)
        close_first_curt(curtain)
        checkStateCurtain(curtain)
        // delete_margin_in_mobile()
    } else if (!window.location.href.includes("profile")){
        curtain.classList.toggle("open")
        curtain.classList.toggle("close")
        toggle_cut_width_curtain(curtain)


        open_overlay(curtain)
        last_open_curtain = curtain
        set_curtain_z_index(curtain)
        close_first_curt(curtain)

        // delete_margin_in_mobile()
    }
}

function checkStateCurtain (curtain) {
    if (curtain.classList.contains("open")){
        if (!document.querySelector(".block_overlay.show")){
            document.querySelector(".block_overlay").classList.add("show")
            document.querySelector(".block_overlay").classList.remove("hidden")
        }
        if (!curtain.parentNode.classList.contains("open")) {
            curtain.parentNode.classList.add("open")
            curtain.parentNode.classList.remove("close")
        }
    }
}

function open_curtain_w_contacts () {
    let curtain = document.querySelector('.menu_buttons.left')
    let btn_handler_left_curtain = document.querySelector(".handler_curtain_left")
    let user_type = getInfoAboutUser()
    if (user_type === 'mobile' && window.location.href.includes("profile") || user_type === 'laptop' && window.location.href.includes("profile")) {
        open_overlay(curtain)
        if (!curtain.parentNode.classList.contains('open') && !curtain.parentNode.classList.contains('close')){
            curtain.parentNode.classList.add("open")
        } else if (curtain.parentNode.classList.contains('open') || curtain.parentNode.classList.contains('close')) {
            curtain.parentNode.classList.toggle("open")
            curtain.parentNode.classList.toggle("close")
        }
        btn_handler_left_curtain.classList.toggle("open")
        btn_handler_left_curtain.classList.toggle("close")
        curtain.classList.toggle('open')
        curtain.classList.toggle('close')
        if (curtain.classList.contains('first')) {
            curtain.classList.remove('first')
            curtain.classList.toggle('open')
            curtain.classList.toggle('close')
        }

        set_curtain_z_index(curtain)
        last_open_curtain = curtain
        close_first_curt(curtain)
        // delete_margin_in_mobile()
    } else if (!window.location.href.includes("profile")){
        if (!curtain.parentNode.classList.contains('open') && !curtain.parentNode.classList.contains('close')){
            curtain.parentNode.classList.add("open")
        } else if (curtain.parentNode.classList.contains('open') || curtain.parentNode.classList.contains('close')) {
            curtain.parentNode.classList.toggle("open")
            curtain.parentNode.classList.toggle("close")
        }
        btn_handler_left_curtain.classList.toggle("open")
        btn_handler_left_curtain.classList.toggle("close")
        open_overlay(curtain)
        curtain.classList.toggle('open')
        curtain.classList.toggle('close')
        if (curtain.classList.contains('first')) {
            curtain.classList.remove('first')
            curtain.classList.toggle('open')
            curtain.classList.toggle('close')
        }

        set_curtain_z_index(curtain)
        last_open_curtain = curtain
        close_first_curt(curtain)
        checkStateCurtain(curtain)
        // delete_margin_in_mobile()
    }
}

function open_overlay (curtain) {
    let overlay = document.querySelector('.block_overlay')
    if (curtain.classList.contains("left")){
        if (!curtain.classList.contains('close') && overlay.classList.contains('show')){
            overlay.classList.toggle("show")
            overlay.classList.toggle("hidden")
        } else if (curtain.classList.contains('close') && overlay.classList.contains('show')){
            overlay.classList.remove("show")
            overlay.classList.add("hidden")
        } else if (!curtain.classList.contains('open') && overlay.classList.contains('hidden')){
            overlay.classList.add('show')
            overlay.classList.remove('hidden')
        } else if (curtain.classList.contains('open') && overlay.classList.contains('hidden') && window.location.href.includes('routes')) {
            overlay.classList.toggle('show')
            overlay.classList.toggle('hidden')
        }
    } else {
        if (!document.querySelector(".menu_buttons.left.first.open") && document.querySelector(".menu_buttons.left.open") && curtain.classList.contains('open') && !window.location.href.includes("profile")){

        } else {
            overlay.classList.toggle("show")
            overlay.classList.toggle("hidden")
        }
    }

}

// function delete__in_mobile () {
//     if (getInfoAboutUser() !== 'desctop'){
//         let left_curtain = document.querySelector(".curtain.left")
//         left_curtain.classList.remove('margin')
//     }
// }margin

function toggle_cut_width_curtain (el) {
    let parent = el.parentNode
    parent.classList.toggle('open')
    parent.classList.toggle('close')
}

function close_first_curt (curtain) {
    if (!window.location.href.includes("profile") || getInfoAboutUser() === 'mobile'){
        let curtains = document.querySelectorAll(".curtain.open")
        let i = 0
        if (curtains.length >= 2){
            curtains.forEach(function (){
                let cur_el = curtains[i]
                if (cur_el !== curtain){
                    cur_el.classList.toggle('open')
                    cur_el.classList.toggle('close')
                    if (cur_el.classList.contains('left')){
                        document.querySelector(".handler_curtain_left").classList.toggle("open")
                        document.querySelector(".handler_curtain_left").classList.toggle("close")
                    }
                    if (window.location.href.includes("route_search_results")){
                        scroll_ev(event,window)
                    }
                }
                i++
            })
        }
    }
}

function set_curtain_z_index (curtain) {

    let curtains = document.querySelectorAll(".curtain")
    let i = 0
    curtains.forEach(function (){
        let cur_el = curtains[i]
        if (cur_el === curtain){
            if (!cur_el.querySelector(".handler_menu") && cur_el.classList.contains("left")){
                let switcher = document.querySelector(".handler_curtain_left")
                switcher.style.zIndex = 101
            }
            cur_el.style.zIndex = 101
            cur_el.parentNode.style.zIndex = 101
        } else {
            if (!cur_el.querySelector(".handler_menu") && cur_el.classList.contains("left")){
                let switcher = document.querySelector(".handler_curtain_left")
                switcher.style.zIndex = 99
            }
            cur_el.style.zIndex = 99
            cur_el.parentNode.style.zIndex = 99
        }
        i++
    })

}

function close_open_curtain () {
    if (getInfoAboutUser() === 'mobile' || getInfoAboutUser() === 'laptop' || !window.location.href.includes("profile")){
        let btn_handler_left_curtain = document.querySelector(".handler_curtain_left")
        if (last_open_curtain){
            if (last_open_curtain.classList.contains("left")) {
                btn_handler_left_curtain.classList.toggle("open")
                btn_handler_left_curtain.classList.toggle("close")
            }
        }
        let curtains = document.querySelectorAll('.curtain.open')
        if (curtains.length >= 2) {
            if (last_open_curtain) {
                last_open_curtain.classList.toggle('open')
                last_open_curtain.classList.toggle('close')
                open_overlay(last_open_curtain)
                toggle_cut_width_curtain(last_open_curtain)
            } else if (window.location.href.includes('profile')) {
                let curtain = document.querySelector('.menu_buttons.right')
                curtain.classList.toggle('open')
                curtain.classList.toggle('close')
                open_overlay(curtain)
                toggle_cut_width_curtain(curtain)
            }

        } else {
            let curtain = document.querySelector('.curtain.open')
            curtain.classList.toggle("open")
            curtain.classList.toggle("close")
            open_overlay(curtain)
            toggle_cut_width_curtain(curtain)
        }

    } else {
        let curtain = document.querySelector('.menu_buttons.right')
        open_overlay(curtain)
    }
}

function reAdressClickOnbackrground (event,el) {
    // let overlay = document.querySelector(".block_overlay")
    if (!window.location.href.includes("profile") || getInfoAboutUser() === 'desctop'){
        let element_click = document.elementFromPoint(event.pageX, event.pageY)
        if (!element_click || element_click === el){
            element_click = document.querySelector(".block_overlay")
        }
        element_click.click()
    }
}

function setStandartSettingsToBlockOverlay () {
    let block_overlay = document.querySelector(".block_overlay")
    block_overlay.classList.remove("show")
    block_overlay.classList.add("hidden")
}

function show_header_list () {
    let el = document.querySelector(".menu_profile_btn")
    if (el) {
        el.classList.toggle("show")
        // closeCurtain()
    }
}

// function closeCurtain (left,right) {
//     if (left){
//         let left = document.querySelector(".curtain.left")
//         open_curtain(left,null,null,'close')
//     } else if (right){
//         let right = document.querySelector(".curtain.right")
//         open_curtain(null,right,null,'close')
//     } else {
//         let curtain = getOpenCurtain()
//         if (curtain) {
//             open_curtain(null, null, null, 'close')
//         }
//     }
// }
//
//
// function open_curtain (left=null,right=null,overlay=null,close=null){
//     let page_profile = true
//     let curtain_name = ''
//     let curtain = ''
//     let left_curtain = document.querySelector(".menu_buttons.left")
//     let right_curtain = document.querySelector(".menu_buttons.right")
//     let container_curtain = document.querySelector(".cut_width_f_curtain")
//     if (left || right){
//         if (left){
//             if (left_curtain){
//                 curtain = left_curtain
//                 newCurtainZIndex(curtain)
//                 if (right_curtain) {
//                     right_curtain.style.zIndex = '99'
//                     curtain_name = curtain.dataset['name']
//                 }
//             }
//
//         } else if (right) {
//             if (right_curtain){
//                 curtain = right_curtain
//                 newCurtainZIndex(curtain)
//                 if (left_curtain) {
//                     left_curtain.style.zIndex = '99'
//                     curtain_name = curtain.dataset['name']
//                 }
//             }
//         }
//     } else {
//         curtain = getOpenCurtain()
//         curtain_name = getInfoCurtain(null,null,'name')['name']
//     }
//     if (!curtain){
//         curtain = getCurtainWIncreaceZindex(left_curtain,right_curtain)
//     }
//     let body = document.querySelector('body')
//     if (curtain[0]){
//         let type_curt = getTypeOfData(curtain)
//         if (type_curt === 'object'){
//             curtain = curtain[0]
//         }
//     }
//     if (close){
//         // curtain = getOpenCurtain()
//         turnOffFirst(curtain)
//         let handler_text = curtain.querySelector('.text_f_curtain')
//
//         handler_text.innerHTML = curtain_name
//
//         let user_type = getInfoAboutUser()
//         if (user_type === 'mobile' || user_type === 'laptop') {
//             curtain.classList.remove("open")
//             curtain.classList.add("close")
//             container_curtain.classList.remove("open")
//             container_curtain.classList.add("close")
//
//             closeOverlay()
//         } else if (!window.location.href.includes('profile')){
//             //
//         } else {
//             curtain.classList.remove("open")
//             curtain.classList.add("close")
//             container_curtain.classList.remove("open")
//             container_curtain.classList.add("close")
//         }
//     } else if (curtain) {
//
//         if (curtain.classList.contains('first')){
//             turnOffFirst(curtain)
//             let overlay = document.querySelector('.block_overlay.show')
//             if (!overlay){
//                 let overlay_clear = document.querySelector(".block_overlay")
//                 overlay_clear.classList.toggle('show')
//                 overlay_clear.classList.toggle('hidden')
//
//             }
//             curtainMove(curtain,container_curtain,curtain_name,body)
//             curtain.classList.add("open")
//             curtain.classList.remove("close")
//         } else  {
//             curtainMove(curtain,container_curtain,curtain_name,body)
//         }
//     }
//
//
// }
//
// function curtainMove (curtain,container_curtain,curtain_name,body){
//     let user_type = getInfoAboutUser()
//     // if (user_type === 'mobile' || !window.location.host.includes('profile') || user_type === 'laptop' )
//     // artem заменил host на href ибо в host хранится имя хоста, а не путь в котором мы ищем profile
//     if (user_type === 'mobile' || !window.location.href.includes('profile') || user_type === 'laptop' )
//     {
//         curtain.classList.toggle("open")
//         curtain.classList.toggle("close")
//
//         // newCurtainZIndex()
//         let curtain_check = getOpenCurtain()
//         if (curtain_check.length > 1){
//             let curtain_old = getCurtainWIncreaceZindex(curtain_check,'decreace')
//             if (curtain_old){
//                 if (curtain_old['increace']){
//                     curtain_old = curtain_old['increace']
//                 }
//             }
//             curtain_old.classList.toggle("close")
//             curtain_old.classList.toggle("open")
//
//         }
//         if (curtain_check.length > 0){
//             let overlay = document.querySelector('.block_overlay.show')
//             if (!overlay){
//                 let overlay_clear = document.querySelector(".block_overlay")
//                 overlay_clear.classList.toggle('show')
//                 overlay_clear.classList.toggle('hidden')
//             }
//         } else {
//
//             closeOverlay()
//
//         }
//     } else {
//         if (!window.location.href.includes('profile')){
//         //
//         }
//     }
//
//
//     let handler_text = curtain.querySelector('.text_f_curtain')
//     // if (curtain.classList.contains('open')){
//     //     handler_text.innerHTML = 'Закрыть'
//     //     handler_text.style.fontSize = '13px'
//     // } else {
//     let name = ''
//     if (curtain_name){
//         name = curtain_name
//     } else {
//         name = curtain.dataset['name']
//     }
//
//     handler_text.innerHTML = name
//     handler_text.style.fontSize = '13px'
//     // }
//
//     if (!curtain.classList.contains('open')){
//         body.style.overflowY = ''
//     } else {
//         body.style.overflowY = 'hidden'
//     }
//     container_curtain.classList.toggle("close")
//     container_curtain.classList.toggle("open")
// }
//
// function check_ev_f_cut_width_f_curtain (event) {
//     let el = event.srcElement
//     if (!el.attributes.hasOwnProperty('onclick')){
//         open_curtain()
//     } else {
//         el.click()
//     }
//
// }
//
// function turnOffFirst (el) {
//     if (el.classList.contains('first')){
//         el.classList.remove('first')
//     }
// }
//
// function closeOverlay () {
//     let curt = getOpenCurtain()
//     if (curt.length > 1){
//     //     nothing to do
//     } else {
//         let overlay = document.querySelector(".block_overlay")
//         overlay.classList.add('hidden')
//         overlay.classList.remove('show')
//     }
// }
//
// function newCurtainZIndex (curtain) {
//     let cur_curtain = ''
//     if (curtain){
//         cur_curtain = curtain
//     } else {
//         cur_curtain = getOpenCurtain()[0]
//     }
//     if (cur_curtain){
//         cur_curtain.style.zIndex = '101'
//         let curtain_f_delete = forloopForCurtains(cur_curtain)
//         if (curtain_f_delete){
//             curtain_f_delete.style.zIndex = '99'
//         }
//     }
//
// }
//
// function forloopForCurtains (cur_curtain) {
//     let curtains = document.querySelectorAll('.curtain')
//     let curtain_f_delete = null
//     let i = 0
//     curtains.forEach(function (){
//         let el = curtains[i]
//         if (!el === cur_curtain){
//             curtain_f_delete = el
//         }
//         i++
//     })
//     return curtain_f_delete
// }
//
// function getInfoCurtain (curtain,zIndex,name) {
//     let data = {}
//     let curtain_f_w = null
//     if (curtain){
//         let type_curt = getTypeOfData(curtain)
//         if (curtain[0] && type_curt === 'object'){
//             curtain_f_w = curtain[0]
//         } else {
//             curtain_f_w = curtain
//         }
//
//
//     } else {
//         curtain_f_w = getOpenCurtain()
//         let type_curt = getTypeOfData(curtain_f_w)
//         if (curtain_f_w[0]){
//             if (type_curt === 'object'){
//                 curtain_f_w = curtain_f_w[0]
//             }
//         }
//
//     }
//     if (name){
//         let dataset = curtain_f_w.dataset
//         let name = ''
//         if (dataset){
//             name = dataset['name']
//             data['name'] = name
//         }
//     }
//     if (zIndex){
//         if (curtain_f_w.style.zIndex){
//             data['zIndex'] = curtain_f_w.style.zIndex
//         } else{
//             data['zIndex'] = 'z-index not finded'
//         }
//
//     }
//     return data
// }
//
// function getOpenCurtain () {
//     let curtains = document.querySelectorAll(".curtain.open")
//     if (!curtains){
//         curtains = document.querySelector('.curtain')
//         console.log(`not finded open curtain choice first curtain named=${curtains.dataset['name']}`)
//     }
//     return curtains
// }
//
//
//
// function getCurtainWIncreaceZindex (curtains,decreace) {
//     let first = getInfoCurtain(curtains[0],'zIndex')
//     let second =  getInfoCurtain(curtains[1],'zIndex')
//     let el_increace = ''
//     if (first['zIndex'] > second['zIndex']){
//         el_increace = first
//         if (decreace){
//             return {
//                 'increace': curtains[0],
//                 'decreace': curtains[1]
//             }
//         } else {
//             return curtains[0]
//         }
//
//     }
//     if (second['zIndex'] > first['zIndex']){
//         el_increace = second
//         if (decreace){
//             return {
//                 'increace': curtains[1],
//                 'decreace': curtains[0]
//             }
//         } else {
//             return curtains[1]
//         }
//
//     }
//
// }
