import{e as W,u as R,j as o,I as G,g as C,A as M,h as U,B,k as K,N as z}from"./index-OkLaRrT4.js";import{r as n,R as Q}from"./mui-icons-DSJWgROT.js";import{l as V,k as w}from"./mui-x-datagrid-Uj6xW2G5.js";import{T as A}from"./mui-x-charts-BC4WZfuI.js";import{D as H}from"./mui-x-pickers-DVr_eQ3-.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:re,useUpdateStoneConversionMutation:Z}=J,v={id:0,tons:1,sqft:0};function ae(){const[t,s]=n.useState(v),[a,D]=n.useState(!1),[P,g]=n.useState(!1),[i,r]=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),r({}),g(!0)},[]),p=n.useCallback(()=>{s(v),r({}),g(!1)},[]),E=n.useCallback(e=>{const{name:c,value:m}=e.target,T=parseFloat(m)||0;s(h=>({...h,[c]:T})),T>0&&r(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"),r(e),Object.keys(e).length===0},[t]),N=n.useCallback(async()=>{if(!x())return;const e={tons:t.tons,sqft:t.sqft};try{a?(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),r(m=>({...m,submit:"Failed to save. Please try again."}))}},[t,a,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(V,{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(Q.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:a?"Edit Conversion":"Add Conversion",open:P,onClose:p,onConfirm:N,maxWidth:"xs",cancelText:"Cancel",confirmText:a?"Update":"Submit",children:C(H,{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{ae as default};
