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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* ========= منطق التحليل والتصدير (متصفح فقط) ========= */
|
| 2 |
+
const EXPORT_COLUMNS = [
|
| 3 |
+
"نوع المشكلة","وقت حدوث المشكلة","اسم صاحب المشكلة",
|
| 4 |
+
"رقم الهوية","رقم الجهاز","رقم الجوال","المسح","المنطقة"
|
| 5 |
+
];
|
| 6 |
+
|
| 7 |
+
const FIELD_ALIASES = {
|
| 8 |
+
"نوع المشكلة": ["نوع المشكله","نوع المشكلة","المشكلة"],
|
| 9 |
+
"وقت حدوث المشكلة": ["وقت حدوث المشكله","وقت حدوث المشكلة","وقت المشكلة","وقت حدوث"],
|
| 10 |
+
"اسم صاحب المشكلة": ["اسم صاحب المشكله","اسم صاحب المشكلة","اسم صاحب البلاغ","الاسم"],
|
| 11 |
+
"رقم الهوية": ["رقم الهويه","رقم الهوية","الهوية"],
|
| 12 |
+
"رقم الجهاز": ["رقم الجهاز","الجهاز"],
|
| 13 |
+
"رقم الجوال": ["رقم الجوال","الجوال","الهاتف"],
|
| 14 |
+
"المسح": ["المسح","اسم المسح"],
|
| 15 |
+
"المنطقة": ["المنطقة","المنطقه","المدينة","المحافظة","منطقة"],
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
const LABEL_SEP = "(?::|:)?\\s*"; // نقطتان اختيارية
|
| 19 |
+
const TICKET_SEP = /\n\s*(?:\n|—+|-{3,}|={3,}|🔴+)+\s*\n/; // فواصل بين التذاكر
|
| 20 |
+
|
| 21 |
+
/* --------- Helpers --------- */
|
| 22 |
+
const arabicDigitsMap = {"٠":"0","١":"1","٢":"2","٣":"3","٤":"4","٥":"5","٦":"6","٧":"7","٨":"8","٩":"9"};
|
| 23 |
+
function normalizeText(s){
|
| 24 |
+
if(typeof s!=="string") return "";
|
| 25 |
+
// أرقام عربية -> إنجليزية + إزالة محارف اتجاه ومسافات خاصة + المدود
|
| 26 |
+
return s.replace(/[\u200f\u200e\u202a-\u202e\u2066-\u2069\u00a0]/g," ")
|
| 27 |
+
.replace(/[٠-٩]/g, d => arabicDigitsMap[d] )
|
| 28 |
+
.replace(/[ــ]+/g,"")
|
| 29 |
+
.trim();
|
| 30 |
+
}
|
| 31 |
+
function normalizeTime(val){
|
| 32 |
+
const m = (val||"").match(/(\d{1,2})[:٫\.\-:](\d{2})\s*(ص|م)?/i);
|
| 33 |
+
if(!m) return (val||"").trim();
|
| 34 |
+
let h = parseInt(m[1],10), mn = m[2], ampm = m[3];
|
| 35 |
+
if(ampm){
|
| 36 |
+
if(/م|pm/i.test(ampm) && h<12) h+=12;
|
| 37 |
+
if(/ص|am/i.test(ampm) && h===12) h=0;
|
| 38 |
+
}
|
| 39 |
+
return `${String(h).padStart(2,"0")}:${mn}`;
|
| 40 |
+
}
|
| 41 |
+
function normalizeDate(v){
|
| 42 |
+
v=(v||"").trim();
|
| 43 |
+
let m=v.match(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2,4})/); // 21/8/2025
|
| 44 |
+
if(m){
|
| 45 |
+
let d=+m[1], mo=+m[2], y=+m[3]; if(y<100) y+=2000;
|
| 46 |
+
return `${y.toString().padStart(4,"0")}-${String(mo).padStart(2,"0")}-${String(d).padStart(2,"0")}`;
|
| 47 |
+
}
|
| 48 |
+
m=v.match(/(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})/); // 2025-08-21
|
| 49 |
+
if(m){
|
| 50 |
+
let y=+m[1], mo=+m[2], d=+m[3];
|
| 51 |
+
return `${y.toString().padStart(4,"0")}-${String(mo).padStart(2,"0")}-${String(d).padStart(2,"0")}`;
|
| 52 |
+
}
|
| 53 |
+
return v;
|
| 54 |
+
}
|
| 55 |
+
function splitTickets(raw){
|
| 56 |
+
raw = normalizeText(raw);
|
| 57 |
+
if(!raw) return [];
|
| 58 |
+
let parts = raw.split(TICKET_SEP);
|
| 59 |
+
if(parts.length===1){ parts = raw.split(/\n\s*\n+/).filter(p=>p.trim()); }
|
| 60 |
+
return parts.map(p=>p.trim()).filter(Boolean);
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
/* compile field regexes (نفس السطر / السطر التالي) */
|
| 64 |
+
function compileFieldPatterns(){
|
| 65 |
+
const pats = {};
|
| 66 |
+
for(const [canonical, labels] of Object.entries(FIELD_ALIASES)){
|
| 67 |
+
const lbls = labels.map(l => l.replace(/[.*+?^${}()|[\]\\]/g,'\\$&')).join("|");
|
| 68 |
+
pats[canonical] = [
|
| 69 |
+
new RegExp(`(?:^|\\n)\\s*(?:${lbls})\\s*${LABEL_SEP}(.+)$`, "mi"),
|
| 70 |
+
new RegExp(`(?:^|\\n)\\s*(?:${lbls})\\s*${LABEL_SEP}\\n\\s*(.+)`, "mi"),
|
| 71 |
+
];
|
| 72 |
+
}
|
| 73 |
+
return pats;
|
| 74 |
+
}
|
| 75 |
+
const FIELD_PATTERNS = compileFieldPatterns();
|
| 76 |
+
|
| 77 |
+
/* استخراج الحقول من تذكرة واحدة */
|
| 78 |
+
function extractFields(ticketText){
|
| 79 |
+
const data = {};
|
| 80 |
+
for(const k of Object.keys(FIELD_ALIASES)) data[k]="";
|
| 81 |
+
const text = normalizeText(ticketText);
|
| 82 |
+
|
| 83 |
+
for(const [fname, patterns] of Object.entries(FIELD_PATTERNS)){
|
| 84 |
+
for(const pat of patterns){
|
| 85 |
+
const m = text.match(pat);
|
| 86 |
+
if(m){
|
| 87 |
+
let val = normalizeText(m[1]);
|
| 88 |
+
if(fname==="وقت حدوث المشكلة") val = normalizeTime(val);
|
| 89 |
+
if(!data[fname]) data[fname]=val;
|
| 90 |
+
break;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
// احتياط للأرقام حتى بدون عناوين
|
| 95 |
+
if(!data["رقم الجهاز"]){
|
| 96 |
+
const m = text.match(/(?:رقم\s*الجهاز|الجهاز)\D*([0-9][0-9\-\s]{2,})/i);
|
| 97 |
+
if(m) data["رقم الجهاز"]=m[1].replace(/\D/g,"").slice(0,20);
|
| 98 |
+
}
|
| 99 |
+
if(!data["رقم الجوال"]){
|
| 100 |
+
const m = text.match(/(05[0-9\-\s]{8,12})/);
|
| 101 |
+
if(m) data["رقم الجوال"]=m[1].replace(/\D/g,"").slice(0,10);
|
| 102 |
+
}
|
| 103 |
+
if(!data["رقم الهوية"]){
|
| 104 |
+
const m = text.match(/(1[0-9\-\s]{9,12})/);
|
| 105 |
+
if(m) data["رقم الهوية"]=m[1].replace(/\D/g,"").slice(0,10);
|
| 106 |
+
}
|
| 107 |
+
if(!data["المسح"]){
|
| 108 |
+
const m = text.match(/(?:اسم\s*المسح|المسح)\s*[::]?\s*(.+)/);
|
| 109 |
+
if(m) data["المسح"]=normalizeText(m[1].split(/\r?\n/)[0]);
|
| 110 |
+
}
|
| 111 |
+
// دمج التاريخ مع الوقت إن وُجد تاريخ
|
| 112 |
+
const dm = text.match(/(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}|\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2})/);
|
| 113 |
+
if(dm){
|
| 114 |
+
const date = normalizeDate(dm[1]);
|
| 115 |
+
const tm = data["وقت حدوث المشكلة"] || "";
|
| 116 |
+
data["وقت حدوث المشكلة"] = `${date} ${tm}`.trim();
|
| 117 |
+
}
|
| 118 |
+
return data;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
/* تحليل مجموعة تذاكر -> صفوف */
|
| 122 |
+
function parseTickets(raw){
|
| 123 |
+
const tickets = splitTickets(raw||"");
|
| 124 |
+
const rows = tickets.map(extractFields);
|
| 125 |
+
return rows;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
/* بناء الجدول وتوسيط المحتوى */
|
| 129 |
+
function buildTable(rows){
|
| 130 |
+
const theadRow = document.getElementById("theadRow");
|
| 131 |
+
const tbody = document.getElementById("tbody");
|
| 132 |
+
// رؤوس الأعمدة
|
| 133 |
+
theadRow.innerHTML = "";
|
| 134 |
+
EXPORT_COLUMNS.forEach(col=>{
|
| 135 |
+
const th=document.createElement("th");
|
| 136 |
+
th.textContent=col;
|
| 137 |
+
theadRow.appendChild(th);
|
| 138 |
+
});
|
| 139 |
+
// جسم الجدول
|
| 140 |
+
tbody.innerHTML = "";
|
| 141 |
+
rows.forEach(r=>{
|
| 142 |
+
const tr=document.createElement("tr");
|
| 143 |
+
EXPORT_COLUMNS.forEach(col=>{
|
| 144 |
+
const td=document.createElement("td");
|
| 145 |
+
td.contentEditable="true";
|
| 146 |
+
td.textContent = r[col]||"";
|
| 147 |
+
tr.appendChild(td);
|
| 148 |
+
});
|
| 149 |
+
tbody.appendChild(tr);
|
| 150 |
+
});
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
/* قراءة الجدول إلى مصفوفة كائنات */
|
| 154 |
+
function readTable(){
|
| 155 |
+
const tbody = document.getElementById("tbody");
|
| 156 |
+
const rows = [];
|
| 157 |
+
[...tbody.querySelectorAll("tr")].forEach(tr=>{
|
| 158 |
+
const obj={};
|
| 159 |
+
[...tr.children].forEach((td,idx)=>{
|
| 160 |
+
obj[EXPORT_COLUMNS[idx]] = td.textContent.trim();
|
| 161 |
+
});
|
| 162 |
+
rows.push(obj);
|
| 163 |
+
});
|
| 164 |
+
return rows;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
/* تصدير إلى Excel (تحميل تلقائي) */
|
| 168 |
+
function exportExcel(){
|
| 169 |
+
const rows = readTable();
|
| 170 |
+
const aoa = [EXPORT_COLUMNS];
|
| 171 |
+
rows.forEach(r=>{
|
| 172 |
+
aoa.push(EXPORT_COLUMNS.map(c=>r[c]||""));
|
| 173 |
+
});
|
| 174 |
+
const ws = XLSX.utils.aoa_to_sheet(aoa);
|
| 175 |
+
// تفعيل اتجاه RTL (مدعوم في بعض العارضات)
|
| 176 |
+
ws["!rtl"] = true;
|
| 177 |
+
const wb = XLSX.utils.book_new();
|
| 178 |
+
XLSX.utils.book_append_sheet(wb, ws, "التذاكر");
|
| 179 |
+
|
| 180 |
+
const now = new Date();
|
| 181 |
+
const ts = now.toISOString().replace(/\D/g,'').slice(0,14); // YYYYMMDDHHMMSS
|
| 182 |
+
const base = (document.getElementById("fname").value || "Ticket").trim() || "Ticket";
|
| 183 |
+
XLSX.writeFile(wb, `${base}_${ts}.xlsx`);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
/* مسح كل شيء */
|
| 187 |
+
function clearAll(){
|
| 188 |
+
document.getElementById("raw").value = "";
|
| 189 |
+
document.getElementById("tbody").innerHTML = "";
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
/* مثال سريع */
|
| 193 |
+
const SAMPLE = `🔴🔴🔴
|
| 194 |
+
نوع المشكلة : لا أقدر أكمل الدخول
|
| 195 |
+
وقت حدوث المشكلة: 21/8/2025 7:00
|
| 196 |
+
اسم صاحب المشكلة : محمد بن علي
|
| 197 |
+
رقم الهوية: 1068891991
|
| 198 |
+
رقم الجهاز: 01438
|
| 199 |
+
رقم الجوال: 0556665323
|
| 200 |
+
اسم المسح: الجبيل
|
| 201 |
+
المنطقة: الشرقية
|
| 202 |
+
|
| 203 |
+
🔴🔴🔴
|
| 204 |
+
نوع المشكلة: تعليق مستمر رغم إعادة التشغيل
|
| 205 |
+
وقت حدوث المشكلة: 20-08-2025 18:10
|
| 206 |
+
اسم صاحب المشكلة: هدى صالح
|
| 207 |
+
رقم الهوية: 1086892231
|
| 208 |
+
رقم الجهاز: 868190043822887
|
| 209 |
+
رقم الجوال: 0552259541
|
| 210 |
+
اسم المسح: الرخص البلدية
|
| 211 |
+
المنطقة: الرياض`;
|
| 212 |
+
|
| 213 |
+
/* ربط الأزرار */
|
| 214 |
+
document.addEventListener("DOMContentLoaded", ()=>{
|
| 215 |
+
const parseBtn = document.getElementById("btn-parse");
|
| 216 |
+
const exportBtn = document.getElementById("btn-export");
|
| 217 |
+
const clearBtn = document.getElementById("btn-clear");
|
| 218 |
+
const sampleBtn = document.getElementById("btn-sample");
|
| 219 |
+
|
| 220 |
+
parseBtn.addEventListener("click", ()=>{
|
| 221 |
+
const raw = document.getElementById("raw").value || SAMPLE;
|
| 222 |
+
const rows = parseTickets(raw);
|
| 223 |
+
buildTable(rows);
|
| 224 |
+
});
|
| 225 |
+
|
| 226 |
+
exportBtn.addEventListener("click", exportExcel);
|
| 227 |
+
clearBtn.addEventListener("click", clearAll);
|
| 228 |
+
sampleBtn.addEventListener("click", ()=>{
|
| 229 |
+
document.getElementById("raw").value = SAMPLE;
|
| 230 |
+
});
|
| 231 |
+
});
|