1import{j as e,O as Y,co as Ce,dv as ke,E as Se,bf as J,cI as Fe,ag as Z,u as S,L as O,T as Ae,dw as ee,d0 as Ee,cK as Ie,dx as Te}from"./vendor-ui-fc56ca80.js";import{e as se,r as n,L as Pe}from"./vendor-react-f9ccc48d.js";import{d as Be,b as K,c as Me,e as He}from"./familyService-10e68e34.js";import{c as Re}from"./memberService-60ce7187.js";import{b as ae,t as ze,v as Ve,w as Oe,n as te,y as $e,F as We,B as y,D as Ue,d as _e,e as Ge,f as X,a1 as ie,a2 as re,a3 as le,a4 as ne,a5 as oe,a6 as ce,a7 as de,a8 as me,aJ as qe,aL as Je,h as Ke,af as Xe}from"./index-90016d22.js";import{L as Qe}from"./ListToolbar-91453e92.js";import{g as Ye,W as Ze}from"./dateUtils-b68232fd.js";import{p as es,a as Q}from"./phone-75d1537e.js";import{u as $}from"./vendor-utils-323b9898.js";import"./vendor-firebase-a557f106.js";import"./civil-5f5e63de.js";import"./coerce-b1c83b65.js";import"./address-333af221.js";import"./vendor-charts-9af8b7c0.js";const ss=({family:s,isSelected:t=!1,onSelect:r,selectionMode:b=!1,headPhone:x})=>{var I;const{t:i}=$("common"),N=se(),[w,p]=n.useState(!1),[D,f]=n.useState(!1),c=Ye(s.marriageAnniversaryDate||void 0,i),v=((I=s.memberRefs)==null?void 0:I.length)||0,P=()=>N(`/app/families/${s.id}`),C=()=>N(`/app/families/edit/${s.id}`),{canEdit:F,canDelete:A}=ae(),u=F("familias"),E=A("familias"),B=async()=>{if(s.id){p(!0),f(!1);try{await Be(s.id),S.success(i("familyList.familyDeletedSuccess")),window.location.reload()}catch(d){console.error("Error deleting family:",d),S.error(i("familyList.familyDeletedError")),p(!1)}}};return e.jsxs(ze,{className:`flex flex-col h-full transition-all duration-300 border border-border hover:shadow-md hover:border-primary/50 ${t?"ring-2 ring-primary ring-offset-2 border-primary bg-primary/5":""}`,children:[e.jsxs(Ve,{className:"flex flex-row items-start space-y-0 pb-2",children:[e.jsx("div",{className:"flex-1 min-w-0 pr-2",children:e.jsx(Oe,{className:"text-lg",children:e.jsx(Pe,{to:`/app/families/${s.id}`,className:"hover:text-primary hover:underline transition-colors truncate block",title:s.name,children:s.name})})}),(b||t)&&r&&e.jsx("div",{className:"pt-1",children:e.jsx(te,{checked:t,onCheckedChange:d=>r(d)})})]}),e.jsxs($e,{className:"flex-grow",children:[e.jsxs("div",{className:"flex items-center text-sm text-muted-foreground mb-2",children:[e.jsx(Y,{className:"h-4 w-4 mr-2"}),e.jsxs("span",{children:[v," ",i(v===1?"familyList.memberSingular":"familyList.memberPlural")]})]}),x&&e.jsxs("div",{className:"flex items-center text-sm mt-2 gap-2",onClick:d=>d.stopPropagation(),children:[e.jsxs("a",{href:es(x)||void 0,className:"flex items-center min-w-0 text-emerald-700 dark:text-emerald-400 hover:underline font-medium",title:i("familyList.callHead","Llamar al jefe de hogar"),children:[e.jsx(Ce,{className:"h-4 w-4 mr-2 flex-shrink-0"}),e.jsx("span",{className:"truncate",children:x})]}),Q(x)&&e.jsx("a",{href:Q(x)||void 0,target:"_blank",rel:"noopener noreferrer",className:"flex-shrink-0 text-[#25D366] hover:opacity-80",title:i("familyList.whatsappHead","Enviar WhatsApp al jefe de hogar"),children:e.jsx(Ze,{className:"h-4 w-4"})})]}),c&&e.jsxs("div",{className:"flex items-center text-sm text-muted-foreground mt-2",children:[e.jsx(ke,{className:"h-4 w-4 mr-2 text-pink-500"}),e.jsx("span",{children:c.displayString})]}),s.address&&e.jsx("div",{className:"mt-2 text-xs text-muted-foreground",children:e.jsxs("p",{className:"truncate",title:`${s.address.addressLine1||""}${s.address.city?", "+s.address.city:""}`,children:[s.address.addressLine1||"",s.address.city&&e.jsxs("span",{className:"ml-1",children:["(",s.address.city,")"]}),s.address.administrativeArea&&e.jsxs("span",{className:"ml-1",children:["[",s.address.administrativeArea,"]"]})]})})]}),e.jsxs(We,{className:"flex items-center justify-between border-t pt-4 mt-auto gap-2",children:[e.jsxs(y,{variant:"outline",size:"sm",onClick:P,className:"flex-1",children:[e.jsx(Se,{className:"h-4 w-4 mr-2"}),i("familyList.viewDetails")]}),e.jsxs("div",{className:"flex items-center gap-1",children:[u&&e.jsx(y,{variant:"ghost",size:"icon",onClick:C,title:i("familyList.edit"),children:e.jsx(J,{className:"h-4 w-4"})}),(u||E)&&e.jsxs(Ue,{children:[e.jsx(_e,{asChild:!0,children:e.jsx(y,{variant:"ghost",size:"icon",className:"hover:bg-accent hover:text-accent-foreground",children:e.jsx(Fe,{className:"h-4 w-4"})})}),e.jsxs(Ge,{align:"end",children:[u&&e.jsxs(X,{onClick:C,children:[e.jsx(J,{className:"mr-2 h-4 w-4"}),e.jsx("span",{children:i("familyList.edit")})]}),E&&e.jsxs(X,{onClick:()=>f(!0),className:"text-destructive focus:text-destructive focus:bg-destructive/10",children:[e.jsx(Z,{className:"mr-2 h-4 w-4"}),e.jsx("span",{children:i("familyList.deleteFamily")})]})]})]})]})]}),e.jsx(ie,{open:D,onOpenChange:f,children:e.jsxs(re,{children:[e.jsxs(le,{children:[e.jsx(ne,{children:i("familyList.cardConfirmDeleteTitle")}),e.jsxs(oe,{children:[i("familyList.cardConfirmDeleteDescription",{name:s.name}),s.memberRefs&&s.memberRefs.length>0&&e.jsx("span",{className:"block mt-2 text-amber-600",children:i("familyList.confirmDeleteWarning",{count:s.memberRefs.length,plural:s.memberRefs.length>
11?"s":""})})]})]}),e.jsxs(ce,{children:[e.jsx(de,{children:i("common:buttons.cancel")}),e.jsx(me,{onClick:B,disabled:w,className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:i("familyList.deleteFamilyButton")})]})]})})]})},as=({families:s,loading:t,error:r,hasMore:b,loadingMore:x,onLoadMore:i,isSelectionMode:N,selectedIds:w,onToggleSelection:p,phoneByFamily:D={}})=>{const{t:f}=$("members");return t?e.jsxs("div",{className:"flex justify-center items-center py-10",children:[e.jsx(O,{className:"h-8 w-8 animate-spin text-primary"}),e.jsx("p",{className:"ml-2",children:f("familyList.loading","Cargando familias...")})]}):r?e.jsxs(qe,{variant:"destructive",className:"my-4",children:[e.jsx(Ae,{className:"h-4 w-4"}),e.jsx(Je,{children:r})]}):s.length===0?e.jsxs("div",{className:"text-center py-10",children:[e.jsx(ee,{className:"h-12 w-12 mx-auto text-muted-foreground mb-4"}),e.jsx("p",{className:"text-muted-foreground",children:f("familyList.noFamilies","No hay familias registradas todavÃa.")})]}):e.jsxs("div",{children:[e.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4 md:gap-6",children:s.map(c=>e.jsx(ss,{family:c,selectionMode:N,isSelected:c.id?w.includes(c.id):!1,onSelect:v=>c.id&&p(c.id,v),headPhone:c.id?D[c.id]:void 0},c.id))}),b&&e.jsx("div",{className:"mt-6 text-center",children:e.jsxs(y,{onClick:i,disabled:x,variant:"outline",children:[x?e.jsx(O,{className:"mr-2 h-4 w-4 animate-spin"}):null,f("familyList.loadMore","Cargar más familias")]})})]})},T=12,ps=()=>{const s=se(),{t}=$("common"),{churchData:r,authLoading:b,initialized:x}=Ke(),[i,N]=n.useState([]),[w,p]=n.useState(!0),[D,f]=n.useState(null),[c,v]=n.useState(null),[P,C]=n.useState(!1),[F,A]=n.useState(!0),[u,E]=n.useState(""),[B,I]=n.useState(0),[d,L]=n.useState([]),[j,W]=n.useState(!1),[he,M]=n.useState(!1),[H,U]=n.useState(!1),[R,xe]=n.useState("asc"),[fe,_]=n.useState({}),ue=a=>{var l,g,m;const o=a.memberRefs||[];if(o.length!==0)return a.headOfHouseholdPersonId||((l=o.find(h=>h.familyRole==="Husband"))==null?void 0:l.personId)||((g=o.find(h=>h.familyRole==="Partner"))==null?void 0:g.personId)||((m=o.find(h=>h.familyRole==="Wife"))==null?void 0:m.personId)||o[0].personId},z=n.useCallback(async(a,o)=>{const l={};o.forEach(m=>{const h=ue(m);m.id&&h&&(l[m.id]=h)});const g=Array.from(new Set(Object.values(l)));if(g.length!==0)try{const m=await Re(a,g),h={};m.forEach(k=>{k.phoneNumber&&(h[k.id]=k.phoneNumber)}),_(k=>{const G={...k};return Object.entries(l).forEach(([De,q])=>{h[q]&&(G[De]=h[q])}),G})}catch(m){console.warn("Failed to load head-of-household phones:",m)}},[]),V=n.useCallback(async(a,o)=>{p(!0),f(null);try{const[{families:l,lastVisible:g},m]=await Promise.all([K(a,{limitCount:T,searchTerm:o,sortDirection:R}),Me(a,o)]);N(l),_({}),v(g),A(l.length===T),I(m),z(a,l)}catch{f(t("familyList.errorLoading"))}finally{p(!1)}},[t,R,z]);n.useEffect(()=>{!x||b||(r!=null&&r.id?V(r.id,u):r||(p(!1),f(t("familyList.errorChurchNotFound"))))},[x,b,r,u,V]);const{canEdit:ge,canDelete:pe}=ae(),je=ge("familias"),ye=pe("familias"),Ne=async()=>{if(!(!(r!=null&&r.id)||!c||!F)){C(!0);try{const{families:a,lastVisible:o}=await K(r.id,{limitCount:T,startAfterDoc:c,searchTerm:u,sortDirection:R});N(l=>[...l,...a]),v(o),A(a.length===T),z(r.id,a)}catch{S.error(t("familyList.errorLoadingMore"))}finally{C(!1)}}},ve=(a,o)=>{L(l=>o?[...l,a]:l.filter(g=>g!==a))},be=a=>{if(a){const o=i.map(l=>l.id).filter(l=>!!l);L(o)}else L([])},Le=async()=>{if(d.length!==0){U(!0);try{await He(d),S.success(t("familyList.successDelete")),L([]),W(!1),M(!1),r!=null&&r.id&&V(r.id,u)}catch(a){console.error(a),S.error(t("familyList.errorDelete"))}finally{U(!1)}}},we=a=>{E(a),L([])};return e.jsxs("div",{className:"space-y-6",children:[e.jsx(Xe,{title:t("familyList.title","Familias"),description:t("familyList.description","Gestiona los núcleos familiares de la congregación"),icon:e.jsx(Y,{className:"h-5 w-5"}),badge:`${B}`,action:je?e.jsxs(y,{onClick:()=>s("/app/families/register"),size:"sm",children:[e.jsx(Ee,{className:"h-4 w-4 mr-2"}),e.jsx("span",{children:t("familyList.addFamilyButton")})]}):void 0}),e.jsx("div",{className:"mb-4",children:e.jsx(Qe,{searchValue:u,onSearchChange:we,searchPlaceholder:t("familyList.searchPlaceholder","Buscar familias..."),filters:e.jsxs(e.Fragment,{children:[e.jsx(y,{variant:"outline",size:"icon",className:"h-9 w-9",onClick:()=>xe(a=>a==="asc"?"desc":"asc"),title:t("familyList.sortByName"),children:e.jsx(Ie,{className:"h-4 w-4 text-muted-foreground"})}),e.jsxs(y,{variant:j?"secondary":"ghost",size:"sm",onClick:()=>{W(!j),L([])},title:t(j?"familyList.cancelSelection":"familyList.multipleSelection"),className:j?"bg-accent text-accent-foreground":"",children:[j?e.jsx(ee,{className:"h-4 w-4"}):e.jsx(Te,{className:"h-4 w-4"}),e.jsx("span",{className:"ml-2 hidden sm:inline",children:t(j?"familyList.cancel":"familyList.select")})]})]}),extraActions:j&&d.length>0?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-2 bg-primary/10 text-primary px-3 h-9 rounded-md border border-primary/20",children:[e.jsx(te,{checked:d.length>0&&d.length===i.length,onCheckedChange:a=>be(a===!0),className:"h-4 w-4 border-primary/30 data-[state=checked]:bg-primary"}),e.jsxs("span",{className:"text-sm font-medium",children:[d.length," ",t("familyList.selected")]})]}),ye&&e.jsxs(y,{variant:"destructive",size:"sm",onClick:()=>M(!0),className:"animate-in fade-in zoom-in duration-200",children:[e.jsx(Z,{className:"h-4 w-4 sm:mr-2"}),e.jsx("span",{className:"hidden sm:inline",children:t("familyList.delete")})]})]}):void 0})}),e.jsx(as,{families:i,loading:w,error:D,hasMore:F,loadingMore:P,onLoadMore:Ne,isSelectionMode:j,selectedIds:d,onToggleSelection:ve,phoneByFamily:fe}),e.jsx(ie,{open:he,onOpenChange:M,children:e.jsxs(re,{children:[e.jsxs(le,{children:[e.jsx(ne,{children:t("familyList.confirmDeleteTitle",{count:d.length})}),e.jsx(oe,{children:t("familyList.confirmDeleteDescription")})]}),e.jsxs(ce,{children:[e.jsx(de,{disabled:H,children:t("familyList.cancel")}),e.jsxs(me,{onClick:a=>{a.preventDefault(),Le()},className:"bg-destructive hover:bg-destructive/90",disabled:H,children:[H?e.jsx(O,{className:"mr-2 h-4 w-4 animate-spin"}):null,t("familyList.delete")]})]})]})})]})};export{ps 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.