<!--保理资料管理-->
<template>
  <div>
    <up-down-layout :header="$t('factorDataManageList.head')" :handleFlag.sync="handleFlag">
      <search-form :formValue.sync="queryParas"
                   searchPerms="search"
                   @handleFilter="handleFilter"
                   @clearParam="clearParam"
                   slot="query"
                   resetPerms="reset"
                   :componentList="componentList"></search-form>
      <template slot="table">
        <data-table :api="api" :deleteApi="api"
                    :headTitle = "$t('factorDataManageList.tableHead')"
                    :showSelection="true"
                    selectable="false"
                    :handleFlag.sync="handleFlag"
                    :menuFlag="true"
                    v-bind="elHeightObj"
                    :queryParas="queryParas"
                    :sourceData.sync="sourceData"
                    :loadCount.sync="loadCount"
                    :columns="columns"
                    :otherHeight="'240'"
                    operateBtnLayout=""
                    :IsRight="false"
                    :pageAll="true"
                    :operatWidth="'200'"
                    @selectRowChange="selectRowChange">
          <template slot="headButton">
            <div align="right">
              <!--生成资料-->
              <auth-button size="medium" type="primary" icon="iconfont iconshengcheng1" :loading="generateMaterialLoading"
                           :openPerms="true" :menuCode="menuId" perms="btnGenerateMaterial"
                           :label="$t('common.generateMaterial')" @click="generateData"></auth-button>
              <!--下载资料-->
              <auth-button size="medium" type="primary" icon="iconfont iconxiazai1" :loading="materialDownloadLoading"
                           :openPerms="true" :menuCode="menuId" perms="btnMaterialDownload"
                           :label="$t('common.materialDownload')" @click="downLoad"></auth-button>
            </div>
          </template>
        </data-table>
      </template>
    </up-down-layout>
    <generate-result :visible.sync="resultVisible" :form="formItem" editModel="V"></generate-result>
  </div>
</template>
<script>
import { searchForm, DataTable, upDownLayout, authButton, SelectCoreEnterprise, SelectFinancialInstitutions, SelectPrimitiveRightsHolder } from 'newepoch-components'
import factorDataManageApi from '@/api/factorDataManageApi'
import fileApi from '@/api/fileApi'
import BasePage from '@/pages/basePage'
import * as format from '@/filters/index'
import { downFile } from '@/utils/devUtils'
import mixin from '@/pages/mixin'
import generateResult from '@/pages/shourang/common/generateResult'

