File size: 7,658 Bytes
ce88bcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
 * Application de comparaison des KPIs COP régionales
 */

// ============================================================
// Initialisation
// ============================================================
document.addEventListener("DOMContentLoaded", () => {
    renderTiles();
    renderTable();
});

// ============================================================
// Tuiles de synthèse
// ============================================================
function renderTiles() {
    const container = document.getElementById("tiles-container");
    container.innerHTML = "";

    for (const [catName, catInfo] of Object.entries(CATEGORIES)) {
        const catKpis = KPI_DATA.filter(k => k.categorie === catName);
        const comparableCount = catKpis.filter(k => k.comparable).length;
        const total = catKpis.length;
        const pct = total > 0 ? Math.round((comparableCount / total) * 100) : 0;

        const col = document.createElement("div");
        col.className = "fr-col-12 fr-col-sm-6 fr-col-md-4 fr-col-lg-3";

        col.innerHTML = `
            <div class="tile-category" onclick="filterByCategory('${catName}')" tabindex="0"
                 role="button" aria-label="Filtrer par ${catName}">
                <div class="tile-category__icon">${catInfo.icon}</div>
                <div class="tile-category__title">${catName}</div>
                <div class="tile-category__count">
                    ${total} KPI${total > 1 ? 's' : ''} — 
                    <strong>${comparableCount}</strong> comparable${comparableCount > 1 ? 's' : ''}
                </div>
                <div class="tile-category__bar">
                    <div class="tile-category__bar-fill bar-fill--green" style="width: ${pct}%"></div>
                </div>
            </div>
        `;

        container.appendChild(col);
    }
}

// ============================================================
// Tableau de comparaison
// ============================================================
function renderTable(filteredData = null) {
    const tbody = document.getElementById("kpi-tbody");
    tbody.innerHTML = "";

    const data = filteredData || KPI_DATA;
    let currentCategory = "";

    data.forEach(kpi => {
        // Ligne catégorie
        if (kpi.categorie !== currentCategory) {
            currentCategory = kpi.categorie;
            const catRow = document.createElement("tr");
            catRow.className = "category-row";
            catRow.dataset.category = currentCategory;

            const catInfo = CATEGORIES[currentCategory] || { icon: "📋" };
            catRow.innerHTML = `
                <td colspan="6" style="border-left: 4px solid ${catInfo.color || '#000091'}">
                    ${catInfo.icon} ${currentCategory}
                </td>
            `;
            tbody.appendChild(catRow);
        }

        // Ligne KPI
        const row = document.createElement("tr");
        row.dataset.category = kpi.categorie;
        row.dataset.comparable = kpi.comparable ? "true" : "false";

        const comparableTag = getComparableTag(kpi);

        row.innerHTML = `
            <td class="col-indicateur">
                <strong>${kpi.indicateur}</strong>
                <br><small style="color: #929292">${kpi.unite}</small>
            </td>
            <td class="col-region">${formatValueWithSource(kpi.grand_est, "grand_est", kpi.grand_est_page)}</td>
            <td class="col-region">${formatValueWithSource(kpi.occitanie, "occitanie", kpi.occitanie_page)}</td>
            <td class="col-region">${formatValueWithSource(kpi.aura, "aura", kpi.aura_page)}</td>
            <td class="col-region">${formatValueWithSource(kpi.normandie, "normandie", kpi.normandie_page)}</td>
            <td class="col-comparable">${comparableTag}</td>
        `;

        tbody.appendChild(row);
    });

    // Compteur
    updateCounter(data);
}

function formatValueWithSource(val, regionKey, page) {
    if (!val || val === "Non chiffré" || val === "Non chiffré spécifiquement" || val === "Non traité" || val.startsWith("Non traité") || val.startsWith("Non chiffré") || val.startsWith("Non détaillé")) {
        const na = `<span class="value-na">${val || '—'}</span>`;
        return na;
    }
    
    // Mettre en avant les chiffres
    let formatted = val.replace(/(\d[\d\s,.×÷%]*(?:\s*(?:ktCO2eq|MtCO2eq|TWh|GWh|MW|MWc|km|ha|%|millions?|milliards?|M€|Mm3))?)/g,
        '<strong>$1</strong>');

    // Ajouter le lien source avec page
    if (page && SOURCES[regionKey]) {
        const link = `${SOURCES[regionKey].url}#page=${page}`;
        formatted += ` <a href="${link}" target="_blank" rel="noopener" class="source-link" title="Voir page ${page} du PDF ${SOURCES[regionKey].name}">p.${page}</a>`;
    }

    return formatted;
}

function getComparableTag(kpi) {
    if (kpi.comparable) {
        return `
            <span class="tag-comparable tag-comparable--yes" title="${kpi.comparableNote}">
                ✓ Oui
            </span>
        `;
    }
    // Check if at least 2 regions have data
    const regions = [kpi.grand_est, kpi.occitanie, kpi.aura, kpi.normandie];
    const withData = regions.filter(v => v && !v.startsWith("Non") && !v.includes("N/D")).length;

    if (withData >= 2) {
        return `
            <span class="tag-comparable tag-comparable--partial" title="${kpi.comparableNote}">
                ~ Partiel
            </span>
        `;
    }

    return `
        <span class="tag-comparable tag-comparable--no" title="${kpi.comparableNote}">
            — Non
        </span>
    `;
}

// ============================================================
// Filtrage
// ============================================================
function filterTable() {
    const category = document.getElementById("filter-category").value;
    const search = document.getElementById("search-input").value.toLowerCase().trim();
    const onlyComparable = document.getElementById("toggle-comparable").checked;

    let filtered = KPI_DATA;

    if (category !== "all") {
        filtered = filtered.filter(k => k.categorie === category);
    }

    if (search) {
        filtered = filtered.filter(k =>
            k.indicateur.toLowerCase().includes(search) ||
            k.categorie.toLowerCase().includes(search) ||
            k.grand_est.toLowerCase().includes(search) ||
            k.occitanie.toLowerCase().includes(search) ||
            k.aura.toLowerCase().includes(search) ||
            k.normandie.toLowerCase().includes(search) ||
            k.unite.toLowerCase().includes(search)
        );
    }

    if (onlyComparable) {
        filtered = filtered.filter(k => k.comparable);
    }

    renderTable(filtered);
}

function filterByCategory(catName) {
    const select = document.getElementById("filter-category");
    if (select.value === catName) {
        select.value = "all";
    } else {
        select.value = catName;
    }
    filterTable();

    // Scroll to table
    document.getElementById("comparaison").scrollIntoView({ behavior: "smooth" });
}

// ============================================================
// Compteur de résultats
// ============================================================
function updateCounter(data) {
    const tableTitle = document.querySelector("#comparaison h2");
    const total = data.length;
    const comparable = data.filter(k => k.comparable).length;
    tableTitle.innerHTML = `Comparaison détaillée des KPIs <span style="font-size: 0.875rem; font-weight: 400; color: #666">(${total} KPI${total > 1 ? 's' : ''}, dont ${comparable} comparable${comparable > 1 ? 's' : ''})</span>`;
}