File size: 3,937 Bytes
6aa6081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { useState, useEffect } from 'react';
import { getHeatmap, type HeatmapEntry } from '../services/attendanceService';

function HeatmapGrid({ data, year }: { data: HeatmapEntry[]; year: number }) {
  const dayCounts: Record<string, number> = {};
  data.forEach((d) => { dayCounts[d.date] = d.count; });

  const startDate = new Date(year, 0, 1);
  const endDate = new Date(year, 11, 31);
  const cells: { date: string; count: number; day: number }[] = [];
  const current = new Date(startDate);
  while (current <= endDate) {
    const key = current.toISOString().split('T')[0];
    cells.push({ date: key, count: dayCounts[key] || 0, day: current.getDay() });
    current.setDate(current.getDate() + 1);
  }

  const weeks: typeof cells[] = [];
  let week: typeof cells = [];
  cells.forEach((c) => {
    week.push(c);
    if (c.day === 6) { weeks.push(week); week = []; }
  });
  if (week.length) weeks.push(week);

  const getColor = (count: number) => {
    if (count === 0) return '#ebedf0';
    if (count <= 2) return '#9be9a8';
    if (count <= 5) return '#40c463';
    if (count <= 10) return '#30a14e';
    return '#216e39';
  };

  return (
    <div>
      <div className="flex gap-0.5 overflow-x-auto">
        {weeks.map((w, wi) => (
          <div key={wi} className="flex flex-col gap-0.5">
            {Array.from({ length: 7 }).map((_, di) => {
              const c = w.find((x) => x.day === di);
              return (
                <div
                  key={di}
                  className="rounded-sm"
                  style={{ width: 12, height: 12, background: c ? getColor(c.count) : 'transparent' }}
                  title={c ? `${c.date}: ${c.count} asistencias` : ''}
                />
              );
            })}
          </div>
        ))}
      </div>
      <div className="flex items-center gap-1 mt-2 text-xs text-gray-400">
        <span>Menos</span>
        {[0, 2, 5, 10, 15].map((v) => (
          <div key={v} className="rounded-sm" style={{ width: 12, height: 12, background: getColor(v) }} />
        ))}
        <span>Más</span>
      </div>
    </div>
  );
}

interface Props {
  studentId: number;
  studentName: string;
  onClose: () => void;
}

export default function HeatmapModal({ studentId, studentName, onClose }: Props) {
  const [data, setData] = useState<HeatmapEntry[]>([]);
  const [year, setYear] = useState(new Date().getFullYear());
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    setLoading(true);
    getHeatmap(studentId, year).then(setData).catch(() => setData([])).finally(() => setLoading(false));
  }, [studentId, year]);

  return (
    <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4" onClick={onClose}>
      <div className="bg-white rounded-2xl shadow-2xl max-w-2xl w-full max-h-[90vh] overflow-y-auto" onClick={(e) => e.stopPropagation()} style={{ maxWidth: 700 }}>
        <div className="sticky top-0 bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between rounded-t-2xl">
          <h2 className="text-xl font-bold">Mapa de Calor: {studentName}</h2>
          <button onClick={onClose} className="text-gray-400 hover:text-gray-600 text-2xl leading-none">&times;</button>
        </div>
        <div className="p-6 space-y-6">
          <div className="flex gap-3 items-end flex-wrap">
            <input type="number" value={year} onChange={(e) => setYear(Number(e.target.value))} className="w-full border border-gray-300 rounded-lg px-3 py-2" style={{ width: 100 }} />
          </div>
          {loading ? (
            <p className="text-sm text-gray-400 text-center py-8">Cargando...</p>
          ) : data.length > 0 ? (
            <HeatmapGrid data={data} year={year} />
          ) : (
            <p className="text-sm text-gray-400 text-center py-8">Sin datos de asistencia para este año</p>
          )}
        </div>
      </div>
    </div>
  );
}