export default {
  extends: new BasePage(),
  mixins: [mixin],
  name: 'factorDataManage',
  components: {
    searchForm,
    upDownLayout,
    DataTable,
    authButton,
    generateResult
  },
  data () {
    return {
      resultVisible: false,
      formItem: {},
      reMsg: this.$i18n.messages[this.$i18n.locale].factorDataManageList,
      generateMaterialLoading: false,
      materialDownloadLoading: false,
      queryParas: {},
      handleFlag: true,
      componentList: [
        {
          // 供应商名称
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.suppliersName,
          disabled: false,
          value: 'creditor',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 付款确认书编号
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.payCode,
          disabled: false,
          value: 'assetCode',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          labelWidth: '160px',
          clearable: true,
          componentName: 'el-input'
        },
        {
          // 核心企业
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.coreEnterpriseName,
          disabled: false,
          value: 'coreEnterpriseId',
          labelWidth: '160px',
          clearable: true,
          componentName: SelectCoreEnterprise
        },
        {
          // 原始权益人
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.initialOwnersName,
          disabled: false,
          value: 'initialOwnersId',
          labelWidth: '160px',
          clearable: true,
          componentName: SelectPrimitiveRightsHolder
        },
        {
          // 计划管理人/再保理银行
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.planningManagerName,
          disabled: false,
          value: 'planningManagerId',
          labelWidth: '160px',
          clearable: true,
          componentName: SelectFinancialInstitutions
        }
      ],
      columns: [
        {
          // 付款确认书编号
          prop: 'assetCode',
          align: 'left',
          width: '200px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.payCode
        },
        {
          // 核心企业
          prop: 'coreEnterpriseName',
          align: 'left',
          width: '200px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.coreEnterpriseName
        },
        {
          // 供应商名称
          prop: 'creditor',
          align: 'left',
          width: '200px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.suppliersName
        },
        {
          // 原始权益人
          prop: 'initialOwnersName',
          align: 'left',
          width: '200px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.initialOwnersName
        },
        {
          // 计划管理人/再保理银行
          prop: 'planningManagerName',
          align: 'left',
          width: '200px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.planningManagerName
        },
        {
          // 保理费率
          prop: 'factoringRate',
          align: 'left',
          width: '150px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.factoringInterestRate,
          format: (row) => {
            if (Number(row.factoringRate) === 0) {
              return ''
            } else {
              return format.accMul(Number(row.factoringRate), 100)
            }
          }
        },
        {
          // 放款日
          prop: 'loanDateFactoring',
          align: 'center',
          width: '150px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.loanDate,
          format: (row) => {
            let temp = format.formatDateTpcModify(row.loanDateFactoring)
            if (temp.indexOf('1970-01-01') >= 0) {
              return ''
            }
            return temp
          }
        },
        {
          // 账款到期日
          prop: 'dueDate',
          align: 'center',
          width: '150px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.dueDate,
          format: (row) => {
            let temp = format.formatDateTpcModify(row.dueDate)
            if (temp.indexOf('1970-01-01') >= 0) {
              return ''
            }
            return temp
          }
        },
        {
          // 保理天数
          prop: 'factoringDate',
          align: 'left',
          width: '150px',
          // type: true,
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.factoringDays
        },
        {
          // 放款金额(元)
          prop: 'loanAmountFactoring',
          align: 'right',
          width: '150px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.loanAmount,
          format: (row) => {
            if (Number(row.loanAmountFactoring) === 0) {
              return ''
            } else {
              return format.thousandFilter(row.loanAmountFactoring)
            }
          }
        },
        {
          // 供应商利息
          prop: 'suppliersInterest',
          align: 'right',
          width: '150px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.suppliersInterest,
          format: (row) => {
            if (Number(row.suppliersInterest) === 0) {
              return ''
            } else {
              return format.thousandFilter(row.suppliersInterest)
            }
          }
        },
        {
          // 项目公司名称
          prop: 'debtor',
          align: 'left',
          width: '200px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.projectCompanyName
        },
        {
          // 基础合同名称
          prop: 'basicContractName',
          align: 'left',
          width: '200px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].factorDataManageList.basicContractName
        }
      ],
      sourceData: [],
      selectRows: []
    }
  },
  computed: {
    api () {
      return factorDataManageApi.factorData
    }
  },
  mounted () {
  },
  methods: {
    getFactoringDays (val) {
      let days = 0
      days = parseInt(Math.abs(val.dueDate - val.blDate) / (24 * 60 * 60 * 1000))
      return days
    },
    selectRowChange (selectRows) {
      this.selectRows = selectRows
    },
    // 生成资料
    generateData () {
      if (this.selectRows.length === 0) {
        this.warning(this.reMsg.pleaseSelectOpearData)
        return
      }
      let generateFile = []
      this.selectRows.forEach(it => {
        generateFile.push({
          assetInfoId: it.keyId,
          buildBussType: '4',
          generateName: this.$store.state.user.user.userName,
          generateor: this.$store.state.user.user.userId
        })
      })
      this.generateMaterialLoading = true
      factorDataManageApi.factorGenerateData.factoring(generateFile).then(res => {
        if (res.success) {
          // this.success('生成资料成功')
          this.formItem = Object.assign({}, { data: res.data })
          this.resultVisible = true
          this.handleFilter()
        } else {
          this.success('生成资料失败')
        }
      }).finally(() => {
        this.generateMaterialLoading = false
      })
    },
    // 下载资料
    downLoad () {
      if (this.selectRows.length === 0) {
        this.warning(`请先勾选要操作的数据!`)
        return
      }
      let object = []
      let obj = {}
      this.selectRows.forEach(it => {
        object.push(it.keyId)
      })
      obj.assetInfoIdList = object
      obj.fileClassifyList = ['4']
      this.materialDownloadLoading = true
      factorDataManageApi.downloadfactorGenerateData.fileDownLoad(obj).then(res => {
        if (res.success) {
          fileApi.file.downloadByUUID({ uuid: res.data, Encoding: 'utf-8' }).then(its => {
            downFile(its)
          })
        } else {
          this.warning('该数据没有可下载的文件！')
        }
      }).finally(() => {
        this.materialDownloadLoading = false
      })
    }
  }
}
</script>
<style scoped>
</style>
