$(document).ready(function(){const r=document.getElementById("related_item"),d=document.getElementById("related-item-count"),m=document.getElementById("client_product_id"),u=document.getElementById("client_domain_id");g();function g(){const t={placeholder:window.ticketConfig.translations.search_client,allowClear:!0,ajax:{url:window.ticketConfig.routes.clientSearch,dataType:"json",delay:250,headers:{Authorization:"Bearer "+document.querySelector('meta[name="api-token"]').getAttribute("content"),Accept:"application/json"},data:function(e){return{q:e.term}},processResults:function(e){return{results:e.results.map(function(n){var i;return{id:n.id,text:n.text,name:n.name||n.text.split(" (")[0],email:n.email||((i=n.text.match(/\((.*?)\)/))==null?void 0:i[1])}})}},cache:!0},templateResult:h,templateSelection:C,minimumInputLength:2};if(window.ticketConfig.selectedClient&&(t.minimumInputLength=0),$("#client_id").select2(t),window.ticketConfig.selectedClient){const e=window.ticketConfig.selectedClient,n=$("<option></option>").val(e.id).text(e.name+" ("+e.email+")").attr("selected",!0).data("name",e.name).data("email",e.email);$("#client_id").append(n).trigger("change")}}function h(t){var o;if(t.loading)return t.text;const e=t.name||t.text.split(" (")[0]||"",n=t.email||((o=t.text.match(/\((.*?)\)/))==null?void 0:o[1])||"";return $('<div class="select2-client-result"><div class="select2-client-name">'+e+'</div><div class="select2-client-email">'+n+"</div></div>")}function C(t){var i;const e=t.name||$(t.element).data("name")||t.text.split(" (")[0]||"",n=t.email||$(t.element).data("email")||((i=t.text.match(/\((.*?)\)/))==null?void 0:i[1])||"";return e+" ("+n+")"}function w(t){if(!t){r.innerHTML='<option value="">'+window.ticketConfig.translations.none+"</option>",d.textContent="",m.value="",u.value="";return}let e=0;const n=window.ticketConfig.routes.clientProducts.replace(":client",t),i=window.ticketConfig.routes.clientDomains.replace(":client",t);r.innerHTML='<option value="">'+window.ticketConfig.translations.none+"</option>",d.textContent="",m.value="",u.value="",Promise.all([fetch(n,{headers:{Authorization:"Bearer "+document.querySelector('meta[name="api-token"]').getAttribute("content"),Accept:"application/json"}}).then(o=>o.json()),fetch(i,{headers:{Authorization:"Bearer "+document.querySelector('meta[name="api-token"]').getAttribute("content"),Accept:"application/json"}}).then(o=>o.json())]).then(([o,s])=>{if(o.length>0){const a=document.createElement("optgroup");a.label=window.ticketConfig.translations.select_product,o.forEach(c=>{const l=document.createElement("option");l.value="product-"+c.id,l.textContent=c.name,a.appendChild(l)}),r.appendChild(a),e+=o.length}if(s.length>0){const a=document.createElement("optgroup");a.label=window.ticketConfig.translations.select_domain,s.forEach(c=>{const l=document.createElement("option");l.value="domain-"+c.id,l.textContent=c.name,a.appendChild(l)}),r.appendChild(a),e+=s.length}if(e>0){const a=o.length>0?o.length+" "+window.ticketConfig.translations.products_found:"",c=s.length>0?s.length+" "+window.ticketConfig.translations.domains_found:"",l=o.length>0&&s.length>0?", ":"";d.textContent=a+l+c}else d.textContent=window.ticketConfig.translations.no_products+", "+window.ticketConfig.translations.no_domains}).catch(o=>{console.error("Error loading products/domains:",o),d.textContent=window.ticketConfig.translations.error_loading})}r.addEventListener("change",function(){const t=this.value;if(m.value="",u.value="",!t)return;const[e,n]=t.split("-");e==="product"?m.value=n:e==="domain"&&(u.value=n)}),$("#client_id").on("change",function(){const t=this.value;w(t)});const p=document.getElementById("fileInput"),f=document.getElementById("selectFilesBtn"),v=document.getElementById("attachments-list");document.getElementById("attachment_ids"),f==null||f.addEventListener("click",()=>{p.click()}),p==null||p.addEventListener("change",function(t){Array.from(t.target.files).forEach(n=>{const i=document.createElement("div");i.className="alert alert-info alert-dismissible fade show mb-2",i.innerHTML=`
                <small><i class="fas fa-file me-1"></i>${n.name}</small>
                <button type="button" class="btn-close btn-close-sm" data-bs-dismiss="alert"></button>
            `,v.appendChild(i)})})});
