import{d as y,nv as B,j as s,B as q,p8 as k,nw as n,a7 as M,a8 as z,p9 as F,pa as $,nx as P}from"./index-0698dbe6.js";import{c as b,a as w,b as v,i as A,u as E}from"./index.esm-85d015f3.js";import{o as V}from"./yup-7a53beb8.js";import"./GenericThumbnailView-dca658f3.js";import{F as C}from"./FormInput-98c39817.js";import"./componentStyles-91278318.js";import"./ScrollableList-fc87149c.js";import"./TextArea-91c96faf.js";import{S as G}from"./SelectComponent-fbded912.js";import{G as p}from"./Grid2-a3850bef.js";import{L as N}from"./LoadingButton-32ceeccc.js";const I=()=>{var u,g,x;const o=y(),{pricebook:{selectedData:i,pageParams:l,loading:R,add_or_update_loading:h,model:m},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:v().required("Please select item"),value:A().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:U,setValue:t}=E({resolver:V(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 a={obj:{item_id:String(e.item.value)||"",price:String(e.price)},pageParams:l,clearDataFn:()=>{o(P(!1)),o(n({}))}};o(F(a))},_=e=>{const a={obj:{id:i==null?void 0:i.id,item_id:String(e.item.value)||"",price:String(e.price)},pageParams:l,clearDataFn:()=>{o(P(!1)),o(n({}))}};o($(a))};return s.jsx(q,{children:s.jsx("form",{style:{width:"100%"},onSubmit:j(i!=null&&i.id?_:S),children:s.jsxs(p,{container:!0,spacing:2,mb:2,alignItems:"center",children:[s.jsx(p,{size:m?{xs:12,md:5}:{xs:12,md:4,xl:3},children:s.jsx(G,{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 c;if(e)try{const a=await o(k({itemId:e.value})).unwrap();a!=null&&a.response?t("price",(c=a==null?void 0:a.response)==null?void 0:c.price):(o(n({item:e})),t("item",e),t("price",""))}catch{t("price",""),t("item",e),o(n({item:e}))}else o(n({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(p,{size:m?{xs:12,md:5}:{xs:12,md:4,xl:3},children:s.jsx(C,{name:"price",label:"Price",required:!0,type:"number",placeholder:"Enter Price here...",control:d})}),s.jsx(p,{size:m?{xs:12,md:2}:{xs:12,md:4},children:s.jsx(N,{loading:h,type:"submit",variant:"contained",size:"large",sx:{mt:3},children:i!=null&&i.id?"Update":"Save"})})]})})})};export{I as A};
