<template>
  <div style="display: inline-block;width: 100%;height: 100%;margin-top: 5px">
    <!-- 文件选择和拖拽区域 -->
    <input
      id="excel-upload-input"
      ref="excel-upload-input"
      accept=".xlsx, .xls"
      class="c-hide"
      multiple="multiple"
      type="file"
      @change="handkeFileChange">
    <div id="drop" @dragenter="handleDragover" @dragover="handleDragover" @drop="handleDrop">
      将Excel文件拖到这或
      <el-button
        icon="el-icon-search"
        plain
        size="small"
        style="margin-left:16px;"
        type="primary"
        @click="handleUpload">浏览
      </el-button>
    </div>
    <!-- 显示Sheet名称的列表 -->
    <div v-if="excelData.sheetNames.length">
      Sheet列表
      <ul>
        <li v-for="(sheet, index) in excelData.sheetNames" :key="index">{{ sheet }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
import * as XLSX from 'xlsx'

export default {
  data() {
    return {
      loading: false,
      excelData: {
        itemFile: [],
        worksheet: [],
        sheetNames: [] // 新增一个数组用于存储Sheet名称
      }
    }
  },
  methods: {
    generateDate({ itemFile, worksheet, sheetName }) {
      this.excelData.itemFile.push(itemFile)
      this.excelData.worksheet.push(worksheet)
      this.excelData.sheetNames.push(sheetName) // 将Sheet名称添加到数组中
      this.$emit('getSheetName', this.excelData.sheetNames)
    },
    handleDrop(e) {
      this.excelData.itemFile = []
      this.excelData.worksheet = []
      this.excelData.sheetNames = [] // 每次拖拽都清空一次
      e.stopPropagation()
      e.preventDefault()
      const files = e.dataTransfer.files
      for (let i = 0; i < files.length; i++) {
        const itemFile = files[i]
        this.readerData(itemFile)
      }
      this.$emit('on-selected-file', this.excelData)
    },
    handleDragover(e) {
      e.stopPropagation()
      e.preventDefault()
      e.dataTransfer.dropEffect = 'copy'
    },
    handleUpload() {
      this.excelData.itemFile = []
      this.excelData.worksheet = []
      this.excelData.sheetNames = [] // 每次点击“浏览”都清空一次
      document.getElementById('excel-upload-input').click()
    },
    handkeFileChange(e) {
      const files = e.target.files
      for (let i = 0; i < files.length; i++) {
        const itemFile = files[i]
        if (!itemFile) return
        this.readerData(itemFile)
      }
      this.$emit('on-selected-file', this.excelData)
      this.$refs['excel-upload-input'].value = null
    },
    readerData(itemFile) {
      const reader = new FileReader()
      reader.onload = e => {
        const data = e.target.result
        const fixedData = this.fixdata(data)
        const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
        for (const sheetName of workbook.SheetNames) {
          const worksheet = workbook.Sheets[sheetName]
          itemFile.fileName = itemFile.name
          itemFile.sheetName = sheetName
          worksheet.sheetName = sheetName
          // itemFile.name = sheetName
          this.generateDate({ itemFile, worksheet, sheetName })
        }
      }
      reader.readAsArrayBuffer(itemFile)
    },
    fixdata(data) {
      let o = ''
      let l = 0
      const w = 10240
      for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
      o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
      return o
    }
  }
}
</script>

<style scoped>
#excel-upload-input {
  display: none;
  z-index: -9999;
}

#drop {
  border: 2px dashed #bbb;
  width: 600px;
  line-height: 160px;
  margin: 0;
  font-size: 24px;
  border-radius: 5px;
  text-align: center;
  color: #bbb;
  position: relative;
}

</style>
