import{a as O,bg as $,c as L,d as x,r as a,e as P,f as t,F as A,j as m,S as _,T as h,R,aq as j,k as J,t as K,az as V,o as T,al as Q,bh as X}from"./index.8f299a7d.js";import{H as Y}from"./HeaderBreadcrumbs.da67f38c.js";import{P as Z}from"./Page.4c1db0aa.js";import{B as F,d as ee}from"./Cancel.e6e714f4.js";import{G as I}from"./Grid.358b274d.js";import{C as te}from"./Card.1ec83a31.js";import"./jsx-runtime_commonjs-proxy.e03e6edd.js";K(h)(({theme:s})=>({...s.typography.subtitle2,color:s.palette.text.secondary,marginBottom:s.spacing(1)}));function se({isEdit:s,currentFormularium:e}){const g=O(),{enqueueSnackbar:c}=$(),v=L().shape({name:x().required("Name is required")}),i=a.exports.useMemo(()=>({code:(e==null?void 0:e.code)||"",name:(e==null?void 0:e.name)||""}),[e]),d=P({resolver:T(v),defaultValues:i}),{reset:p,watch:C,control:W,setValue:S,getValues:D,setError:y,handleSubmit:q,formState:{isSubmitting:b}}=d;C(),a.exports.useEffect(()=>{s&&e&&p(i),s||p(i)},[s,e]);const u=async n=>{var N,w;try{if(s){const r=await V.put("/master/formulariums/"+(e==null?void 0:e.id),n)}else{const r=await V.post("/master/formulariums",n)}p(),c(s?"Formularium Udpated Successfully!":"Formularium Created Successfully!",{variant:"success"}),g("/master/formularium")}catch(r){if(r&&r.response.status===422)for(const[G,B]of Object.entries(r.response.data.errors))y(G,{message:B[0]}),c((N=B[0])!=null?N:"Failed Processing Request",{variant:"error"});else c((w=r.message)!=null?w:"Failed Processing Request",{variant:"error"})}const l=document==null?void 0:document.querySelector("ascent");l!=null&&(l.innerHTML="")};a.exports.useCallback(n=>{S("logo",n.map(l=>Object.assign(l,{preview:URL.createObjectURL(l)})))},[S]);const o=a.exports.useRef(null),[ae,E]=a.exports.useState(null),[f,k]=a.exports.useState(null),H=()=>{E(null)},M=()=>{o!=null&&o.current?(H(),o.current?o.current.click():console.log("No File selected")):alert("No file selected")},U=()=>{o.current.value="",o.current.dispatchEvent(new Event("change",{bubbles:!0}))},z=n=>{n.target.files[0]?k(n.target.files[0].name):k(null)};return t(A,{methods:d,onSubmit:q(u),children:m(_,{spacing:3,children:[t(h,{variant:"h6",children:"Formularium Detail"}),m("div",{children:[t(R,{name:"code",label:"Code"}),!(e!=null&&e.id)&&t(h,{variant:"caption",children:"Will be generated if empty"})]}),t(R,{name:"name",label:"Name"}),t(h,{variant:"h6",children:"Formularium Drug List Import"}),t("input",{type:"file",id:"file",ref:o,style:{display:"none"},onChange:z,accept:".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, text/plain"}),m(F,{variant:"outlined","aria-label":"outlined button group",fullWidth:!0,children:[t(j,{onClick:M,fullWidth:!0,children:f!=null?f:"No File Selected"}),f&&t(j,{onClick:U,size:"small",fullWidth:!1,sx:{p:1.8},children:t(ee,{color:"error"})})]}),t(J,{type:"submit",variant:"contained",size:"large",fullWidth:!0,loading:b,children:s?"Save Update":"Save New Corporate"})]})})}function pe(){Q();const[s,e]=a.exports.useState(!1),[g,c]=a.exports.useState({}),v=L().shape({name:x().required("Name is required"),code:x().required("Corporate Code is required"),active:X().required("Corporate Status is required")}),i=a.exports.useMemo(()=>({code:""}),[]),d=P({resolver:T(v),defaultValues:i}),{reset:p,watch:C,control:W,setValue:S,getValues:D,setError:y,handleSubmit:q,formState:{isSubmitting:b}}=d,u="Create Formularium";return m(Z,{title:u,children:[t(Y,{heading:u,links:[{name:"Master",href:"/master"},{name:"Formularium",href:"/master/formularium/"},{name:"Create",href:"/master/formularium/create/"}]}),t(I,{container:!0,spacing:2,children:t(I,{item:!0,xs:12,children:t(te,{sx:{p:2},children:t(se,{isSubmitting:b,isEdit:s,currentFormularium:g})})})})]})}export{pe as default};
