<template>
  <div style="margin-top:30px;margin-left:30px">
    <el-col :span="3"><auth-button type="primary" class="iconfont iconycxiazai" size="" @click="getFileId('chargebackListCode')" :label="$t('coordManageMain.tdqdDown')"></auth-button></el-col>
    <el-col :span="3"><auth-button type="primary" class="iconfont iconycxiazai" size="" @click="getFileId('loanListCode')" :label="$t('coordManageMain.yfkqdDown')"></auth-button></el-col>
    <el-col :span="3"><auth-button type="primary" class="iconfont iconycxiazai" size="" @click="getFileId('opinionList')" :label="$t('coordManageMain.yjbbDown')"></auth-button></el-col>
    <el-col :span="3"><auth-button type="primary" class="iconfont iconycxiazai" size="" @click="getFileId('allListCode')" :label="$t('coordManageMain.bqzbbDown')"></auth-button></el-col>
  </div>
</template>
<script>
import { authButton } from 'newepoch-components'
import fileApi from '@/api/fileApi.js'
import { downFile } from '@/utils/devUtils'
import coordManageApi from '@/api/assetoutfeedback/coordManage/coordManageApi.js'
export default {
  name: 'relationReportSub',
  components: {
    authButton
  },
  props: {
    projectInfo: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      uuidSwitch: false
    }
  },
  computed: {
    coordManageApi () {
      return coordManageApi.opnionListDown
    }
  },
  methods: {
    // 获取下载文件id
    getFileId (type) {
      if (type !== 'opinionList') {
        let data = {
          createType: 'CREATE',
          map: {
            projectId: this.projectInfo.projectId,
            periods: this.projectInfo.periods
          },
          templateCode: type
        }
        fileApi.file.templateFile(data).then(res => {
          if (res.success && res.data && res.data.keyId) {
            let fileID = res.data.keyId
            this.downloadSuccess(fileID)
          } else {
            this.warning('没有可以下载的数据')
          }
        })
      } else {
        this.coordManageApi.getDefault(this.projectInfo).then(res => {
          if (res.success && res.data) {
            let fileID = res.data
            this.downloadSuccess(fileID)
          } else {
            this.warning('没有可以下载的数据')
          }
        })
      }
    },
    // 下载
    downloadSuccess (fileID) {
      if (this.UtilJs.isEmpty(fileID)) {
        this.warning(this.noDownloadFileMsg)
      } else {
        if (this.uuidSwitch) {
          fileApi.file.downloadByUUID(this.$xhr, { uuid: fileID }).then(res => {
            downFile(res)
          })
        } else {
          fileApi.file.download({ keyId: fileID }).then(res => {
            downFile(res)
          })
        }
      }
    }
  }
}
</script>
