import{g as f,r as w}from"./api-DZ3cXm1m.js";import{s as m,a as R}from"./toastr-helper-Wdj0mZjX.js";document.addEventListener("DOMContentLoaded",function(){const b=document.getElementById("aiStaffEditForm");if(!b)return;const{routes:p,translations:s}=window;b.addEventListener("submit",function(n){var t;if(!T())return n.preventDefault(),!1;const e=b.querySelector('button[type="submit"]')||document.querySelector(`button[type="submit"][form="${b.id}"]`);return e&&(e.disabled=!0,e.innerHTML='<i class="fas fa-spinner fa-spin me-2"></i>'+(((t=s.buttons)==null?void 0:t.saving)||"Saving...")),h=!1,!0});function T(){var r,a;let n=!0;const e=document.getElementById("name");e&&!e.value.trim()?(e.classList.add("is-invalid"),_(e,((r=s.validation)==null?void 0:r.name_required)||"Name is required."),n=!1):e&&(e.classList.remove("is-invalid"),g(e));const t=document.getElementById("job_title");return t&&!t.value.trim()?(t.classList.add("is-invalid"),_(t,((a=s.validation)==null?void 0:a.job_title_required)||"Job title is required."),n=!1):t&&(t.classList.remove("is-invalid"),g(t)),n}function _(n,e){g(n);const t=document.createElement("div");t.className="invalid-feedback",t.textContent=e,n.parentNode.appendChild(t)}function g(n){const e=n.parentNode.querySelector(".invalid-feedback");e&&e.remove()}["name","job_title"].forEach(function(n){const e=document.getElementById(n);e&&(e.addEventListener("blur",function(){var t;if(this.value.trim())this.classList.remove("is-invalid"),g(this);else{this.classList.add("is-invalid");const r=((t=s.validation)==null?void 0:t[n+"_required"])||n.replace("_"," ")+" is required.";_(this,r)}}),e.addEventListener("input",function(){this.value.trim()&&(this.classList.remove("is-invalid"),g(this))}))});let h=!1;b.addEventListener("input",function(){h=!0}),b.addEventListener("change",function(){h=!0}),window.addEventListener("beforeunload",function(n){var e;h&&(n.preventDefault(),n.returnValue=((e=s.messages)==null?void 0:e.unsaved_changes)||"You have unsaved changes. Are you sure you want to leave?")});const v=document.getElementById("departmentRulesContainer"),$=document.getElementById("addDepartmentRuleBtn");v&&y();async function y(){var n;if((n=p.departmentRules)!=null&&n.index)try{let e=await fetch(p.departmentRules.index,{method:"GET",headers:{Authorization:`Bearer ${f()}`,Accept:"application/json"}});e.status===401&&await w()&&(e=await fetch(p.departmentRules.index,{method:"GET",headers:{Authorization:`Bearer ${f()}`,Accept:"application/json"}}));const t=await e.json();t.success&&x(t.data||[])}catch(e){console.error("Load department rules error:",e)}}function x(n){var t,r,a,i;if(!v)return;if(n.length===0){v.innerHTML=`<p class="text-muted mb-0">${((t=s.department_rules)==null?void 0:t.no_rules)||"No department rules configured."}</p>`;return}let e=`
            <table class="table table-sm mb-0">
                <thead>
                    <tr>
                        <th>${((r=s.department_rules)==null?void 0:r.department)||"Department"}</th>
                        <th>${((a=s.department_rules)==null?void 0:a.mode)||"Mode"}</th>
                        <th class="text-end">${((i=s.department_rules)==null?void 0:i.actions)||"Actions"}</th>
                    </tr>
                </thead>
                <tbody>`;n.forEach(function(o){var l,d,c,u,L,E,S;e+=`
                <tr data-rule-id="${o.id}">
                    <td>
                        <span class="rule-department-text">${o.department_name||"-"}</span>
                        <select class="form-select form-select-sm rule-department-select d-none" data-original="${o.department_id}">
                            ${B(o.department_id)}
                        </select>
                    </td>
                    <td>
                        <span class="rule-mode-text">${j(o.mode)}</span>
                        <select class="form-select form-select-sm rule-mode-select d-none" data-original="${o.mode}">
                            <option value="auto" ${o.mode==="auto"?"selected":""}>${((l=s.department_rules)==null?void 0:l.mode_auto)||"Auto"}</option>
                            <option value="suggest" ${o.mode==="suggest"?"selected":""}>${((d=s.department_rules)==null?void 0:d.mode_suggest)||"Suggest"}</option>
                            <option value="disabled" ${o.mode==="disabled"?"selected":""}>${((c=s.department_rules)==null?void 0:c.mode_disabled)||"Disabled"}</option>
                        </select>
                    </td>
                    <td class="text-end">
                        <button type="button" class="btn btn-sm btn-outline-primary edit-rule-btn d-inline-flex align-items-center justify-content-center" style="width: 32px; height: 32px;" data-rule-id="${o.id}" data-bs-toggle="tooltip" title="${((u=s.buttons)==null?void 0:u.edit)||"Edit"}">
                            <i class="fas fa-edit"></i>
                        </button>
                        <button type="button" class="btn btn-sm btn-outline-success save-rule-btn d-none d-inline-flex align-items-center justify-content-center ms-1" style="width: 32px; height: 32px;" data-rule-id="${o.id}" data-bs-toggle="tooltip" title="${((L=s.buttons)==null?void 0:L.save)||"Save"}">
                            <i class="fas fa-check"></i>
                        </button>
                        <button type="button" class="btn btn-sm btn-outline-secondary cancel-rule-btn d-none d-inline-flex align-items-center justify-content-center ms-1" style="width: 32px; height: 32px;" data-rule-id="${o.id}" data-bs-toggle="tooltip" title="${((E=s.buttons)==null?void 0:E.cancel)||"Cancel"}">
                            <i class="fas fa-times"></i>
                        </button>
                        <button type="button" class="btn btn-sm btn-outline-danger delete-rule-btn d-inline-flex align-items-center justify-content-center ms-1" style="width: 32px; height: 32px;" data-rule-id="${o.id}" data-bs-toggle="tooltip" title="${((S=s.buttons)==null?void 0:S.delete)||"Delete"}">
                            <i class="fas fa-trash"></i>
                        </button>
                    </td>
                </tr>`}),e+="</tbody></table>",v.innerHTML=e}function B(n){var r;const e=window.departments||[];let t=`<option value="">${((r=s.department_rules)==null?void 0:r.select_department)||"-- Select --"}</option>`;return e.forEach(function(a){t+=`<option value="${a.id}" ${a.id==n?"selected":""}>${a.name}</option>`}),t}function j(n){var t,r,a;return{auto:((t=s.department_rules)==null?void 0:t.mode_auto)||"Auto",suggest:((r=s.department_rules)==null?void 0:r.mode_suggest)||"Suggest",disabled:((a=s.department_rules)==null?void 0:a.mode_disabled)||"Disabled"}[n]||n}$&&$.addEventListener("click",async function(){var t,r,a,i,o;if(!((t=p.departmentRules)!=null&&t.store))return;const n=document.getElementById("newRuleDepartment"),e=document.getElementById("newRuleMode");if(!n||!n.value){m("error",((r=s.department_rules)==null?void 0:r.select_department_error)||"Please select a department.");return}try{let l=await fetch(p.departmentRules.store,{method:"POST",headers:{Authorization:`Bearer ${f()}`,Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify({department_id:n.value,mode:e?e.value:"auto"})});l.status===401&&await w()&&(l=await fetch(p.departmentRules.store,{method:"POST",headers:{Authorization:`Bearer ${f()}`,Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify({department_id:n.value,mode:e?e.value:"auto"})}));const d=await l.json();d.success?(m("success",d.message||((a=s.department_rules)==null?void 0:a.added)||"Department rule added."),n.value="",e&&(e.value="auto"),y()):m("error",d.message||((i=s.messages)==null?void 0:i.error)||"An error occurred.")}catch(l){console.error("Add rule error:",l),m("error",((o=s.messages)==null?void 0:o.error)||"An error occurred.")}}),document.addEventListener("click",function(n){const e=n.target.closest(".edit-rule-btn");if(e){const i=e.getAttribute("data-rule-id");A(i,!0)}const t=n.target.closest(".cancel-rule-btn");if(t){const i=t.getAttribute("data-rule-id");A(i,!1)}const r=n.target.closest(".save-rule-btn");if(r){const i=r.getAttribute("data-rule-id");q(i)}const a=n.target.closest(".delete-rule-btn");if(a){const i=a.getAttribute("data-rule-id");D(i)}});function A(n,e){const t=document.querySelector(`tr[data-rule-id="${n}"]`);if(!t)return;const r=t.querySelector(".rule-department-text"),a=t.querySelector(".rule-department-select"),i=t.querySelector(".rule-mode-text"),o=t.querySelector(".rule-mode-select"),l=t.querySelector(".edit-rule-btn"),d=t.querySelector(".save-rule-btn"),c=t.querySelector(".cancel-rule-btn"),u=t.querySelector(".delete-rule-btn");e?(r.classList.add("d-none"),a.classList.remove("d-none"),i.classList.add("d-none"),o.classList.remove("d-none"),l.classList.add("d-none"),u.classList.add("d-none"),d.classList.remove("d-none"),c.classList.remove("d-none")):(r.classList.remove("d-none"),a.classList.add("d-none"),i.classList.remove("d-none"),o.classList.add("d-none"),l.classList.remove("d-none"),u.classList.remove("d-none"),d.classList.add("d-none"),c.classList.add("d-none"),a.value=a.getAttribute("data-original"),o.value=o.getAttribute("data-original"))}async function q(n){var i,o,l;const e=document.querySelector(`tr[data-rule-id="${n}"]`);if(!e)return;const t=e.querySelector(".rule-department-select"),r=e.querySelector(".rule-mode-select"),a=p.departmentRules.update.replace(":id",n);try{let d=await fetch(a,{method:"PUT",headers:{Authorization:`Bearer ${f()}`,Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify({department_id:t.value,mode:r.value})});d.status===401&&await w()&&(d=await fetch(a,{method:"PUT",headers:{Authorization:`Bearer ${f()}`,Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify({department_id:t.value,mode:r.value})}));const c=await d.json();c.success?(m("success",c.message||((i=s.department_rules)==null?void 0:i.updated)||"Department rule updated."),y()):m("error",c.message||((o=s.messages)==null?void 0:o.error)||"An error occurred.")}catch(d){console.error("Save rule error:",d),m("error",((l=s.messages)==null?void 0:l.error)||"An error occurred.")}}async function D(n){var r,a,i,o,l,d;if(!await R(((r=s.department_rules)==null?void 0:r.confirm_delete)||"Are you sure you want to delete this department rule?",{confirmClass:"btn-danger",confirmText:((a=s.buttons)==null?void 0:a.confirm)||"Confirm",cancelText:((i=s.buttons)==null?void 0:i.cancel)||"Cancel"}))return;const t=p.departmentRules.destroy.replace(":id",n);try{let c=await fetch(t,{method:"DELETE",headers:{Authorization:`Bearer ${f()}`,Accept:"application/json"}});c.status===401&&await w()&&(c=await fetch(t,{method:"DELETE",headers:{Authorization:`Bearer ${f()}`,Accept:"application/json"}}));const u=await c.json();u.success?(m("success",u.message||((o=s.department_rules)==null?void 0:o.deleted)||"Department rule deleted."),y()):m("error",u.message||((l=s.messages)==null?void 0:l.error)||"An error occurred.")}catch(c){console.error("Delete rule error:",c),m("error",((d=s.messages)==null?void 0:d.error)||"An error occurred.")}}});
