export const importExcel = {
  data() {
    return {
      res:'',//存储excel导出的tr、td
    }
  },
  methods: {
    importBtn() {
      if (this.selected && this.selected.length>0) {//部分导出
        
      } else {//全部导出
        
      }
      if (this.selected.length > 0) {
        // 点击导出时获取勾选中标注的信息，放在selectedExportExcel数组中
        this.selected.forEach(item => {
          if (this.componentType == 'indiciaCard') {
            var a = tableCollapseList.find(item1 => {
              return item == item1.id
            })
            this.selectedExportExcel.push(a)
            
          }
          // else {
          //   this.collapseList.forEach(itemForEach => {
          //     var a= itemForEach.data.find(item1 => {
          //       return item == item1.scratchId
          //     })
          //     this.selectedExportExcel.push(a)
          //   })
          // }
        })
        // const fields = ['专利号','专利标题','标注','标注的位置','标注人','标注时间']
        // this.importExcel(fields, this.selectedExportExcel)
        const fields = {
          remark: '标注',
          nameAndDate: '标注人/时间',
          // date: '标注时间',
          address: '标注的位置',
          patentNo: '专利号',
          pName: '专利标题',
        }
        let title = '标注库'
        this.exportExcel(fields, this.selectedExportExcel, title)
        this.isExcel = !this.isExcel
        this.selectedExportExcel = []
        this.selected = []
      } else {
        this.$message.error('请先勾选标注再进行导出')
        return false
      }
    },
      // 导出excel方法
      exportExcel(filesValue,dataExcel,title) {
        // 拼接标题行
        const head = Object.values(filesValue).map(item => {
          return `<td style='background:#FFC000;font-weight: bold;'>${item}</td>`
        }).join('')
        // 拼接数据行
        dataExcel.map(item => {
          this.getStr(item, filesValue)
        }).join()
        const body = this.res
        this.res=''
        const worksheet = title
        const url = 'data:application/vnd.ms-excel;base64,'
  
        // 下载的表格模板数据
        const template = `<html xmlns:o="urn:schemas-microsoft-com:office:office"
                        xmlns:x="urn:schemas-microsoft-com:office:excel"
                        xmlns="http://www.w3.org/TR/REC-html40">
                        <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
                        <x:Name>${worksheet}</x:Name>
                        <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
                        </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
                      </head><body><table><tr style="mso-number-format:'\@';height:45px;line-height:45px;text-align:center;width:150px;">${head}</tr>${body}</table></body></html>`
        // 下载模板
        let link = document.createElement('a')
        link.setAttribute('href', url + window.btoa(unescape(encodeURIComponent(template))))
        link.setAttribute('download', title + '.xls')
        link.click()
        
      },
      getStr(item, filesValue) {
        this.res += `<tr style="mso-number-format:'\@';height:80px;line-height:80px;text-align:center;" >`
        for (const key in filesValue) {
          if (key == 'address') {
            this.res += `<td style='width:150px'>${'[' + item.scratchField + ']' + item.text}</td>`
          } else if (key == 'nameAndDate') {
            this.res += `<td style='width:150px'>${ item.name + '&nbsp;' + item.date}</td>`
          } else {
            this.res += `<td style='width:150px'>${item[key]}</td>`
          }
        }
        this.res += '</tr>'
      },
  },
}
