/**
* @class CustomerManager
* @description ระบบจัดการข้อมูลลูกค้า การจัดส่ง และการแสดงผลข้อมูลที่เกี่ยวข้อง
*/
const CustomerManager = {
/** @type {CustomerStorage} ตัวจัดการข้อมูลลูกค้าใน Storage */
storage: null,
/**
* @method init
* @description เริ่มต้นระบบจัดการข้อมูลลูกค้า
*/
init() {
this.storage = new CustomerStorage();
this.setupTemplates();
this.setupEventListeners();
this.updateProfileDisplay();
},
/**
* @method setupTemplates
* @description ลงทะเบียนเทมเพลตสำหรับแสดงผลข้อมูลลูกค้า
*/
setupTemplates() {
TemplateManager.registerTemplate('customer-info-template',
`
`
);
},
/**
* @method setupEventListeners
* @description ตั้งค่าการรับฟังอีเวนต์ต่างๆ ที่เกี่ยวข้องกับข้อมูลลูกค้า
*/
setupEventListeners() {
document.addEventListener('click', (e) => {
const target = e.target;
if (target.id === 'editProfileBtn') {
this.showEditForm();
} else if (target.id === 'viewOrderHistoryBtn') {
OrderHistoryManager.showOrderHistory();
}
});
document.addEventListener('submit', async (e) => {
if (e.target.id === 'profileForm') {
e.preventDefault();
await this.handleProfileSubmit(e.target);
}
});
EventBus.on('state:changed', ({property, value}) => {
if (property === 'customerInfo') {
this.updateProfileDisplay();
}
});
},
/**
* @method showEditForm
* @description แสดงฟอร์มแก้ไขข้อมูลลูกค้า
* @param {Function} [callback] - ฟังก์ชันที่จะทำงานหลังบันทึกข้อมูล
*/
showEditForm(callback) {
const customerInfo = this.storage.customerInfo;
const modal = TemplateManager.showModal('customer-info-template', {
...customerInfo
});
if (modal) {
this.setupFormValidation(modal.querySelector('form'));
if (callback) {
this.onSaveCallback = callback;
}
}
},
/**
* @method setupFormValidation
* @description ตั้งค่าการตรวจสอบความถูกต้องของฟอร์ม
* @param {HTMLFormElement} form - ฟอร์มที่ต้องการตรวจสอบ
*/
setupFormValidation(form) {
if (!form) return;
const handleFieldValidation = (field) => {
this.clearFieldError(field);
const fieldName = field.getAttribute('name');
const fieldValue = field.value;
const validationResult = this.storage.validateCustomerInfo({
[fieldName]: fieldValue
});
if (!validationResult.isValid && validationResult.field === fieldName) {
this.showFieldError(field, validationResult.message);
}
};
form.querySelectorAll('input, textarea').forEach(field => {
field.addEventListener('blur', () => handleFieldValidation(field));
field.addEventListener('input', () => this.clearFieldError(field));
});
},
/**
* @method handleProfileSubmit
* @description จัดการการบันทึกข้อมูลลูกค้า
* @param {HTMLFormElement} form - ฟอร์มข้อมูลลูกค้า
*/
async handleProfileSubmit(form) {
try {
const formData = new FormData(form);
const profileData = this.processFormData(formData);
const validationResult = this.storage.validateCustomerInfo(profileData);
if (!validationResult.isValid) {
const field = form.querySelector(`[name="${validationResult.field}"]`);
if (field) {
this.showFieldError(field, validationResult.message);
field.focus();
}
NotificationManager.warning(validationResult.message);
return;
}
const loadingId = NotificationManager.loading('กำลังบันทึกข้อมูล...');
await this.storage.updateCustomerInfo(profileData);
NotificationManager.updateLoading(loadingId, 'บันทึกข้อมูลเรียบร้อย', 'success');
TemplateManager.closeModal(document.getElementById('customerInfoModal'));
if (this.onSaveCallback) {
this.onSaveCallback(this.storage.customerInfo);
this.onSaveCallback = null;
}
} catch (error) {
console.error('Error saving profile:', error);
NotificationManager.error('เกิดข้อผิดพลาดในการบันทึกข้อมูล');
}
},
/**
* @method processFormData
* @description แปลงข้อมูลจากฟอร์มให้อยู่ในรูปแบบที่ต้องการ
* @param {FormData} formData - ข้อมูลจากฟอร์ม
* @returns {Object} ข้อมูลที่แปลงแล้ว
*/
processFormData(formData) {
const profileData = {};
const notifications = {};
for (const [key, value] of formData.entries()) {
if (key === 'notifications') {
notifications[value] = true;
} else {
profileData[key] = value;
}
}
profileData.notifications = notifications;
return profileData;
},
/**
* @method showFieldError
* @description แสดงข้อความแจ้งเตือนข้อผิดพลาดในช่องกรอกข้อมูล
* @param {HTMLElement} field - ช่องกรอกข้อมูล
* @param {string} message - ข้อความแจ้งเตือน
*/
showFieldError(field, message) {
field.classList.add('error');
const errorElement = document.createElement('div');
errorElement.className = 'error-message';
errorElement.textContent = message;
field.parentNode.insertBefore(errorElement, field.nextSibling);
},
/**
* @method clearFieldError
* @description ลบข้อความแจ้งเตือนข้อผิดพลาดในช่องกรอกข้อมูล
* @param {HTMLElement} field - ช่องกรอกข้อมูล
*/
clearFieldError(field) {
field.classList.remove('error');
const errorMessage = field.parentNode.querySelector('.error-message');
if (errorMessage) {
errorMessage.remove();
}
},
/**
* @method updateProfileDisplay
* @description อัพเดทการแสดงผลชื่อลูกค้า
*/
updateProfileDisplay() {
const customerInfo = this.storage.customerInfo;
const profileName = document.getElementById('editProfileBtn');
if (profileName && customerInfo?.name) {
profileName.textContent = customerInfo.name;
}
},
/**
* @method getCustomerInfo
* @description ดึงข้อมูลลูกค้าปัจจุบัน
* @returns {Object} ข้อมูลลูกค้า
*/
getCustomerInfo() {
return this.storage.customerInfo;
}
};