export const csvUtils = { exportToCSV(data, filename) { if (!data || data.length === 0) return const headers = Object.keys(data[0]) const csvContent = [ headers.join(','), ...data.map(row => headers.map(header => { const value = row[header] ?? '' // Escape quotes and wrap in quotes if contains comma or newline const strValue = String(value).replace(/"/g, '""') return `"${strValue}"` }).join(',') ) ].join('\n') const BOM = '\uFEFF' const blob = new Blob([BOM + csvContent], { type: 'text/csv;charset=utf-8;' }) const link = document.createElement('a') link.href = URL.createObjectURL(blob) link.download = `${filename}.csv` link.click() URL.revokeObjectURL(link.href) }, exportPayrollItems(items, payrollMonth) { const exportData = items.map(item => ({ 'รหัสพนักงาน': item.emp_id, 'ชื่อ': item.employees?.name || item.emp_id, 'ชื่อเล่น': item.employees?.nickname || '', 'จำนวนวันทำงาน': item.work_days, 'ค่าแรงพื้นฐาน': item.base_pay, 'โบนัสก่อนหัก': item.bonus_before_penalty, 'จำนวนวันลาในเดือน': item.leave_days_in_month, 'มีการหักโบนัส': item.bonus_penalty_applied ? 'ใช่' : 'ไม่ใช่', 'เปอร์เซ็นต์การหัก': item.bonus_penalty_percentage, 'โบนัสหลังหัก': item.bonus_after_penalty, 'รวมทั้งหมด': item.total_pay })) this.exportToCSV(exportData, `payroll_${payrollMonth}`) } }