import{a as e}from"./rolldown-runtime-B0Z9INg1.js";import{$ as t}from"./vendor-charts-iXlq6Rne.js";import{_ as n,g as r}from"./vendor-react-Dzc9WstK.js";import{c as i}from"./api-CqQWIBIg.js";import{t as a}from"./arrow-left-BHEa0Gjf.js";import{t as o}from"./key-round-DLPxX-v4.js";import{_ as s}from"./vendor-flow-IzlrcDx6.js";import{i as c,n as l}from"./index-HkVQGc9M.js";import{t as u}from"./ConfirmDialog-BTjlqE-B.js";import{t as d}from"./extractErrorMessage-DBWk03ld.js";import{Ct as f,H as p,Qt as m,ht as h}from"./mastersRtkApi-BlaMikAe.js";import{t as g}from"./StatusBadge-D6eiAqxN.js";import{t as _}from"./AgentsTab-DH9tEmjp.js";var v=e(t(),1),y=s();function b(){let{id:e=``}=n(),t=r(),{canEdit:s,canDelete:b}=l(`masters`),{data:S,isLoading:C,isError:w}=h(e),[T,E]=(0,v.useState)(!1),[D,O]=(0,v.useState)(!1),[k,{isLoading:A}]=p(),[j,{isLoading:M}]=f(),[N]=m();return C?(0,y.jsx)(`div`,{className:`p-8 text-center text-[var(--color-text-muted)]`,children:`Loading agent…`}):w||!S?(0,y.jsxs)(`div`,{className:`p-8 text-center`,children:[(0,y.jsx)(`p`,{className:`text-[var(--color-text-secondary)]`,children:`Couldn't load this agent.`}),(0,y.jsxs)(c,{variant:`outline`,className:`mt-3`,onClick:()=>t(`/admin/masters/agents`),children:[(0,y.jsx)(a,{className:`w-4 h-4`}),`Back to agents`]})]}):(0,y.jsxs)(y.Fragment,{children:[(0,y.jsxs)(`div`,{className:`max-w-3xl mx-auto`,children:[(0,y.jsxs)(`div`,{className:`flex items-center gap-4 mb-6`,children:[(0,y.jsx)(`button`,{onClick:()=>t(`/admin/masters/agents`),className:`p-2 rounded-lg hover:bg-slate-100 dark:hover:bg-slate-800 text-[var(--color-text-muted)] hover:text-[var(--color-text-primary)] transition-colors`,children:(0,y.jsx)(a,{className:`w-4 h-4`})}),(0,y.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,y.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,y.jsx)(`h1`,{className:`text-xl font-bold text-[var(--color-text-primary)]`,children:S.name}),(0,y.jsx)(g,{label:S.is_active?`Active`:`Inactive`,variant:S.is_active?`success`:`default`})]}),(0,y.jsx)(`p`,{className:`text-sm text-[var(--color-text-muted)] font-mono`,children:S.code})]}),s&&(0,y.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,y.jsx)(c,{variant:`outline`,onClick:()=>E(!0),children:`Edit`}),b&&(0,y.jsx)(c,{variant:`ghost`,onClick:()=>O(!0),children:`Delete`})]})]}),(0,y.jsxs)(`div`,{className:`bg-[var(--color-surface)] border border-[var(--color-border)] rounded-xl shadow-[var(--shadow-sm)] divide-y divide-[var(--color-border)]`,children:[(0,y.jsx)(x,{label:`Phone`,value:S.phone||`—`}),(0,y.jsx)(x,{label:`Email`,value:S.email||`—`}),(0,y.jsx)(x,{label:`Zone`,value:S.zone_name||`—`}),(0,y.jsx)(x,{label:`Service areas`,value:S.service_area_names?.length?S.service_area_names.join(`, `):`—`}),(0,y.jsx)(x,{label:`ERP code`,value:S.erp_code||`—`}),(0,y.jsx)(x,{label:`Sort order`,value:String(S.sort_order??0)}),(0,y.jsx)(x,{label:`Portal login`,children:S.can_sign_in?(0,y.jsxs)(`span`,{className:`inline-flex items-center gap-2`,children:[(0,y.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-sm font-medium text-[var(--color-success)]`,children:[(0,y.jsx)(o,{className:`h-3 w-3`}),S.login_username]}),s&&(0,y.jsx)(`button`,{type:`button`,onClick:async()=>{if(S)try{await N(S.id).unwrap(),i.success(`Portal access removed for ${S.name}.`)}catch(e){i.error(d(e))}},className:`text-[11px] text-[var(--color-text-muted)] hover:text-[var(--color-error)]`,children:`Revoke`})]}):s?(0,y.jsxs)(c,{variant:`ghost`,size:`sm`,className:`gap-1.5`,onClick:async()=>{if(S)try{await j(S.id).unwrap(),i.success(`${S.name} can now sign in with ${S.phone}.`)}catch(e){i.error(d(e))}},isLoading:M,children:[(0,y.jsx)(o,{className:`h-3.5 w-3.5`}),`Grant access`]}):(0,y.jsx)(`span`,{className:`text-sm text-[var(--color-text-muted)]`,children:`—`})}),(0,y.jsx)(x,{label:`Customers`,value:String(S.customer_count??0)}),S.address&&(0,y.jsx)(x,{label:`Address`,value:S.address}),S.description&&(0,y.jsx)(x,{label:`Notes`,value:S.description})]})]}),T&&(0,y.jsx)(_,{agent:S,onClose:()=>E(!1)}),(0,y.jsx)(u,{isOpen:D,title:`Delete agent`,isLoading:A,message:(0,y.jsxs)(y.Fragment,{children:[(0,y.jsx)(`strong`,{children:S.name}),` will be permanently deleted. An agent with customers booked under them cannot be deleted.`]}),onConfirm:async()=>{try{await k(e).unwrap(),i.success(`Agent deleted`),t(`/admin/masters/agents`)}catch(e){i.error(d(e))}},onCancel:()=>O(!1)})]})}function x({label:e,value:t,children:n}){return(0,y.jsxs)(`div`,{className:`flex items-start gap-4 px-5 py-3.5`,children:[(0,y.jsx)(`span`,{className:`w-36 flex-shrink-0 text-sm text-[var(--color-text-muted)]`,children:e}),n??(0,y.jsx)(`span`,{className:`text-sm text-[var(--color-text-primary)]`,children:t})]})}export{b as default};