import{b as w,uL as C,j as i,B as E,Q as c,ae as N,af as U,gk as G,gl as Q,gm as R,vT as T,vU as H,uN as y,uM as M}from"./index-76d4793e.js";import{c as g,a as J,b as h,f as K}from"./index.esm-ad8a7c9f.js";import{o as O}from"./yup-688ec4f6.js";import{u as W}from"./index.esm-3adfd9f7.js";import"./componentStyles-0803b39e.js";import{F as X}from"./FormInput-8f40ded1.js";/* empty css                   */import"./ScrollableList-4dae3697.js";import{S as B}from"./SelectComponent-3c1140e6.js";import{L as Y}from"./LoadingButton-89ba2db7.js";const te=()=>{var x,f,_,j,v,S;const o=w(),{pricebook:{selectedData:r,pageParams:P,loading:Z,add_or_update_loading:z,model:p},mini:{miniItemsList:a,miniAreaList:d,miniAreaLoading:k,miniAreaParams:n}}=C(),A=g({price:J().typeError("Price must be a number").required("Please enter price").min(0,"Price must be greater than or equal to 0").test("is-decimal","Invalid price format",e=>e==null||/^\d*\.?\d{0,2}$/.test(e.toString())),item:g({label:h().required("Please select item"),value:K().test("is-valid-value","Value is required",e=>e!=null&&(typeof e=="string"||typeof e=="number")).required("Please select item")}).required("Please select item"),area:g({label:h().required("Please select area"),value:h().required("Please select area")}).required("Please select area")}),{control:u,handleSubmit:V,reset:D,setValue:m,getValues:b}=W({resolver:O(A),values:{price:r!=null&&r.price?Number(r.price):0,item:r!=null&&r.item?{label:(x=r.item)==null?void 0:x.name,value:(f=r.item)==null?void 0:f.id}:null,area:r!=null&&r.area?{label:(_=r.area)==null?void 0:_.name,value:(j=r.area)==null?void 0:j.id}:null}}),F=e=>{const t={obj:{item_id:String(e.item.value)||"",area_id:String(e.area.value)||"",price:String(e.price)},pageParams:P,clearDataFn:()=>{o(y(!1)),o(M({}))}};o(T(t))},$=e=>{const t={obj:{id:r==null?void 0:r.id,item_id:String(e.item.value)||"",area_id:String(e.area.value)||"",price:String(e.price)},pageParams:P,clearDataFn:()=>{o(y(!1)),o(M({}))}};o(H(t))};return i.jsx(E,{children:i.jsx("form",{style:{width:"100%"},onSubmit:V(r!=null&&r.id?$:F),children:i.jsxs(c,{container:!0,spacing:2,mb:2,alignItems:"center",children:[i.jsx(c,{size:p?{xs:12,md:6}:{xs:12,md:4,xl:3},children:i.jsx(B,{name:"item",label:"Item",control:u,required:!0,options:(v=a==null?void 0:a.list)==null?void 0:v.map(e=>({id:e.id,name:e.name})),onChange:async e=>{(()=>{m("area",null),m("price","")})()},loading:a==null?void 0:a.loading,selectParams:{page:a==null?void 0:a.miniParams.page,page_size:a==null?void 0:a.miniParams.page_size,search:a==null?void 0:a.miniParams.search,no_of_pages:a==null?void 0:a.miniParams.no_of_pages},hasMore:(a==null?void 0:a.miniParams.page)<(a==null?void 0:a.miniParams.no_of_pages),fetchapi:N,clearData:U})}),i.jsx(c,{size:p?{xs:12,md:6}:{xs:12,md:4,xl:3},children:i.jsx(B,{name:"area",label:"Area",control:u,required:!0,disabled:!((S=b("item"))!=null&&S.value),options:d==null?void 0:d.map(e=>({id:e.id,name:e.name})),onChange:async e=>{var t,q;console.log(e);const l=()=>{m("area",e)};if(e)try{const s=await o(G({itemId:(t=b("item"))==null?void 0:t.value,area_id:e.value})).unwrap();s!=null&&s.response?m("price",(q=s==null?void 0:s.response)==null?void 0:q.price):l()}catch{l()}else l()},loading:k,selectParams:{page:n.page,page_size:n.page_size,search:n.search,no_of_pages:n.no_of_pages},hasMore:n.page<n.no_of_pages,fetchapi:Q,clearData:R})}),i.jsx(c,{size:p?{xs:12,md:6}:{xs:12,md:4,xl:3},children:i.jsx(X,{name:"price",label:"Price",required:!0,type:"number",placeholder:"Enter Price here...",control:u})}),i.jsx(c,{size:!p&&{xs:12,md:4},children:i.jsx(Y,{loading:z,type:"submit",variant:"contained",size:"large",sx:{mt:3},children:r!=null&&r.id?"Update":"Save"})})]})})})};export{te as A};
