import{O as I,ac as K,r as m,j as s,b as r,S as g,q as h,E as L,d as R,an as W,a0 as A,aI as b,af as U,aK as D,B as p,T as f,aL as M,aM as P,a7 as q,o as y,a4 as G,aN as H,aO as Q,aP as v,F as J,a6 as O}from"./index-ktJf6c6X.js";import{d as V}from"./ExpandLess-D1GvYHTD.js";import{u as X}from"./QuotationApi-B-FNbfPn.js";const Y=()=>({_key:crypto.randomUUID(),serviceCode:"",serviceName:"",isChargeable:!0,remarks:"",charges:[]}),re=({module:x,services:i,readOnly:o=!1,onServicesChange:d,taxMode:z="UNKNOWN"})=>{const u=I(),[N,{data:S=[]}]=X(),[w,_]=K.useState(new Set);m.useEffect(()=>{N(x)},[x]);const $=m.useCallback(()=>{d([...i,Y()])},[i,d]),j=m.useCallback(e=>{d(i.filter((a,t)=>t!==e))},[i,d]),n=m.useCallback((e,a)=>{const t=[...i];t[e]={...t[e],...a},d(t)},[i,d]),E=m.useCallback((e,a)=>{if(!a){n(e,{serviceCode:"",serviceName:"",serviceConfigId:void 0});return}n(e,{serviceConfigId:a.id,serviceCode:a.serviceCode,serviceName:a.name,isChargeable:a.defaultChargeable,remarks:a.defaultRemarks??""})},[n]),T=e=>{_(a=>{const t=new Set(a);return t.has(e)?t.delete(e):t.add(e),t})},B=(e,a)=>{var t;return e._key??((t=e.id)==null?void 0:t.toString())??String(a)};return s(p,{children:[s(g,{direction:"row",justifyContent:"space-between",alignItems:"center",sx:{mb:1.5},children:[s(h,{variant:"subtitle1",fontWeight:600,children:["Services",i.length>0&&r(L,{label:i.length,size:"small",color:"primary",sx:{ml:1,height:20,fontSize:11}})]}),!o&&r(W,{variant:"outlined",size:"small",startIcon:r(R,{}),onClick:$,children:"Add Service"})]}),i.length===0&&r(h,{variant:"body2",color:"text.disabled",sx:{py:2},children:'No services added. Click "Add Service" to begin.'}),r(g,{spacing:1.5,children:i.map((e,a)=>{var k;const t=B(e,a),C=w.has(t),F=((k=e.charges)==null?void 0:k.length)??0;return s(A,{variant:"outlined",sx:{p:1.5,borderRadius:1.5,borderColor:b(u.palette.primary.main,.2),bgcolor:b(u.palette.primary.main,.02)},children:[s(g,{direction:{xs:"column",sm:"row"},spacing:1.5,alignItems:"flex-start",children:[r(U,{sx:{flex:1,minWidth:200},size:"small",options:S,getOptionLabel:l=>l.name,value:S.find(l=>l.id===e.serviceConfigId)??null,onChange:(l,c)=>E(a,c),disabled:o,freeSolo:!1,renderInput:l=>r(f,{...l,label:"Service",placeholder:"Select or type service name",size:"small"}),renderOption:(l,c)=>D(p,{component:"li",...l,key:c.id},s(p,{children:[r(h,{variant:"body2",children:c.name}),s(h,{variant:"caption",color:"text.secondary",children:[c.serviceCode,c.description?` — ${c.description}`:""]})]}))}),!e.serviceConfigId&&r(f,{sx:{flex:1,minWidth:160},size:"small",label:"Service Name",value:e.serviceName,onChange:l=>n(a,{serviceName:l.target.value}),disabled:o,placeholder:"Custom service name"}),r(f,{sx:{flex:1.5,minWidth:180},size:"small",label:"Remarks",value:e.remarks??"",onChange:l=>n(a,{remarks:l.target.value}),disabled:o,placeholder:"Optional remarks"}),r(M,{sx:{mx:0,whiteSpace:"nowrap"},control:r(P,{size:"small",checked:e.isChargeable,onChange:l=>n(a,{isChargeable:l.target.checked}),disabled:o}),label:"Chargeable"}),e.isChargeable&&r(q,{title:C?"Hide charges":`${F} charge(s) — click to manage`,children:r(y,{size:"small",onClick:()=>T(t),color:"primary",children:C?r(V,{fontSize:"small"}):r(G,{fontSize:"small"})})}),!o&&r(y,{size:"small",color:"error",onClick:()=>j(a),"aria-label":"Remove service",children:r(H,{fontSize:"small"})})]}),e.isChargeable&&C&&r(p,{sx:{mt:1.5,pl:1.5,borderLeft:`3px solid ${b(u.palette.primary.main,.3)}`},children:r(Q,{charges:e.charges??[],onChange:l=>n(a,{charges:l,totalChargeAmount:v(l)}),readOnly:o,taxMode:z,title:"Charges under this service"})})]},t)})}),i.length>0&&s(J,{children:[r(O,{sx:{mt:2,mb:1}}),r(g,{direction:"row",justifyContent:"flex-end",children:s(h,{variant:"body2",color:"text.secondary",children:["Services charges total:"," ",s("strong",{children:["₹",i.filter(e=>e.isChargeable).reduce((e,a)=>e+v(a.charges??[]),0).toFixed(2)]})]})})]})]})};export{re as S};
