const FIELD_MAP = { name: "إسم المنشأة", phone: "رقم التواصل", email: "البريد الالكتروني", city: "المدينة الصناعية", commercialRegister: "السجل التجاري من الاطار", activityCode: "كود النشاط", activity: "النشاط" }; const ICONS = { phone: ``, whatsapp: `` }; const queryInput = document.getElementById("query"); const resultsEl = document.getElementById("results"); const clearButton = document.getElementById("clearButton"); const emptyTemplate = document.getElementById("emptyTemplate"); function escapeHtml(value) { return String(value ?? "") .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """) .replaceAll("'", "'"); } function normalizeArabic(value) { return String(value || "") .toLowerCase() .replace(/[إأآا]/g, "ا") .replace(/ى/g, "ي") .replace(/ة/g, "ه") .replace(/ؤ/g, "و") .replace(/ئ/g, "ي") .replace(/[ً-ٰٟ]/g, "") .replace(/\s+/g, " ") .trim(); } function digitsOnly(value) { return String(value || "").replace(/\D/g, ""); } function formatPhoneDisplay(value) { const digits = digitsOnly(value); if (!digits || digits === "0") return "غير متاح"; if (digits.length === 9 && digits.startsWith("5")) return `0${digits}`; return digits; } function hasUsefulValue(value) { const text = String(value ?? "").trim(); if (!text) return false; const normalized = text.replace(/\s+/g, "").replace(/[ـ-]/g, ""); if (!normalized) return false; if (/^0+$/.test(digitsOnly(normalized)) && !/[^\d\s+()-]/.test(normalized)) return false; return !["غيرمتاح", "لايوجد", "لاينطبق", "nan", "null", "undefined"].includes(normalizeArabic(normalized)); } function toSaudiInternational(value) { let digits = digitsOnly(value); if (!digits || digits === "0") return ""; if (digits.startsWith("00")) digits = digits.slice(2); if (digits.startsWith("+")) digits = digits.slice(1); if (digits.startsWith("966")) return digits; if (digits.startsWith("0") && digits.length >= 9) return `966${digits.slice(1)}`; if (digits.startsWith("5") && digits.length === 9) return `966${digits}`; return digits.length >= 7 ? digits : ""; } function highlightName(name, query) { const original = String(name || ""); const q = String(query || "").trim(); if (!original || !q) return escapeHtml(original || "-"); const index = original.toLowerCase().indexOf(q.toLowerCase()); if (index === -1) return escapeHtml(original); const before = escapeHtml(original.slice(0, index)); const match = escapeHtml(original.slice(index, index + q.length)); const after = escapeHtml(original.slice(index + q.length)); return `${before}${match}${after}`; } function field(label, value, options = {}) { if (!hasUsefulValue(value)) return ""; const display = value && String(value).trim() ? value : "-"; const content = options.html || escapeHtml(display); const classes = ["info-field", options.full ? "full" : ""].filter(Boolean).join(" "); const valueClasses = ["value", options.ltr ? "ltr" : ""].filter(Boolean).join(" "); const action = options.action || ""; return `
${escapeHtml(label)}
${content}
${action}
`; } function whatsappAction(phone) { const normalized = toSaudiInternational(phone); if (!normalized) return ""; const disabled = !normalized; const whatsHref = disabled ? "#" : `https://wa.me/${normalized}`; const disabledClass = disabled ? " disabled" : ""; return ` ${ICONS.whatsapp} `; } function renderEmpty() { const node = emptyTemplate.content.cloneNode(true); resultsEl.replaceChildren(node); } function renderResults(items, query) { if (!query.trim()) { renderEmpty(); return; } if (!items.length) { renderEmpty(); return; } resultsEl.innerHTML = items.map((item) => { const name = item[FIELD_MAP.name] || "-"; const phone = item[FIELD_MAP.phone] || ""; const email = item[FIELD_MAP.email] || ""; const city = item[FIELD_MAP.city] || ""; const emailHtml = hasUsefulValue(email) ? `${escapeHtml(email)}` : ""; return `

${highlightName(name, query)}

${hasUsefulValue(city) ? `

${escapeHtml(city)}

` : ""}
${field("رقم التواصل", phone, { html: escapeHtml(formatPhoneDisplay(phone)), ltr: true, action: whatsappAction(phone) })} ${field("البريد الالكتروني", email, { html: emailHtml, ltr: true })} ${field("السجل التجاري", item[FIELD_MAP.commercialRegister], { ltr: true })} ${field("كود النشاط", item[FIELD_MAP.activityCode], { ltr: true })} ${field("المدينة الصناعية", city)} ${field("النشاط", item[FIELD_MAP.activity], { full: true })}
`; }).join(""); } function search() { const query = queryInput.value.trim(); const normalizedQuery = normalizeArabic(query); const matches = DATA.filter((item) => { const name = normalizeArabic(item[FIELD_MAP.name]); return normalizedQuery && name.includes(normalizedQuery); }).sort((a, b) => String(a[FIELD_MAP.name] || "").localeCompare(String(b[FIELD_MAP.name] || ""), "ar")); renderResults(matches, query); } queryInput.addEventListener("input", search); queryInput.addEventListener("keydown", (event) => { if (event.key === "Enter") search(); }); clearButton.addEventListener("click", () => { queryInput.value = ""; queryInput.focus(); renderEmpty(); }); renderEmpty();