<!-- 资料下载 -->
<template>
  <div class="assetFileDownPop">
    <!-- 归档资料下载 -->
    <externalTransition class="externalFileDown" :isCollapse="true" :title="$t('feedBackMain.dataDownload')">
      <template slot="transition">
        <data-table :sourceData.sync="tempData"
                    :columns="archiveColumns"
                    :queryParas="queryParas"
                    ref="dataTable"
                    v-bind="{maxHeight:400}">
          <template slot="headButton">
             <div align="right">
                <auth-button :label="$t('effectDealFeedBackMain.oneClickDownload')" type="primary" size="medium" @click="downloadAll"></auth-button>
             </div>
          </template>
          <el-table-column :label="$t('effectDealFeedBackMain.fileDown')" align="center" width="120px" fixed="right">
            <template slot-scope="scope">
              <btn-download size="small" :title="$t('common.downLoad')" :fileID="scope.row.ossId"></btn-download>
            </template>
          </el-table-column>
        </data-table>
      </template>
      </externalTransition>
      <!-- 原始资料下载 -->
      <externalTransition class="externalFileDown" :isCollapse="true" :title="$t('effectDealFeedBackMain.sourceMaterialDownload')">
        <template slot="transition">
          <data-table :sourceData.sync="originalData" :columns="originalColumns" :api="api" :loadCount="loadCount" :queryParas.sync="queryParas" :loadPage="'getDefaultfile'" v-bind="{maxHeight:400}">
            <el-table-column :label="$t('effectDealFeedBackMain.fileDown')" align="center" width="120px">
              <template slot-scope="scope">
                <btn-download :disabled="scope.row.fileId === ''" size="small" :title="$t('common.downLoad')" :fileID="scope.row.fileId"></btn-download>
              </template>
            </el-table-column>
          </data-table>
        </template>
      </externalTransition>
      <!-- 项目公司和供应商的共用文件下载 -->
      <!-- <externalTransition class="externalFileDown" :isCollapse="true" :title="$t('effectDealFeedBackMain.xmgsAndgysFileDown')" :color="true">
        <template slot="transition"> -->
          <!-- <data-table :sourceData.sync="xmgsAndgysData" :columns="xmgsAndgysColumns">
            <template slot="headButton">
              <div align="right">
                  <btn-batch-download :fileID="fileID" type="primary" :downloadButtonMsg="'一键下载'" :classType="classType"></btn-batch-download>
              </div>
            </template>
            <el-table-column :label="'文件下载'" align="center" width="120px" fixed="right">
              <template>
                <btn-download size="small" :title="$t('common.downLoad')" :fileID="'132'"></btn-download>
              </template>
            </el-table-column>
          </data-table> -->
        <!-- </template>
      </externalTransition> -->
  </div>
</template>

