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