import type { Giftcardcode } from '@/model/giftCard' import { metalLabel } from '@/utils/adminMetalLabels' import { toJalali } from '@core/utils/useDataTime' export interface GiftCardCodesExportMeta { id?: number unit?: number count?: number customerName?: string customerMobile?: string createdAt?: string } interface ExportRow { index: number code: string unit: string amount: string status: string expiryDate: string usedAt: string assignedName: string assignedMobile: string } const CSV_HEADERS = [ 'ردیف', 'کد', 'نوع', 'مقدار', 'وضعیت', 'تاریخ انقضا', 'تاریخ استفاده', 'نام کاربر', 'موبایل کاربر' ] function statusLabel(sts: number): string { if (sts === 1) return 'فعال' if (sts === 2) return 'استفاده شده' return 'غیرفعال' } function unitLabel(unit: number): string { if (unit === 2) return metalLabel.goldWalletShort() if (unit === 3) return 'تخفیف' return 'ریالی' } function amountLabel(code: Giftcardcode): string { const suffix = code.unit === 2 ? 'گرم' : 'ریال' return `${code.amount ?? '-'} ${suffix}` } function buildRows(codes: Giftcardcode[]): ExportRow[] { return codes.map((code, index) => ({ index: index + 1, code: code.code, unit: unitLabel(code.unit), amount: amountLabel(code), status: statusLabel(code.sts), expiryDate: code.expiry_date ? toJalali(code.expiry_date) : '-', usedAt: code.used_at ? toJalali(code.used_at) : '-', assignedName: code.assigned_to?.name ?? '-', assignedMobile: code.assigned_to?.mobile ?? '-' })) } function csvCell(value: unknown): string { const text = value == null || value === '' ? '-' : String(value) if (/[",\n\r]/.test(text)) { return `"${text.replace(/"/g, '""')}"` } return text } function buildFilename(meta: GiftCardCodesExportMeta, extension: string): string { const idPart = meta.id ? `-${meta.id}` : '' const datePart = new Date().toISOString().slice(0, 10) return `giftcard-codes${idPart}-${datePart}.${extension}` } function downloadBlob(content: string, filename: string, mime: string) { const blob = new Blob([content], { type: mime }) const url = URL.createObjectURL(blob) const link = document.createElement('a') link.href = url link.download = filename link.click() URL.revokeObjectURL(url) } function buildSummary(meta: GiftCardCodesExportMeta): string[] { const lines: string[] = [] if (meta.id) lines.push(`شناسه کارت هدیه: ${meta.id}`) if (meta.unit != null) lines.push(`نوع: ${unitLabel(meta.unit)}`) if (meta.count != null) lines.push(`تعداد کدها: ${meta.count}`) if (meta.customerName) lines.push(`مشتری: ${meta.customerName}`) if (meta.customerMobile) lines.push(`موبایل: ${meta.customerMobile}`) if (meta.createdAt) lines.push(`تاریخ ثبت: ${toJalali(meta.createdAt)}`) return lines } export function downloadGiftCardCodesExcel(codes: Giftcardcode[], meta: GiftCardCodesExportMeta = {}) { const rows = buildRows(codes) const lines = [ CSV_HEADERS.map(csvCell).join(','), ...rows.map((row) => [ row.index, row.code, row.unit, row.amount, row.status, row.expiryDate, row.usedAt, row.assignedName, row.assignedMobile ] .map(csvCell) .join(',') ) ] const content = `\uFEFF${lines.join('\r\n')}` downloadBlob(content, buildFilename(meta, 'csv'), 'text/csv;charset=utf-8') } export function downloadGiftCardCodesPdf(codes: Giftcardcode[], meta: GiftCardCodesExportMeta = {}) { const rows = buildRows(codes) const summary = buildSummary(meta) const title = meta.id ? `لیست کدهای کارت هدیه #${meta.id}` : 'لیست کدهای کارت هدیه' const tableRows = rows .map( (row) => ` ${row.index} ${row.code} ${row.unit} ${row.amount} ${row.status} ${row.expiryDate} ${row.usedAt} ${row.assignedName} ${row.assignedMobile} ` ) .join('') const html = ` ${title}

${title}

${summary.length ? `
${summary.map((line) => `
${line}
`).join('')}
` : ''} ${CSV_HEADERS.map((header) => ``).join('')} ${tableRows}
${header}
` const printWindow = window.open('', '_blank') if (!printWindow) return printWindow.document.open() printWindow.document.write(html) printWindow.document.close() printWindow.focus() window.setTimeout(() => { printWindow.print() printWindow.close() }, 300) }