<template>
  <el-dialog :title="dialogtitle" :visible.sync="dialogVisible" width="500px" :before-close="handleClose" center>
    <div class="upload">
      <div class="upload-left">导入文件</div>
      <el-upload class="upload-right" ref="upload" accept=".xls,.xlsx" :action="fileApi" :file-list="fileList" :data="fileData" :auto-upload="false" :on-success="onSuccess" :limit="1">
        <el-button slot="trigger" style="margin-right: 30px" size="small" type="primary">选择文件</el-button>
        <el-button size="small" type="success" @click="submitUpload">导入数据</el-button>
        <div slot="tip" class="el-upload__tip" style="margin-left: 10px; margin-top: 10px; font-size: 14px">只能导入 xls / xlsx 文件</div>
      </el-upload>
    </div>
  </el-dialog>
</template>

<script>
import { useMessage } from '@/utils/element-ui'
export default {
  data() {
    return {
      dialogtitle: '导入定检任务信息',
      dialogVisible: false,
      fileApi: process.env.VUE_APP_BASE_API + '/als/smTask/importData',
      fileList: [],
      fileData: {
        userName: ''
      }
    }
  },
  methods: {
    triggerDialog() {
      this.dialogVisible = true
    },

    handleClose() {
      this.dialogVisible = false
    },

    // 上传
    submitUpload() {
      const userInfo = this.$store.state.user.userInfo.user
      this.fileData.userName = userInfo.userName
      this.$refs.upload.submit()
    },

    // 上传完成后
    onSuccess(result) {
      this.$refs.upload.clearFiles()
      this.fileList = []
      const { msg, code } = result
      useMessage(code == 200 ? 'success' : 'warning', msg, 2000)
      if (code !== 200) return
      this.handleClose()
      this.$emit('submitCallBack')
    }
  }
}
</script>
<style lang="scss" scoped>
.el-dialog__wrapper {
  width: 100%;
}
.upload {
  height: 106px;
  padding-top: 30px;
  display: flex;
  &-left {
    margin-right: 40px;
  }
}
</style>
