-
-
+
+
+
+
+
+
+
+
+
@@ -436,6 +447,62 @@
}
}
+ // 馃敟 FUNCI脫N DE FILTRADO Y ORDENACI脫N 馃敟
+ function filterServices() {
+ const query = document.getElementById('searchInput').value.toLowerCase().trim();
+ const sortBy = document.getElementById('sortSelect').value;
+
+ // 1. Primero filtramos por texto
+ let filtered = availableServices;
+ if (query !== "") {
+ filtered = availableServices.filter(s => {
+ const raw = s.raw_data || {};
+ const searchString = `
+ ${s.service_ref || ""}
+ ${raw["Nombre Cliente"] || raw["CLIENTE"] || ""}
+ ${raw["Direcci贸n"] || raw["DOMICILIO"] || ""}
+ ${raw["Poblaci贸n"] || raw["POBLACION-PROVINCIA"] || ""}
+ ${raw["C贸digo Postal"] || raw["C.P."] || ""}
+ ${raw["Descripci贸n"] || raw["DESCRIPCION"] || raw["Averia"] || ""}
+ ${raw["Compa帽铆a"] || raw["Procedencia"] || ""}
+ `.toLowerCase();
+ return searchString.includes(query);
+ });
+ }
+
+ // 2. Luego ordenamos el resultado
+ filtered.sort((a, b) => {
+ const rawA = a.raw_data || {};
+ const rawB = b.raw_data || {};
+
+ if (sortBy === 'urgent') {
+ // Urgentes primero (1 vs 0)
+ const urgA = a.is_urgent ? 1 : 0;
+ const urgB = b.is_urgent ? 1 : 0;
+ if (urgA !== urgB) return urgB - urgA;
+ // Si ambos son o no urgentes, desempatamos por fecha
+ return new Date(b.created_at || 0) - new Date(a.created_at || 0);
+ }
+ else if (sortBy === 'cp') {
+ const cpA = (rawA["C贸digo Postal"] || rawA["C.P."] || "99999").toString();
+ const cpB = (rawB["C贸digo Postal"] || rawB["C.P."] || "99999").toString();
+ return cpA.localeCompare(cpB);
+ }
+ else if (sortBy === 'pop') {
+ const popA = (rawA["Poblaci贸n"] || rawA["POBLACION-PROVINCIA"] || "ZZZ").toString();
+ const popB = (rawB["Poblaci贸n"] || rawB["POBLACION-PROVINCIA"] || "ZZZ").toString();
+ return popA.localeCompare(popB);
+ }
+ else {
+ // 'recent' (Por defecto): Los m谩s nuevos arriba
+ return new Date(b.created_at || 0) - new Date(a.created_at || 0);
+ }
+ });
+
+ // 3. Pintamos la pantalla
+ renderServices(filtered);
+ }
+
function showToast(msg) {
const t = document.getElementById('toast');
document.getElementById('toastMsg').innerText = msg;