<!--财务部核对放款信息-->
<template>
  <div>
    <up-down-layout :header="$t('financeLendingInfoList.head')" :handleFlag.sync="handleFlag">
      <search-form :formValue.sync="queryParas"
                   searchPerms="search"
                   @handleFilter="handleFilter"
                   @clearParam="clearParam"
                   slot="query"
                   resetPerms="reset"
                   @selectChanged="selectChanged"
                   :componentList="componentList">
      </search-form>
      <template slot="table">
        <data-table :api="api"
                    :selectTable="selectTable"
                    :handleFlag.sync="handleFlag"
                    :menuFlag="true"
                    class="data-table"
                    :showSelection="false"
                    v-bind="elHeightObj"
                    :queryParas="queryParas"
                    :sourceData.sync="sourceData"
                    :loadCount.sync="loadCount"
                    :columns="columns"
                    operateBtnLayout=""
                    @getTableData="getTableData"
                    @selectRowChange="selectRowChange"
                    ref="loanTable">
          <el-table-column slot="index" fixed="left"
                           type="selection"
                           align="center"
                           label="#"
                           :selectable="selectTable"
                           width="55"></el-table-column>
          <template slot="factoringMoneyhead" slot-scope="scope">
            <div class="customHeadDiv">
              <p class="headPHeight25px">{{scope.data.column.label}}</p>
              <p class="headPHeight25px headTotalRed">{{total[scope.data.column.property]}}</p>
            </div>
          </template>
          <!--开票金额-->
          <template slot="totalAmounthead" slot-scope="scope">
            <div class="customHeadDiv">
              <p class="headPHeight25px">{{scope.data.column.label}}</p>
              <p class="headPHeight25px headTotalRed">{{total[scope.data.column.property]}}</p>
            </div>
          </template>
          <!--待开票金额-->
          <template slot="invoicedAmounthead" slot-scope="scope">
            <div class="customHeadDiv">
              <p class="headPHeight25px">{{scope.data.column.label}}</p>
              <p class="headPHeight25px headTotalRed">{{total[scope.data.column.property]}}</p>
            </div>
          </template>
          <!--支付供应商金额-->
          <template slot="loanAmountFactoringhead" slot-scope="scope">
            <div class="customHeadDiv">
              <p class="headPHeight25px">{{scope.data.column.label}}</p>
              <p class="headPHeight25px headTotalRed">{{total[scope.data.column.property]}}</p>
            </div>
          </template>
          <template slot="headButton">
            <div class="form-bottom form-bottomDis">
              <el-form :model="form" label-width="70px" class="width100Style">
                <el-form-item :label="$t('financeLendingInfoList.auditOpinions')">
                  <el-input v-model="auditOpinions" :maxlength="100" size="medium"></el-input>
                </el-form-item>
              </el-form>
              <div class="rightDiv">
                <!--导出-->
                <btn-export btnSize="medium" :openPerms="true" :queryParas="queryParas" perms="btnExport" :menuCode="menuId"
                            :templateCode="templateCode"></btn-export>
              </div>
            </div>
          </template>
        </data-table>
        <div align="center">
          <!--通过-->
          <auth-button size="medium" type="primary" :label="$t('common.through')"
                       :openPerms="true" :menuCode="menuId" perms="through"
                       @click="through('Y')" :loading="confirmLoading"></auth-button>
          <!--不通过-->
          <auth-button size="medium" type="primary" :label="$t('common.notThrough')"
                       :openPerms="true" :menuCode="menuId" perms="notThrough"
                       @click="through('N')" :loading="cancelLoading"></auth-button>
        </div>
      </template>
    </up-down-layout>
  </div>
</template>
<script>
import { upDownLayout, searchForm, DataTable, authButton, btnExport, SelectProductName,
  SelectCoreEnterprise, SelectFinancialInstitutions } from 'newepoch-components'
import * as format from '@/filters/index'
import financeLendingApi from '@/api/financeLendingApi'
import BasePage from '@/pages/basePage'
import mixin from '@/pages/mixin.js'
import componentsApi from '@/api/componentsApi'
import { regExp } from '@/validator/index'

