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