import{b as w,uQ as C,j as i,B as E,Q as c,ae as Q,af as R,gq as G,gr as N,gs as U,vY as Y,vZ as Z,uS as y,uR as B}from"./index-60a3c753.js";import{c as g,a as H,b as h,e as J}from"./index.esm-62d6b2f7.js";import{o as K}from"./yup-92678dde.js";import{u as O}from"./index.esm-c494934a.js";import"./componentStyles-7753bbb9.js";import{F as T}from"./FormInput-74e2c4d2.js";/* empty css                   */import"./ScrollableList-1962b023.js";import{S as M}from"./SelectComponent-e431868e.js";import{L as W}from"./LoadingButton-3975ddf9.js";const te=()=>{var x,f,_,j,S,v;const s=w(),{pricebook:{selectedData:r,pageParams:P,loading:X,add_or_update_loading:z,model:p},mini:{miniItemsList:a,miniAreaList:d,miniAreaLoading:k,miniAreaParams:n}}=C(),A=g({price:H().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:J().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}=O({resolver:K(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:()=>{s(y(!1)),s(B({}))}};s(Y(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:()=>{s(y(!1)),s(B({}))}};s(Z(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(M,{name:"item",label:"Item",control:u,required:!0,options:(S=a==null?void 0:a.list)==null?void 0:S.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:Q,clearData:R})}),i.jsx(c,{size:p?{xs:12,md:6}:{xs:12,md:4,xl:3},children:i.jsx(M,{name:"area",label:"Area",control:u,required:!0,disabled:!((v=b("item"))!=null&&v.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 o=await s(G({itemId:(t=b("item"))==null?void 0:t.value,area_id:e.value})).unwrap();o!=null&&o.response?m("price",(q=o==null?void 0:o.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:N,clearData:U})}),i.jsx(c,{size:p?{xs:12,md:6}:{xs:12,md:4,xl:3},children:i.jsx(T,{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(W,{loading:z,type:"submit",variant:"contained",size:"large",sx:{mt:3},children:r!=null&&r.id?"Update":"Save"})})]})})})};export{te as A};
