import{H as N}from"./HeaderBreadcrumbs.da67f38c.js";import{P as R}from"./Page.4c1db0aa.js";import{bg as j,a as F,bi as P,c as H,d as v,r as d,e as T,f as a,F as V,j as m,S as z,T as C,R as S,k as G,az as p,o as U,al as _}from"./index.8f299a7d.js";import{R as A}from"./RHFSwitch.509d024a.js";import{G as l}from"./Grid.358b274d.js";import{C as x}from"./Card.1ec83a31.js";import"./FormControlLabel.4a0a74ee.js";function D({isEdit:s,currentCorporateBenefit:e}){const{enqueueSnackbar:r}=j(),n=F(),{corporate_id:i}=P(),c=H().shape({name:v().required("Name is required"),code:v().required("Corporate Code is required")}),o=d.exports.useMemo(()=>({name:(e==null?void 0:e.name)||"",code:(e==null?void 0:e.code)||"",active:(e==null?void 0:e.active)===1}),[e]);d.exports.useEffect(()=>{s&&e&&u(o),s||u(o)},[s,e]);const h=T({resolver:U(c),defaultValues:o}),{reset:u,watch:E,control:L,setValue:M,getValues:O,setError:y,handleSubmit:k,formState:{isSubmitting:q}}=h;return a(V,{methods:h,onSubmit:k(async f=>{s?await p.put("/corporates/"+i+"/corporate-benefits/"+(e==null?void 0:e.id),f).then(t=>{r("Corporate Benefit updated successfully",{variant:"success"})}).then(t=>{n("/corporates/"+i+"/corporate-benefits/",{replace:!0})}).catch(({response:t})=>{r("Update Failed : "+t.data.message,{variant:"error"})}):await p.post("/corporates/"+i+"/corporate-benefits",f).then(t=>{r("Corporate Plan created successfully",{variant:"success"})}).then(t=>{n("/corporates/"+i+"/corporate-benefits",{replace:!0})}).catch(({response:t})=>{var b;if(t.status===422)for(const[w,g]of Object.entries(t.data.errors))y(w,{message:g[0]}),r((b=g[0])!=null?b:"Failed Processing Request",{variant:"error"});else r("Create Failed : "+t.data.message,{variant:"error"})})}),children:m(l,{container:!0,spacing:2,children:[a(l,{item:!0,xs:8,children:a(x,{sx:{p:2},children:m(z,{spacing:3,children:[a(C,{variant:"h6",children:"Corporate Benefit Detail"}),a(S,{name:"name",label:"Name"}),a(S,{name:"code",label:"Code"}),a(G,{type:"submit",variant:"contained",size:"large",fullWidth:!0,loading:q,children:s?"Update":"Create"})]})})}),a(l,{item:!0,xs:4,children:a(x,{sx:{p:2},children:a(A,{name:"active",label:"Active"})})})]})})}function Z(){_();const{corporate_id:s,id:e}=P(),[r,n]=d.exports.useState(),i=F(),c=!!e;return d.exports.useEffect(()=>{c&&p.get("/corporates/"+s+"/corporate-benefits/"+e+"/edit").then(o=>{n(o.data)}).catch(o=>{o.response.status===404&&i("/404")})},[s,e]),m(R,{title:"Create Corporate Benefit",children:[a(N,{heading:"Create Corporate Benefit",links:[{name:"Corporates",href:"/corporates"},{name:"Corporate Name",href:"/corporates/"+s},{name:"Corporate Benefits",href:"/corporates/"+s+"/corporate-benefits"},{name:c?"Edit":"Create",href:"/corporates/"+s+"/corporate-benefits/"+e}]}),a(D,{isEdit:c,currentCorporateBenefit:r})]})}export{Z as default};
