1import{j as e,u as i}from"./vendor-ui-fc56ca80.js";import{r as E,e as Q,R as Y}from"./vendor-react-f9ccc48d.js";import{u as J}from"./index.esm-08da3b2e.js";import{t as W}from"./zod-182dcd03.js";import{h as X,t as I,v as M,w as T,y as F,I as p,S as j,o as f,p as b,q as g,r as y,bu as Z,B as N,z as ee,bq as re,br as ae}from"./index-90016d22.js";import{F as oe,a as t,b as c,c as m,d,e as h,f as se}from"./form-42ce9a37.js";import{G as V,M as z,b as D,h as ne}from"./memberService-60ce7187.js";import{F as q,f as le,g as ie,a as te}from"./familyService-10e68e34.js";import{N as ce,a4 as me}from"./vendor-firebase-a557f106.js";import{i as de,n as he}from"./phone-75d1537e.js";import{o as pe,s as u,a1 as w,d as xe,e as A,u as ue,f as L}from"./vendor-utils-323b9898.js";import"./vendor-charts-9af8b7c0.js";import"./civil-5f5e63de.js";import"./coerce-b1c83b65.js";import"./address-333af221.js";const je=pe({firstName:u().min(2,{message:"El nombre debe tener al menos 2 caracteres."}).max(50),lastName:u().min(2,{message:"El apellido paterno debe tener al menos 2 caracteres."}).max(50),maternalSurname:u().max(50).optional().or(w("")),email:u().email({message:"Correo electrónico inválido."}).optional().or(w("")),phoneNumber:u().max(25).refine(de,{message:"Teléfono inválido. Usa solo números, ej: +56912345678"}).optional().or(w("")),birthDate:xe().optional(),gender:A(V).optional(),maritalStatus:A(z).optional(),membershipStatus:A(D).optional(),familyId:u().optional().or(w("")),familyRole:A(q).optional()}),Pe=()=>{const[$,O]=E.useState([]),[S,k]=E.useState(null),[x,v]=E.useState(null),[R,G]=E.useState(!1),{userData:l}=X(),_=Q(),{t:C}=ue(["members","common"]),o=(r,a)=>typeof a=="string"||typeof a=="object"||a===void 0?C(r,a):C(r);Y.useEffect(()=>{(async()=>{if(l!=null&&l.churchId)try{const a=await ie(l.churchId);O(a)}catch(a){console.error("Error fetching families:",a),i.error("Error al cargar las familias")}})()},[l==null?void 0:l.churchId]);const n=J({resolver:W(je),defaultValues:{firstName:"",lastName:"",maternalSurname:"",email:"",phoneNumber:"",birthDate:void 0,gender:void 0,maritalStatus:void 0,membershipStatus:void 0,familyId:"",familyRole:void 0}}),B=async r=>{if(!(l!=null&&l.churchId)){i.error("Error: No se pudo identificar la iglesia.");return}try{const s=await re(l.churchId);if(ae(s,1)){i.error(C("members:quota.limitReached",{defaultValue:"Has alcanzado el lÃmite de {{limit}} miembros de tu plan. Actualiza tu plan para agregar más.",limit:s.limit}));return}}catch(s){console.error("[MemberFormPage] quota check failed:",s)}const a={...r,churchId:l.churchId,systemStatus:"active",birthDate:r.birthDate?L(r.birthDate,"yyyy-MM-dd"):void 0,phoneNumber:he(r.phoneNumber)||void 0};try{const s=await ne(a);if(s){if(r.familyId&&r.familyRole)try{await te(r.familyId,s,r.familyRole)}catch(P){console.error("Error adding member to family:",P),i.error("Miembro creado, pero hubo un error al asociarlo a la familia.")}i.success("Miembro creado con éxito"),k(s),n.reset(),v(null)}else i.error("Miembro creado, pero no se pudo obtener el ID para generar el enlace.")}catch{i.error("Error al crear miembro")}},H=async()=>{if(!S){i.error("No hay un miembro recién creado para generar un enlace.");return}if(!(l!=null&&l.churchId)){i.error("No se pudo identificar la iglesia para generar el enlace.");return}G(!0),v(null);try{const r=ce(),s=await me(r,"generateProfileCompletionToken")({personId:S,churchId:l.churchId});if(s.data&&s.data.token){const P=`${window.location.origin}/complete-profile-token/${s.data.token}`;v(P),i.success("Enlace de completado de perfil generado.")}else i.error("Error al generar el token: respuesta inesperada.")}catch(r){console.error("Error generating profile completion link:",r),i.error(`Error al generar el enlace: ${r.message||"Error desconocido"}`)}finally{G(!1)}},U=()=>{k(null),v(null),n.reset()},K=()=>{x&&navigator.clipboard.writeText(x).then(()=>{i.success("Enlace copiado al portapapeles")}).catch(r=>{console.error("Error al copiar el enlace: ",r),i.error("No se pudo copiar el enlace")})};return e.jsx("div",{className:"max-w-2xl mx-auto p-4 sm:p-6 lg:p-8",children:S?e.jsxs("div",{className:"space-y-6",children:[e.jsx("h2",{className:"text-2xl font-semibold text-green-600",children:"¡Miembro Creado con Ãxito!"}),e.jsx("p",{children:"Ahora puedes generar un enlace único para que el miembro complete su perfil."}),e.jsx(N,{onClick:H,disabled:R||!!x,children:R?"Generando Enlace...":x?"Enlace Generado":"Generar Enlace de Completado de Perfil"}),x&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(ee,{children:"Enlace de Completado de Perfil:"}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(p,{type:"text",value:x||"",readOnly:!0,className:"flex-grow"}),e.jsx(N,{variant:"outline",onClick:K,children:"Copiar"})]})]}),e.jsxs("div",{className:"flex space-x-4 pt-4",children:[e.jsx(N,{variant:"outline",onClick:U,children:"Añadir Otro Miembro"}),e.jsx(N,{onClick:()=>_("/app/miembros"),children:"Ver Lista de Miembros"})]})]}):e.jsxs(e.Fragment,{children:[e.jsx("h1",{className:"text-2xl font-semibold mb-6",children:o("members:addMemberTitle","Añadir Nuevo Miembro")}),e.jsx(oe,{...n,children:e.jsxs("form",{onSubmit:n.handleSubmit(B),className:"space-y-6",children:[e.jsxs(I,{children:[e.jsx(M,{children:e.jsx(T,{className:"text-xl",children:o("members:sectionTitles.personalInfo","Información Personal")})}),e.jsxs(F,{className:"pt-4 space-y-4",children:[e.jsx(t,{control:n.control,name:"firstName",render:({field:r})=>e.jsxs(c,{children:[e.jsx(m,{children:o("common:form.firstName","Nombre(s)")}),e.jsx(d,{children:e.jsx(p,{placeholder:o("common:placeholders.firstName","Ej: Juan Pablo"),...r})}),e.jsx(h,{})]})}),e.jsx(t,{control:n.control,name:"lastName",render:({field:r})=>e.jsxs(c,{children:[e.jsx(m,{children:o("common:form.lastName","Apellido Paterno")}),e.jsx(d,{children:e.jsx(p,{placeholder:o("common:placeholders.lastName","Ej: Pérez"),...r})}),e.jsx(h,{})]})}),e.jsx(t,{control:n.control,name:"maternalSurname",render:({field:r})=>e.jsxs(c,{children:[e.jsxs(m,{children:[o("common:form.maternalSurname","Apellido Materno")," (Opcional)"]}),e.jsx(d,{children:e.jsx(p,{placeholder:o("common:placeholders.maternalSurname","Ej: González"),...r})}),e.jsx(h,{})]})}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsx(t,{control:n.control,name:"familyId",render:({field:r})=>e.jsxs(c,{children:[e.jsx(m,{children:"Familia (Opcional)"}),e.jsxs(j,{onValueChange:r.onChange,defaultValue:r.value,children:[e.jsx(d,{children:e.jsx(f,{children:e.jsx(b,{placeholder:"Seleccionar familia"})})}),e.jsx(g,{children:$.map(a=>e.jsx(y,{value:a.id,children:a.name},a.id))})]}),e.jsx(h,{})]})}),e.jsx(t,{control:n.control,name:"familyRole",render:({field:r})=>e.jsxs(c,{children:[e.jsx(m,{children:"Rol Familiar (Opcional)"}),e.jsxs(j,{onValueChange:r.onChange,defaultValue:r.value,disabled:!n.watch("familyId"),children:[e.jsx(d,{children:e.jsx(f,{children:e.jsx(b,{placeholder:"Seleccionar rol"})})}),e.jsx(g,{children:q.map(a=>e.jsx(y,{value:a,children:le[a]||a},a))})]}),e.jsx(h,{})]})})]})]})]}),e.jsxs(I,{children:[e.jsx(M,{children:e.jsx(T,{className:"text-xl",children:o("members:sectionTitles.contactInfo","Información de Contacto")})}),e.jsxs(F,{className:"pt-4 space-y-4",children:[e.jsx(t,{control:n.control,name:"email",render:({field:r})=>e.jsxs(c,{children:[e.jsxs(m,{children:[o("common:form.email","Correo Electrónico")," (Opcional)"]}),e.jsx(d,{children:e.jsx(p,{type:"email",placeholder:o("common:placeholders.email","ej: [email protected]"),...r})}),e.jsx(h,{})]})}),e.jsx(t,{control:n.control,name:"phoneNumber",render:({field:r})=>e.jsxs(c,{children:[e.jsxs(m,{children:[o("common:form.phoneNumber","Número de Teléfono")," (Opcional)"]}),e.jsx(d,{children:e.jsx(p,{type:"tel",placeholder:o("common:placeholders.phoneNumber","+56912345678"),...r})}),e.jsx(h,{})]})})]})]}),e.jsxs(I,{children:[e.jsx(M,{children:e.jsx(T,{className:"text-xl",children:o("members:sectionTitles.additionalDetails","Detalles Adicionales")})}),e.jsxs(F,{className:"pt-4 space-y-4",children:[e.jsx(t,{control:n.control,name:"birthDate",render:({field:r})=>e.jsxs(c,{children:[e.jsxs(m,{children:[o("common:form.birthDate","Fecha de Nacim
1iento")," (Opcional)"]}),e.jsx(d,{children:e.jsx(p,{type:"date",...r,value:r.value?L(new Date(r.value),"yyyy-MM-dd"):"",onChange:a=>{const s=a.target.value;r.onChange(s?new Date(s+"T00:00:00"):void 0)}})}),e.jsx(h,{})]})}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6 pt-2",children:[e.jsx(t,{control:n.control,name:"gender",render:({field:r})=>e.jsxs(c,{children:[e.jsxs(m,{children:[o("common:form.gender","Género")," (Opcional)"]}),e.jsxs(j,{onValueChange:r.onChange,defaultValue:r.value,children:[e.jsx(d,{children:e.jsx(f,{children:e.jsx(b,{placeholder:o("members:placeholders.selectGender","Selecciona un género")})})}),e.jsx(g,{children:V.map(a=>{const s=`common:form.genderValues.${a.toLowerCase()}`;return e.jsx(y,{value:a,children:o(s)},a)})})]}),e.jsx(h,{})]})}),e.jsx(t,{control:n.control,name:"maritalStatus",render:({field:r})=>e.jsxs(c,{children:[e.jsxs(m,{children:[o("common:form.maritalStatus","Estado Civil")," (Opcional)"]}),e.jsxs(j,{onValueChange:r.onChange,defaultValue:r.value,children:[e.jsx(d,{children:e.jsx(f,{children:e.jsx(b,{placeholder:o("members:placeholders.selectMaritalStatus","Selecciona un estado civil")})})}),e.jsx(g,{children:z.map(a=>{const s=`common:form.maritalStatusValues.${a.toLowerCase()}`;return e.jsx(y,{value:a,children:o(s)},a)})})]}),e.jsx(h,{})]})})]})]})]}),e.jsxs(I,{children:[e.jsx(M,{children:e.jsx(T,{className:"text-xl",children:o("members:sectionTitles.membershipInfo","Información de MembresÃa")})}),e.jsx(F,{className:"pt-4 space-y-4",children:e.jsx(t,{control:n.control,name:"membershipStatus",render:({field:r})=>{const a=Z(r.value);return e.jsxs(c,{children:[e.jsxs(m,{children:[o("members:form.membershipStatus","Estado de MembresÃa")," (",o("common:optional","Opcional"),")"]}),e.jsxs(j,{onValueChange:r.onChange,defaultValue:r.value,children:[e.jsx(d,{children:e.jsx(f,{children:e.jsx(b,{placeholder:o("members:placeholders.selectMembershipStatus","Selecciona un estado de membresÃa")})})}),e.jsx(g,{children:D.map(s=>e.jsx(y,{value:s,children:o(`members:membershipStatusValues.${s}`,s)},s))})]}),e.jsxs(se,{children:[o("members:form.membershipStatusHelp")," ",e.jsx("strong",{children:o(a==="visita"?"members:form.willEnrollAsVisitor":"members:form.willEnrollAsMember")})]}),e.jsx(h,{})]})}})})]}),e.jsx(N,{type:"submit",disabled:n.formState.isSubmitting||S!==null,className:"w-full md:w-auto",children:n.formState.isSubmitting?o("common:buttons.saving","Guardando..."):o("members:buttons.saveMember","Guardar Miembro")})]})})]})})};export{Pe as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.