export default {
  extends: new BasePage(),
  mixins: [mixin],
  name: 'financeLendingInfo',
  components: {
    searchForm,
    upDownLayout,
    DataTable,
    btnExport,
    authButton,
    SelectProductName
  },
  data () {
    return {
      confirmLoading: false,
      cancelLoading: false,
      handleFlag: true,
      auditOpinions: '',
      loadCount: 0,
      total: {
        factoringMoney: 0,
        invoicedAmount: 0,
        loanAmountFactoring: 0,
        totalAmount: 0
      },
      queryParas: {
        loanDateFactoringStart: '',
        loanDateFactoringEnd: ''
      },
      form: {
      },
      queryData: {
      },
      templateCode: 'shourangsql001',
      componentList: [
        {
          // 产品名称
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.productName,
          disabled: false,
          value: 'projectId',
          name: 'projectId',
          labelWidth: '160px',
          clearable: true,
          componentName: SelectProductName
        },
        {
          // 核心企业
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.coreEnterpriseName,
          disabled: false,
          value: 'coreEnterpriseId',
          labelWidth: '160px',
          clearable: true,
          componentName: SelectCoreEnterprise,
          queryName: 'projectId',
          queryValue: 'projectId'
        },
        {
          // 计划管理人/再保理银行
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.planningManagerName,
          disabled: false,
          value: 'planningManagerId',
          labelWidth: '160px',
          clearable: true,
          componentName: SelectFinancialInstitutions,
          queryName: 'projectId',
          queryValue: 'projectId'
        },
        {
          // 供应商名称
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.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].financeLendingInfoList.specialPlanPeriods,
          disabled: false,
          value: 'specialPlanPeriods',
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInputNum,
          labelWidth: '160px',
          clearable: true,
          maxlength: '3',
          componentName: 'el-input'
        },
        {
          // 付款确认书编号
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.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].financeLendingInfoList.protectRateDate,
          disabled: false,
          value: 'protectRateDate',
          name: 'protectRateDate',
          startPlaceholder: this.$i18n.messages[this.$i18n.locale].common.startDate,
          endPlaceholder: this.$i18n.messages[this.$i18n.locale].common.endDate,
          labelWidth: '160px',
          type: 'daterange',
          clearable: true,
          format: 'timestamp',
          componentName: 'el-date-picker'
        },
        {
          // 单据状态
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.documentStatus,
          disabled: false,
          value: 'openInvoiceStatus',
          labelWidth: '160px',
          clearable: true,
          componentName: 'SelectDictionary',
          dictTypeCode: 'SCRS0111',
          store: this.$store
        }
      ],
      columns: [
        {
          // 核心企业
          prop: 'coreEnterpriseName',
          align: 'left',
          width: '200px',
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.coreEnterpriseName
        },
        {
          // 计划管理人/再保理银行
          prop: 'planningManagerName',
          align: 'left',
          width: '200px',
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.planningManagerName
        },
        {
          // 期数
          prop: 'specialPlanPeriods',
          align: 'right',
          width: '80px',
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.specialPlanPeriods
        },
        {
          // 付款确认书编号
          prop: 'assetCode',
          align: 'left',
          width: '180px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.payCode
        },
        {
          // 供应商全称
          prop: 'creditor',
          align: 'left',
          width: '200px',
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.supplierFullName
        },
        {
          // 地区
          prop: 'coreAreaName',
          align: 'left',
          width: '130px',
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.region
        },
        {
          // 应收账款金额(元)
          prop: 'factoringMoney',
          align: 'right',
          width: '200px',
          sortable: true,
          headslot: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.accountsReceivable,
          format: (row) => {
            return format.thousandFilter(row.factoringMoney)
          }
        },
        {
          // 费率
          prop: 'factoringRate',
          align: 'right',
          width: '130px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.factoringInterestRate,
          format: (row) => {
            if (!this.UtilJs.isEmpty(row.factoringRate)) {
              if (Number(row.factoringRate) === 0) {
                return ''
              }
              return (row.factoringRate * 100).toFixed(2)
            }
            return ''
          }
        },
        {
          // 保理起息日期
          prop: 'loanDateFactoring',
          align: 'center',
          width: '150px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.factoringStartDate,
          format: (row) => {
            if (row.loanDateFactoring) {
              return format.formatDateTpcModify(row.loanDateFactoring)
            } else {
              return ''
            }
          }
        },
        {
          // 保理到期日期
          prop: 'dueDate',
          align: 'center',
          width: '150px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.factoringExpirationDate,
          format: (row) => {
            if (row.dueDate) {
              return format.formatDateTpcModify(row.dueDate)
            } else {
              return ''
            }
          }
        },
        {
          // 保理天数
          prop: 'factoringDays',
          align: 'right',
          width: '130px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.factoringDays,
          format: (row) => {
            try {
              if (Number(row.factoringDays) >= 0) {
                return row.factoringDays
              }
            } catch (e) {
              return ''
            }
          }
        },
        {
          // 开票金额(元)
          prop: 'totalAmount',
          align: 'right',
          width: '160px',
          sortable: true,
          headslot: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.invoiceMoney,
          format: (row) => {
            return format.thousandFilter(row.totalAmount)
          }
        },
        {
          // 开票日期
          prop: 'openDate',
          align: 'center',
          width: '150px',
          sortable: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.openDate,
          format: (row) => {
            return format.formatDateTpcModify(row.openDate)
          }
        },
        {
          // 待开票金额(元)
          prop: 'invoicedAmount',
          align: 'right',
          width: '180px',
          sortable: true,
          headslot: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.stayInvoicedAmount,
          format: (row) => {
            return format.thousandFilter(row.invoicedAmount)
          }
        },
        {
          // 支付供应商金额(元)
          prop: 'loanAmountFactoring',
          align: 'right',
          width: '200px',
          sortable: true,
          headslot: true,
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.paymentSupplierAmount,
          format: (row) => {
            return format.thousandFilter(row.loanAmountFactoring)
          }
        },
        {
          // 是否自垫
          prop: 'selfPayFlag',
          align: 'center',
          width: '130px',
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.ifsSelfAdvance,
          format: (row) => {
            return format.formatDict(row.selfPayFlag, 'ZD0003')
          }
        },
        {
          // 单据状态
          prop: 'openInvoiceStatus',
          align: 'center',
          width: '150px',
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.documentStatus,
          format: (row) => {
            return format.formatDict(row.openInvoiceStatus, 'SCRS0111')
          }
        },
        {
          // 金融部处理意见
          prop: 'auditOpinions',
          align: 'left',
          width: '200px',
          label: this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.disposalOpinions,
          format: (row) => {
            return this.UtilJs.isEmpty(row.auditOpinions) ? '-' : row.auditOpinions
          }
        }
      ],
      sourceData: [],
      selectRows: []
    }
  },
  computed: {
    api () {
      return financeLendingApi.financeLending
    }
  },
  methods: {
    clearParam () {
      this.queryParas = {
        pageIndex: 1,
        pageSize: 10
      }
      this.selectChangeLocal('1')
    },
    selectChanged (a) {
      if (a.hasOwnProperty('projectIdInfo')) {
        if (a.projectIdInfo && a.projectIdInfo.keyId) {
          this.getTypeById(a.projectIdInfo.keyId)
        } else {
          this.selectChangeLocal('1')
        }
      }
    },
    // 改变选择信息、日期
    selectChangeLocal (isAbs) {
      this.componentList[4].disabled = isAbs !== '1'
      if (this.componentList[4].disabled) {
        this.componentList[4].placeholder = ' '
      } else {
        this.componentList[4].placeholder = this.$i18n.messages[this.$i18n.locale].common.pleaseInput
      }
    },
    // 根据ID获取产品类别
    getTypeById (projectId) {
      componentsApi.productenterprise.getclassify({ projectId: projectId }).then(res => {
        var a = res.data ? res.data.productType : '2'
        this.selectChangeLocal(a)
      })
    },
    getTableData (data) {
      // 应收账款金额
      let factoringMoney = 0
      // 待开票金额
      let invoicedAmount = 0
      // 支付供应商金额
      let loanAmountFactoring = 0
      // 开票金额
      let totalAmount = 0
      this.sourceData = []
      financeLendingApi.amountTotal.getPage(this.queryParas).then(res => {
        if (res.data) {
          factoringMoney = res.data.factoringMoney
          invoicedAmount = res.data.invoicedAmount
          loanAmountFactoring = res.data.loanAmountFactoring
          totalAmount = res.data.totalAmount
          this.$set(this.total, 'factoringMoney', format.thousandFilter(factoringMoney))
          this.$set(this.total, 'invoicedAmount', format.thousandFilter(invoicedAmount))
          this.$set(this.total, 'loanAmountFactoring', format.thousandFilter(loanAmountFactoring))
          this.$set(this.total, 'totalAmount', format.thousandFilter(totalAmount))
          this.sourceData.push(...data)
        }
      })
    },
    handleFilter () {
      if (!this.UtilJs.isEmpty(this.queryParas.specialPlanPeriods) && !regExp.number.test(this.queryParas.specialPlanPeriods)) {
        return this.warning(this.$i18n.messages[this.$i18n.locale].expressDataList.specialPlanPeriodsCheck)
      }
      if (Array.isArray(this.queryParas.protectRateDate) && this.queryParas.protectRateDate.length > 0) {
        this.$set(this.queryParas, 'loanDateFactoringStart', this.queryParas.protectRateDate[0])
        this.$set(this.queryParas, 'loanDateFactoringEnd', this.queryParas.protectRateDate[1])
      } else {
        delete this.queryParas.loanDateFactoringStart
        delete this.queryParas.loanDateFactoringEnd
        delete this.queryParas.protectRateDate
      }
      Object.keys(this.queryParas).forEach(it => {
        if (this.UtilJs.isEmpty(this.queryParas[it])) delete this.queryParas[it]
      })
      let _this = this
      _this.loadCount = _this.loadCount + 1
    },
    selectRowChange (selectRows) {
      this.selectRows = selectRows
    },
    selectTable (row) {
      return this.UtilJs.isEmpty(row.openInvoiceStatus) ? false : (row.openInvoiceStatus === '10')
    },
    through (val) {
      let $this = this
      let auditData = []
      if (this.selectRows.length === 0) {
        this.warning(this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.selectAuditDataMsg)
        return
      }
      if (this.UtilJs.isEmpty(this.auditOpinions.trim()) && val === 'N') {
        this.warning(this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.auditOpinionsMsg)
        return
      }
      if (this.UtilJs.isEmpty(this.$store.state.user.user.userName) ||
        this.UtilJs.isEmpty(this.$store.state.user.user.userId)) {
        this.warning(this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.userFailMsg)
        return
      }
      this.selectRows.forEach(it => {
        let obj = {}
        obj.auditOpinions = $this.auditOpinions
        obj.auditor = this.$store.state.user.user.userName
        obj.auditorId = this.$store.state.user.user.userId
        obj.auditType = '0'
        obj.assetInfoId = it.assetInfoId
        obj.operInfoId = it.operInfoId
        if (val === 'Y') {
          obj.auditStatus = '1'
        } else {
          obj.auditStatus = '0'
        }
        auditData.push(obj)
      })
      if (val === 'Y') {
        this.confirmLoading = true
      } else {
        this.cancelLoading = true
      }
      financeLendingApi.financeAudit.audit(auditData).then(res => {
        if (res.success) {
          $this.success(this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.auditSucMsg)
          $this.handleFilter()
          $this.auditOpinions = ''
        } else {
          $this.error(this.$i18n.messages[this.$i18n.locale].financeLendingInfoList.auditFailMsg)
        }
      }).finally(() => {
        this.confirmLoading = false
        this.cancelLoading = false
      })
    }
  }
}
</script>
<style scoped lang="scss" src="@/styles/shourang/financeLendingInfo/financeLendingInfoList.scss">
</style>
