import{r as i,ad as q,ae as M,j as e,D as _,N as L,k as F,e as y,A as T,Q as I,g as A,af as N,ag as Q,ah as O,B as R,T as B,C as H,I as w}from"./index-B1H8BCL7.js";import{d as U}from"./Add-DvXlrcIU.js";import{d as X}from"./Edit-BkCAgh-f.js";import{d as $,a as z,U as W}from"./UpgradeModal-BNZzCice.js";import{S as k}from"./Stack-BIAbaqpj.js";import{F as P}from"./FormControlLabel-DYmaLmBq.js";import{C as G}from"./Checkbox-C0X963rT.js";import{C as J}from"./Card-Dkp4tDNB.js";import{C as K}from"./CardContent-uvNuWwEy.js";import{T as V,c as Y,b as E,a as Z}from"./TableRow-BfKE_xn9.js";import{T as t}from"./TableCell-D8CotAsC.js";import"./useThemeProps-DL_l3A7P.js";import"./SwitchBase-BKMEO22T.js";const S={name:"",phone:"",address:"",isHq:!1};function ee({open:h,onClose:c,initial:r,onPlanLimitExceeded:u}){const[s,d]=i.useState(S),[o,x]=i.useState(null),[p,{isLoading:b}]=q(),[m,{isLoading:f}]=M();i.useEffect(()=>{h&&(d(r?{name:r.name||"",phone:r.phone||"",address:r.address||"",isHq:!!r.isHq}:S),x(null))},[h,r]);const g=async()=>{var n,C;x(null);try{r?await m({id:r.id,body:{name:s.name,phone:s.phone,address:s.address}}).unwrap():await p({name:s.name,phone:s.phone,address:s.address,isHq:s.isHq}).unwrap(),c==null||c()}catch(l){if(((n=l==null?void 0:l.data)==null?void 0:n.code)==="PLAN_LIMIT_EXCEEDED"){u==null||u();return}x(((C=l==null?void 0:l.data)==null?void 0:C.message)||"Save failed.")}};return e.jsxs(_,{open:h,onClose:c,maxWidth:"sm",fullWidth:!0,children:[e.jsx(L,{children:r?"Edit branch":"Add branch"}),e.jsx(F,{children:e.jsxs(k,{spacing:2,sx:{mt:1},children:[e.jsx(y,{label:"Name",value:s.name,required:!0,onChange:n=>d({...s,name:n.target.value})}),e.jsx(y,{label:"Phone",value:s.phone,onChange:n=>d({...s,phone:n.target.value})}),e.jsx(y,{label:"Address",value:s.address,multiline:!0,minRows:2,onChange:n=>d({...s,address:n.target.value})}),!r&&e.jsx(P,{control:e.jsx(G,{checked:s.isHq,onChange:n=>d({...s,isHq:n.target.checked})}),label:"Mark as HQ (only one HQ allowed)"}),o&&e.jsx(T,{severity:"error",children:o})]})}),e.jsxs(I,{children:[e.jsx(A,{onClick:c,children:"Cancel"}),e.jsx(A,{variant:"contained",onClick:g,disabled:b||f||!s.name,children:r?"Save":"Create"})]})]})}function me(){const{data:h=[],isLoading:c,refetch:r}=N(),[u]=Q(),[s]=O(),[d,o]=i.useState(!1),[x,p]=i.useState(null),[b,m]=i.useState(!1),[f,g]=i.useState(null),n=()=>{p(null),o(!0)},C=a=>{p(a),o(!0)},l=a=>{var j;return((j=a==null?void 0:a.data)==null?void 0:j.code)==="PLAN_LIMIT_EXCEEDED"?(o(!1),m(!0),!0):!1},D=async a=>{var j;g(null);try{a.active?await u(a.id).unwrap():await s(a.id).unwrap(),r()}catch(v){g(((j=v==null?void 0:v.data)==null?void 0:j.message)||"Action failed")}};return e.jsxs(R,{sx:{p:3},children:[e.jsxs(k,{direction:"row",justifyContent:"space-between",sx:{mb:2},children:[e.jsx(B,{variant:"h4",children:"Branches"}),e.jsx(A,{variant:"contained",startIcon:e.jsx(U,{}),onClick:n,children:"Add branch"})]}),f&&e.jsx(T,{severity:"error",sx:{mb:2},children:f}),e.jsx(J,{children:e.jsx(K,{children:c?e.jsx(B,{children:"Loading…"}):e.jsxs(V,{children:[e.jsx(Y,{children:e.jsxs(E,{children:[e.jsx(t,{children:"Name"}),e.jsx(t,{children:"Phone"}),e.jsx(t,{children:"HQ"}),e.jsx(t,{children:"Status"}),e.jsx(t,{align:"right",children:"Actions"})]})}),e.jsxs(Z,{children:[h.map(a=>e.jsxs(E,{children:[e.jsx(t,{children:a.name}),e.jsx(t,{children:a.phone||"—"}),e.jsx(t,{children:a.isHq?e.jsx(H,{size:"small",label:"HQ",color:"primary"}):"—"}),e.jsx(t,{children:e.jsx(H,{size:"small",color:a.active?"success":"default",label:a.active?"Active":"Disabled"})}),e.jsxs(t,{align:"right",children:[e.jsx(w,{onClick:()=>C(a),children:e.jsx(X,{})}),e.jsx(w,{onClick:()=>D(a),children:a.active?e.jsx($,{}):e.jsx(z,{})})]})]},a.id)),h.length===0&&e.jsx(E,{children:e.jsx(t,{colSpan:5,align:"center",children:e.jsx(B,{color:"text.secondary",children:"No branches yet."})})})]})]})})}),e.jsx(ee,{open:d,onClose:()=>o(!1),initial:x,onPlanLimitExceeded:()=>{o(!1),m(!0)},onError:l}),e.jsx(W,{open:b,onClose:()=>m(!1),featureCode:"MAX_BRANCHES"})]})}export{me as default};
