<template>
  <span>
    <a-modal
      ref="icicmatrixExportModal"
      :body-style="bodyStyle"
      :title="title"
      :destroy-on-close="true"
      :visible="visible"
      :width="modalWidth"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-form>
        <div style="width:80%;margin-left: 10%;;margin-left: 5%;">
          <p>提示：填写分类层级，导出不带数据的模板；请填写2到10的层级数值。 </p>
          <p
            >导出分类层级：<a-input-number
              id="level"
              v-model="level"
              :min="2"
              :max="10"
              style="width:60%;"
            />
          </p>
          <p>
            <a @click="exportItemTemplate">
              内控矩阵导入模板
            </a>
          </p>
          <p>说明 </p>
          <p>1）增量式导入，只导入新增数据；</p>
          <p>2）修改式导入，对存在数据进行修改；</p>
          <p>2）覆盖式导入，删除已经存在的数据，重新导入；</p>
          <p
            >导入模式:
            <a-select v-model="importType" style="width:40%;" @change="changedImportType">
              <a-select-option checked="true" value="over">覆盖式导入</a-select-option>
              <a-select-option value="inter">增量式导入</a-select-option>
              <a-select-option value="update">修改式导入</a-select-option>
            </a-select>
          </p>
          <a-form-item>
            <span v-if="Message !== ''" style="padding:0 15px">
              {{ Message }}
            </span>
            <br v-if="Message !== '' && !hasattach" />

            <sd-attachment :max="1" :group-id="groupId" accept=".xls,.xlsx" @change="fnchangefile">
            </sd-attachment>
          </a-form-item>
        </div>
      </a-form>
    </a-modal>
    <a-modal
      v-model="isErrorInfo"
      title="错误信息"
      :width="700"
      @ok="isErrorInfo = false"
      @cancel="isErrorInfo = false"
    >
      <a-descriptions bordered>
        <a-descriptions-item label="错误信息">
          <p v-for="(item, index) in errorInfo" :key="index">{{ item }}</p>
        </a-descriptions-item>
      </a-descriptions>
    </a-modal>
  </span>
</template>

<script>
import { Modal, Message } from 'ant-design-vue'
import download from '@/common/services/download'
import MtxProcessService from './mtx-process-service'
import components from './_import-components/mtx-process-export-modal-import'

export default {
  name: 'MtxProcessExportModal',
  metaInfo: {
    title: 'MtxProcessExportModal',
  },

  components,
  props: {
    // 弹出窗标题
    title: {
      type: String,
      default: '内控矩阵导入',
    },
    // 弹出窗宽度
    modalWidth: {
      type: String,
      default: '700px',
    },
    // 弹出窗显示参数
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      bodyStyle: {
        padding: 0,
      },
      level: 2,
      errorInfo: [],
      isErrorInfo: false,
      groupId: Math.round(Math.random() * 100000000000000000).toString(),
      hasattach: false,
      Message: '',
      importType: 'inter',
    }
  },
  created() {},
  methods: {
    handleOk(e) {
      if (!this.hasattach) {
        // 未上传附件，给出提示
        Message.error('请上传文件！')
        return
      }
      // 导入
      const groupId = this.groupId
      const orgId = this.$parent.$parent.$refs.icmatrixCatalogTree.depvalue
      const versionId = this.$parent.$parent.$refs.icmatrixCatalogTree.versionId
      MtxProcessService.importItemTemplate(this.importType, groupId, versionId, orgId).then(
        (res) => {
          if (res.data.length > 0) {
            Message.error('导入数据失败！')
            this.isErrorInfo = true
            this.errorInfo = res.data
            this.$emit('import', false)
            return false
          } else {
            Message.success('导入成功！')
            this.hasattach = false
            this.groupId = Math.round(Math.random() * 100000000000000000).toString()
            this.level = 2
            this.$parent.$parent.visible = !this.$parent.$parent.visible
            this.$emit('import', true)
          }
        }
      )
    },
    handleCancel(e) {
      this.hasattach = false
      this.groupId = Math.round(Math.random() * 100000000000000000).toString()
      this.$parent.$parent.visible = !this.$parent.$parent.visible
      this.level = 2
    },
    changedImportType() {},
    exportItemTemplate() {
      MtxProcessService.exportItemTemplate(this.level).then((res) => {
        if (res.status === 200) {
          const url = URL.createObjectURL(res.data)
          const filename = res.headers['content-disposition']
          const fname = filename.substring(filename.indexOf('filename=') + 9, filename.length)
          download(url, decodeURI(fname))
        } else {
          Modal.warning({
            title: '提示',
            content: '导出报错，请联系管理员!',
          })
          return false
        }
      })
    },
    fnchangefile(attach) {
      if (attach.length > 0) {
        this.hasattach = true
      } else {
        this.hasattach = false
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
