!function(){"use strict";var e=document.querySelector(".easychat-crm");if(e&&"1"!==e.dataset.ready){e.dataset.ready="1";var t=e.querySelector("#crm-status"),a=Array.prototype.slice.call(e.querySelectorAll("[data-crm-tab]")),n=Array.prototype.slice.call(e.querySelectorAll("[data-crm-form]")),r=[];a.forEach(function(e,t){e.addEventListener("click",function(){o(e.dataset.crmTab,!1)}),e.addEventListener("keydown",function(e){if("ArrowLeft"===e.key||"ArrowRight"===e.key){e.preventDefault();var n="ArrowRight"===e.key?(t+1)%a.length:(t+a.length-1)%a.length;o(a[n].dataset.crmTab,!0)}})}),n.forEach(function(t){var a=t.dataset.crmForm,n=t.querySelector("[data-crm-test]"),c=t.querySelector("[data-crm-save]");t.addEventListener("submit",function(r){r.preventDefault(),u("save",t,c).then(function(){n.disabled=!1;var r=t.querySelector("[data-amo-prepare]");r&&(r.disabled=!1);var c=e.querySelector('[data-crm-tab="'+a+'"] .easychat-crm__tab-state');c&&(c.textContent=t.elements.enabled.checked?"Включено":"Выключено")}).catch(function(){})}),n.addEventListener("click",function(){u("test",t,n).then(function(e){"amocrm"===a&&function(e,t){r=Array.isArray(e)?e:[];var a=t.querySelector("[data-amo-pipeline]"),n=String(a.value||"");if(a.textContent="",!r.length)return a.add(new Option("Доступные воронки не найдены","0")),void s(t);r.forEach(function(e){a.add(new Option(e.name,String(e.id),!1,String(e.id)===n))}),a.value||(a.selectedIndex=0),s(t)}(e.pipelines,t),"retailcrm"===a&&function(e,t){var a=t.querySelector("[data-retail-sites]");a&&(a.textContent="",(Array.isArray(e)?e:[]).forEach(function(e){var t=document.createElement("option");t.value=String(e.code),t.label=e.name,a.appendChild(t)}))}(e.sites,t)}).catch(function(){})});var o=t.querySelector("[data-amo-pipeline]");o&&o.addEventListener("change",function(){s(t)});var i=t.querySelector("[data-amo-prepare]");i&&i.addEventListener("click",function(){u("prepare",t,i).then(function(){var e=t.querySelector("[data-amo-fields-state]");e.textContent="Готовы",e.classList.add("is-ready")}).catch(function(){})})})}function c(e,a){t.textContent=e,t.className="alert "+(a?"alert-success":"alert-danger"),!window.matchMedia("(prefers-reduced-motion: reduce)").matches&&t.animate&&t.animate([{opacity:.35},{opacity:1}],{duration:160,easing:"ease-out"}),a||t.focus({preventScroll:!0})}function o(n,r){a.forEach(function(e){var t=e.dataset.crmTab===n;e.classList.toggle("is-active",t),e.setAttribute("aria-selected",t?"true":"false"),e.tabIndex=t?0:-1,t&&r&&e.focus()}),e.querySelectorAll("[data-crm-panel]").forEach(function(e){e.hidden=e.dataset.crmPanel!==n}),t.className="alert d-none",t.textContent=""}function i(e){e&&n.forEach(function(t){t.elements.nonce.value=e})}function d(e,t){t?(e.dataset.label=e.textContent,e.disabled=!0,e.setAttribute("aria-busy","true"),e.textContent="Подождите…"):(e.disabled=!1,e.removeAttribute("aria-busy"),e.textContent=e.dataset.label||e.textContent)}function u(e,t,a){return d(a,!0),fetch(Request.routeUrl("index.php?route=crm_integration/"+e),{method:"POST",body:new FormData(t),credentials:"same-origin",headers:{Accept:"application/json"}}).then(function(e){return e.json().catch(function(){return{}}).then(function(t){if(!e.ok)throw t;return t})}).then(function(e){return i(e.nonce),c(e.message||"Готово.",!0),e}).catch(function(e){throw i(e.nonce),c(e.error||"Не удалось выполнить действие. Проверьте подключение и повторите.",!1),e}).finally(function(){d(a,!1)})}function s(e){var t=e.querySelector("[data-amo-pipeline]"),a=e.querySelector("[data-amo-stage]"),n=String(a.value||""),c=r.find(function(e){return String(e.id)===String(t.value)});a.textContent="",(c&&Array.isArray(c.statuses)?c.statuses:[]).forEach(function(e){a.add(new Option(e.name,String(e.id),!1,String(e.id)===n))}),a.options.length||a.add(new Option("Доступные стадии не найдены","0"))}}();