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.deliveryType) {
      switch (e.deliveryType) {
        case 0:
          e.deliveryType = "到店"
          break;
        case 1:
          e.deliveryType = "上门"
          break;
        case '':
          e.deliveryType = ""
          break;
      }
    }
    if (e.afterSaleStatus) {
      switch (e.afterSaleStatus) {
        case 0:
          e.afterSaleStatus = "未申请"
          break;
        case 1:
          e.afterSaleStatus = "已申请"
          break;
        case 2:
          e.afterSaleStatus = "审核失败"
          break;
        case 3:
          e.afterSaleStatus = "审核成功（退款中）"
          break;
        case 4:
          e.afterSaleStatus = "退款成功"
          break;
        case 4:
          e.afterSaleStatus = "退款失败（联系客服解决）"
          break;
      }
    }
    if (e.orderStatusEnum) {
      switch (e.orderStatusEnum) {
        case 'WAIT_DRIVER_ORDER':
          e.orderStatusEnum = "待接单"
          break;
        case 'WAIT_PENDING_ORDERS':
          e.orderStatusEnum = "待出单"
          break;
        case 'IN_PROGRESS':
          e.orderStatusEnum = "进行中"
          break;
        case 'TRADE_COMPLETED':
          e.orderStatusEnum = "交易完成"
          break;
        case 'TRADE_CLOSED':
          e.orderStatusEnum = "交易关闭"
          break;
        case '':
          e.orderStatusEnum = ""
          break;
      }
    }
  });

  return data;
}
export default {
  exportExcel
}
