1import{r as T,R as c,j as e}from"./react-vendor-BhAyUcBJ.js";import{r as Y,t as ee,q,w as $,T as te,o as se,d as z,k as ae,j as ne,s as re,u as le,b as oe,c as V,f as ce,l as ie,g as Z,z as J,h as K}from"./appEntry-D3Ky44Xp.js";import{u as de}from"./useStudents-CS7QmMVi.js";import{u as ue}from"./useClassSchedules-CcWLwxhR.js";import{u as me}from"./useWhatsAppTemplate-CSrxacY_.js";import{P as X}from"./phone-gHmFbE-S.js";import{C as he}from"./chevron-up-DOkLX1u-.js";import{C as xe}from"./chevron-down-BZBXmlbe.js";import{U as ge}from"./users-wS6ag6ve.js";import{C as pe}from"./clock-epjGKz7Q.js";const fe=(f,n=30)=>{const[h,P]=T.useState([]),[_,L]=T.useState(!0),[C,E]=T.useState(!1),[M,B]=T.useState(n),[j,k]=T.useState(!1),R=T.useRef(-1),w=T.useRef(!1);return T.useEffect(()=>{if(!f){P([]),L(!1);return}const D=Y(new Date,M),N=ee(z,"callingAssignments"),H=q(N,$("volunteerId","==",f),$("createdAt",">=",te.fromDate(D)));return se(H,x=>{console.log("[useCallingAssignments] Fetched Assignments:",x.docs.length);const b=x.docs.map(r=>{var S,m,a,l;const s=r.data(),u=r.ref.path.split("/"),i=u.length>=2?u[1]:s.scheduleId||"";return{id:r.id,scheduleId:i,studentId:s.studentId,volunteerId:s.volunteerId,status:s.status||"",comments:s.comments||"",attendance:s.attendance||"",createdAt:((m=(S=s.createdAt)==null?void 0:S.toDate)==null?void 0:m.call(S))||void 0,updatedAt:((l=(a=s.updatedAt)==null?void 0:a.toDate)==null?void 0:l.call(a))||void 0}});w.current&&(b.length<=R.current&&k(!0),w.current=!1,E(!1)),R.current=b.length,P(b),L(!1)},x=>{console.error("[useCallingAssignments] Snapshot error",x),P([]),L(!1),w.current&&(w.current=!1,E(!1))})},[f,M]),{assignments:h,loading:_,loadingMore:C,noMoreData:j,updateStudentEntry:async(D,N,H)=>{try{const A=ae(z,"classSchedules",N,"callingAssignments",D);await ne(A,{...H,updatedAt:re()})}catch(A){throw console.error("Failed to update assignment doc:",A),A}},loadMore:()=>{j||w.current||(w.current=!0,E(!0),B(D=>D+90))}}},be=({assignment:f,student:n,schedule:h,allSchedules:P,onUpdate:_})=>{const{openWhatsApp:L}=me(),[C,E]=c.useState(!1),[M,B]=c.useState([]),[j,k]=c.useState(!1),[R,w]=c.useState([]),[F,O]=c.useState(0),[D,N]=c.useState(!1),[H,A]=c.useState(!1),x=4,[b,r]=c.useState(f.status||""),[s,u]=c.useState(f.comments||""),[i,S]=c.useState(f.attendance||""),m=async t=>{t.status!==void 0&&r(t.status),t.comments!==void 0&&u(t.comments),t.attendance!==void 0&&S(t.attendance),await _(f.id,h.id,{status:t.status??b,comments:t.comments??s,attendance:t.attendance??i})},a=async t=>{k(!0);try{const I=(await Promise.allSettled(t.map(async o=>{const g=q(V(z,"classSchedules",o.id,"callingAssignments"),$("studentId","==",n.id)),U=await Z(g);if(!U.empty){const p=U.docs[0].data();return{id:o.id,date:o.date,title:o.title,status:p.status,comments:p.comments,attendance:p.attendance,volunteerName:p.volunteerName||null,volunteerId:p.volunteerId||null}}return null}))).filter(o=>o.status==="fulfilled"&&o.value!=null).map(o=>o.value),W=[...new Set(I.filter(o=>o.volunteerId&&!o.volunteerName).map(o=>o.volunteerId))];if(W.length>0){const o=new Map;for(let g=0;g<W.length;g+=10){const U=W.slice(g,g+10),p=q(V(z,"users"),$(K(),"in",U));(await Z(p)).docs.forEach(G=>{o.set(G.id,G.data().name||"Unknown")})}I.forEach(g=>{g.volunteerId&&!g.volunteerName&&(g.volunteerName=o.get(g.volunteerId)||null)})}B(o=>[...o,...I])}catch(v){console.error(v),J.error("Failed to load history")}finally{k(!1)}},l=async()=>{var t;if(!C&&!H){const v=((n==null?void 0:n.batch)||"").trim();if(v){k(!0);try{const I=h.date instanceof Date?h.date:(t=h.date)!=null&&t.toDate?h.date.toDate():new Date(h.date),W=q(V(z,"classSchedules"),$("date","<",I),ce("date","desc"),ie(50)),g=(await Z(W)).docs.map(p=>{var G;const Q=p.data();return{id:p.id,...Q,date:((G=Q.date)==null?void 0:G.toDate())||new Date}}).filter(p=>p.id===h.id?!1:(p.batch||"").split(",").map(G=>G.trim()).includes(v));w(g);const U=g.slice(0,x);O(x),N(g.length>x),U.length>0&&await a(U),A(!0)}catch(I){console.error("Failed to fetch past schedules:",I),J.error("Failed to load history")}finally{k(!1)}}else A(!0)}E(!C)},d=async()=>{const t=R.slice(F,F+x);t.length>0?(O(v=>v+x),N(R.length>F+x),await a(t)):N(!1)},y=t=>{switch(t){case"Will Come":return"bg-green-100 text-green-800";
1case"May Come":return"bg-yellow-100 text-yellow-800";case"Didn't Pick Call":return"bg-red-100 text-red-800";case"Can't Come":return"bg-gray-100 text-gray-800";default:return"bg-gray-50 text-gray-600"}};return e.jsxs("div",{className:"p-3 bg-gray-50 rounded-lg border border-gray-200",children:[e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsx("p",{className:"font-medium text-gray-900",children:(n==null?void 0:n.name)||"Unknown"})}),e.jsxs("div",{className:"flex flex-wrap gap-2 text-sm text-gray-600 mt-0.5",children:[e.jsx("span",{children:n==null?void 0:n.phone}),(n==null?void 0:n.batch)&&e.jsxs(e.Fragment,{children:[e.jsx("span",{children:"â¢"}),e.jsx("span",{className:"inline-flex items-center px-2 py-0.5 rounded bg-blue-50 text-blue-700 text-xs font-medium",children:n.batch})]})]})]}),e.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[(n==null?void 0:n.phone)&&e.jsx("button",{onClick:t=>{t.stopPropagation(),L(n.phone.replace(/\D/g,""),n)},className:"inline-flex items-center justify-center p-2 rounded-md border border-green-600 bg-white text-green-600 hover:bg-green-50 transition-colors",title:"Chat on WhatsApp",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"currentColor",className:"w-4 h-4",children:e.jsx("path",{d:"M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.008-.57-.008-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413Z"})})}),e.jsxs("a",{href:`tel:${(n==null?void 0:n.phone)||""}`,className:"inline-flex items-center px-3 py-1.5 rounded-md border text-sm bg-green-600 hover:bg-green-700 text-white shrink-0",title:"Call",children:[e.jsx(X,{className:"h-4 w-4 mr-1"})," Call"]})]})]}),e.jsxs("div",{className:"mt-3 grid grid-cols-1 md:grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs text-gray-600 mb-1",children:"Status"}),e.jsxs("select",{className:"w-full border rounded-md px-2 py-1.5 text-sm bg-white",value:b,onChange:t=>m({status:t.target.value}),children:[e.jsx("option",{value:"",children:"Select statusâ¦"}),e.jsx("option",{value:"Will Come",children:"ð¢ Will Come"}),e.jsx("option",{value:"May Come",children:"ð¡ May Come"}),e.jsx("option",{value:"Didn't Pick Call",children:"ð´ Didn't Pick Call"}),e.jsx("option",{value:"Can't Come",children:"â« Can't Come"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs text-gray-600 mb-1",children:"Comments"}),e.jsx("input",{className:"w-full border rounded-md px-2 py-1.5 text-sm",value:s,placeholder:"Comments (optional)",onChange:t=>u(t.target.value),onBlur:()=>m({comments:s})})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs text-gray-600 mb-1",children:"Attendance"}),e.jsxs("select",{className:"w-full border rounded-md px-2 py-1.5 text-sm bg-white",value:i,onChange:t=>m({attendance:t.target.value}),children:[e.jsx("option",{value:"",children:"â"}),e.jsx("option",{value:"Absent",children:"ð´ Absent"}),e.jsx("option",{value:"Present",children:"ð¢ Present"})]})]})]}),e.jsxs("div",{className:"mt-3",children:[e.jsxs("button",{onClick:l,className:"flex items-center text-xs text-blue-600 hover:text-blue-800 font-medium",children:[C?e.jsx(he,{className:"h-3 w-3 mr-1"}):e.jsx(xe,{className:"h-3 w-3 mr-1"}),"Show Previous Calling History"]}),C&&e.jsxs("div",{className:"mt-2 space-y-2 pl-2 border-l-2 border-blue-100",children:[M.length>0&&e.jsx("div",{className:"space-y-3",children:M.map((t,v)=>e.jsxs("div",{className:"text-sm py-2 border-b border-gray-100 last:border-0",children:[e.jsxs("div",{className:"flex justify-between items-start mb-1",children:[e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"font-medium text-gray-900",children:new Date(t.date).toLocaleDateString("en-GB",{day:"numeric",month:"long"})}),t.volunteerName&&e.jsxs("span",{className:"text-xs text-gray-500",children:["Called by: ",t.volunteerName]})]}),e.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[e.jsx("span",{className:`px-2 py-0.5 rounded text-xs font-medium whitespace-nowrap ${y(t.status)}`,children:t.status||"No Status"}),e.jsx("span",{title:t.attendance,children:t.attendance==="Present"?"ð¢":t.attendance==="Absent"?"ð´":"âªï¸"})]})]}),t.comments&&e.jsxs("div",{className:"text-gray-600 mt-1 pl-2 border-l-2 border-gray-200 text-xs italic break-words",children:['"',t.comments,'"']})]},v))}),j&&e.jsxs("div",{className:"text-xs text-gray-500 py-2 flex items-center justify-center",children:[e.jsx("div",{className:"animate-spin rounded-full h-4 w-4 border-2 border-blue-500 border-t-transparent mr-2"}),"Loading history..."]}),!j&&M.length===0&&e.jsx("div",{className:"text-xs text-gray-500 py-1",children:"No relevant history found."}),!j&&D&&e.jsx("button",{onClick:d,className:"w-full text-center text-xs text-blue-600 hover:text-blue-800 font-medium py-1 mt-1 hover:underline",children:"See More"})]})]})]})},ye=()=>{const{user:f}
1=le(),n=oe(),{assignments:h,loading:P,updateStudentEntry:_,loadMore:L,loadingMore:C,noMoreData:E}=fe(f==null?void 0:f.id),{students:M,loading:B}=de({fetch:!0}),{classSchedules:j}=ue();c.useState(null);const[k,R]=c.useState(!1),[w,F]=c.useState([]),[O,D]=c.useState(!1),[N,H]=c.useState(3);c.useEffect(()=>{if(P||h.length===0)return;const r=new Set(j.map(i=>i.id)),s=[...new Set(h.map(i=>i.scheduleId))].filter(i=>i&&!r.has(i));if(s.length===0){F([]);return}(async()=>{D(!0);try{const i=[];for(let m=0;m<s.length;m+=30)i.push(s.slice(m,m+30));const S=[];for(const m of i){const a=q(V(z,"classSchedules"),$(K(),"in",m));(await Z(a)).docs.forEach(d=>{var t,v,I;const y=d.data();S.push({id:d.id,...y,date:((t=y.date)==null?void 0:t.toDate())||new Date,createdAt:((v=y.createdAt)==null?void 0:v.toDate())||new Date,updatedAt:((I=y.updatedAt)==null?void 0:I.toDate())||new Date})})}F(S)}catch(i){console.error("Failed to fetch past schedules:",i)}finally{D(!1)}})()},[h,j,P]);const A=c.useMemo(()=>{const r=new Map;return j.forEach(s=>r.set(s.id,s)),w.forEach(s=>r.set(s.id,s)),Array.from(r.values())},[j,w]),x=c.useMemo(()=>{const r={};return h.forEach(s=>{r[s.scheduleId]||(r[s.scheduleId]={scheduleId:s.scheduleId,studentAssignments:[]}),r[s.scheduleId].studentAssignments.push(s)}),Object.values(r).sort((s,u)=>s.scheduleId.localeCompare(u.scheduleId))},[h]),b=r=>A.find(s=>s.id===r);return c.useMemo(()=>new Map(M.map(r=>[r.id,r])),[M]),P||B?e.jsx("div",{className:"flex items-center justify-center min-h-64",children:e.jsx("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold text-gray-900",children:"Calling Service"}),e.jsx("p",{className:"text-gray-600 mt-1",children:"Manage your calling assignments for upcoming classes."})]}),(()=>{if(x.length===0)return e.jsxs("div",{className:"bg-white rounded-lg shadow-sm border border-gray-200 p-6 text-center text-gray-500",children:[e.jsx(X,{className:"h-12 w-12 mx-auto mb-4 text-gray-400"}),e.jsx("p",{children:"No calling assignments yet."})]});const r=new Date;r.setHours(0,0,0,0);const s=x.filter(a=>{const l=b(a.scheduleId);if(!l)return!1;const d=new Date(l.date);return d.setHours(0,0,0,0),d.getTime()>=r.getTime()});let u=x.filter(a=>{const l=b(a.scheduleId);if(!l)return!1;const d=new Date(l.date);return d.setHours(0,0,0,0),d.getTime()<r.getTime()});u=u.sort((a,l)=>{const d=b(a.scheduleId),y=b(l.scheduleId);return new Date((y==null?void 0:y.date)||0).getTime()-new Date((d==null?void 0:d.date)||0).getTime()});const i=u.slice(0,N),S=()=>{const a=N+3;H(a),a>=u.length&&!E&&L()},m=a=>{const l=b(a.scheduleId),d=a.studentAssignments.length,y=d>0&&a.studentAssignments.every(t=>(t.status||"").toString().trim()!=="");return e.jsx("div",{className:"bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden",children:e.jsxs("div",{className:"p-4 border-b border-gray-200 flex flex-col sm:flex-row sm:items-center justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-gray-900",children:(l==null?void 0:l.title)||"Unknown Schedule"}),e.jsxs("div",{className:"mt-1 text-sm text-gray-600 flex flex-wrap gap-4",children:[e.jsxs("span",{className:"inline-flex items-center gap-1",children:[e.jsx(ge,{className:"h-4 w-4"})," ",(l==null?void 0:l.speaker)||"Unknown Speaker"]}),e.jsxs("span",{className:"inline-flex items-center gap-1",children:[e.jsx(pe,{className:"h-4 w-4"})," ",l?`${new Date(l.date).toLocaleDateString()} at ${l.time}`:"Unknown Date"]}),y&&e.jsx("span",{className:"inline-flex items-center px-2 py-0.5 text-xs rounded-full bg-green-100 text-green-700",children:"Completed"})]})]}),e.jsxs("button",{onClick:()=>n(`/calling/details?scheduleId=${a.scheduleId}`),className:"w-full sm:w-auto inline-flex justify-center items-center px-3 py-1.5 rounded-md border text-sm hover:bg-gray-50",children:["View assigned (",d,")"]})]})},a.scheduleId)};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"space-y-4",children:[s.length===0&&e.jsx("div",{className:"bg-white rounded-lg shadow-sm border border-gray-200 p-4 text-sm text-gray-600",children:"No upcoming calling services."}),s.map(a=>m(a))]}),e.jsxs("div",{children:[e.jsxs("button",{className:"w-full text-left px-6 py-3 bg-blue-100 text-base font-medium text-gray-800 border border-blue-200 rounded-md flex items-center justify-between hover:bg-blue-200 transition-colors",onClick:()=>R(a=>!a),children:[e.jsx("span",{children:"Past Calling Services"}),e.jsx("svg",{className:`h-5 w-5 transition-transform ${k?"rotate-180":""}`,viewBox:"0 0 20 20",fill:"currentColor",children:e.jsx("path",{fillRule:"evenodd",d:"M5.23 7.21a.75.75 0 011.06.02L10 11.187l3.71-3.956a.75.75 0 111.08 1.04l-4.25 4.53a.75.75 0 01-1.08 0l-4.25-4.53a.75.75 0 01.02-1.06z",clipRule:"evenodd"})})]}),k&&e.jsxs("div",{className:"mt-4 space-y-4",children:[u.length===0&&!C&&e.jsx("div",{className:"bg-white rounded-lg shadow-sm border border-gray-200 p-4 text-sm text-gray-600",children:"No past calling services."}),i.map(a=>m(a)),C&&e.jsxs("div",{className:"flex items-center justify-center py-4",children:[e.jsx("div",{className:"animate-spin rounded-full h-5 w-5 border-2 border-blue-500 border-t-transparent mr-2"}),e.jsx("span",{className:"text-sm text-gray-600",children:"Loading more..."})]}),!C&&u.length>0&&(N<u.length||!E)&&e.jsx("button",{onClick:S,className:"w-full text-center py-3 text-sm text-blue-600 hover:text-blue-800 font-medium hover:underline rounded-lg border border-gray-200 bg-white hover:bg-gray-50 transition-colors",children:"See more past services"})]})]})]})})()]})},ke=Object.freeze(Object.defineProperty({__proto__:null,StudentAssignmentRow:be,default:ye},Symbol.toStringTag,{value:"Module"}));
1export{ke as C,be as S,fe as u};
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.