import{T as Z,M as j}from"./bootstrap.esm-Bb7dPm-H.js";import{s as P}from"./toastr-helper-Wdj0mZjX.js";document.addEventListener("DOMContentLoaded",function(){console.log("Ticket department edit page loaded"),U(),G(),Q(),ee(),te();const e=document.getElementById("editTicketDepartmentForm");console.log("Form elementssssss:",e)});function U(){[...document.querySelectorAll('[data-bs-toggle="tooltip"]')].map(s=>new Z(s,{boundary:document.body}))}function G(){const e=document.getElementById("edit_have_feedback_request"),s=document.getElementById("feedback_after_hours_container");e&&s&&(s.style.display=e.checked?"flex":"none",e.addEventListener("change",function(){s.style.display=this.checked?"flex":"none"}))}function Q(){const e=document.getElementById("addCustomField"),s=document.getElementById("customFieldsContainer");e&&s&&(V(),e.addEventListener("click",function(){X()}))}function V(){console.log("Loading existing custom fields..."),window.ticketDepartment&&window.ticketDepartment.customFields?(console.log("Found custom fields:",window.ticketDepartment.customFields),window.ticketDepartment.customFields.forEach((e,s)=>{console.log(`Adding custom field ${s}:`,e),X(e,s)})):console.log("No custom fields found")}function X(e={},s=null){var t,p,c,u,f,_,x,v,y,o,n,d,b,r,h,w,$,g,k,E,q,S,L,T,M,C,A,B,I,F,H,O,R,D;const a=document.getElementById("customFieldsContainer"),i=s!==null?s:a.children.length,l=`
        <div class="custom-field-row border rounded p-3 mb-3" data-index="${i}">
            <div class="row">
                <div class="col-md-3">
                    <label class="form-label">${((p=(t=window.translations)==null?void 0:t.custom_fields)==null?void 0:p.field_name)||"Field Name"}</label>
                    <input type="text" class="form-control" name="custom_fields[${i}][name]" value="${e.name||""}" required>
                </div>
                <div class="col-md-2">
                    <label class="form-label">${((u=(c=window.translations)==null?void 0:c.custom_fields)==null?void 0:u.field_type)||"Alan Tipi"}</label>
                    <select class="form-select field-type-select" name="custom_fields[${i}][type]" required>
                        <option value="text" ${e.type==="text"?"selected":""}>${((x=(_=(f=window.translations)==null?void 0:f.custom_fields)==null?void 0:_.types)==null?void 0:x.text)||"Metin"}</option>
                        <option value="textarea" ${e.type==="textarea"?"selected":""}>${((o=(y=(v=window.translations)==null?void 0:v.custom_fields)==null?void 0:y.types)==null?void 0:o.textarea)||"Uzun Metin"}</option>
                        <option value="select" ${e.type==="select"?"selected":""}>${((b=(d=(n=window.translations)==null?void 0:n.custom_fields)==null?void 0:d.types)==null?void 0:b.select)||"Select List"}</option>
                        <option value="checkbox" ${e.type==="checkbox"?"selected":""}>${((w=(h=(r=window.translations)==null?void 0:r.custom_fields)==null?void 0:h.types)==null?void 0:w.checkbox)||"Onay Kutusu"}</option>
                        <option value="radio" ${e.type==="radio"?"selected":""}>${((k=(g=($=window.translations)==null?void 0:$.custom_fields)==null?void 0:g.types)==null?void 0:k.radio)||"Radyo Buton"}</option>
                        <option value="email" ${e.type==="email"?"selected":""}>${((S=(q=(E=window.translations)==null?void 0:E.custom_fields)==null?void 0:q.types)==null?void 0:S.email)||"E-posta"}</option>
                        <option value="number" ${e.type==="number"?"selected":""}>${((M=(T=(L=window.translations)==null?void 0:L.custom_fields)==null?void 0:T.types)==null?void 0:M.number)||"Number"}</option>
                        <option value="date" ${e.type==="date"?"selected":""}>${((B=(A=(C=window.translations)==null?void 0:C.custom_fields)==null?void 0:A.types)==null?void 0:B.date)||"Tarih"}</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">${((F=(I=window.translations)==null?void 0:I.custom_fields)==null?void 0:F.required)||"Zorunlu"}</label>
                    <div class="form-check form-switch">
                        <input type="hidden" name="custom_fields[${i}][required]" value="0">
                        <input class="form-check-input" type="checkbox" name="custom_fields[${i}][required]" value="1" ${e.required?"checked":""}>
                    </div>
                </div>
                <div class="col-md-4 options-container" style="display: ${e.type==="select"||e.type==="radio"?"block":"none"};">
                    <label class="form-label">${((O=(H=window.translations)==null?void 0:H.custom_fields)==null?void 0:O.options)||"Options"}</label>
                    <div class="options-list">
                        ${e.options?e.options.map((J,K)=>{var z;return`
                            <div class="input-group mb-2">
                                <input type="text" class="form-control" name="custom_fields[${i}][options][]" value="${J}" placeholder="${((z=window.translations)==null?void 0:z.common_option)||"Option"} ${K+1}">
                                <button type="button" class="btn btn-outline-danger remove-option-btn">
                                    <i class="fas fa-minus"></i>
                                </button>
                            </div>
                        `}).join(""):""}
                    </div>
                    <button type="button" class="btn btn-sm btn-outline-primary add-option-btn">
                        <i class="fas fa-plus"></i> ${((D=(R=window.translations)==null?void 0:R.custom_fields)==null?void 0:D.add_option)||"Add Option"}
                    </button>
                </div>
                <div class="col-md-1 d-flex align-items-end">
                    <button type="button" class="btn btn-outline-danger remove-field-btn">
                        <i class="fas fa-trash"></i>
                    </button>
                </div>
            </div>
        </div>
    `;a.insertAdjacentHTML("beforeend",l);const m=a.lastElementChild;W(m,i)}function W(e,s){const a=e.querySelector(".field-type-select"),i=e.querySelector(".options-container"),l=e.querySelector(".remove-field-btn"),m=e.querySelector(".add-option-btn");a.addEventListener("change",function(){const t=this.value==="select"||this.value==="radio";i.style.display=t?"block":"none"}),l.addEventListener("click",function(){e.remove(),Y()}),m.addEventListener("click",function(){const t=e.querySelector(".options-list"),p=t.children.length,c=`
            <div class="input-group mb-2">
                <input type="text" class="form-control" name="custom_fields[${s}][options][]" placeholder="Seçenek ${p+1}">
                <button type="button" class="btn btn-outline-danger remove-option-btn">
                    <i class="fas fa-minus"></i>
                </button>
            </div>
        `;t.insertAdjacentHTML("beforeend",c);const u=t.lastElementChild;u.querySelector(".remove-option-btn").addEventListener("click",function(){u.remove()})}),e.querySelectorAll(".remove-option-btn").forEach(t=>{t.addEventListener("click",function(){t.closest(".input-group").remove()})})}function Y(){const e=document.getElementById("customFieldsContainer");Array.from(e.children).forEach((s,a)=>{s.setAttribute("data-index",a),s.querySelectorAll("input, select").forEach(i=>{const l=i.getAttribute("name");if(l&&l.includes("custom_fields[")){const m=l.replace(/custom_fields\[\d+\]/g,"custom_fields["+a+"]");i.setAttribute("name",m)}})})}function ee(){var x,v,y;const e=document.getElementById("mailboxesContainer"),s=document.getElementById("addMailboxBtn"),a=3,i=document.getElementById("mailboxEditModal");if(!e)return;const l=i?new j(i):null,m=window.mbxLabels||{},t=o=>document.getElementById(o);function p(){const o=e.querySelectorAll(".mailbox-card").length;if(s){s.disabled=o>=a;const n=s.querySelector("small");n&&(n.textContent=`(${o}/${a})`)}}function c(o){return m[o]||o}function u(){const o=Date.now(),n=document.createElement("tr");return n.className="mailbox-card",n.dataset.index=o,n.innerHTML=`
            <td>
                <input type="hidden" name="mailboxes[${o}][id]" value="" class="mbx-id">
                <input type="hidden" name="mailboxes[${o}][label]" value="" class="mbx-label">
                <input type="hidden" name="mailboxes[${o}][protocol]" value="imap" class="mbx-protocol">
                <input type="hidden" name="mailboxes[${o}][hostname]" value="" class="mbx-hostname">
                <input type="hidden" name="mailboxes[${o}][port]" value="993" class="mbx-port">
                <input type="hidden" name="mailboxes[${o}][email]" value="" class="mbx-email">
                <input type="hidden" name="mailboxes[${o}][password]" value="" class="mbx-password">
                <input type="hidden" name="mailboxes[${o}][post_action]" value="mark_read" class="mbx-postaction">
                <input type="checkbox" name="mailboxes[${o}][ssl]" class="mbx-ssl d-none" checked>
                <input type="checkbox" name="mailboxes[${o}][enabled]" class="mbx-enabled d-none" checked>
                <span class="fw-semibold mbx-label-text"></span>
            </td>
            <td><span class="badge bg-secondary mbx-protocol-text">IMAP</span></td>
            <td class="small mbx-email-text"></td>
            <td class="text-center mbx-ssl-text"><i class="fas fa-check text-success"></i></td>
            <td class="text-center mbx-enabled-text"><span class="badge bg-success">${m.enabled||"Enabled"}</span></td>
            <td><span class="badge bg-info mbx-postaction-text">${c("mark_read")}</span></td>
            <td class="text-center"><div class="btn-group btn-group-sm">
                <button type="button" class="btn btn-outline-primary edit-mailbox"><i class="fas fa-edit"></i></button>
                <button type="button" class="btn btn-outline-info test-mailbox-btn"><i class="fas fa-plug"></i></button>
                <button type="button" class="btn btn-outline-danger remove-mailbox"><i class="fas fa-trash"></i></button>
            </div></td>`,n}function f(o){const n=b=>o.querySelector("."+b);t("mbm_index").value=o.dataset.index,t("mbm_label").value=n("mbx-label").value,t("mbm_protocol").value=n("mbx-protocol").value||"imap",t("mbm_hostname").value=n("mbx-hostname").value,t("mbm_port").value=n("mbx-port").value||993,t("mbm_email").value=n("mbx-email").value,t("mbm_password").value="",t("mbm_post_action").value=n("mbx-postaction").value||"mark_read",t("mbm_ssl").checked=n("mbx-ssl").checked,t("mbm_enabled").checked=n("mbx-enabled").checked;const d=!!n("mbx-id").value;t("mbm_pw_hint").classList.toggle("d-none",!d),t("mbm_password").placeholder=d?"••••••":""}function _(o){const n=(w,$)=>{const g=o.querySelector("."+w);g&&(g.value=$)},d=t("mbm_label").value,b=t("mbm_protocol").value,r=t("mbm_email").value;n("mbx-label",d),n("mbx-protocol",b),n("mbx-hostname",t("mbm_hostname").value),n("mbx-port",t("mbm_port").value),n("mbx-email",r),n("mbx-postaction",t("mbm_post_action").value);const h=t("mbm_password").value;h&&n("mbx-password",h),o.querySelector(".mbx-ssl").checked=t("mbm_ssl").checked,o.querySelector(".mbx-enabled").checked=t("mbm_enabled").checked,o.querySelector(".mbx-label-text").textContent=d||"—",o.querySelector(".mbx-protocol-text").textContent=b.toUpperCase(),o.querySelector(".mbx-email-text").textContent=r,o.querySelector(".mbx-ssl-text").innerHTML=t("mbm_ssl").checked?'<i class="fas fa-check text-success"></i>':'<i class="fas fa-minus text-muted"></i>',o.querySelector(".mbx-enabled-text").innerHTML=t("mbm_enabled").checked?`<span class="badge bg-success">${m.enabled||"Enabled"}</span>`:'<span class="badge bg-secondary">—</span>',o.querySelector(".mbx-postaction-text").textContent=c(t("mbm_post_action").value)}s==null||s.addEventListener("click",()=>{var n;if(e.querySelectorAll(".mailbox-card").length>=a)return;(n=t("noMailboxMessage"))==null||n.remove();const o=u();e.appendChild(o),p(),f(o),l==null||l.show()}),e.addEventListener("click",o=>{const n=o.target.closest(".edit-mailbox");if(n){f(n.closest(".mailbox-card")),l==null||l.show();return}const d=o.target.closest(".remove-mailbox");if(d){d.closest(".mailbox-card").remove(),p();return}const b=o.target.closest(".test-mailbox-btn");if(b){const r=b.closest(".mailbox-card");N({hostname:r.querySelector(".mbx-hostname").value,port:r.querySelector(".mbx-port").value,email:r.querySelector(".mbx-email").value,password:r.querySelector(".mbx-password").value,protocol:r.querySelector(".mbx-protocol").value,mailbox_id:r.querySelector(".mbx-id").value||null})}}),(x=t("mbm_protocol"))==null||x.addEventListener("change",o=>{const n=t("mbm_port"),d=parseInt(n.value);([993,995,110,143].includes(d)||!d)&&(n.value=o.target.value==="imap"?993:995)}),(v=t("mbm_save"))==null||v.addEventListener("click",()=>{const o=e.querySelector(`.mailbox-card[data-index="${t("mbm_index").value}"]`);if(o){if(!t("mbm_hostname").value||!t("mbm_email").value){window.showAlert&&P("error",m.host_email_required||"Hostname and email are required.");return}_(o),l==null||l.hide()}}),(y=t("mbm_test"))==null||y.addEventListener("click",()=>{var o;N({hostname:t("mbm_hostname").value,port:t("mbm_port").value,email:t("mbm_email").value,password:t("mbm_password").value,protocol:t("mbm_protocol").value,mailbox_id:((o=e.querySelector(`.mailbox-card[data-index="${t("mbm_index").value}"] .mbx-id`))==null?void 0:o.value)||null})})}function N(e){const s=e.hostname,a=e.port,i=e.email,l=e.password,m=e.protocol||"imap",t=e.mailbox_id||null;if(!s||!i){window.showAlert&&P("error","Hostname and email are required.");return}new j(document.getElementById("mailTestModal")).show(),document.getElementById("mailTestResults").innerHTML='<div class="text-center"><i class="fas fa-spinner fa-spin"></i> Testing connection...</div>',fetch(window.routes.ticketDepartments.testMailConnection,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer "+document.querySelector('meta[name="api-token"]').getAttribute("content")},body:JSON.stringify({hostname:s,port:parseInt(a)||995,email:i,password:l||"",protocol:m,mailbox_id:t})}).then(c=>c.json()).then(c=>{var f;const u=document.getElementById("mailTestResults");c.success?u.innerHTML=`<div class="alert alert-success mb-0"><i class="fas fa-check-circle me-2"></i>${c.message}${((f=c.details)==null?void 0:f.messages)!==void 0?" ("+c.details.messages+" messages)":""}</div>`:u.innerHTML=`<div class="alert alert-danger mb-0"><i class="fas fa-times-circle me-2"></i>${c.message}</div>`}).catch(()=>{document.getElementById("mailTestResults").innerHTML='<div class="alert alert-danger mb-0">Connection test failed.</div>'})}function te(){document.querySelectorAll('[data-bs-toggle="tab"]').forEach(s=>{s.addEventListener("shown.bs.tab",function(a){U()})})}
