import{b as $,uQ as C,j as i,B as E,P as c,ae as R,af as G,gn as N,go as Q,gp as U,w0 as H,w1 as J,uS as y,uR as B}from"./index-d2b25495.js";import{c as g,a as K,b as P,f as O}from"./index.esm-3043d6d4.js";import{o as T}from"./yup-267e586f.js";import{u as W}from"./index.esm-ce3c2c20.js";import"./componentStyles-0e851d91.js";import{F as X}from"./FormInput-d202e905.js";/* empty css                   */import"./ScrollableList-fc1de312.js";import{S as M}from"./SelectComponent-d286bfef.js";import{L as Y}from"./LoadingButton-c8e88148.js";const te=()=>{var x,f,_,j,S,v;const o=$(),{pricebook:{selectedData:r,pageParams:h,loading:Z,add_or_update_loading:z,model:p},mini:{miniItemsList:a,miniAreaList:d,miniAreaLoading:k,miniAreaParams:n}}=C(),A=g({price:K().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:P().required("Please select item"),value:O().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:P().required("Please select area"),value:P().required("Please select area")}).required("Please select area")}),{control:u,handleSubmit:V,reset:D,setValue:m,getValues:b}=W({resolver:T(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}}),w=e=>{const t={obj:{item_id:String(e.item.value)||"",area_id:String(e.area.value)||"",price:String(e.price)},pageParams:h,clearDataFn:()=>{o(y(!1)),o(B({}))}};o(H(t))},F=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:h,clearDataFn:()=>{o(y(!1)),o(B({}))}};o(J(t))};return i.jsx(E,{children:i.jsx("form",{style:{width:"100%"},onSubmit:V(r!=null&&r.id?F:w),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:R,clearData:G})}),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 s=await o(N({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:U})}),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};
