<template>
  <div class="app-container">
    <el-button type="primary" @click="toExport">导出</el-button>
  </div>
</template>
<script>
import { toExport } from '@/api/export_express'

const moduleList = {
  '订单编号': '',
  '寄件人': '大卫博士',
  '联系方式': '4006867733',
  '发件人详细地址': '河南省郑州市经开区',
  '下单人昵称': '',
  '下单人姓名': '',
  '下单人手机号': '',
  '收件人': '',
  '电话': '',
  '收件人省': '',
  '收件人市': '',
  '收件人区': '',
  '收件人街道地址': '',
  'L': '',
  'XL': '',
  'XXL': '',
  'XXXL': '',
  '女M': '',
  '女L': '',
  '女XL': '',
  '女XXL': '',
  '5XL': '',
  '备注': ''
}

export default {
  name: 'ExportExpressIndex',
  data() {
    return {
      expressData: {}
    }
  },
  methods: {
    async toExport() {
      await toExport().then(res => {
        if (res.code === 200) {
          this.expressData = res.data
          const exportData = this.getExportModule()
          import('@/vendor/Export2Excel').then(excel => {
            excel.export_json_to_excel({
              header: Object.keys(moduleList),
              data: exportData.map(listItem => Object.keys(exportData[0]).map(j => listItem[j])),
              filename: `快递单导出`,
              bookType: 'xlsx',
              isCSV: true
            })
          })
        } else {
          this.$message.error(res.message)
        }
      })
    },
    getExportModule() {
      const data = this.expressData
      const out = []
      for (const i in data) {
        const item = data[i]
        const info = item.data
        let a = 0
        let b = 0
        let c = 0
        let d = 0
        while (a < item['精装'].length) {
          const obj = Object.assign({}, moduleList)
          obj['订单编号'] = i
          obj['下单人昵称'] = info.u_nickname
          obj['下单人姓名'] = info.u_name
          obj['下单人手机号'] = info.u_mobile
          obj['收件人'] = info.username
          obj['电话'] = info.mobile
          obj['收件人省'] = info.province
          obj['收件人市'] = info.city
          obj['收件人区'] = info.town
          obj['收件人街道地址'] = info.address
          obj['备注'] = info.remark
          if (item['精装'][a].size === '3XL') {
            item['精装'][a].size = 'XXXL'
          }
          if (item['精装'][a].sec_attr === '男款') {
            obj[item['精装'][a].size] = `${item['精装'][a].num}${item['精装'][a].main_attr}`
          }
          if (item['精装'][a].sec_attr === '女款') {
            obj['女' + item['精装'][a].size] = `${item['精装'][a].num}${item['精装'][a].main_attr}`
          }
          out.push(obj)
          a++
        }
        while (b < item['普通'].length) {
          const obj = Object.assign({}, moduleList)
          obj['订单编号'] = i
          obj['下单人昵称'] = info.u_nickname
          obj['下单人姓名'] = info.u_name
          obj['下单人手机号'] = info.u_mobile
          obj['收件人'] = info.username
          obj['电话'] = info.mobile
          obj['收件人省'] = info.province
          obj['收件人市'] = info.city
          obj['收件人区'] = info.town
          obj['收件人街道地址'] = info.address
          obj['备注'] = info.remark
          if (item['普通'][b].size === '3XL') {
            item['普通'][b].size = 'XXXL'
          }
          if (item['普通'][b].sec_attr === '男款') {
            obj[item['普通'][b].size] = `${item['普通'][b].num}${item['普通'][b].main_attr}`
          }
          if (item['普通'][b].sec_attr === '女款') {
            obj['女' + item['普通'][b].size] = `${item['普通'][b].num}${item['普通'][b].main_attr}`
          }
          out.push(obj)
          b++
        }
        while (c < item['精装混合'].length) {
          const obj = Object.assign({}, moduleList)
          obj['订单编号'] = i
          obj['下单人昵称'] = info.u_nickname
          obj['下单人姓名'] = info.u_name
          obj['下单人手机号'] = info.u_mobile
          obj['收件人'] = info.username
          obj['电话'] = info.mobile
          obj['收件人省'] = info.province
          obj['收件人市'] = info.city
          obj['收件人区'] = info.town
          obj['收件人街道地址'] = info.address
          obj['备注'] = info.remark
          for (let j = 0; j < item['精装混合'][c].length; j++) {
            if (item['精装混合'][c][j].size === '3XL') {
              if (item['精装混合'][c][j].sec_attr === '男款') {
                obj['XXXL'] += `${item['精装混合'][c][j].num}${item['精装混合'][c][j].main_attr}`
              }
            } else {
              if (item['精装混合'][c][j].sec_attr === '男款') {
                obj[item['精装混合'][c][j].size] += `${item['精装混合'][c][j].num}${item['精装混合'][c][j].main_attr}`
              }
              if (item['精装混合'][c][j].sec_attr === '女款') {
                obj['女' + item['精装混合'][c][j].size] += `${item['精装混合'][c][j].num}${item['精装混合'][c][j].main_attr}`
              }
            }
          }
          out.push(obj)
          c++
        }
        while (d < item['普通混合'].length) {
          const obj = Object.assign({}, moduleList)
          obj['订单编号'] = i
          obj['下单人昵称'] = info.u_nickname
          obj['下单人姓名'] = info.u_name
          obj['下单人手机号'] = info.u_mobile
          obj['收件人'] = info.username
          obj['电话'] = info.mobile
          obj['收件人省'] = info.province
          obj['收件人市'] = info.city
          obj['收件人区'] = info.town
          obj['收件人街道地址'] = info.address
          obj['备注'] = info.remark
          for (let j = 0; j < item['普通混合'][d].length; j++) {
            if (item['普通混合'][d][j].size === '3XL') {
              if (item['普通混合'][d][j].sec_attr === '男款') {
                obj['XXXL'] += `${item['普通混合'][d][j].num}${item['普通混合'][d][j].main_attr}`
              }
            } else {
              if (item['普通混合'][d][j].sec_attr === '男款') {
                obj[item['普通混合'][d][j].size] += `${item['普通混合'][d][j].num}${item['普通混合'][d][j].main_attr}`
              }
              if (item['普通混合'][d][j].sec_attr === '女款') {
                obj['女' + item['普通混合'][d][j].size] += `${item['普通混合'][d][j].num}${item['普通混合'][d][j].main_attr}`
              }
            }
          }
          out.push(obj)
          d++
        }
      }
      return out
    }
  }
}
</script>
