File size: 9,067 Bytes
21918f8
 
 
 
 
 
 
 
 
 
 
 
 
 
c54c5ea
 
 
 
 
 
 
 
 
 
 
 
21918f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c54c5ea
 
21918f8
 
c54c5ea
21918f8
c54c5ea
21918f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c54c5ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bd4dcb
 
c54c5ea
 
 
 
2bd4dcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c54c5ea
2bd4dcb
 
 
 
 
 
 
 
 
 
 
 
c54c5ea
 
2bd4dcb
c54c5ea
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
203
204
205
206
207
// Sample data for demonstration
const filtersData = {
    "Post1": {
        "filter1": ["Field1", "Field2"],
        "filter2": ["Field3"],
        "filter3": []
    },
    "Post2": {
        "filter1": ["Field4"],
        "filter2": [],
        "filter3": ["Field5", "Field6"]
    }
};
const allFields = ["Poste", "Field1", "Field2", "Field3", "Field4", "Field5", "Field6", "Field7", "Field8"];
const sampleData = {
    "Post1": {
        "Field1": ["Value1", "Value2", "Value1", "Value3"],
        "Field2": ["A", "B", "C", "A"],
        "Field3": ["X", "Y", "Z"]
    },
    "Post2": {
        "Field4": ["One", "Two", "Three"],
        "Field5": ["1", "2", "3", "2"],
        "Field6": ["Alpha", "Beta", "Alpha"]
    }
};
const selectedPosts = ["Post1", "Post2"];

// Initialize the page
$(document).ready(function() {
    renderPosts();
    renderFilters();
});

function renderPosts() {
    const postsContainer = $('#postsContainer');
    postsContainer.empty();
    
    allFields.filter(field => field === 'Poste').forEach(poste => {
        const isChecked = selectedPosts.includes(poste);
        postsContainer.append(`
            <label class="flex items-center gap-2">
                <input type="checkbox" name="postes" id="${poste}" value="${poste}" ${isChecked ? 'checked' : ''} onchange="updateSelectedPosts()">
                ${poste}
            </label>
        `);
    });
}

function renderFilters() {
    const filtersContainer = $('#filtersContainer');
    filtersContainer.empty();
    
    selectedPosts.forEach((post, idx) => {
        const filters = filtersData[post] || {filter1: [], filter2: [], filter3: []};
        
        filtersContainer.append(`
            <div class="mt-8">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">Filters for ${post}</h3>
                <div class="flex space-x-3 mb-4">
                    <button type="button" class="inline-flex items-center px-3 py-2 bg-offline-red text-white rounded-lg hover:bg-red-600 transition-colors" onclick="deselectAllFilters(${idx})">
                        <i data-feather="trash-2" class="mr-2 w-4 h-4"></i>
                        Deselect All
                    </button>
                    <button type="button" class="inline-flex items-center px-3 py-2 bg-atos-green text-white rounded-lg hover:bg-green-700 transition-colors" onclick="addFilter(${idx})">
                        <i data-feather="plus" class="mr-2 w-4 h-4"></i>
                        Add Filter
                    </button>
                    <button type="submit" name="submit" value="${post}" class="inline-flex items-center px-3 py-2 bg-atos-blue text-white rounded-lg hover:bg-dark-blue transition-colors">
                        <i data-feather="check" class="mr-2 w-4 h-4"></i>
                        Save Filters
                    </button>
                </div>
                <div class="filtersContainer" data-index="${idx}">
                    <div class="grid grid-cols-1 gap-6 md:grid-cols-3">
                        ${[1, 2, 3].map(c => {
                            const filterName = `${post}_filter${c}_fields`;
                            const currentFields = filters[`filter${c}`] || [];
                            const otherFields = c === 1 ? 
                                (filters.filter2 || []).concat(filters.filter3 || []) : 
                                (c === 2 ? 
                                    (filters.filter1 || []).concat(filters.filter3 || []) : 
                                    (filters.filter1 || []).concat(filters.filter2 || []));
                            const isAdded = currentFields.length > 0;
                            
                            return `
                                <div class="filter-card bg-white p-4 rounded-lg border border-gray-200 ${isAdded ? '' : 'cacher'} filter${c}">
                                    <h2 class="text-base font-medium text-gray-700 mb-3">Filter ${c}</h2>
                                    <div class="filter-column space-y-2">
                                        ${allFields.filter(field => field !== 'Poste').map(field => {
                                            const checked = currentFields.includes(field);
                                            const disabled = otherFields.includes(field);
                                            return `
                                                <div class="checkbox-container">
                                                    <label>
                                                        <input type="checkbox" name="${filterName}" id="${field}_${idx}_${c}" 
                                                            value="${field}" ${checked ? 'checked' : ''} ${disabled ? 'disabled' : ''}
                                                            onchange="showFieldStats('${post}', '${field}')">
                                                        <span>${field}</span>
                                                    </label>
                                                    <div id="stats_${post}_${field}" class="field-stats hidden text-xs mt-1 pl-6"></div>
                                                </div>
`;
                                        }).join('')}
                                    </div>
                                </div>
                            `;
                        }).join('')}
                    </div>
                </div>
            </div>
        `);
    });
    
    feather.replace();
}

