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

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

    // 设置列标题
    worksheet.columns = [
        { header: '订单序号', key: 'index', width: 10 },
        { header: '订单编号', key: 'orderNumber', width: 25 },
        { header: '下单时间', key: 'createTime', width: 15 },
        { header: '用户名', key: 'userName', width: 15 },
        { header: '商品名称', key: 'goodName', width: 20 },
        { header: '商品单价(元)', key: 'price', width: 15 },
        { header: '数量', key: 'quantity', width: 10 },
        { header: '总价(元)', key: 'itemTotalAmount', width: 15 },
        { header: '收货人', key: 'recipient', width: 20 },
        { header: '手机号', key: 'phone', width: 15 },
        { header: '地区', key: 'region', width: 20 },
        { header: '详细地址', key: 'detailAddress', width: 40 },
        { header: '订单实付金额(元)', key: 'totalAmount', width: 15 },
        { header: '订单状态', key: 'orderStatus', width: 15 },
        { header: '快递单号', key: 'trackingNumber', width: 20 },
    ];

    // 遍历 tableData 并填充数据
    tableData.forEach((order, index) => {
        // 展平每个订单的数据
        order.orderItemList.forEach((item) => {
            const row = worksheet.addRow({
                index: index + 1,
                orderNumber: order.orderNumber,
                createTime: order.createTime,
                userName: order.userName,
                goodName: item.goodSnapshot.name,
                price: item.goodSnapshot.price,
                quantity: item.quantity,
                itemTotalAmount: item.itemTotalAmount,
                recipient: order.addressSnapshot.name,
                phone: order.addressSnapshot.phone,
                region: order.addressSnapshot.region,
                detailAddress: order.addressSnapshot.detailAddress,
                totalAmount: order.totalAmount,
                orderStatus: order.orderStatus,
                trackingNumber: order.trackingNumber,
            });

            // 设置列的背景色
            row.getCell('B').style = { fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'A9D08E' } } }; // 订单编号
            row.getCell('I').style = { fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'A2C4E7' } } }; // 收货人
            row.getCell('J').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');
    });
};
