// 供应商上传文件
<template>
  <data-table operateBtnLayout
              :columns="columns"
              :showPage="true"
              :showSelection="true"
              :api="api"
              :sourceData="sourceData"
              :loadCount.sync="loadCount"
              :queryParas="queryParas"
              @selectRowChange="selectRowChange">
    <template slot="headButton">
      <div class="head_button_right">
        <!-- 批量下载 -->
        <btnBatchDownload :xhr="$xhr"
                          :store="$store"
                          icon="iconfont iconxiazai"
                          size="medium"
                          type="primary"
                          class="margin_left10"
                          :fileID="fileIds"
                          :classType="true"
                          :downloadButtonMsg="$t('common.batchDownload')"
                          :menuCode="menuId"
                          perms="download"
                          :beforeBtchDownload="beforeBtchDownload"
                          :noDownloadFileMsg="$t('message.noDownloadFileMsg')">
        </btnBatchDownload>
      </div>
    </template>
    <!-- 操作列 -->
    <el-table-column align="center" label="操作" width="120px">
      <template slot-scope="scope">
        <!-- 下载 -->
        <btn-download class="iconCircle"
                      :fileID="scope.row.fileId"
                      :classType="true">
        </btn-download>
      </template>
    </el-table-column>
  </data-table>
</template>
<script>
import BasePage from '@/pages/basePage'
import { DataTable, authButton, btnDownload, btnBatchDownload } from 'newepoch-components'
import fileApi from '@/api/fileApi.js'
import { downFile } from '@/utils/devUtils'
export default {
  extends: new BasePage(),
  components: {
    DataTable,
    authButton,
    btnDownload,
    btnBatchDownload
  },
  props: {
    query: {
      type: Object,
      required: true,
      default () {
        return {}
      }
    },
    api: {
      type: Object,
      required: true,
      default () {
        return {}
      }
    }
  },
  mounted () {
    this.queryParas.assetInfoId = this.query.assetInfoId
    this.queryParas.assetPoolId = this.query.assetPoolId
    this.api.getPage(this.queryParas).then(res => {
      if (res && res.data.datas) {
        this.sourceData = res.data.datas
      }
    })
  },
  data () {
    const { factoringBusinessInfo } = this.$i18n.messages[this.$i18n.locale]
    return {
      fileIds: [],
      sourceData: [],
      columns: [
        // 文件名称
        {
          prop: 'fileName',
          align: 'left',
          width: '110px',
          label: factoringBusinessInfo.file_name
        }
      ]
    }
  },
  methods: {
    selectRowChange (selection) {
      this.fileIds = []
      selection.forEach(item => {
        this.fileIds.push(item.fileId)
      })
    },
    // 批量下载特殊处理，勾选一条数据点击批量下载时调用下载接口，不生成压缩包
    beforeBtchDownload () {
      if (this.fileIds.length === 1) {
        fileApi.file.download({ keyId: this.fileIds[0] }).then(res => {
          downFile(res)
        })
        return false
      } else {
        return true
      }
    }
  },
  watch: {
    query (val) {
      this.queryParas.assetInfoId = val.assetInfoId
      this.loadCount += 1
    }
  }
}
</script>
