<template>
  <a-card :class="$style.initDataCard">
    <h2 :class="$style.title">初始化运维工具</h2>
    <a-form :label-col="{ span: 6 }">
      <a-form-item v-for="(item, index) in initInfo" :key="index" :label="item.name">
        <a-button
          v-if="item.buttons.includes('export')"
          :class="$style.btn"
          :loading="item.exportLoading"
          @click="exportData(item)"
          >导出</a-button
        >
        <a-button v-if="item.buttons.includes('import')" @click="importData(item, index)"
          >导入</a-button
        >
        <a-button
          v-if="item.buttons.includes('spicImportExcel')"
          @click="spicImportData(item, index)"
          >历史项目整改反馈数据导入处理</a-button
        >
      </a-form-item>
    </a-form>
    <div :class="[$style.btns]">
      <audit-advanced-import
        ref="sjzgImport"
        :config-id="'172'"
        :table-id="'dataTable'"
        :v-if="false"
      >
      </audit-advanced-import>
    </div>
    <a-modal
      v-model="isSingleImport"
      :title="modalTitle"
      destroy-on-close
      v-bind="{ width: 800 }"
      :mask-closable="false"
    >
      <template slot="footer">
        <a-button
          key="cancel"
          type="primary"
          :disabled="attachmentCode ? false : true"
          @click="singleImport"
        >
          确定
        </a-button>
        <a-button key="sure" @click="cancelModal">
          取消
        </a-button>
      </template>
      <a-form :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="模板文件" :required="true">
          <sd-attachment
            :group-id="groupIdCurrent"
            :max="1"
            @change="getAttachmentCode"
          ></sd-attachment>
        </a-form-item>
      </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>
  </a-card>
</template>

<script>
import { Message } from 'ant-design-vue'
import download from '@/common/services/download'
import auditAdvancedImport from '../../src_product/iam/components/audit-advanced-import.vue'
import components from './_import-components/sd-init-operate-tools-import'
import WorkbenchService from './workbench-service'

export default {
  name: 'SdInitOperateTools',
  metaInfo: {
    title: '初始化运维工具',
  },
  components: { ...components, auditAdvancedImport },
  data() {
    return {
      initInfo: [
        {
          name: '业务类型',
          formId: 'oaBaseBusinessTypeCat',
          buttons: ['import', 'export'],
          exportLoading: false,
        },
        {
          name: '发文编号',
          formId: 'oaDocNumberConfig',
          buttons: ['import', 'export'],
          exportLoading: false,
        },
        {
          name: '自动编号',
          formId: 'oaAutonumber',
          buttons: ['import', 'export'],
          exportLoading: false,
        },
        {
          name: '流程业务属性配置',
          formId: 'oaFlowAttr',
          buttons: ['import', 'export'],
          exportLoading: false,
        },
        {
          name: '正文模版',
          formId: 'oaRedhead',
          buttons: ['import', 'export'],
          exportLoading: false,
        },
        {
          name: '历史整改数据导入',
          formId: 'spicImportExcel',
          buttons: ['spicImportExcel'],
          exportLoading: false,
        },
      ],
      isSingleImport: false, // 导入弹窗
      modalTitle: '',
      currentIndex: '',
      groupIdCurrent: '',
      attachmentCode: '',
      isErrorInfo: false,
      errorInfo: [], // 导入数据错误信息
    }
  },
  methods: {
    // 历史整改数据导入
    spicImportData(item, index) {
      this.$refs.sjzgImport.showImportPanel()
    },
    // 导出数据
    exportData(item) {
      item.exportLoading = true
      if (item.formId === 'oaBaseBusinessTypeCat') {
        download(`api/framework/v1/export/tree?formId=${item.formId}`, `${item.name}.gz`).then(
          () => {
            item.exportLoading = false
          }
        )
      } else {
        download(`api/framework/v1/export/list?formId=${item.formId}`, `${item.name}.gz`).then(
          () => {
            item.exportLoading = false
          }
        )
      }
    },
    // 导入数据弹窗
    importData(item, index) {
      WorkbenchService.getGroupCode().then((res) => {
        if (res.status === 200) {
          if (res.data) {
            this.groupIdCurrent = res.data.toString()
            this.modalTitle = `${item.name}导入`
            this.currentIndex = index
            this.isSingleImport = true // 弹出导入面板
          } else {
            Message.error('获取groupId失败')
          }
        } else {
          Message.error(res.statusText)
        }
      })
    },
    // 获取上传文件code值
    getAttachmentCode(arr) {
      this.attachmentCode = arr[0].code
    },
    singleImport() {
      const currentInfo = this.initInfo[this.currentIndex]
      if (currentInfo.formId === 'oaBaseBusinessTypeCat') {
        WorkbenchService.importTreeData({
          formId: currentInfo.formId,
          fileId: this.attachmentCode,
        }).then((res) => {
          if (res.data.length === 0) {
            Message.success('导入成功')
            this.isSingleImport = false
          } else {
            this.errorInfo = res.data
            this.isSingleImport = false
            this.isErrorInfo = true
          }
        })
      } else {
        WorkbenchService.importListData({
          formId: currentInfo.formId,
          fileId: this.attachmentCode,
        }).then((res) => {
          if (res.data.length === 0) {
            Message.success('导入成功')
            this.isSingleImport = false
          } else {
            this.errorInfo = res.data
            this.isSingleImport = false
            this.isErrorInfo = true
          }
        })
      }
    },
    cancelModal() {
      this.attachmentCode = ''
      this.isSingleImport = false
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.init-data-card {
  .title {
    text-align: center;
  }
  .btn {
    margin: 5px;
  }
}
</style>
