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.goodsAmount) {
            e.goodsAmount = e.goodsAmount.toFixed(2)
        }
        if (e.specsValue.length < 1) {
            e.specsValue = ""
        }
        if (e.orderStatus == '0') {
            e.orderStatus = '待商家确认修改运费';
        } else if (e.orderStatus == '10') {
            e.orderStatus = '等待买家支付';
        } else if (e.orderStatus == '20') {
            e.orderStatus = '等待商家发布货运信息';
        } else if (e.orderStatus == '90') {
            e.orderStatus = '等待司机接单';
        } else if (e.orderStatus == '100') {
            e.orderStatus = '等待司机支付保证金';
        } else if (e.orderStatus == '110') {
            e.orderStatus = '等待司机装货';
        } else if (e.orderStatus == '120') {
            e.orderStatus = '已装货，运输中';
        } else if (e.orderStatus == '30') {
            e.orderStatus = '司机已到达，等待买家签收';
        } else if (e.orderStatus == '40') {
            e.orderStatus = '买家已收货';
        } else if (e.orderStatus == '50') {
            e.orderStatus = '已取消';
        } else if (e.orderStatus == '60') {
            e.orderStatus = '退款中';
        } else if (e.orderStatus == '70') {
            e.orderStatus = '退款成功';
        } else if (e.orderStatus == '80') {
            e.orderStatus = '退款失败';
        } else if (e.orderStatus == '130') {
            e.orderStatus = '待处理售后';
        } else if (e.orderStatus == '140') {
            e.orderStatus = '已处理售后';
        }

        if (e.afterSaleStatus == '0') {
            e.afterSaleStatus = '未申请';
        } else if (e.afterSaleStatus == '1') {
            e.afterSaleStatus = '已申请';
        } else if (e.afterSaleStatus == '2') {
            e.afterSaleStatus = '审核失败';
        } else if (e.afterSaleStatus == '3') {
            e.afterSaleStatus = '审核成功（退款中）';
        } else if (e.afterSaleStatus == '4') {
            e.afterSaleStatus = '退款成功';
        } else if (e.afterSaleStatus == '5') {
            e.afterSaleStatus = '退款失败（联系客服解决)';
        }



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