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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
وقت حدوث المشكلة: 1446/09/10 هـ
|
| 39 |
اسم صاحب المشكلة: نوف الناصر
|
| 40 |
رقم الهوية: 1234567890
|
|
@@ -42,14 +497,156 @@ const SAMPLE=`نوع المشكلة: لا استطيع اكمال الاستما
|
|
| 42 |
رقم الجوال: 0558174717
|
| 43 |
المسح: الخبر
|
| 44 |
المنطقة: الشرقية`;
|
| 45 |
-
|
| 46 |
-
const
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
init();
|
|
|
|
| 1 |
+
const EXPORT_COLUMNS = [
|
| 2 |
+
"التصنيف","نوع المشكلة","وقت حدوث المشكلة","اسم صاحب المشكلة",
|
| 3 |
+
"رقم الهوية","رقم الجهاز","رقم الجوال","المسح","المنطقة","اسم الدعم الفني","الحالة"
|
| 4 |
+
];
|
| 5 |
+
|
| 6 |
+
const FIELD_ALIASES = {
|
| 7 |
+
"نوع المشكلة": ["نوع المشكله","نوع المشكلة","المشكلة","نوع-المشكلة","نوع المشكلة"],
|
| 8 |
+
"وقت حدوث المشكلة": ["وقت حدوث المشكله","وقت حدوث المشكلة","وقت المشكلة","وقت حدوث","وقت حدوث المشكله:","وقت حدوث المشكله :"],
|
| 9 |
+
"اسم صاحب المشكلة": ["اسم صاحب المشكله","اسم صاحب المشكلة","اسم صاحب البلاغ","الاسم"],
|
| 10 |
+
"رقم الهوية": ["رقم الهويه","رقم الهوية","الهوية","هوية"],
|
| 11 |
+
"رقم الجهاز": ["رقم الجهاز","الجهاز"],
|
| 12 |
+
"رقم الجوال": ["رقم الجوال","الجوال","الهاتف","جوال"],
|
| 13 |
+
"المسح": ["المسح","اسم المسح"],
|
| 14 |
+
"المنطقة": ["المنطقة","المنطقه","اسم المنطقة","المدينة","المحافظة","منطقة"]
|
| 15 |
+
};
|
| 16 |
+
const START_LABELS = Array.from(new Set(Object.values(FIELD_ALIASES).flat()));
|
| 17 |
+
|
| 18 |
+
const CLASS_RULES = {
|
| 19 |
+
"استفسار": ["استفسار","سؤال","استعلام","معلومة","استفسارات"],
|
| 20 |
+
"إضافة أجهزة": ["اضافة جهاز","إضافة أجهزة","اضافة اجهزة","تركيب جهاز","جهاز جديد","تسجيل جهاز","ربط جهاز","اضافة ماسح","إضافة ماسح"],
|
| 21 |
+
"الاستمارة": ["الاستمارة","استمارة","النموذج","نموذج","الفورم","تعليق الاستمارة","لا استطيع اكمال الاستمارة","التعبئة"],
|
| 22 |
+
"التقييم": ["التقييم","تقييم","feedback","survey","رضا","نجوم"],
|
| 23 |
+
"الخرائط": ["الخرائط","خرائط","map","gps","تحديد الموقع","احداثيات","إحداثيات","الموقع الجغرافي"],
|
| 24 |
+
"السوتي": ["السوتي","سوتي","soti","soti assist","mobicontrol","soti mobicontrol"],
|
| 25 |
+
"الشبكة": ["الشبكة","شبكة","نت","انترنت","إنترنت","wifi","واي فاي","4g","5g","ضعف الشبكة","stc","mobily","زين","weak signal","no signal"],
|
| 26 |
+
"النسخة": ["النسخة","نسخة","الإصدار","اصدار","version","build","release","تحديث نسخة","ترقية النسخة"],
|
| 27 |
+
"النظام المكتبي": ["النظام المكتبي","نسخة ويندوز","ويندوز","windows","pc app","برنامج المكتب","التطبيق على الكمبيوتر","الديسكتوب"],
|
| 28 |
+
"تسجيل دخول": ["تسجيل دخول","تسجيل الدخول","login","signin","رفض تسجيل الدخول","لا يقبل الدخول","اسم المستخدم","كلمة المرور","نسيت كلمة السر","إعادة تعيين"],
|
| 29 |
+
"تفعيل حساب": ["تفعيل حساب","تفعيل","activation","activate","رمز التفعيل","كود التفعيل"],
|
| 30 |
+
"تناقل البيانات": ["تناقل البيانات","ترحيل البيانات","مزامنة","sync","مزامنه","نقل البيانات","رفع البيانات","sync failed","المزامنة"],
|
| 31 |
+
"صيانة وتحديث الأجهزة": ["صيانة","تحديث الأجهزة","تحديث جهاز","ترقية الجهاز","اعطال الجهاز","تصليح","صيانة وتحديث الأجهزة","صيانة الجهاز"]
|
| 32 |
+
};
|
| 33 |
+
const CLASS_PRIORITY = [
|
| 34 |
+
"صيانة وتحديث الأجهزة","إضافة أجهزة","تسجيل دخول","تفعيل حساب",
|
| 35 |
+
"الاستمارة","التقييم","الخرائط","السوتي","الشبكة","النسخة",
|
| 36 |
+
"النظام المكتبي","تناقل البيانات","استفسار"
|
| 37 |
+
];
|
| 38 |
+
|
| 39 |
+
const TICKET_SEP = /\n\s*(?:\n{2,}|—+|-{3,}|={3,}|🔴+)\s*\n/;
|
| 40 |
+
const MIN_SPLIT_SPAN = 40;
|
| 41 |
+
const arabicDigitsMap = {"٠":"0","١":"1","٢":"2","٣":"3","٤":"4","٥":"5","٦":"6","٧":"7","٨":"8","٩":"9"};
|
| 42 |
+
|
| 43 |
+
function normalizeText(s){
|
| 44 |
+
if(typeof s!=="string") return "";
|
| 45 |
+
return s
|
| 46 |
+
.replace(/\r\n/g,"\n")
|
| 47 |
+
.replace(/[\u200f\u200e\u202a-\u202e\u2066-\u2069\u00a0]/g," ")
|
| 48 |
+
.replace(/[٠-٩]/g, d => arabicDigitsMap[d] )
|
| 49 |
+
.replace(/[ــ]+/g,"")
|
| 50 |
+
.replace(/[ \t]+\n/g,"\n")
|
| 51 |
+
.replace(/\n{3,}/g,"\n\n")
|
| 52 |
+
.trim();
|
| 53 |
+
}
|
| 54 |
+
function lettersOnly(ar){ return (ar||"").replace(/[^A-Za-z\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\s]/g,"").replace(/\s{2,}/g," ").trim(); }
|
| 55 |
+
function alnumAr(s){ return (s||"").replace(/[^0-9A-Za-z\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\s\-\._/]/g,"").replace(/\s{2,}/g," ").trim(); }
|
| 56 |
+
function digitsOnly(s){ return (s||"").replace(/\D+/g,""); }
|
| 57 |
+
|
| 58 |
+
const LABEL_FIXES = [
|
| 59 |
+
[/(^|\n)\s*نوع\s*المشكله/gi, "$1نوع المشكلة"],
|
| 60 |
+
[/(^|\n)\s*وقت\s*حدوث\s*المشكله/gi, "$1وقت حدوث المشكلة"],
|
| 61 |
+
[/(^|\n)\s*اسم\s*صاحب\s*المشكله/gi, "$1اسم صاحب المشكلة"],
|
| 62 |
+
[/(^|\n)\s*رقم\s*الهويه/gi, "$1رقم الهوية"],
|
| 63 |
+
[/(^|\n)\s*المنطقه/gi, "$1المنطقة"],
|
| 64 |
+
[/(^|\n)\s*اسم\s*المنطقة/gi, "$1المنطقة"],
|
| 65 |
+
[/(^|\n)\s*اسم\s*المسح/gi, "$1المسح"],
|
| 66 |
+
[/(^|\n)\s*الهاتف/gi, "$1رقم الجوال"],
|
| 67 |
+
[/(^|\n)\s*جوال/gi, "$1رقم الجوال"]
|
| 68 |
+
];
|
| 69 |
+
function fixLabels(s){
|
| 70 |
+
let t = s;
|
| 71 |
+
LABEL_FIXES.forEach(([re, rep])=> t = t.replace(re, rep));
|
| 72 |
+
return t;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
const H_MONTHS = ["محرم","صفر","ربيع الأول","ربيع الاول","ربيع الآخر","ربيع الاخر","جمادى الأولى","جمادى الاولى","جمادى الآخرة","جمادى الاخرة","رجب","شعبان","رمضان","شوال","ذو القعدة","ذو القعده","ذو الحجة","��و الحجه"];
|
| 76 |
+
function monthIndexHijri(name){
|
| 77 |
+
const i = H_MONTHS.findIndex(m => new RegExp("^"+m+"$", "i").test(name.trim()));
|
| 78 |
+
if(i<0) return -1;
|
| 79 |
+
const map = {0:1,1:2,2:3,3:3,4:4,5:4,6:5,7:5,8:6,9:6,10:7,11:8,12:9,13:10,14:11,15:11,16:12,17:12};
|
| 80 |
+
return map[i] || -1;
|
| 81 |
+
}
|
| 82 |
+
function hijriToGregorian(hy, hm, hd){
|
| 83 |
+
const jd = Math.floor((11*hy+3)/30) + 354*hy + 30*hm - Math.floor((hm-1)/2) + hd + 1948440 - 385;
|
| 84 |
+
let l = jd + 68569;
|
| 85 |
+
let n = Math.floor(4*l/146097); l = l - Math.floor((146097*n + 3)/4);
|
| 86 |
+
let i = Math.floor(4000*(l+1)/1461001); l = l - Math.floor(1461*i/4) + 31;
|
| 87 |
+
let j = Math.floor(80*l/2447); const d = l - Math.floor(2447*j/80);
|
| 88 |
+
l = Math.floor(j/11); const m = j + 2 - 12*l; const y = 100*(n-49) + i + l;
|
| 89 |
+
return [y,m,d];
|
| 90 |
+
}
|
| 91 |
+
function detectHijriDate(str){
|
| 92 |
+
const t = normalizeText(str);
|
| 93 |
+
let m = t.match(/(\d{1,2})\s+([^\s]+)\s+(\d{3,4})\s*(هـ|ه|هجري)?/i);
|
| 94 |
+
if(m){
|
| 95 |
+
const d = +m[1];
|
| 96 |
+
const hm = monthIndexHijri(m[2]);
|
| 97 |
+
const y = +m[3];
|
| 98 |
+
if(hm>=1 && hm<=12) return {hy:y, hm:hm, hd:d};
|
| 99 |
+
}
|
| 100 |
+
m = t.match(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{3,4})\s*(هـ|ه|هجري)/i);
|
| 101 |
+
if(m) return {hy:+m[3], hm:+m[2], hd:+m[1]};
|
| 102 |
+
return null;
|
| 103 |
+
}
|
| 104 |
+
function normalizeDateOnly(raw){
|
| 105 |
+
const t = normalizeText(raw);
|
| 106 |
+
const hj = detectHijriDate(t);
|
| 107 |
+
const hasGreg1 = /(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2,4})/.test(t);
|
| 108 |
+
const hasGreg2 = /(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})/.test(t);
|
| 109 |
+
const timeOnly1 = /(^|\s)\d{1,2}\s*(?:[:٫\.:\-]\d{2})\s*(?:ص|صباح(?:اً|ا)?|am|م|مساء|pm)?($|\s)/i.test(t);
|
| 110 |
+
const timeOnly2 = /(^|\s)\d{1,2}\s*(?:ص|صباح(?:اً|ا)?|am|م|مساء|pm)($|\s)/i.test(t);
|
| 111 |
+
if((timeOnly1||timeOnly2) && !hasGreg1 && !hasGreg2 && !hj) return "";
|
| 112 |
+
if(hj){
|
| 113 |
+
const [gy,gm,gd] = hijriToGregorian(hj.hy, hj.hm, hj.hd);
|
| 114 |
+
return `${gy.toString().padStart(4,"0")}-${String(gm).padStart(2,"0")}-${String(gd).toString().padStart(2,"0")}`;
|
| 115 |
+
}
|
| 116 |
+
let m = t.match(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2,4})/);
|
| 117 |
+
if(m){
|
| 118 |
+
let d=+m[1], mo=+m[2], y=+m[3];
|
| 119 |
+
if(y<100) y+=2000;
|
| 120 |
+
return `${y.toString().padStart(4,"0")}-${String(mo).padStart(2,"0")}-${String(d).toString().padStart(2,"0")}`;
|
| 121 |
+
}
|
| 122 |
+
m = t.match(/(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})/);
|
| 123 |
+
if(m){
|
| 124 |
+
let y=+m[1], mo=+m[2], d=+m[3];
|
| 125 |
+
return `${y.toString().padStart(4,"0")}-${String(mo).padStart(2,"0")}-${String(d).toString().padStart(2,"0")}`;
|
| 126 |
+
}
|
| 127 |
+
return t;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
function findStartsByLabels(text, labels){
|
| 131 |
+
const lblRe = labels.map(l=>l.replace(/[.*+?^${}()|[\]\\]/g,'\\$&')).join("|");
|
| 132 |
+
const re = new RegExp(`(^|\\n)\\s*(?:[-–—\\*•]+|\\d+[\\)\\.]\\s*)?\\s*(?:${lblRe})(?:\\s*[::]|\\s+)`,"gi");
|
| 133 |
+
const idxs=[]; let m;
|
| 134 |
+
while((m = re.exec(text))){ idxs.push(m.index + (m[1]?m[1].length:0)); }
|
| 135 |
+
return idxs;
|
| 136 |
+
}
|
| 137 |
+
function findAfterLabel(text, labels){
|
| 138 |
+
const hay = "\n" + normalizeText(text) + "\n";
|
| 139 |
+
for(const rawLbl of labels){
|
| 140 |
+
const lbl = rawLbl.replace(/[.*+?^${}()|[\]\\]/g,'\\$&');
|
| 141 |
+
let m = hay.match(new RegExp(`(?:^|\\n)\\s*${lbl}\\s*[::]\\s*([^\\n]+)`, "i"));
|
| 142 |
+
if(m) return m[1].trim();
|
| 143 |
+
m = hay.match(new RegExp(`(?:^|\\n)\\s*${lbl}\\s+([^\\n]+)`, "i"));
|
| 144 |
+
if(m) return m[1].trim();
|
| 145 |
+
}
|
| 146 |
+
return "";
|
| 147 |
+
}
|
| 148 |
+
function findBlockAfterLabel(text, labels, allLabels = START_LABELS){
|
| 149 |
+
const hay = "\n" + normalizeText(text) + "\n";
|
| 150 |
+
const esc = s => s.replace(/[.*+?^${}()|[\]\\]/g,'\\$&');
|
| 151 |
+
const lblAlt = labels.map(esc).join("|");
|
| 152 |
+
const allAlt = allLabels.map(esc).join("|");
|
| 153 |
+
const re = new RegExp(
|
| 154 |
+
`(?:^|\\n)\\s*(?:${lblAlt})\\s*(?::|:|\\s)\\s*([\\s\\S]*?)(?=\\n\\s*(?:${allAlt})\\s*(?::|:|\\s)|$)`,
|
| 155 |
+
"i"
|
| 156 |
+
);
|
| 157 |
+
const m = hay.match(re);
|
| 158 |
+
return m ? m[1].trim() : "";
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
function splitTickets(raw){
|
| 162 |
+
const text = normalizeText(fixLabels(raw));
|
| 163 |
+
if(!text) return [];
|
| 164 |
+
|
| 165 |
+
if(TICKET_SEP.test(text)){
|
| 166 |
+
return text.split(TICKET_SEP).map(p=>p.trim()).filter(Boolean);
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
const niu = findStartsByLabels(text, ["نوع المشكلة","نوع المشكله"]).sort((a,b)=>a-b);
|
| 170 |
+
if(niu.length >= 2){
|
| 171 |
+
const parts=[];
|
| 172 |
+
for(let i=0;i<niu.length;i++){
|
| 173 |
+
const s = niu[i];
|
| 174 |
+
const e = i+1<niu.length ? niu[i+1] : text.length;
|
| 175 |
+
const slice = text.slice(s,e).trim();
|
| 176 |
+
if(slice) parts.push(slice);
|
| 177 |
+
}
|
| 178 |
+
if(parts.length) return parts;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
return [text];
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
function extractFields(ticketText){
|
| 185 |
+
const text = normalizeText(fixLabels(ticketText));
|
| 186 |
+
const out = {
|
| 187 |
+
"نوع المشكلة":"", "وقت حدوث المشكلة":"", "اسم صاحب المشكلة":"",
|
| 188 |
+
"رقم الهوية":"", "رقم الجهاز":"", "رقم الجوال":"", "المسح":"", "المنطقة":""
|
| 189 |
+
};
|
| 190 |
+
|
| 191 |
+
let v = findBlockAfterLabel(text, FIELD_ALIASES["نوع المشكلة"], START_LABELS);
|
| 192 |
+
if(v) out["نوع المشكلة"] = normalizeText(v);
|
| 193 |
+
|
| 194 |
+
v = findAfterLabel(text, FIELD_ALIASES["وقت حدوث المشكلة"]);
|
| 195 |
+
if(v) out["وقت حدوث المشكلة"] = normalizeDateOnly(v);
|
| 196 |
+
|
| 197 |
+
v = findAfterLabel(text, FIELD_ALIASES["اسم صاحب المشكلة"]);
|
| 198 |
+
if(v) out["اس�� صاحب المشكلة"] = v;
|
| 199 |
+
|
| 200 |
+
v = findAfterLabel(text, FIELD_ALIASES["رقم الهوية"]);
|
| 201 |
+
if(v) out["رقم الهوية"] = digitsOnly(v);
|
| 202 |
+
if(!out["رقم الهوية"]){
|
| 203 |
+
const m = text.match(/(?:^|\D)((?:1|2)\d{9})(?:\D|$)/);
|
| 204 |
+
if(m) out["رقم الهوية"] = m[1];
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
v = findAfterLabel(text, FIELD_ALIASES["رقم الجهاز"]);
|
| 208 |
+
if(v) out["رقم الجهاز"] = digitsOnly(v);
|
| 209 |
+
if(!out["رقم الجهاز"]){
|
| 210 |
+
const m = text.match(/(?:^|\D)(\d{5,20})(?:\D|$)/);
|
| 211 |
+
if(m) out["رقم الجهاز"] = m[1];
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
v = findAfterLabel(text, FIELD_ALIASES["رقم الجوال"]);
|
| 215 |
+
if(v) out["رقم الجوال"] = digitsOnly(v);
|
| 216 |
+
if(!out["رقم الجوال"]){
|
| 217 |
+
const m = text.match(/(?:^|\D)(05\d{7,})(?:\D|$)/);
|
| 218 |
+
if(m) out["رقم الجوال"] = m[1];
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
v = findAfterLabel(text, FIELD_ALIASES["المسح"]);
|
| 222 |
+
if(v) out["المسح"] = alnumAr(v);
|
| 223 |
+
|
| 224 |
+
v = findAfterLabel(text, FIELD_ALIASES["المنطقة"]);
|
| 225 |
+
if(v) out["المنطقة"] = lettersOnly(v);
|
| 226 |
+
|
| 227 |
+
return out;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
function classifyTicket(text, fields){
|
| 231 |
+
const hay = normalizeText(`${text}\n${fields?.["نوع المشكلة"]||""}`).toLowerCase();
|
| 232 |
+
for(const label of CLASS_PRIORITY){
|
| 233 |
+
const kws = CLASS_RULES[label] || [];
|
| 234 |
+
for(const kw of kws){
|
| 235 |
+
const needle = normalizeText(kw).toLowerCase();
|
| 236 |
+
if(needle && hay.includes(needle)) return label;
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
return "استفسار";
|
| 240 |
+
}
|
| 241 |
+
function catClass(label){
|
| 242 |
+
if(/تسجيل دخول/.test(label)) return "login";
|
| 243 |
+
if(/الشبكة/.test(label)) return "network";
|
| 244 |
+
if(/الاستمارة/.test(label)) return "form";
|
| 245 |
+
if(/النسخة|تحديث/.test(label)) return "update";
|
| 246 |
+
if(/أجهزة/.test(label)) return "device";
|
| 247 |
+
return "default";
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
function parseTicketsWithExtras(raw, agentName, defaultRegion){
|
| 251 |
+
const regionChosen = (defaultRegion || "").toString();
|
| 252 |
+
return splitTickets(raw||"").map(t => {
|
| 253 |
+
const f = extractFields(t);
|
| 254 |
+
const cls = classifyTicket(t, f);
|
| 255 |
+
const region = regionChosen ? regionChosen : (f["المنطقة"] || "");
|
| 256 |
+
return {
|
| 257 |
+
"التصنيف": cls,
|
| 258 |
+
"نوع المشكلة": f["نوع المشكلة"] || "",
|
| 259 |
+
"وقت حدوث المشكلة": f["وقت حدوث المشكلة"] || "",
|
| 260 |
+
"اسم صاحب المشكلة": f["اسم صاحب المشكلة"] || "",
|
| 261 |
+
"رقم الهوية": f["رقم الهوية"] || "",
|
| 262 |
+
"رقم الجهاز": f["رقم الجهاز"] || "",
|
| 263 |
+
"رقم الجوال": f["رقم الجوال"] || "",
|
| 264 |
+
"المسح": f["المسح"] || "",
|
| 265 |
+
"المنطقة": region,
|
| 266 |
+
"اسم الدعم الفني": agentName || "",
|
| 267 |
+
"الحالة": "تم الحل"
|
| 268 |
+
};
|
| 269 |
+
});
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
function buildTable(rows){
|
| 273 |
+
const theadRow = document.getElementById("theadRow");
|
| 274 |
+
const tbody = document.getElementById("tbody");
|
| 275 |
+
|
| 276 |
+
theadRow.innerHTML = "";
|
| 277 |
+
EXPORT_COLUMNS.forEach(col=>{
|
| 278 |
+
const th=document.createElement("th");
|
| 279 |
+
th.textContent=col;
|
| 280 |
+
theadRow.appendChild(th);
|
| 281 |
+
});
|
| 282 |
+
|
| 283 |
+
tbody.innerHTML = "";
|
| 284 |
+
rows.forEach(r=>{
|
| 285 |
+
const tr=document.createElement("tr");
|
| 286 |
+
EXPORT_COLUMNS.forEach(col=>{
|
| 287 |
+
const td=document.createElement("td");
|
| 288 |
+
if(col==="التصنيف"){
|
| 289 |
+
const span=document.createElement("span");
|
| 290 |
+
const type = catClass(r[col]||"");
|
| 291 |
+
span.className = `cat ${type}`;
|
| 292 |
+
span.textContent = r[col] || "";
|
| 293 |
+
td.appendChild(span);
|
| 294 |
+
}else{
|
| 295 |
+
td.contentEditable="true";
|
| 296 |
+
td.textContent = r[col]||"";
|
| 297 |
+
}
|
| 298 |
+
tr.appendChild(td);
|
| 299 |
+
});
|
| 300 |
+
tbody.appendChild(tr);
|
| 301 |
+
});
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
function readTable(){
|
| 305 |
+
const tbody = document.getElementById("tbody");
|
| 306 |
+
const rows = [];
|
| 307 |
+
[...tbody.querySelectorAll("tr")].forEach(tr=>{
|
| 308 |
+
const obj={};
|
| 309 |
+
[...tr.children].forEach((td,idx)=>{
|
| 310 |
+
const col = EXPORT_COLUMNS[idx];
|
| 311 |
+
obj[col] = td.textContent.trim();
|
| 312 |
+
});
|
| 313 |
+
rows.push(obj);
|
| 314 |
+
});
|
| 315 |
+
return rows;
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
function updateBadge(n){
|
| 319 |
+
const b = document.getElementById("countBadge");
|
| 320 |
+
b.textContent = n;
|
| 321 |
+
b.hidden = (n===0);
|
| 322 |
+
}
|
| 323 |
+
function setButtonsEnabled(hasRows){
|
| 324 |
+
document.getElementById("btn-export").disabled = !hasRows;
|
| 325 |
+
document.getElementById("btn-copy").disabled = !hasRows;
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
function validateCells(){
|
| 329 |
+
const tbody=document.getElementById("tbody");
|
| 330 |
+
const idxPhone = EXPORT_COLUMNS.indexOf("رقم الجوال");
|
| 331 |
+
const idxID = EXPORT_COLUMNS.indexOf("رقم الهوية");
|
| 332 |
+
const required = new Set(["نوع المشكلة","اسم صاحب المشكلة","رقم الهوية","رقم الجهاز","رقم الجوال","المسح","المنطقة","وقت حدوث المشكلة"]);
|
| 333 |
+
|
| 334 |
+
let missing=0;
|
| 335 |
+
[...tbody.rows].forEach(tr=>{
|
| 336 |
+
[...tr.children].forEach((td,idx)=>{
|
| 337 |
+
const col = EXPORT_COLUMNS[idx];
|
| 338 |
+
const val = (td.textContent||"").trim();
|
| 339 |
+
let invalid=false;
|
| 340 |
+
|
| 341 |
+
if(required.has(col) && !val){
|
| 342 |
+
invalid=true; missing++;
|
| 343 |
+
}
|
| 344 |
+
if(col==="رقم الهوية"){
|
| 345 |
+
const digits = val.replace(/\D/g,"");
|
| 346 |
+
if(val && digits.length!==10) invalid=true;
|
| 347 |
+
}
|
| 348 |
+
if(col==="رقم الجوال"){
|
| 349 |
+
const digits = val.replace(/\D/g,"");
|
| 350 |
+
if(val && digits.length<9) invalid=true;
|
| 351 |
+
}
|
| 352 |
+
td.classList.toggle("invalid", invalid);
|
| 353 |
+
});
|
| 354 |
+
});
|
| 355 |
+
|
| 356 |
+
const warn=document.getElementById("warn");
|
| 357 |
+
if(missing>0){
|
| 358 |
+
warn.hidden=false;
|
| 359 |
+
warn.textContent=`هناك ${missing} حقول مطلوبة فارغة أو غير صحيحة. يرجى إكمالها.`;
|
| 360 |
+
}else{
|
| 361 |
+
warn.hidden=true;
|
| 362 |
+
warn.textContent="";
|
| 363 |
+
}
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
document.addEventListener("input",(e)=>{
|
| 367 |
+
if(e.target && e.target.closest && e.target.closest("#tbody")){
|
| 368 |
+
validateCells();
|
| 369 |
+
saveState();
|
| 370 |
+
}
|
| 371 |
+
});
|
| 372 |
+
|
| 373 |
+
function toast(msg){
|
| 374 |
+
const t = document.getElementById("toast");
|
| 375 |
+
t.textContent = msg;
|
| 376 |
+
t.hidden = false;
|
| 377 |
+
t.classList.remove("show"); void t.offsetWidth; t.classList.add("show");
|
| 378 |
+
setTimeout(()=>{ t.hidden = true; }, 2000);
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
async function exportExcel(){
|
| 382 |
+
const rows = readTable();
|
| 383 |
+
if(!rows.length){ toast("لا يوجد بيانات لتصديرها."); return; }
|
| 384 |
+
|
| 385 |
+
const TEMPLATE_HEADERS = [
|
| 386 |
+
"التصنيف","نوع المشكلة","وصف المشكلة","المنطقة","اسم المسح","اسم المشغل",
|
| 387 |
+
"رقم الجوال","رقم الهوية ID","رقم الجهاز","تاريخ اليوم بالميلادي","الحالة","اسم الدعم الفني"
|
| 388 |
+
];
|
| 389 |
+
const mapRow = (r)=>{
|
| 390 |
+
const today = new Date();
|
| 391 |
+
const yyyy=today.getFullYear(), mm=String(today.getMonth()+1).padStart(2,"0"), dd=String(today.getDate()).padStart(2,"0");
|
| 392 |
+
const todayStr = `${yyyy}-${mm}-${dd}`;
|
| 393 |
+
return {
|
| 394 |
+
"التصنيف": r["التصنيف"]||"",
|
| 395 |
+
"نوع المشكلة": r["نوع المشكلة"]||"",
|
| 396 |
+
"وصف المشكلة": r["التصنيف"] || r["نوع المشكلة"] || "",
|
| 397 |
+
"المنطقة": r["المنطقة"]||"",
|
| 398 |
+
"اسم المسح": r["المسح"]||"",
|
| 399 |
+
"اسم المشغل": r["اسم صاحب المشكلة"]||"",
|
| 400 |
+
"رقم الجوال": (r["رقم الجوال"]||"").toString(),
|
| 401 |
+
"رقم الهوية ID":(r["رقم الهوية"]||"").toString(),
|
| 402 |
+
"رقم الجهاز": (r["رقم الجهاز"]||"").toString(),
|
| 403 |
+
"تاريخ اليوم بالميلادي": todayStr,
|
| 404 |
+
"الحالة": r["الحالة"]||"تم الحل",
|
| 405 |
+
"اسم الدعم الفني": r["اسم الدعم الفني"]||""
|
| 406 |
+
};
|
| 407 |
+
};
|
| 408 |
+
|
| 409 |
+
const wb = new ExcelJS.Workbook();
|
| 410 |
+
const ws = wb.addWorksheet("التذاكر", { views: [{ rightToLeft: true }] });
|
| 411 |
+
|
| 412 |
+
const colWidths = [16,18,24,16,18,20,18,18,18,22,14,18];
|
| 413 |
+
TEMPLATE_HEADERS.forEach((h,i)=> ws.getColumn(i+1).width = colWidths[i]||18);
|
| 414 |
+
|
| 415 |
+
ws.addRow(TEMPLATE_HEADERS);
|
| 416 |
+
const headerRow = ws.getRow(1);
|
| 417 |
+
headerRow.height = 24;
|
| 418 |
+
headerRow.eachCell((cell)=>{
|
| 419 |
+
cell.font = { bold:true, color:{argb:"FFFFFFFF"} };
|
| 420 |
+
cell.alignment = { horizontal:"center", vertical:"middle" };
|
| 421 |
+
cell.fill = { type:"pattern", pattern:"solid", fgColor:{argb:"FF4137A8"} };
|
| 422 |
+
cell.border = {
|
| 423 |
+
top:{style:"thin",color:{argb:"FFCDD2E1"}},
|
| 424 |
+
bottom:{style:"thin",color:{argb:"FFCDD2E1"}},
|
| 425 |
+
left:{style:"thin",color:{argb:"FFE5E7EB"}},
|
| 426 |
+
right:{style:"thin",color:{argb:"FFE5E7EB"}}
|
| 427 |
+
};
|
| 428 |
+
});
|
| 429 |
+
|
| 430 |
+
const toTextCols = new Set(["رقم الجوال","رقم الهوية ID","رقم الجهاز"]);
|
| 431 |
+
const rawRows = readTable();
|
| 432 |
+
rawRows.forEach((r,idx)=>{
|
| 433 |
+
const m = mapRow(r);
|
| 434 |
+
const vals = TEMPLATE_HEADERS.map(h => (m[h] ?? ""));
|
| 435 |
+
const row = ws.addRow(vals);
|
| 436 |
+
row.alignment = { horizontal:"center", vertical:"middle" };
|
| 437 |
+
const even = (idx % 2) === 1;
|
| 438 |
+
row.eachCell((cell, colNumber)=>{
|
| 439 |
+
cell.border = {
|
| 440 |
+
top:{style:"thin",color:{argb:"FFE5E7EB"}},
|
| 441 |
+
bottom:{style:"thin",color:{argb:"FFE5E7EB"}},
|
| 442 |
+
left:{style:"thin",color:{argb:"FFE5E7EB"}},
|
| 443 |
+
right:{style:"thin",color:{argb:"FFE5E7EB"}}
|
| 444 |
+
};
|
| 445 |
+
if(even) cell.fill = { type:"pattern", pattern:"solid", fgColor:{argb:"FFF5F8FF"} };
|
| 446 |
+
const header = TEMPLATE_HEADERS[colNumber-1];
|
| 447 |
+
if(toTextCols.has(header)) cell.value = String(cell.value ?? "");
|
| 448 |
+
});
|
| 449 |
+
});
|
| 450 |
+
|
| 451 |
+
const ts = new Date().toISOString().replace(/\D/g,"").slice(0,14);
|
| 452 |
+
const filename = `Ticket_${ts}.xlsx`;
|
| 453 |
+
|
| 454 |
+
const buffer = await wb.xlsx.writeBuffer();
|
| 455 |
+
const blob = new Blob([buffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
|
| 456 |
+
const file = new File([blob], filename, { type: blob.type });
|
| 457 |
+
|
| 458 |
+
if (navigator.canShare && navigator.canShare({ files: [file] })) {
|
| 459 |
+
try { await navigator.share({ files: [file], title: "ملف التذاكر" }); toast("تمت المشاركة/الحفظ."); return; }
|
| 460 |
+
catch(e){}
|
| 461 |
+
}
|
| 462 |
+
const url = URL.createObjectURL(blob);
|
| 463 |
+
const a = document.createElement("a"); a.href = url; a.download = filename;
|
| 464 |
+
document.body.appendChild(a); a.click(); a.remove();
|
| 465 |
+
setTimeout(()=>URL.revokeObjectURL(url), 1000);
|
| 466 |
+
toast("تم تنزيل الملف بتنسيق القالب.");
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
async function copyToClipboardTSV(){
|
| 470 |
+
const rows = readTable();
|
| 471 |
+
if(!rows.length){ toast("لا يوجد بيانات لنسخها."); return; }
|
| 472 |
+
const textCols = new Set(["رقم الهوية","رقم الجهاز","رقم الجوال"]);
|
| 473 |
+
const header = EXPORT_COLUMNS.join("\t");
|
| 474 |
+
const body = rows.map(r =>
|
| 475 |
+
EXPORT_COLUMNS.map(c => {
|
| 476 |
+
let v = (r[c] ?? "").toString().replace(/\t/g," ");
|
| 477 |
+
if(textCols.has(c) && v && /^[0-9]+$/.test(v)) v = "'" + v;
|
| 478 |
+
return v;
|
| 479 |
+
}).join("\t")
|
| 480 |
+
).join("\r\n");
|
| 481 |
+
const tsv = "\uFEFF" + header + "\r\n" + body;
|
| 482 |
+
|
| 483 |
+
try{ await navigator.clipboard.writeText(tsv); toast("تم النسخ — الصق/ي مباشرة في Excel."); }
|
| 484 |
+
catch(e){
|
| 485 |
+
const ta = document.createElement("textarea");
|
| 486 |
+
ta.value = tsv; document.body.appendChild(ta);
|
| 487 |
+
ta.select(); document.execCommand("copy"); document.body.removeChild(ta);
|
| 488 |
+
toast("تم النسخ — الصق/ي مباشرة في Excel.");
|
| 489 |
+
}
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
const SAMPLE = `نوع المشكلة: لا استطيع اكمال الاستمارة بسبب تعليق عند الحفظ
|
| 493 |
وقت حدوث المشكلة: 1446/09/10 هـ
|
| 494 |
اسم صاحب المشكلة: نوف الناصر
|
| 495 |
رقم الهوية: 1234567890
|
|
|
|
| 497 |
رقم الجوال: 0558174717
|
| 498 |
المسح: الخبر
|
| 499 |
المنطقة: الشرقية`;
|
| 500 |
+
|
| 501 |
+
const STATE_KEY = "ticketParserState_v10_10";
|
| 502 |
+
const ALL_STATE_KEYS = [
|
| 503 |
+
"ticketParserState_v8","ticketParserState_v9","ticketParserState_v10",
|
| 504 |
+
"ticketParserState_v10_1","ticketParserState_v10_2","ticketParserState_v10_3",
|
| 505 |
+
"ticketParserState_v10_5","ticketParserState_v10_6","ticketParserState_v10_7",
|
| 506 |
+
"ticketParserState_v10_8","ticketParserState_v10_9","ticketParserState_v10_10"
|
| 507 |
+
];
|
| 508 |
+
|
| 509 |
+
function ensureColumns(rows, agentName, defaultRegion){
|
| 510 |
+
if(!Array.isArray(rows)) return rows||[];
|
| 511 |
+
return rows.map(r=>{
|
| 512 |
+
const out = {...r};
|
| 513 |
+
if(!("التصنيف" in out) || !out["التصنيف"]){
|
| 514 |
+
const fakeText = Object.values(out).join("\n");
|
| 515 |
+
out["التصنيف"] = classifyTicket(fakeText, out);
|
| 516 |
+
}
|
| 517 |
+
if(!("اسم الدعم الفني" in out)) out["اسم الدعم الفني"] = agentName || out["اسم الدعم الفني"] || "";
|
| 518 |
+
if(!("الحالة" in out) || !out["الحالة"]) out["الحالة"] = "تم الحل";
|
| 519 |
+
if(defaultRegion) out["المنطقة"] = defaultRegion;
|
| 520 |
+
return out;
|
| 521 |
+
});
|
| 522 |
+
}
|
| 523 |
+
|
| 524 |
+
function saveState(){
|
| 525 |
+
try{
|
| 526 |
+
const raw = document.getElementById("raw")?.value || "";
|
| 527 |
+
const agent = document.getElementById("agentName")?.value || "";
|
| 528 |
+
const region= document.getElementById("regionDefault")?.value || "";
|
| 529 |
+
const rows = readTable();
|
| 530 |
+
localStorage.setItem(STATE_KEY, JSON.stringify({
|
| 531 |
+
raw, agent, region, rows,
|
| 532 |
+
theme:document.body.classList.contains('dark')?'dark':'light'
|
| 533 |
+
}));
|
| 534 |
+
}catch{}
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
function loadState(){
|
| 538 |
+
try{
|
| 539 |
+
const s = localStorage.getItem(STATE_KEY);
|
| 540 |
+
if(!s) return false;
|
| 541 |
+
let { raw, agent, region, rows, theme } = JSON.parse(s);
|
| 542 |
+
const rawEl = document.getElementById("raw");
|
| 543 |
+
if(typeof raw === "string" && rawEl) rawEl.value = raw;
|
| 544 |
+
const agentEl = document.getElementById("agentName");
|
| 545 |
+
if(typeof agent === "string" && agentEl) agentEl.value = agent;
|
| 546 |
+
const regionEl = document.getElementById("regionDefault");
|
| 547 |
+
if(typeof region === "string" && regionEl) regionEl.value = region;
|
| 548 |
+
if(theme === 'dark') document.body.classList.add('dark');
|
| 549 |
+
updateThemeLabel();
|
| 550 |
+
|
| 551 |
+
rows = ensureColumns(rows, agent, region);
|
| 552 |
+
if(Array.isArray(rows) && rows.length){
|
| 553 |
+
buildTable(rows); validateCells(); updateBadge(rows.length); setButtonsEnabled(true);
|
| 554 |
+
}
|
| 555 |
+
return true;
|
| 556 |
+
}catch{ return false; }
|
| 557 |
+
}
|
| 558 |
+
|
| 559 |
+
function clearAll(){
|
| 560 |
+
const rawEl = document.getElementById("raw");
|
| 561 |
+
const tbody = document.getElementById("tbody");
|
| 562 |
+
const agentEl = document.getElementById("agentName");
|
| 563 |
+
const regionEl= document.getElementById("regionDefault");
|
| 564 |
+
if(rawEl) rawEl.value = "";
|
| 565 |
+
if(tbody) tbody.innerHTML = "";
|
| 566 |
+
if(agentEl) agentEl.value = "";
|
| 567 |
+
if(regionEl) regionEl.value = "";
|
| 568 |
+
updateBadge(0); setButtonsEnabled(false);
|
| 569 |
+
document.getElementById("warn").hidden = true;
|
| 570 |
+
try{ ALL_STATE_KEYS.forEach(k=>localStorage.removeItem(k)); }catch{}
|
| 571 |
+
toast("تم مسح كل البيانات والتخزين.");
|
| 572 |
+
}
|
| 573 |
+
|
| 574 |
+
function mergeDuplicatesRows(rows){
|
| 575 |
+
if(!rows.length) return rows;
|
| 576 |
+
const map = new Map();
|
| 577 |
+
rows.forEach(r=>{
|
| 578 |
+
const key = [
|
| 579 |
+
r["رقم الهوية"]||"",
|
| 580 |
+
r["رقم الجهاز"]||"",
|
| 581 |
+
r["رقم الجوال"]||"",
|
| 582 |
+
r["وقت حدوث المشكلة"]||"",
|
| 583 |
+
(r["نوع المشكلة"]||"").slice(0,40)
|
| 584 |
+
].join("|");
|
| 585 |
+
if(!map.has(key)) map.set(key, r);
|
| 586 |
+
});
|
| 587 |
+
return [...map.values()];
|
| 588 |
+
}
|
| 589 |
+
|
| 590 |
+
function updateThemeLabel(){
|
| 591 |
+
const btn = document.getElementById("btn-theme");
|
| 592 |
+
if(!btn) return;
|
| 593 |
+
btn.textContent = document.body.classList.contains("dark") ? "☀️ وضع نهار" : "🌙 وضع ليلي";
|
| 594 |
+
}
|
| 595 |
+
|
| 596 |
+
function init(){
|
| 597 |
+
const parseBtn = document.getElementById("btn-parse");
|
| 598 |
+
const exportBtn = document.getElementById("btn-export");
|
| 599 |
+
const copyBtn = document.getElementById("btn-copy");
|
| 600 |
+
const clearBtn = document.getElementById("btn-clear");
|
| 601 |
+
const themeBtn = document.getElementById("btn-theme");
|
| 602 |
+
const rawEl = document.getElementById("raw");
|
| 603 |
+
const agentEl = document.getElementById("agentName");
|
| 604 |
+
const regionEl = document.getElementById("regionDefault");
|
| 605 |
+
|
| 606 |
+
rawEl.placeholder = SAMPLE;
|
| 607 |
+
|
| 608 |
+
loadState();
|
| 609 |
+
|
| 610 |
+
parseBtn.addEventListener("click", ()=>{
|
| 611 |
+
const raw = (rawEl.value || "").trim();
|
| 612 |
+
if(!raw){ toast("فضلاً الصق/ي تذاكر أولاً."); return; }
|
| 613 |
+
|
| 614 |
+
const cleaned = normalizeText(fixLabels(raw));
|
| 615 |
+
const agent = agentEl.value || "";
|
| 616 |
+
const defRegion = regionEl.value || "";
|
| 617 |
+
|
| 618 |
+
let rows = parseTicketsWithExtras(cleaned, agent, defRegion);
|
| 619 |
+
rows = mergeDuplicatesRows(rows);
|
| 620 |
+
|
| 621 |
+
buildTable(rows); validateCells();
|
| 622 |
+
updateBadge(rows.length); setButtonsEnabled(rows.length>0);
|
| 623 |
+
saveState();
|
| 624 |
+
toast(`تم استخراج ${rows.length} تذكرة.`);
|
| 625 |
+
});
|
| 626 |
+
|
| 627 |
+
exportBtn.addEventListener("click", exportExcel);
|
| 628 |
+
copyBtn.addEventListener("click", copyToClipboardTSV);
|
| 629 |
+
clearBtn.addEventListener("click", clearAll);
|
| 630 |
+
|
| 631 |
+
themeBtn.addEventListener("click", ()=>{
|
| 632 |
+
document.body.classList.toggle("dark");
|
| 633 |
+
updateThemeLabel();
|
| 634 |
+
saveState();
|
| 635 |
+
});
|
| 636 |
+
|
| 637 |
+
rawEl.addEventListener("input", saveState);
|
| 638 |
+
agentEl.addEventListener("input", saveState);
|
| 639 |
+
regionEl.addEventListener("change", saveState);
|
| 640 |
+
|
| 641 |
+
document.addEventListener("keydown", (e)=>{
|
| 642 |
+
const ctrl = e.ctrlKey || e.metaKey;
|
| 643 |
+
if(ctrl && e.key === "Enter"){ e.preventDefault(); parseBtn.click(); }
|
| 644 |
+
else if(ctrl && e.key.toLowerCase() === "e"){ e.preventDefault(); exportBtn.click(); }
|
| 645 |
+
else if(ctrl && e.shiftKey && e.key.toLowerCase() === "c"){ e.preventDefault(); copyBtn.click(); }
|
| 646 |
+
else if(e.key === "Escape"){ e.preventDefault(); clearAll(); }
|
| 647 |
+
});
|
| 648 |
+
|
| 649 |
+
setButtonsEnabled(!!document.getElementById("tbody")?.children.length);
|
| 650 |
+
updateThemeLabel();
|
| 651 |
+
}
|
| 652 |
init();
|