<template>
  <div>
    <el-dialog :title="title" :visible.sync="isShow" :append-to-body="true" @open="beforeDialogOpen" close="dialogClose" class="catalog-import-dlg">
      <el-form ref="form" label-width="25%">
        <el-form-item label="文件：">
          <el-upload
            class="upload-Image"
            ref="catalogUpload"
            action=''
            :limit="1"
            :on-change="selectCatalogChangeHandler"
            :before-upload="beforeCatalogUploadHandler"
            :http-request="catalogFileUploadHandler"
            :on-remove="removeCatalogFileHandler"
            :auto-upload="false"
            accept='.zdt'>
            <el-button slot="trigger" type="primary">选取文件</el-button>
            <div slot="tip" class="el-upload__tip">只能上传.zdt 文件</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" v-loading="submitLoading" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import util from '@src/common/util.js'
export default {
  props: {
    show: Boolean,
    title: String,
    catalogId: [Number, String],
    contentType: String
  },
  data() {
    return {
      submitLoading: false,
      fileList: [] //  上传文件
    }
  },
  computed: {
    isShow: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    }
  },
  methods: {
    dialogClose() {
      this.fileList = []
      this.$refs.catalogUpload.clearFiles()
    },
    beforeDialogOpen() {
      this.submitLoading = false
    },
    // 提交导入栏目
    confirm() {
      if (!this.fileList || this.fileList.length === 0) {
        this.$emit('dialog', '请选择要上传的文件')
        return
      }
      this.$refs.catalogUpload.submit()
    },
    // 文件列表移除文件
    removeCatalogFileHandler(file, files) {
      this.fileList = JSON.parse(JSON.stringify(files))
    },
    selectCatalogChangeHandler(file, files) {
      this.fileList = JSON.parse(JSON.stringify(files.slice(-1)))
      this.beforeCatalogUploadHandler(file)
    },
    beforeCatalogUploadHandler(file) {
      let extension = file.name.substring(
        file.name.lastIndexOf('.'),
        file.name.length
      )
      if (extension === '.zdt') {
        return true
      }
      this.$emit('dialog', '请上传.zdt格式的文件')
      return false
    },
    // 文件上传事件
    async catalogFileUploadHandler(event) {
      const { file, onProgress } = event
      const formData = new FormData()
      formData.append('file', file)
      formData.append('catalogID', this.catalogId)
      formData.append('contentType', this.contentType)
      try {
        this.submitLoading = true
        let res = await axios.post(
          '/ui/catalogs/import',
          formData,
          event => {
            onProgress(event, file)
          },
          {
            headers: { 'Content-Type': 'multipart/form-data' }
          }
        )
        this.submitLoading = false
        if (res.data.status === 1) {
          this.fileList = []
          this.$refs.catalogUpload.clearFiles()
          this.$emit('update:show', false)
          await util.showProgress(res.data.taskID, `正在${this.title}`)
        } else {
          util.showMessage(res)
        }
      } catch (e) {
        this.submitLoading = false
        // util.showErrorNotification(e)
      }
    }
  }
}
</script>
<style>
.catalog-import-dlg .el-dialog{
  width:400px;
}
@media (max-width:480px){
  .catalog-import-dlg .el-dialog{
    width:96%;
  }
}
</style>
