import{d as j}from"./datatables-I6jthWPb.js";import{r as w,g as p}from"./api-DZ3cXm1m.js";import{s as u,a as A}from"./toastr-helper-Wdj0mZjX.js";import{i as v}from"./tooltip-manager-D9RaGGY4.js";import"./bootstrap.esm-Bb7dPm-H.js";import"./datatables-Bo82YvhT.js";/* empty css                                  */let b;const{permissions:g,routes:m,translations:a}=window;function C(){const i=document.getElementById("aiStaffTable");i&&(b=$(i).DataTable({...j.getDefaultConfig(),serverSide:!0,dom:'<"d-flex justify-content-between align-items-center mb-3"<"d-flex align-items-center"l><"d-flex align-items-center"f>>t<"d-flex justify-content-between align-items-center mt-3"<"d-flex align-items-center"i><"d-flex align-items-center"p>>',ajax:{url:m.records,type:"GET",beforeSend:function(e){e.setRequestHeader("Authorization",`Bearer ${p()}`)},error:async function(e){e.status===401&&await w()&&b.ajax.reload()}},columns:[{data:"name",name:"name",render:function(e,n,t){var s;return(s=g.aiStaff)!=null&&s.update?`<a href="${m.edit.replace(":id",t.id)}" class="text-decoration-none fw-bold">${e}</a>`:e}},{data:"job_title",name:"job_title",render:function(e){return e||"-"}},{data:"status",name:"status",render:function(e){var s;const n={active:"bg-success",paused:"bg-warning",training:"bg-info"},t=((s=a.statuses)==null?void 0:s[e])||e;return`<span class="badge ${n[e]||"bg-secondary"}">${t}</span>`}},{data:"departments",name:"departments",orderable:!1,render:function(e){return!e||!e.length?"-":e.map(n=>`<span class="badge bg-secondary me-1">${n}</span>`).join("")}},{data:"created_at",name:"created_at",render:function(e){return e?`<small>${e}</small>`:"-"}},{data:"id",name:"actions",orderable:!1,searchable:!1,className:"text-end",render:function(e,n,t){var r,o,l,f,c,d,h;const s=[];if((r=g.aiStaff)!=null&&r.update){const y=m.edit.replace(":id",e);s.push(`
                            <a href="${y}"
                               class="btn btn-sm btn-outline-primary d-inline-flex align-items-center justify-content-center"
                               style="width: 32px; height: 32px;"
                               data-bs-toggle="tooltip"
                               title="${((o=a.buttons)==null?void 0:o.edit)||"Edit"}">
                                <i class="fas fa-edit"></i>
                            </a>`)}if((l=g.aiStaff)!=null&&l.update){const y=t.status==="active"?"fa-pause":"fa-play",T=t.status==="active"?((f=a.buttons)==null?void 0:f.pause)||"Pause":((c=a.buttons)==null?void 0:c.activate)||"Activate",x=t.status==="active"?"btn-outline-warning":"btn-outline-success";s.push(`
                            <button type="button"
                                    class="btn btn-sm ${x} d-inline-flex align-items-center justify-content-center ms-1 toggle-status-btn"
                                    style="width: 32px; height: 32px;"
                                    data-id="${e}"
                                    data-status="${t.status}"
                                    data-bs-toggle="tooltip"
                                    title="${T}">
                                <i class="fas ${y}"></i>
                            </button>`)}return(d=g.aiStaff)!=null&&d.delete&&s.push(`
                            <button type="button"
                                    class="btn btn-sm btn-outline-danger d-inline-flex align-items-center justify-content-center ms-1 delete-btn"
                                    style="width: 32px; height: 32px;"
                                    data-id="${e}"
                                    data-bs-toggle="tooltip"
                                    title="${((h=a.buttons)==null?void 0:h.delete)||"Delete"}">
                                <i class="fas fa-trash"></i>
                            </button>`),s.length===0?'<span class="text-muted">-</span>':`<div class="d-inline-flex justify-content-end">${s.join("")}</div>`}}],drawCallback:function(){v()}}))}async function E(i){var n,t,s;const e=m.toggleStatus.replace(":id",i);try{let r=await fetch(e,{method:"POST",headers:{Authorization:`Bearer ${p()}`,Accept:"application/json","Content-Type":"application/json"}});r.status===401&&await w()&&(r=await fetch(e,{method:"POST",headers:{Authorization:`Bearer ${p()}`,Accept:"application/json","Content-Type":"application/json"}}));const o=await r.json();o.success?(u("success",o.message||((n=a.messages)==null?void 0:n.status_updated)||"Status updated successfully."),b.ajax.reload(null,!1)):u("error",o.message||((t=a.messages)==null?void 0:t.error)||"An error occurred.")}catch(r){console.error("Toggle status error:",r),u("error",((s=a.messages)==null?void 0:s.error)||"An error occurred.")}}async function S(i){var t,s,r,o,l,f;if(!await A(((t=a.messages)==null?void 0:t.confirm_delete)||"Are you sure you want to delete this AI staff member?",{confirmClass:"btn-danger",confirmText:((s=a.buttons)==null?void 0:s.confirm)||"Confirm",cancelText:((r=a.buttons)==null?void 0:r.cancel)||"Cancel"}))return;const n=m.destroy.replace(":id",i);try{let c=await fetch(n,{method:"DELETE",headers:{Authorization:`Bearer ${p()}`,Accept:"application/json"}});c.status===401&&await w()&&(c=await fetch(n,{method:"DELETE",headers:{Authorization:`Bearer ${p()}`,Accept:"application/json"}}));const d=await c.json();d.success?(u("success",d.message||((o=a.messages)==null?void 0:o.deleted)||"AI staff member deleted successfully."),b.ajax.reload(null,!1)):u("error",d.message||((l=a.messages)==null?void 0:l.error)||"An error occurred.")}catch(c){console.error("Delete error:",c),u("error",((f=a.messages)==null?void 0:f.error)||"An error occurred.")}}function k(){document.addEventListener("click",function(i){const e=i.target.closest(".toggle-status-btn");if(e){const t=e.getAttribute("data-id");E(t)}const n=i.target.closest(".delete-btn");if(n){const t=n.getAttribute("data-id");S(t)}})}document.addEventListener("DOMContentLoaded",function(){C(),k()});
