// 供应商上传文件
<template>
  <data-table operateBtnLayout
              :columns="columns"
              :showPage="true"
              :showSelection="false"
              :api="api"
              :sourceData="sourceData"
              :loadCount.sync="loadCount"
              :queryParas="queryParas">
    <template slot="headButton">
      <div class="head_button_right">
        <!-- 文件上传 -->
        <btn-upload :store="$store"
                    btnSize="small"
                    :btnDisplay="true"
                    :menuCode="menuId"
                    perms="import_refunds"
                    :uploadName="$t('common.fileUpload')"
                    :uploadApi="uploadApi"
                    @uploadSuccess="uploadSuccess"
                    type="primary">
        </btn-upload >
      </div>
    </template>
    <!-- 操作列 -->
    <el-table-column align="center" label="操作" width="300px">
      <template slot-scope="scope">
        <!-- 重新上传 -->
        <btn-upload :store="$store"
                    btnSize="small"
                    :btnDisplay="true"
                    :menuCode="menuId"
                    :classType="true"
                    perms="import_refunds"
                    :title="$t('common.reUpload')"
                    :uploadApi="uploadApi"
                    :rowData="scope.row"
                    @uploadSuccess="reUploadSuccess">
        </btn-upload>
        <!-- 下载 -->
        <btn-download :xhr="$xhr"
                      :store="$store"
                      icon="iconfont iconxiazai"
                      size="small"
                      :fileID="scope.row.fileId"
                      class="iconCircle"
                      :title="$t('common.downLoad')"
                      :menuCode="menuId"
                      perms="download">
        </btn-download>
        <!-- 删除 -->
        <auth-button size="small"
                     :icon="'iconfont iconjian'"
                     :title="$t('common.del')"
                     class="iconCircleDel"
                     @click="del(scope.row.keyId)"
                     :menuCode="menuId"
                     perms="completion_refund">
        </auth-button>
      </template>
    </el-table-column>
  </data-table>
</template>
<script>
import BasePage from '@/pages/basePage'
import { DataTable, authButton, btnDownload, btnImport, btnUpload } from 'newepoch-components'
import { UPLOAD_API } from '@/constant.js'
import factoringBusinessInfoApi from '@/api/factoringBusinessInfoApi'
export default {
  extends: new BasePage(),
  components: {
    DataTable,
    authButton,
    btnDownload,
    btnImport,
    btnUpload
  },
  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 {
      fileID: '',
      uploadApi: UPLOAD_API,
      sourceData: [],
      columns: [
        // 文件名称
        {
          prop: 'fileName',
          align: 'left',
          width: '110px',
          label: factoringBusinessInfo.file_name
        }
      ]
    }
  },
  methods: {
    // 删除
    del (keyId) {
      let $this = this
      $this.$confirm('此操作会删除该文件，是否继续？', '提示', {
        type: 'warning'
      }).then(() => {
        factoringBusinessInfoApi.assetApi.fileDelete(keyId).then(res => {
          if (res && res.success) {
            this.success('删除文件成功')
            this.loadCount += 1
          }
        })
      }, () => {})
    },
    // 文件上传成功回调
    uploadSuccess (returnData, rowData, fileList) {
      let fileName = returnData.fileName.substring(0, returnData.fileName.indexOf('.'))
      factoringBusinessInfoApi.assetApi.fileUpload({ assetInfoId: this.query.assetInfoId, fileId: returnData.fileID, fileName: fileName }).then(res => {
        if (res && res.success) {
          this.success('上传文件成功！')
          this.loadCount += 1
        }
      })
    },
    // 重新上传成功回调
    reUploadSuccess (returnData, rowData, fileList) {
      let fileName = returnData.fileName.substring(0, returnData.fileName.indexOf('.'))
      factoringBusinessInfoApi.assetApi.fileReupload({ assetInfoId: this.query.assetInfoId, keyId: rowData.keyId, fileId: returnData.fileID, fileName: fileName }).then(res => {
        if (res && res.success) {
          this.success('重新上传文件成功！')
          this.loadCount += 1
        }
      })
    }
  },
  watch: {
    query (val) {
      this.queryParas.assetInfoId = val.assetInfoId
      this.queryParas.assetPoolId = val.assetPoolId
      this.loadCount += 1
    }
  }
}
</script>