<script>
import externalTransition from '@/components/element/assetoutfeedback/externalTransition'
import effectDealFeedBackApi from '@/api/assetoutfeedback/effectDealFeedBack/effectDealFeedBackApi'
import componentsApi from '@/api/assetoutfeedback/componentsApi'
import { DataTable, authButton, btnDownload, btnBatchDownload } from 'newepoch-components'
import fileApi from '@/api/fileApi.js'
import { downFile } from '@/utils/devUtils'
export default {
  components: { DataTable, authButton, btnDownload, externalTransition, btnBatchDownload },
  props: {
    form: {
      type: Object,
      default () {
        return {}
      }
    },
    fileForm: {
      type: Object,
      default () {
        return {}
      }
    },
    editModel: {
      type: String,
      default () {
        return ''
      }
    },
    assetInfoId: {
      type: String,
      default () {
        return ''
      }
    }
  },
  computed: {
    baseForm: {
      get: function () {
        return this.form
      }
    },
    api () {
      return effectDealFeedBackApi.getComOriginal
    },
    danganApi () {
      return effectDealFeedBackApi.getDanganData
    },
    danganPageApi () {
      return effectDealFeedBackApi.getDanganPageData
    },
    fileListApi () {
      return componentsApi.fileList
    },
    tempData: {
      get: function () {
        return this.archiveData.slice((this.queryParas.pageIndex - 1) * this.queryParas.pageSize, this.queryParas.pageIndex * this.queryParas.pageSize)
      },
      set: function () {
        return this.archiveData.slice((this.queryParas.pageIndex - 1) * this.queryParas.pageSize, this.queryParas.pageIndex * this.queryParas.pageSize)
      }
    }
  },
  data () {
    return {
      queryParas: {
        pageIndex: 1,
        pageSize: 10
      },
      loadCount: 0,
      classType: true,
      archiveData: [],
      originalData: [],
      xmgsAndgysData: [],
      fileAllID: [],
      fileAllType: [],
      archiveColumns: [
        {
          // 文件名称
          prop: 'fileName',
          align: 'left',
          width: '50px',
          label: this.$t('effectDealFeedBackMain.fileName')
        }, {
          // 归档份数
          prop: 'archiveNum',
          align: 'center',
          width: '50px',
          label: this.$t('effectDealFeedBackMain.archiveNum')
        }
      ],
      originalColumns: [
        {
          // 文件名称
          prop: 'fileName',
          align: 'left',
          width: '160px',
          label: this.$t('effectDealFeedBackMain.fileName')
        }
      ]
    }
  },
  mounted () {
    let arr = []
    if (this.editModel === 'I' || this.editModel === 'IS') {
      if (this.assetInfoId === '') {
        arr.push(this.form.notImplementAssetInfoDto.assetInfoId)
      } else {
        arr.push(this.assetInfoId)
      }
    } else {
      arr.push(this.form.assetInfoId)
    }
    // arr = ['2005211629233400001']
    Object.assign(this.queryParas, { assetInfoIds: arr })
    this.$nextTick(() => {
      this.handleFilter()
      this.getFile(this.queryParas)
    })
  },
  methods: {
    // 更新表数据
    handleFilter () {
      this.loadCount = this.loadCount + 1
    },
    // 一键下载
    downloadAll () {
      this.downloadFile(this.fileAllID)
    },
    // 获取其他文件fileCode
    getFileCode (fileTypes) {
      let fileId = ''
      return new Promise((resolve, reject) => {
        this.fileListApi.getList({ fileQueryType: '4', stampWay: '3' }).then(res => {
          if (res.success && res.data && res.data.length > 0) {
            res.data.forEach(item => {
              if (item.fileType === this.fileForm.auditFileType) {
                fileId = item.fileId
              }
            })
            resolve(fileId)
          } else {
            reject(new Error('error'))
          }
        })
      })
    },
    // 获取归档资料查询条件
    async getFile (params) {
      let fileTypes = []
      let fileCodes = []
      if (this.fileForm.auditFileType === '1001') {
        fileTypes = ['0']
      } else if (this.fileForm.auditFileType === '2001') {
        fileTypes = ['1', '2']
      } else if (this.fileForm.auditFileType === '2002') {
        fileTypes = ['1']
      } else if (this.fileForm.auditFileType === '2003') {
        fileTypes = ['2']
      } else {
        fileTypes = ['3']
        fileCodes = await this.getFileCode(this.fileForm.auditFileType)
      }
      params.fileTypes = fileTypes
      params.fileCodes = fileCodes
      this.loadTable1Data(params)
    },
    loadTable1Data (params) {
      this.danganApi.getDefaultfile(params).then(res => {
        if (res.success && res.data && res.data.length > 0) {
          let tableData = []
          res.data.forEach(item => {
            if (item.fileInfos.length > 0) {
              item.fileInfos.forEach(it => {
                this.fileAllID.push(it.ossId)
                tableData.push(Object.assign(it, { assetInfoId: item.assetInfoId }))
              })
            }
          })
          this.$refs.dataTable.currentTotal = tableData.length
          this.archiveData = tableData
        }
      })
    },
    // 下载
    downloadFile (fileID) {
      if (fileID.length === 0) {
        this.warning(this.$t('common.noDownloadFileMsg'))
      } else {
        if (fileID.length > 0) {
          fileApi.file.batchDownload({ keyIds: fileID }).then(res => {
            downFile(res)
          })
        } else {
          this.warning(this.$t('common.noDownloadFileMsg'))
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  @import '~@/styles/assetoutfeedback/common.scss'
</style>
