import{c as C,b4 as P,a as h,aW as A,hg as D,r as I,j as e,I as k,J as E,B as F,bA as M,T as w,K as r,am as z,an as T,E as c,hl as $,hm as B,qh as L,qi as Q}from"./index-aa586041.js";import{c as f,a as R,b as d}from"./index.esm-5d8102a7.js";import{o as V}from"./yup-75085264.js";import{u as U}from"./index.esm-c1bef48d.js";import"./CustomDatepicker-26155063.js";import{F as g}from"./FormInput-7412b024.js";import"./index-e3cd0864.js";import"./ScrollableList-fcc6a14c.js";import"./SectionDivider-2f9208fa.js";import{S as W}from"./SelectComponent-aad66251.js";import{T as G}from"./TextArea-1d2de9d0.js";import{D as J}from"./Dialog-26aea2b0.js";import{D as K}from"./DialogTitle-35e72887.js";import{D as X}from"./DialogContent-5a256860.js";import{D as H}from"./DialogContentText-ab749083.js";import{D as N}from"./DialogActions-90af69f6.js";const he=({selectedAssetId:b})=>{const n=C();P();const{miniItemsList:t,miniAsset:O}=h(A),{selectedData:a,loading:y,isModelVisible:j,pageParams:_}=h(i=>D(i)),o=()=>{n($({})),n(B(!1)),m({part_name:null,description:"",life_span:"",quantity:""})},l=a==null?void 0:a.id,S=f().shape({quantity:R().typeError("Quantity must be a number").positive("Quantity must be greater than 0"),life_span:d().notRequired(),part_name:f().shape({label:d().required("Please select an item"),value:d().required("Please select an item")})}),{control:p,handleSubmit:q,setValue:s,reset:m,formState:{errors:Y}}=U({resolver:V(S)});I.useEffect(()=>{a&&a.id?(s("life_span",a==null?void 0:a.life_span),s("quantity",a==null?void 0:a.quantity),s("part_name",a!=null&&a.item?{label:a.item.value,value:a.item.label}:null),s("description",(a==null?void 0:a.description)??"")):m({part_name:null,description:"",life_span:"",quantity:""})},[a,m,s]);const v=i=>{console.log("onsubmmit is being triggered");const x={formData:{description:i.description,quantity:i.quantity,life_span:i.life_span,item_id:i.part_name.value,asset_id:b||""},id:l??void 0,pageParams:_,clearDataFn:()=>{o()}};n(l?Q(x):L(x))};return e.jsx(e.Fragment,{children:e.jsxs(J,{open:j,onClose:(i,u)=>{u!=="backdropClick"&&o()},"aria-labelledby":"alert-dialog-title","aria-describedby":"alert-dialog-description",children:[e.jsxs(K,{sx:{bgcolor:"",color:"black",p:1,px:2,display:"flex",alignItems:"center",justifyContent:"space-between"},variant:"h4",id:"alert-dialog-title",children:[l?"Update ":"Add ","Spare",e.jsx(k,{onClick:o,children:e.jsx(E,{color:"black"})})]}),e.jsx(X,{sx:{px:"24px",pt:"12px !important",pb:0,backgroundColor:"transparent",boxShadow:"none"},children:y?e.jsxs(F,{children:[e.jsx(M,{}),e.jsx(w,{textAlign:"center",children:"Loading..."})]}):e.jsx(H,{id:"alert-dialog-description",sx:{width:500},children:e.jsxs("form",{style:{width:"100%"},onSubmit:q(v),children:[e.jsxs(r,{container:!0,spacing:2,children:[e.jsx(r,{size:{xs:12},children:e.jsx(W,{required:!0,name:"part_name",label:"Spare",control:p,rules:{required:!0},options:t==null?void 0:t.list.map(i=>({id:i.id,name:`${i.name}(${i.code})`})),loading:t.loading,selectParams:{page:t.miniParams.page,page_size:t.miniParams.page_size,search:t.miniParams.search,no_of_pages:t.miniParams.no_of_pages},hasMore:t.miniParams.page<t.miniParams.no_of_pages,fetchapi:z,clearData:T})}),e.jsx(r,{size:{xs:12},children:e.jsx(g,{required:!0,name:"quantity",label:"Quantity",type:"text",placeholder:"Enter Quantity here...",control:p})}),e.jsx(r,{size:{xs:12},children:e.jsx(g,{name:"life_span",label:"Life Span(months)",type:"text",placeholder:"Enter LifeSpan here...",control:p})}),e.jsx(r,{size:{xs:12},children:e.jsx(G,{name:"description",label:"Description",type:"text",placeholder:"Write Description here...",minRows:3,maxRows:5,containerSx:{display:"grid",gap:1},control:p})})]}),e.jsxs(N,{sx:{p:3,display:"flex",justifyContent:"flex-end",pr:0},children:[e.jsx(c,{onClick:o,variant:"outlined",color:"secondary",children:"Cancel"}),l?e.jsx(c,{variant:"contained",type:"submit",color:"success",autoFocus:!0,children:"Update"}):e.jsx(c,{variant:"contained",type:"submit",color:"success",autoFocus:!0,children:"Submit"})]})]})})})]})})};export{he as A};
