import{q as N,r as _,S as O,j as e,J as B}from"./app-Bup04Qnx.js";import{I as m}from"./Input-Bs1ncZB7.js";import{S as p,u as J,a as M}from"./Select-Cs0aMrtU.js";import{u as T,a as k,m as q}from"./format-CisCU4Rs.js";import{h as P,u as H}from"./useFormChange-D35moWjQ.js";function W({userData:l=null}){var v;const{props:c}=N(),{location:r,status:d=null,roles:j=null,owners:a=null,companies:S,page:f}=N().props,b=(v=c==null?void 0:c.auth)==null?void 0:v.user,x=b?b.role:"guest",[u,R]=_.useState((l==null?void 0:l.picture)??null),{data:s,setData:t,post:w,processing:F,errors:U,reset:L}=O({location:r,name:(l==null?void 0:l.name)??"",username:(l==null?void 0:l.username)??"",phone:(l==null?void 0:l.phone)??"",email:(l==null?void 0:l.email)??"",role:(l==null?void 0:l.role)??"",status:(l==null?void 0:l.status)??"active",picture:u??"",password:"",password_confirmation:"",manager_id:(l==null?void 0:l.manager_id)??"",company_id:(l==null?void 0:l.company_id)??""});_.useEffect(()=>{if(r){const n=new URLSearchParams(new URL(r).search);n.get("role")&&t(o=>({...o,role:n.get("role")}))}return()=>{L("password","password_confirmation")}},[]);function C(n){let{name:o,value:E,files:I}=H(n,s);["picture"].includes(o)&&R(URL.createObjectURL(I[0])),t(z=>({...z,[o]:E}))}const V=n=>{if(n.preventDefault(),l)return s._method="put",w(`/admin/users/${l.id}`);w("/admin/users")},i={handleChange:C,data:s,errors:U},{params:h}=T(r);let g=(c==null?void 0:c.ref)??h.get("ref");g||(g=k(f==null?void 0:f.url));let y=null;return(a==null?void 0:a.length)>0&&(y=s!=null&&s.manager_id?a==null?void 0:a.findIndex(n=>n.value==(s==null?void 0:s.manager_id)):null),e.jsxs("form",{onSubmit:V,className:"flex flex-col items-center justify-center w-full gap-4 p-4 border rounded-lg shadow-lg",children:[e.jsx("p",{className:"text-lg font-semibold",children:l?"Edit Pengguna":"Tambah Pengguna"}),e.jsxs("div",{className:"flex flex-col md:flex-row w-full gap-4",children:[e.jsx(m,{type:"text",name:"name",label:"nama",...i}),l?e.jsx(m,{type:"text",name:"username",label:"username",...i}):null,e.jsx(m,{type:"email",name:"email",label:"email",...i})]}),e.jsxs("div",{className:"flex flex-col md:flex-row w-full gap-4",children:[e.jsx(m,{type:"tel",name:"phone",label:"nomor whatsapp",alt:"Opsional",...i}),e.jsx(m,{type:"password",name:"password",label:"password",...i}),e.jsx(m,{type:"password",name:"password_confirmation",label:"Konfirmasi Password",...i})]}),e.jsxs("div",{className:"flex flex-col md:flex-row w-full gap-4",children:[e.jsxs("div",{className:"flex gap-2 items-end w-full",children:[e.jsx(m,{type:"file",name:"picture",label:"Upload Foto",alt:"Opsional",...i,accept:q("image")}),u?e.jsx("img",{src:u,alt:"Foto Profil",className:"w-16 h-16 object-cover rounded-full"}):null]}),["director","manager"].includes(x)&&d?e.jsx(e.Fragment,{children:e.jsxs(p,{name:"status",label:"status",...i,defaultValue:s.status,children:[e.jsx("option",{value:"",children:"Pilih salah satu"}),d.length==0?null:d.map((n,o)=>e.jsx("option",{value:n,className:"capitalize",children:J(n)},o))]})}):null,["director"].includes(x)&&d?e.jsx(e.Fragment,{children:e.jsxs(p,{name:"role",label:"role",...i,defaultValue:s.role,handleChange:n=>P({e:n,name:"role",setData:t,params:h,only:["owners","companies"],isHtmlSelect:!0}),children:[e.jsx("option",{value:"",children:"Pilih salah satu"}),j.length==0?null:j.map((n,o)=>e.jsx("option",{value:n,className:"capitalize",children:M(n)},o))]})}):null]}),e.jsxs("div",{className:"flex flex-col md:flex-row w-full gap-4",children:[["director"].includes(x)&&["user"].includes(s.role)&&a?e.jsx(e.Fragment,{children:e.jsx(p,{...i,isReactSelect:!0,isVirtualized:!0,name:"manager_id",label:"Pilih Manajer",options:a,defaultValue:a[y],handleChange:n=>P({e:n,name:"manager_id",setData:t,params:h,only:["companies"]})})}):null,["","user","manager"].includes(s.role)?e.jsx(p,{...i,isReactSelect:!0,name:"company_id",label:"Pilih Perusahaan",defaultValue:s.company_id,options:S,isMulti:!0,handleChange:n=>t("company_id",n)}):null]}),e.jsx("div",{className:"flex flex-col gap-4 my-4",children:e.jsx("div",{className:"flex flex-col",children:e.jsxs("div",{className:"flex justify-center gap-2 mt-4",children:[e.jsx(B,{type:"button",href:g,className:"btn btn-primary btn-outline btn-sm",children:"Batal"}),e.jsx("button",{className:"btn btn-sm btn-primary",disabled:F,children:"Simpan"})]})})})]})}export{W as R};
