import ExcelJS from 'exceljs';
import moment from 'moment'

function getDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;//获取当前月份的日期
    var d = dd.getDate();
    return y + '-' + (m < 10 ? '0' + m : m) + '-' + d;
}

function exportExcel(columns, datas, downloadName) {
    let datalist = JSON.parse(JSON.stringify(datas));
    let tableData = dataImpl(datalist)
    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet('Sheet 1'); // 创建一个工作表
    // 设置列标题
    worksheet.columns = columns
    // 准备数据并添加到工作表中
    const data = tableData
    worksheet.addRows(data); // 添加数据行
    // 创建一个 Blob 并下载文件

    workbook.xlsx.writeBuffer().then((buffer) => {
        const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
        const link = document.createElement('a');
        link.href = URL.createObjectURL(blob);
        link.download = downloadName + moment().format('YYYY-MM-DD HH:mm:ss') || 'example.xlsx';
        link.click(); // 模拟点击下载文件
    });
}
function dataImpl(data) {
    data.forEach(e => {
        if (e.afterSaleStatus) {
            if (e.afterSaleStatus == 0) {
                e.afterSaleStatus = '未申请';
            }
            switch (e.afterSaleStatus) {
                case 1: e.afterSaleStatus = '已申请'; break;
                case 2: e.afterSaleStatus = '审核失败'; break;
                case 3: e.afterSaleStatus = '审核成功（退款中）'; break;
                case 4: e.afterSaleStatus = '退款成功'; break;
                case 5: e.afterSaleStatus = '退款失败（联系客服解决)'; break;
            }
        } else if (e.orderStatus) {
            switch (e.orderStatus) {
                case 0:
                    e.orderStatus = '待商家确认修改运费';
                    break;
                case 10: e.orderStatus = '等待买家支付'; break;
                case 20: e.orderStatus = '等待商家发布货运信息'; break;
                case 90: e.orderStatus = '等待司机接单'; break;
                case 100: e.orderStatus = '等待司机支付保证金'; break;
                case 110: e.orderStatus = '等待司机装货)'; break;
                case 120: e.orderStatus = '已装货，运输中'; break;
                case 30: e.orderStatus = '司机已到达，等待买家签收'; break;
                case 40: e.orderStatus = '买家已收货'; break;
                case 50: e.orderStatus = '已取消'; break;
                case 60: e.orderStatus = '退款中'; break;
                case 70: e.orderStatus = '退款成功'; break;
                case 80: e.orderStatus = '退款失败'; break;
                case 130: e.orderStatus = '待处理售后'; break;
                case 140: e.orderStatus = '已处理售后'; break;
            }
        }

    });
    return data;
}
export default {
    exportExcel
}