import{d as y,mX as B,j as s,B as q,oI as k,mY as c,Y as M,Z as z,oJ as F,oK as $,mZ as P}from"./index-c5c88b67.js";import{c as b,a as w,b as A,i as E,u as V}from"./index.esm-81620445.js";import{o as C}from"./yup-41605283.js";import"./GenericThumbnailView-43782201.js";import{F as G}from"./FormInput-86f14880.js";import"./componentStyles-a5e5a830.js";import"./ScrollableList-21ab2b9c.js";import"./TextArea-aad9dffe.js";import{S as Y}from"./SelectComponent-8c888daa.js";import{G as m}from"./Grid2-0ff60c92.js";import{L as Z}from"./LoadingButton-3a5d0643.js";const I=()=>{var u,g,x;const a=y(),{pricebook:{selectedData:i,pageParams:l,loading:v,add_or_update_loading:h,model:p},mini:{miniItemsList:r}}=B(),f=b({price:w().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:b({label:A().required("Please select item"),value:E().test("is-valid-value","Value is required",e=>e!=null&&(typeof e=="string"||typeof e=="number")).required("Please select item")}).required("Please select item")}),{control:d,handleSubmit:j,reset:J,setValue:t}=V({resolver:C(f),values:{price:i!=null&&i.price?Number(i.price):0,item:i!=null&&i.item?{label:(u=i.item)==null?void 0:u.name,value:(g=i.item)==null?void 0:g.id}:null}}),S=e=>{const o={obj:{item_id:String(e.item.value)||"",price:String(e.price)},pageParams:l,clearDataFn:()=>{a(P(!1)),a(c({}))}};a(F(o))},_=e=>{const o={obj:{id:i==null?void 0:i.id,item_id:String(e.item.value)||"",price:String(e.price)},pageParams:l,clearDataFn:()=>{a(P(!1)),a(c({}))}};a($(o))};return s.jsx(q,{children:s.jsx("form",{style:{width:"100%"},onSubmit:j(i!=null&&i.id?_:S),children:s.jsxs(m,{container:!0,spacing:2,mb:2,alignItems:"center",children:[s.jsx(m,{size:p?{xs:12,md:5}:{xs:12,md:4,xl:3},children:s.jsx(Y,{name:"item",label:"Item",control:d,required:!0,options:(x=r==null?void 0:r.list)==null?void 0:x.map(e=>({id:e.id,name:e.name})),onChange:async e=>{var n;if(console.log(e),e)try{const o=await a(k({itemId:e.value})).unwrap();o!=null&&o.response?t("price",(n=o==null?void 0:o.response)==null?void 0:n.price):(a(c({item:e})),t("item",e),t("price",""))}catch{t("price",""),t("item",e),a(c({item:e}))}else a(c({item:e})),t("item",e),t("price","")},loading:r==null?void 0:r.loading,selectParams:{page:r==null?void 0:r.miniParams.page,page_size:r==null?void 0:r.miniParams.page_size,search:r==null?void 0:r.miniParams.search,no_of_pages:r==null?void 0:r.miniParams.no_of_pages},hasMore:(r==null?void 0:r.miniParams.page)<(r==null?void 0:r.miniParams.no_of_pages),fetchapi:M,clearData:z})}),s.jsx(m,{size:p?{xs:12,md:5}:{xs:12,md:4,xl:3},children:s.jsx(G,{name:"price",label:"Price",required:!0,type:"number",placeholder:"Enter Price here...",control:d})}),s.jsx(m,{size:p?{xs:12,md:2}:{xs:12,md:4},children:s.jsx(Z,{loading:h,type:"submit",variant:"contained",size:"large",sx:{mt:3},children:i!=null&&i.id?"Update":"Save"})})]})})})};export{I as A};
