File size: 4,812 Bytes
6ea9821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
const map = L.map('map', {
    zoomControl: false,
    dragging: false,
    scrollWheelZoom: false,
    doubleClickZoom: false,
    attributionControl: false
});

function triggerReset() {
    const pass = prompt("Enter Admin Password to Wipe ALL Attendance Data:");
    if (!pass) return;

    if (confirm("Are you ABSOLUTELY sure? This will mark every teacher in the CSV as 'Pending' again.")) {
        fetch('/api/reset_all', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ password: pass })
        })
        .then(res => res.json())
        .then(data => {
            if (data.success) {
                alert("System Reset Successful!");
                location.reload();
            } else {
                alert("Error: " + data.error);
            }
        });
    }
}

// --- NEW DATA RESOLVER FUNCTION ---
// This ensures map boundaries perfectly match your database names
function resolve(n) { 
    if(!n) return ""; 
    let c = n.trim().toLowerCase(); 
    if(c.includes("sas") || c.includes("mohali") || c.includes("sahibzada")) return "MOHALI"; 
    if(c.includes("muktsar")) return "MUKTSAR"; 
    if(c.includes("rupnagar") || c.includes("ropar")) return "RUPNAGAR"; 
    if(c.includes("bhagat") || c.includes("nawanshahr") || c.includes("sbs")) return "NAWANSHAHR"; 
    if(c.includes("taran") || c.includes("tarn")) return "TARN TARAN"; 
    if(c.includes("firozpur") || c.includes("ferozepur")) return "FEROZEPUR"; 
    if(c.includes("fatehgarh")) return "FATEHGARH SAHIB"; 
    return c.toUpperCase(); 
}

fetch('/api/district_counts')
    .then(response => response.json())
    .then(districtCounts => {
        fetch('/static/punjab_districts.geojson')
            .then(response => response.json())
            .then(geojsonData => {
                
                const punjabLayer = L.geoJSON(geojsonData, {
                    style: function (feature) {
                        return {
                            color: "#ffffff",
                            weight: 2,
                            fillColor: "#ff9933",
                            fillOpacity: 1
                        };
                    },
                    onEachFeature: function (feature, layer) {
                        const rawMapName = feature.properties.dtname;
                        
                        // Pass the raw map name through the resolver to match the backend
                        const resolvedName = resolve(rawMapName); 
                        const count = districtCounts[resolvedName] || 0;

                        // Display the raw map name to the user, but use the correct count
                        layer.bindTooltip(`<b>${rawMapName}</b><br>Total Teachers: ${count}`);

                        layer.on('click', function () {
                            // Use the resolved name to fetch data from the backend
                            fetch(`/api/teachers/${resolvedName}`)
                                .then(res => res.json())
                                .then(teachers => {
                                    const container = document.getElementById('table-container');
                                    let html = `<h4>Teachers in ${rawMapName}</h4>`;

                                    if (teachers.length === 0) {
                                        html += `<p>No data available.</p>`;
                                    } else {
                                        html += `<table>

                                                    <tr>

                                                        <th>Name</th>

                                                        <th>Subject</th>

                                                        <th>School</th>

                                                    </tr>`;
                                        teachers.forEach(t => {
                                            html += `<tr>

                                                        <td>${t.TeacherName}</td>

                                                        <td>${t.Subject}</td>

                                                        <td>${t.School}</td>

                                                     </tr>`;
                                        });
                                        html += `</table>`;
                                    }
                                    container.innerHTML = html;
                                });
                        });
                    }
                }).addTo(map);

                map.fitBounds(punjabLayer.getBounds());
            });
    });