export default {
  methods: {
    getTime() {
      var date = new Date()
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      month = month > 9 ? month : '0' + month
      var day = date.getDate()
      day = day > 9 ? day : '0' + day
      var hour = date.getHours()
      hour = hour > 9 ? hour : '0' + hour
      var min = date.getMinutes()
      min = min > 9 ? min : '0' + min
      return year + '' + month + '' + day + '' + hour + '' + min
    },
    toExportExcel(obj) {
      this.exportExcel(obj.rows || this.parentData.listConfig.rows, obj.tableData || this.parentData.tableData)
    },
    exportExcel(rows, tableData) {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      const configArr = [...rows]
      if (configArr[configArr.length - 1].type === 'handle2' || configArr[configArr.length - 1].type === 'handle') {
        configArr.pop()
      }
      const tHeader = []
      const filterVal = []
      configArr.forEach(item => {
        tHeader.push(item.label)
        filterVal.push(item.prop)
      })
      if (this.exportExcelMore && typeof this.exportExcelMore === 'function') {
        this.exportExcelMore(filterVal, tHeader)
        loading.close()
      } else {
        // import('@/vendor/Export2Excel').then(excel => {
        //   const data = this.formatJson(filterVal, tableData)
        //   const filename = `${this.$route.meta.title}${this.getTime()}`
        //   excel.export_json_to_excel({
        //     header: tHeader,
        //     data,
        //     filename,
        //     autoWidth: true,
        //     bookType: 'xlsx'
        //   })
        //   loading.close()
        // })
      }
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
