<template>
  <el-dialog :visible.sync="show" class="ems-dialog" title="点位上传" width="400px" append-to-body
             :close-on-click-modal="false" :close-on-press-escape="false">
    <!--    <file-upload :fileType='["xls", "xlsx"]' value="String" :limit="1" :drag="false"  @input="updateFile"/>-->
    <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
               :data="upload.data" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress"
               :on-success="handleFileSuccess" :auto-upload="false" drag>
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip text-center" slot="tip">
        <span>仅允许导入xls、xlsx格式文件。</span>
        <!--        <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>-->
      </div>
    </el-upload>
    <div slot="footer">
      <el-button @click="handleClosed">取消</el-button>
      <el-button type="primary" @click="submitFileForm">确定</el-button>
    </div>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
<script>
import {getToken} from "@/utils/auth"

export default {
  data() {
    return {
      show: false,
      // 用户导入参数
      upload: {
        data: {},
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/ems/pointMatch/importDataByDevice"
      },
    }
  },
  methods: {
    showDialog({siteId, deviceCategory, deviceId}) {
      this.upload.data = {siteId, deviceCategory, deviceId}
      this.show = true;
    },
    //关闭弹窗 重置数据
    handleClosed() {
      this.$refs.upload.clearFiles()//写在before-closed无效
      this.upload.data = {}
      this.show = false
    },
    // updateFile(data){
    //   console.log('上传文件emit',data)
    // },
    // saveDialog(){
    //  this.show=true
    // },
    // 文件上传中处理 返回参数event, file, fileList
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理 返回参数response, file, fileList
    handleFileSuccess(response) {
      console.log('上传文件结果', response)
      this.upload.isUploading = false
      if (response.code === 500 && !response.msg) response.msg = '上传失败'
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + (response.msg || '上传成功') + "</div>", "导入结果", {dangerouslyUseHTMLString: true})
      this.$emit('update')
      this.handleClosed()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    }
  }
}
</script>