import{a as W,u as R,j as o,I as G,h as C,A as M,i as U,H as B,l as K,N as z}from"./index-Da-VPv7f.js";import{r as n,R as H}from"./mui-icons-CVqIXJUc.js";import{l as Q,k as w}from"./mui-x-datagrid-D42xrz8e.js";import{T as A}from"./mui-x-charts-CP3e2jhe.js";import{D as V}from"./mui-x-pickers-BflNfF54.js";const J=W.injectEndpoints({endpoints:t=>({createStoneConversion:t.mutation({query:({stoneConversion:s})=>({url:"stone-conversion-configuration",method:"POST",body:s})}),getStoneConversionList:t.query({query:()=>({url:"stone-conversion-configuration",method:"GET"})}),checkDuplicateStone:t.mutation({query:({stoneConversion:s})=>({url:"stone-conversion-configuration/duplicate",method:"POST",body:s})}),updateStoneConversion:t.mutation({query:({stoneConversion:s})=>({url:"stone-conversion-configuration",method:"PUT",body:s})})})}),{useCreateStoneConversionMutation:X,useLazyGetStoneConversionListQuery:Y,useCheckDuplicateStoneMutation:ae,useUpdateStoneConversionMutation:Z}=J,v={id:0,tons:1,sqft:0};function re(){const[t,s]=n.useState(v),[r,D]=n.useState(!1),[P,g]=n.useState(!1),[i,a]=n.useState({}),[S,L]=n.useState([]),{openSnackbar:y}=R(),[j,u]=n.useState(null),[l,{data:d,isLoading:F,error:b}]=Y(),[q,{isLoading:_}]=X(),[k,{isLoading:$}]=Z(),I=S.length>0;n.useEffect(()=>{l().then(e=>{e.error?u(e.error):u(null)})},[l]),n.useEffect(()=>{d&&L(d.map(e=>({id:e.id??0,tons:e.tons??0,sqft:Number(e.sqft)||0})))},[d]);const f=n.useCallback(e=>{s(e||v),D(!!e),a({}),g(!0)},[]),p=n.useCallback(()=>{s(v),a({}),g(!1)},[]),E=n.useCallback(e=>{const{name:c,value:m}=e.target,T=parseFloat(m)||0;s(h=>({...h,[c]:T})),T>0&&a(h=>({...h,[c]:""}))},[]),x=n.useCallback(()=>{const e={};return(!t.tons||t.tons<=0)&&(e.tons="Enter valid Ton value"),(!t.sqft||t.sqft<=0)&&(e.sqft="Enter valid Sqft value"),a(e),Object.keys(e).length===0},[t]),N=n.useCallback(async()=>{if(!x())return;const e={tons:t.tons,sqft:t.sqft};try{r?(await k({stoneConversion:{...e,id:t.id,sqft:t.sqft.toString()}}).unwrap(),y("Stone Conversion updated successfully","success")):(await q({stoneConversion:{...e,sqft:t.sqft.toString()}}).unwrap(),y("Stone Conversion created successfully","success")),p(),l()}catch(c){u(c),a(m=>({...m,submit:"Failed to save. Please try again."}))}},[t,r,k,q,p,l,x]),O=n.useMemo(()=>[{field:"tons",headerName:"Tons",flex:1},{field:"sqft",headerName:"Sqft",flex:1},{field:"actions",headerName:"Actions",flex:1,sortable:!1,renderCell:e=>o(G,{type:"Edit",onClick:()=>f(e.row)})}],[f]);return o(z,{error:j||b,children:C(K,{children:[C(Q,{display:"flex",justifyContent:"space-between",alignItems:"center",mb:2,children:[o(A,{variant:"h6",fontWeight:600,children:"Stone Conversion Configuration"}),o(M,{btnType:"create",onClick:()=>f(),disabled:I,children:"Add Conversion"})]}),b&&o(A,{color:"error",mb:2,children:"Failed to load data. Please refresh."}),o(H.Suspense,{fallback:o("div",{children:"Loading table..."}),children:o(U,{rows:S,columns:O,getRowId:e=>e.id,loading:F,disableRowSelectionOnClick:!0,autoPagination:"client"})}),o(B,{title:r?"Edit Conversion":"Add Conversion",open:P,onClose:p,onConfirm:N,maxWidth:"xs",cancelText:"Cancel",confirmText:r?"Update":"Submit",children:C(V,{children:[o(w,{fullWidth:!0,margin:"normal",label:"Tons",name:"tons",type:"number",onKeyDown:e=>{["e","E","+","-"].includes(e.key)&&e.preventDefault()},onWheel:e=>{e.target.blur()},inputProps:{min:1},value:t.tons,onChange:E,error:!!i.tons,helperText:i.tons}),o(w,{fullWidth:!0,margin:"normal",label:"Sqft",name:"sqft",type:"number",onKeyDown:e=>{["e","E","+","-"].includes(e.key)&&e.preventDefault()},onWheel:e=>{e.target.blur()},inputProps:{min:1},value:t.sqft,onChange:E,error:!!i.sqft,helperText:i.sqft})]})})]})})}export{re as default};
