File size: 20,143 Bytes
cca0cf3 | 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | /**
* @fileoverview Contact Record Editor & Modal Controller
* @module controllers/contacts-editor-controller
* @description وحدة التحكم في نافذة إنشاء وتعديل العملاء، إدارة الأرقام المتعددة، والحقول المخصصة.
*/
(global => {
'use strict';
/**
* تنظيف وتنسيق رقم الهاتف عبر المحرك المعتمد
* @param {string} phone
* @returns {string}
*/
function normalizePhone(phone) {
if (global.FritreeContactsResolver && typeof global.FritreeContactsResolver.normalize === 'function') {
return global.FritreeContactsResolver.normalize(phone);
}
return phone ? String(phone).replace(/[^0-9+]/g, '') : '';
}
/**
* وحدة التحكم في محرر جهات الاتصال
*/
const ContactEditorController = {
/**
* إضافة سطر إدخال رقم هاتف ديناميكي مع تحديد نوع الرقم
* @param {string} phoneVal
* @param {string} phoneType ('mobile' | 'whatsapp' | 'work' | 'home' | 'other')
*/
addPhoneInputRow: function(phoneVal = '', phoneType = 'mobile') {
const container = document.getElementById('pe2oiv9nvs');
if (!container) return;
const row = document.createElement('div');
row.className = 'qp7emstoll';
row.style.cssText = 'display:flex; gap:6px; align-items:center; margin-bottom:6px;';
const typeSelect = document.createElement('select');
typeSelect.className = 'xy26ymifmf f0jy0nh7wu';
typeSelect.style.cssText = 'width:130px; padding:8px; font-size:11px; border-radius:6px; background:#fff; font-weight:bold;';
typeSelect.innerHTML = `
<option value="mobile" ${phoneType === 'mobile' ? 'selected' : ''}>جوال رئيسي</option>
<option value="whatsapp" ${phoneType === 'whatsapp' ? 'selected' : ''}>واتساب مباشر</option>
<option value="work" ${phoneType === 'work' ? 'selected' : ''}>موبايل عمل</option>
<option value="home" ${phoneType === 'home' ? 'selected' : ''}>تليفون منزل</option>
<option value="other" ${phoneType === 'other' ? 'selected' : ''}>رقم تاني</option>
`;
const input = document.createElement('input');
input.type = 'text';
input.className = 'xy26ymifmf kljqnbvx11';
input.placeholder = 'مثال: +201069794840';
input.value = phoneVal;
input.style.cssText = 'flex:1; padding:8px 12px; direction:ltr; text-align:left;';
const removeBtn = document.createElement('button');
removeBtn.type = 'button';
removeBtn.className = 'btn-danger';
removeBtn.style.cssText = 'padding:6px 10px; font-size:11px; height:36px; border-radius:6px; background:#fee2e2; color:#ef4444; border:1px solid #fca5a5;';
removeBtn.innerHTML = '<i class="fa-solid fa-xmark" style="margin:0;"></i> ';
removeBtn.title = 'حذف الرقم ده';
removeBtn.onclick = () => {
if (container.querySelectorAll('.qp7emstoll').length > 1) {
row.remove();
} else {
input.value = '';
}
};
row.appendChild(typeSelect);
row.appendChild(input);
row.appendChild(removeBtn);
container.appendChild(row);
// إضافة سطر جديد تلقائياً عند البدء في كتابة رقم بالسطر الأخير
input.addEventListener('input', () => {
const rows = container.querySelectorAll('.kljqnbvx11');
const lastInput = rows[rows.length - 1];
if (input === lastInput && input.value.trim().length > 0) {
this.addPhoneInputRow('', 'mobile');
}
const val = input.value.trim();
if (val.length >= 8 && global.FritreeContacts) {
const match = global.FritreeContacts.lookup(val);
if (match) {
this.populateFormFields(match);
}
}
});
},
/**
* إضافة سطر حقل مخصص جديد
* @param {string} key
* @param {string} val
*/
addCustomFieldRow: function(key = '', val = '') {
const container = document.getElementById('du5h7y5kct');
if (!container) return;
const row = document.createElement('div');
row.className = 'tupbvypcqw';
row.style.cssText = 'display:flex; gap:6px; align-items:center; margin-bottom:6px;';
const keyInput = document.createElement('input');
keyInput.type = 'text';
keyInput.className = 'xy26ymifmf h83d317lfm';
keyInput.placeholder = 'اسم الحقل (زي: الميزانية)';
keyInput.value = key;
keyInput.style.cssText = 'flex:1; padding:6px 10px; font-size:11px;';
const valInput = document.createElement('input');
valInput.type = 'text';
valInput.className = 'xy26ymifmf cv3mq1mhux';
valInput.placeholder = 'القيمة (زي: 50,000 ج.م)';
valInput.value = val;
valInput.style.cssText = 'flex:1.5; padding:6px 10px; font-size:11px;';
const removeBtn = document.createElement('button');
removeBtn.type = 'button';
removeBtn.className = 'btn-danger';
removeBtn.style.cssText = 'padding:4px 8px; font-size:10px; height:32px; border:2px solid var(--border);';
removeBtn.innerHTML = '<i class="fa-solid fa-xmark"></i> ';
removeBtn.onclick = () => row.remove();
row.appendChild(keyInput);
row.appendChild(valInput);
row.appendChild(removeBtn);
container.appendChild(row);
},
/**
* ملء حقول النموذج ببيانات العميل المطابق
* @param {Object} match
*/
populateFormFields: function(match) {
document.getElementById('qfv7jp577g').value = match.id || '';
document.getElementById('sdd0e5bwjh').value = match.name || '';
document.getElementById('n8v43cmfzp').value = match.email || '';
document.getElementById('bluyqoyzyu').value = match.company || '';
document.getElementById('hns86xlu9u').value = match.jobTitle || '';
document.getElementById('tai1zux829').value = match.department || '';
document.getElementById('foockjmk6s').value = match.lifecycle || 'lead';
document.getElementById('g1oufrwen8').value = match.priority || 'medium';
document.getElementById('x1cb3ek74e').value = match.source || 'direct';
document.getElementById('s2admrtim1').value = match.nextFollowupDate ? match.nextFollowupDate.slice(0, 10) : '';
document.getElementById('ftiovdh9zk').value = match.preferredChannel || 'whatsapp';
document.getElementById('m6m8pmqk2g').value = match.preferredTime || 'any';
document.getElementById('kgb8k4l7x8').value = match.address || '';
document.getElementById('c4aemm27ll').value = match.website || '';
document.getElementById('abqk9m5uxg').value = match.tags ? match.tags.join(', ') : '';
document.getElementById('ab9w3psogf').value = match.notes || '';
document.getElementById('baysuwhq61').checked = !!match.blacklisted;
if (match.customFields && typeof match.customFields === 'object') {
const customContainer = document.getElementById('du5h7y5kct');
if (customContainer) {
customContainer.innerHTML = '';
Object.entries(match.customFields).forEach(([k, v]) => {
this.addCustomFieldRow(k, v);
});
}
}
const title = document.getElementById('kvcvcii0b6');
if (title) title.textContent = `تعديل بيانات العميل: ${match.name}`;
const badge = document.getElementById('o497sju1mo');
if (badge) {
badge.style.display = 'inline-flex';
badge.innerHTML = `تم جلب بيانات العميل: ${match.name}`;
}
['sdd0e5bwjh', 'n8v43cmfzp', 'bluyqoyzyu', 'hns86xlu9u', 'abqk9m5uxg', 'ab9w3psogf'].forEach(id => {
const el = document.getElementById(id);
if (el) {
el.style.backgroundColor = '#dcfce7';
setTimeout(() => { el.style.backgroundColor = ''; }, 1800);
}
});
},
/**
* فتح نافذة تحرير العميل (جديد أو تعديل)
* @param {string|null} contactId
*/
openModal: function(contactId = null) {
const modal = document.getElementById('g2vl6bip8z');
const title = document.getElementById('kvcvcii0b6');
const phoneContainer = document.getElementById('pe2oiv9nvs');
const customContainer = document.getElementById('du5h7y5kct');
const badge = document.getElementById('o497sju1mo');
if (!modal || !phoneContainer) return;
phoneContainer.innerHTML = '';
if (customContainer) customContainer.innerHTML = '';
if (badge) badge.style.display = 'none';
const contacts = global.FritreeContacts ? global.FritreeContacts.getContacts() : [];
if (contactId) {
const c = contacts.find(x => x.id === contactId);
if (!c) return;
this.populateFormFields(c);
this.addPhoneInputRow(c.phone, 'mobile');
if (c.altPhones && Array.isArray(c.altPhones)) {
c.altPhones.forEach(ph => {
const pVal = typeof ph === 'object' ? ph.phone : ph;
const pType = typeof ph === 'object' ? ph.type : 'work';
this.addPhoneInputRow(pVal, pType);
});
}
this.addPhoneInputRow('', 'mobile');
} else {
document.getElementById('qfv7jp577g').value = '';
document.getElementById('sdd0e5bwjh').value = '';
document.getElementById('n8v43cmfzp').value = '';
document.getElementById('bluyqoyzyu').value = '';
document.getElementById('hns86xlu9u').value = '';
document.getElementById('tai1zux829').value = '';
document.getElementById('foockjmk6s').value = 'lead';
document.getElementById('x1cb3ek74e').value = 'direct';
document.getElementById('g1oufrwen8').value = 'medium';
document.getElementById('s2admrtim1').value = '';
document.getElementById('ftiovdh9zk').value = 'whatsapp';
document.getElementById('m6m8pmqk2g').value = 'any';
document.getElementById('kgb8k4l7x8').value = '';
document.getElementById('c4aemm27ll').value = '';
document.getElementById('abqk9m5uxg').value = '';
document.getElementById('ab9w3psogf').value = '';
document.getElementById('baysuwhq61').checked = false;
if (title) title.textContent = 'إضافة عميل جديد بكامل البيانات';
this.addPhoneInputRow('', 'mobile');
}
modal.style.display = 'flex';
},
/**
* حفظ بيانات العميل في الخزنة المشفرة وتحديث التقويم
*/
saveContactSubmit: async function() {
const core = global.FritreeContacts;
if (!core || !core.state) return;
const state = core.state;
const id = document.getElementById('qfv7jp577g').value;
const name = document.getElementById('sdd0e5bwjh').value.trim();
const email = document.getElementById('n8v43cmfzp').value.trim();
const company = document.getElementById('bluyqoyzyu').value.trim();
const jobTitle = document.getElementById('hns86xlu9u').value.trim();
const department = document.getElementById('tai1zux829').value.trim();
const lifecycle = document.getElementById('foockjmk6s').value;
const source = document.getElementById('x1cb3ek74e').value;
const priority = document.getElementById('g1oufrwen8').value;
const nextFollowupDate = document.getElementById('s2admrtim1').value;
const preferredChannel = document.getElementById('ftiovdh9zk').value;
const preferredTime = document.getElementById('m6m8pmqk2g').value;
const address = document.getElementById('kgb8k4l7x8').value.trim();
const website = document.getElementById('c4aemm27ll').value.trim();
const tagsStr = document.getElementById('abqk9m5uxg').value;
const notes = document.getElementById('ab9w3psogf').value.trim();
const blacklisted = document.getElementById('baysuwhq61').checked;
const phoneRows = document.querySelectorAll('.qp7emstoll');
const phoneList = [];
phoneRows.forEach(row => {
const f = row.querySelector('.kljqnbvx11');
const t = row.querySelector('.f0jy0nh7wu');
if (f && f.value) {
const norm = normalizePhone(f.value);
if (norm && norm.length >= 7 && !phoneList.some(p => p.phone === norm)) {
phoneList.push({ phone: norm, type: t ? t.value : 'mobile' });
}
}
});
if (phoneList.length === 0) {
alert('اكتب رقم موبايل صحيح واحد على الأقل بالمفتاح الدولي!');
return;
}
const customFields = {};
const customRows = document.querySelectorAll('.tupbvypcqw');
customRows.forEach(cr => {
const kEl = cr.querySelector('.h83d317lfm');
const vEl = cr.querySelector('.cv3mq1mhux');
if (kEl && kEl.value.trim()) {
customFields[kEl.value.trim()] = vEl ? vEl.value.trim() : '';
}
});
const mainPhone = phoneList[0].phone;
const altPhones = phoneList.slice(1);
const tags = tagsStr.split(',').map(t => t.trim()).filter(t => t);
if (id) {
const idx = state.contacts.findIndex(c => c.id === id);
if (idx !== -1) {
state.contacts[idx] = {
...state.contacts[idx],
name: name || 'المستلم',
phone: mainPhone,
altPhones: altPhones,
email: email,
company: company,
jobTitle: jobTitle,
department: department,
lifecycle: lifecycle,
source: source,
priority: priority,
nextFollowupDate: nextFollowupDate,
preferredChannel: preferredChannel,
preferredTime: preferredTime,
address: address,
website: website,
customFields: customFields,
tags: tags,
notes: notes,
blacklisted: blacklisted,
updatedAt: new Date().toISOString()
};
}
} else {
const newContact = {
id: 'cnt_' + Date.now() + '_' + Math.random().toString(36).substr(2, 4),
name: name || 'المستلم',
phone: mainPhone,
altPhones: altPhones,
email: email,
company: company,
jobTitle: jobTitle,
department: department,
lifecycle: lifecycle,
source: source,
priority: priority,
nextFollowupDate: nextFollowupDate,
preferredChannel: preferredChannel,
preferredTime: preferredTime,
address: address,
website: website,
customFields: customFields,
tags: tags,
notes: notes,
blacklisted: blacklisted,
createdAt: new Date().toISOString()
};
state.contacts.unshift(newContact);
}
await core.save();
core.refreshUI();
document.getElementById('g2vl6bip8z').style.display = 'none';
// تسجيل المتابعة تلقائياً في التقويم الذكي
if (global.FritreeCalendar && typeof global.FritreeCalendar.logActivity === 'function') {
global.FritreeCalendar.logActivity({
title: id ? `[تحديث عميل] ${name || 'عميل'}` : `[إضافة عميل جديد] ${name || 'عميل'} (${mainPhone})`,
type: 'followup',
category: 'followup',
notes: `الهاتف: ${mainPhone} | الشركة: ${company || '-'} | المسمى: ${jobTitle || '-'} | تاريخ المتابعة: ${nextFollowupDate || 'غير محدد'}`,
contactId: id || '',
contactName: name,
priority: priority === 'urgent' ? 'urgent' : (priority === 'high' ? 'high' : 'medium'),
timestamp: nextFollowupDate ? new Date(nextFollowupDate).toISOString() : new Date().toISOString()
});
}
if (typeof window.addLog === 'function') {
window.addLog(`تم بنجاح حفظ بيانات العميل: ${name || 'جديد'} (${mainPhone})`, 'success');
}
},
/**
* ربط أحداث أزرار نافذة التحرير
*/
bindEvents: function() {
const btnAddModal = document.getElementById('lndjiz92pl');
const btnCloseEditor = document.getElementById('z9jjbpxjm0');
const btnSaveContact = document.getElementById('du3bb5vayt');
const btnAddCustomField = document.getElementById('duv92vxgty');
if (btnAddModal) btnAddModal.onclick = () => this.openModal(null);
if (btnCloseEditor) btnCloseEditor.onclick = () => document.getElementById('g2vl6bip8z').style.display = 'none';
if (btnSaveContact) btnSaveContact.onclick = () => this.saveContactSubmit();
if (btnAddCustomField) btnAddCustomField.onclick = () => this.addCustomFieldRow();
}
};
/**
* تصدير وحدة تحكم محرر جهات الاتصال
*/
global.FritreeContactsEditor = ContactEditorController;
})(typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : this); |