import { saveAs } from "file-saver";
import ExcelJS from "exceljs";


// const getBase64Image = (url ) => {
//     return new Promise((resolve, reject) => {
//         const img = new Image()
//         const canvas = document.createElement('canvas');
//         img.crossOrigin = '*';
//         img.onload = function () {
//             const width = img.width, height = img.height;
//             canvas.width = width;
//             canvas.height = height;
//
//             const ctx = canvas.getContext('2d');
//             ctx.fillStyle = 'white';
//             ctx.fillRect(0, 0, canvas.width, canvas.height);
//             ctx.drawImage(img, 0, 0, width, height);
//             const base64 = canvas.toDataURL(type);
//             resolve(base64);
//         };
//         img.onerror = function () {
//             reject(new Error('message'));
//         };
//         img.src = url;
//     });
// };



export const serviceOrderList = (tableData, downloadUrl) => {
    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet('Order Details');

    // 设置列标题
    worksheet.columns = [
        { header: '订单序号', key: 'index', width: 10 },
        { header: '订单编号', key: 'orderNumber', width: 30 },
        { header: '下单时间', key: 'createTime', width: 20 },
        { header: '用户名', key: 'userName', width: 15 },
        { header: '所购买的商品', key: 'goodName', width: 30 },
        { header: '商品类别', key: 'type', width: 20 },
        { header: '商品单价(元)', key: 'price', width: 15 },
        { header: '数量', key: 'quantity', width: 10 },
        { header: '总价(元)', key: 'itemTotalAmount', width: 15 },
        { header: '预约日期', key: 'reservationDate', width: 15 },
        { header: '预约时间段', key: 'timeSlot', width: 15 },
        { header: '联系人姓名', key: 'contactName', width: 20 },
        { header: '联系人手机号', key: 'contactPhone', width: 20 },
        { header: '订单实付金额(元)', key: 'totalAmount', width: 20 },
        { header: '订单状态', key: 'orderStatus', width: 20 },
    ];

    // 遍历 tableData 并填充数据
    tableData.forEach((order, index) => {
        // 展平每个订单的数据
        order.orderItemList.forEach(async (item) => {
            // 订单每个商品的行
            const row = worksheet.addRow({
                index: index + 1,
                orderNumber: order.orderNumber,
                createTime: order.createTime,
                userName: order.userName,
                goodName: item.goodSnapshot.name,
                type: item.goodSnapshot.type,
                price: item.goodSnapshot.price,
                quantity: item.quantity,
                itemTotalAmount: item.itemTotalAmount,
                reservationDate: item.reservationDate,
                timeSlot: item.timeSlot,
                contactName: order.contactsSnapshot.name,
                contactPhone: order.contactsSnapshot.phone,
                totalAmount: order.totalAmount,
                orderStatus: order.orderStatus,
            });

            // 设置列的背景色
            row.getCell('B').style = { fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'A9D08E' } } }; // 订单编号
            row.getCell('J').style = { fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'A2C4E7' } } }; // 收货人
            row.getCell('K').style = { fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'A2C4E7' } } }; // 手机号
            row.getCell('O').style = { fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'F4CCCC' } } }; // 订单状态
            row.getCell('M').style = { fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: '98EBFF' } } }; // 订单状态
            row.getCell('N').style = { fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'F7FF72' } } }; // 订单实付金额（改为黄色）
            // 调整行高为自适应
            const rowHeight = Math.max(...row._cells.map(cell => String(cell.value).length));
            row.height = rowHeight > 10 ? rowHeight + 5 : 15; // 设置最小行高为15，最大行高为文字长度 + 5
        });
    });

    // 启用筛选功能
    worksheet.autoFilter = { from: 'A1', to: 'O1' }; // 设置 A1 到 O1 列范围作为筛选区域

    // 导出为 Excel 文件
    workbook.xlsx.writeBuffer().then((buffer) => {
        const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
        saveAs(blob, '服务类商品订单.xlsx');
    });
};
