<!--下载模板导入-->
<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" class="zIndex-1100">
      <p slot="header">{{modalTitle}}</p>
      <Alert v-if="showError" type="error" show-icon style="margin-top: 10px">{{errorMessage}}</Alert>
      <Alert v-if="showSuccess" type="success" show-icon style="margin-top: 10px">{{successMessage}}</Alert>
      <div v-show="isLoad">
        <Spin >
          <Icon type="load-c" size=18 class="file-spin-icon-load"></Icon>
          <div>正在导入,请稍后...</div>
        </Spin>
      </div>
      <div>
        <label>
          <Upload
            :action="importUrl"
            :on-success="_handelsuccess"
            :before-upload="_beupload"
            :on-progress="_handelprogress"
            :on-format-error="_formatError"
            :data="dataObj"
            :with-credentials="true"
            :show-upload-list="false"
            :format="format"
          >
            <Button :disabled="isDisabled" type="primary">上传</Button>
          </Upload>
        </label>
      </div>
      <div slot="footer" class="btn-width">
        <Button @click="_cancel" :loading="isLoad">取消</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 导入并下载模板
 */
import global from '../../../api/config'
export default {
  components: {},
  data() {
    return {
      modalTitle: '上传文件',
      showModal: false,
      isLoad: false,
      isDisabled: false,
      showError: false,
      showSuccess: false,
      dataObj: {
        file: ''
      },
      errorMessage: '',
      successMessage: '',
      format: ['pdf', 'PDF'],
      importUrl: ''
    }
  },
  methods: {
    _open(data) {
      this.isLoad = false
      this.showModal = true
      this.isDisabled = false
      this.showError = false
      this.showSuccess = false
      this.importUrl = global.baseURL + data.importUrl
    },
    _cancel() {
      this.showModal = false
    },
    _beupload(file) {
      this.dataObj.file = file.name
    },
    _handelprogress(event, file, fileList) {
      this.isLoad = true
      this.isDisabled = true
    },
    _handelsuccess(response, file, fileList) {
      // 上传成功
      if (response.success) {
        this.isLoad = false
        this.$Message.success('导入成功！')
        this.$emit('on-result-change')
        this.showModal = false
        this.isDisabled = false
        this.showError = false
        this.showSuccess = true
        this.successMessage = response.msg
      } else {
        this.isLoad = false
        this.showSuccess = false
        this.isDisabled = false
        this.errorMessage = response.msg
        this.showError = true
        this.$Message.error(response.msg)
      }
    },
    _formatError(file) {
      this.$Notice.error({
        title: '文件格式不正确',
        duration: 10,
        desc: '文件 ' + file.name + ' 格式不正确，请上传格式为 pdf 格式的文件！'
      })
      this.isLoad = false
    }
  }
}
</script>