function deselectAllPosts() {
    $('input[name="postes"]:checked').prop('checked', false);
    updateSelectedPosts();
}

function updateSelectedPosts() {
    const selected = $('input[name="postes"]:checked').map(function() {
        return $(this).val();
    }).get();
    
    $('#selectedPostsHidden').val(JSON.stringify(selected));
    // In a real app, you would update the filters section here
}

function deselectAllFilters(postIndex) {
    $(`.filtersContainer[data-index="${postIndex}"] input[type="checkbox"]:checked`).prop('checked', false);
}
function addFilter(postIndex) {
    const emptyFilter = $(`.filtersContainer[data-index="${postIndex}"] .cacher`).first();
    if (emptyFilter.length) {
        emptyFilter.removeClass('cacher');
    }
}
function showFieldStats(post, field) {
    const statsElement = $(`#stats_${post}_${field}`);
    const isChecked = $(`input[id^="${field}_"]:checked`).length > 0;
    
    if (!isChecked) {
        statsElement.addClass('hidden');
        return;
    }
    
    const values = sampleData[post]?.[field] || [];
    const totalCount = values.length;
    
    // Count occurrences of each value
    const valueCounts = {};
    values.forEach(value => {
        valueCounts[value] = (valueCounts[value] || 0) + 1;
    });
    
    // Sort all values by count (descending)
    const sortedValues = Object.entries(valueCounts)
        .sort((a, b) => b[1] - a[1]);
    
    let statsHTML = `
        <div class="text-gray-600">
            <div class="font-semibold mb-1">${field} - ${post}</div>
            <div class="mb-2">Total entries: ${totalCount}</div>
            <div class="max-h-60 overflow-y-auto border rounded p-2 bg-white">
                <table class="w-full text-sm">
                    <thead>
                        <tr class="border-b">
                            <th class="text-left py-1">Value</th>
                            <th class="text-right py-1">Count</th>
                            <th class="text-right py-1">%</th>
                        </tr>
                    </thead>
                    <tbody>
                        ${sortedValues.map(([value, count]) => {
                            const percentage = ((count / totalCount) * 100).toFixed(1);
                            const isDuplicate = count > 1;
                            return `
                                <tr class="${isDuplicate ? 'bg-red-50' : ''}">
                                    <td class="py-1">${value}</td>
                                    <td class="text-right py-1">${count}</td>
                                    <td class="text-right py-1">${percentage}%</td>
                                </tr>
                            `;
                        }).join('')}
                    </tbody>
                </table>
            </div>
            ${sortedValues.some(([_, count]) => count > 1) ? 
                `<div class="mt-2 text-red-600 font-medium">
                    <i data-feather="alert-triangle" class="inline mr-1 w-4 h-4"></i>
                    ${sortedValues.filter(([_, count]) => count > 1).length} duplicate values found
                </div>` : 
                `<div class="mt-2 text-green-600 font-medium">
                    <i data-feather="check-circle" class="inline mr-1 w-4 h-4"></i>
                    No duplicates found
                </div>`
            }
        </div>
    `;
    
    statsElement.html(statsHTML).removeClass('hidden');
    feather.replace();
}