<template>
  <span>
    <a-modal
      :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>
          <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 auditMattersService from './iam-audit-source-problem-import-service'
import components from './_import-components/iam-audit-source-problem-import-modal-import'

export default {
  name: 'IamAuditSourceProblemImportModal',
  components,
  props: {
    // 弹出窗标题
    title: {
      type: String,
      default: '导入审计问题',
    },
    // 弹出窗宽度
    modalWidth: {
      type: String,
      default: '700px',
    },
    // 弹出窗显示参数
    visible: {
      type: Boolean,
      default: false,
    },
    // 父节点id
    parentId: {
      type: String,
      default: null,
    },
  },
  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.problemTree.depvalue

      auditMattersService.importItemTemplate(this.parentId, groupId, orgId).then((res) => {
        if (res.data.length > 0) {
          Message.error('导入数据失败！')
          this.isErrorInfo = true
          this.errorInfo = res.data
          return false
        } else {
          Message.success('导入成功！')
          this.hasattach = false
          this.groupId = Math.round(Math.random() * 100000000000000000).toString()
          this.level = 2
          this.$parent.$parent.showImportModal()
        }
      })
    },
    handleCancel(e) {
      this.hasattach = false
      this.groupId = Math.round(Math.random() * 100000000000000000).toString()
      this.$parent.$parent.importVisible = !this.$parent.$parent.importVisible
      this.level = 2
    },
    changedImportType() {},
    exportItemTemplate() {
      auditMattersService.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>
