<template>
  <div>
    <up-down-layout :handleFlag="true">
      <!-- 搜索栏 -->
      <search-form @clearParam="clearParam"
                   @handleFilter="handleFilters"
                   :menuCode="menuId"
                   searchPerms="search"
                   resetPerms="reset"
                   :formValue.sync="queryParas"
                   :componentList="componentList"
                   slot="query">
      </search-form>
      <!-- 表格栏 -->
      <data-table :api="api"
                  slot="table"
                  operateBtnLayout=""
                  :columns="columns"
                  :loadCount.sync="loadCount"
                  :queryParas="queryParas"
                  :menuFlag="true"
                  v-bind="elHeightObj">
        <!-- 操作列 -->
        <el-table-column fixed="right" align="center" label="操作" width="120px">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <auth-button size="small" class="iconCircle"
                         :icon="'iconfont iconbianji'"
                         :title="$t('common.edit')"
                          @click="showEdit(scope.row)"
                          :menuCode="menuId"
                          perms="remove">
            </auth-button>
            <!-- 重新生成 -->
            <auth-button size="small" class="iconCircle"
                          :icon="'iconfont iconreload'"
                          :title="$t('common.remake')"
                          @click="regenerate(scope.row)"
                          :menuCode="menuId"
                          perms="detail">
            </auth-button>
          </template>
        </el-table-column>
      </data-table>
    </up-down-layout>
    <!-- 保理信息详情 -->
    <factoringInfoDetail :visible.sync="visible" editModel="E" :form="item" :api="api" @handleFilter="handleFilters"></factoringInfoDetail>
  </div>
</template>
<script>
import BasePage from '@/pages/basePage'
import { upDownLayout, searchForm, DataTable, authButton, SelectFinancialInstitutions, SelectCoreEnterprise } from 'newepoch-components'
import * as format from '@/filters/index'
import mixin from '@/pages/mixin'
import factoringInfoDetail from './factoringInfoDetail'
import factoringBusinessInfoApi from '@/api/factoringBusinessInfoApi'
import { regExp } from '@/validator/index'
export default {
  name: 'factoringManage',
  mixins: [mixin],
  extends: new BasePage(),
  components: {
    upDownLayout,
    searchForm,
    DataTable,
    authButton,
    factoringInfoDetail
  },
  computed: {
    api () {
      return factoringBusinessInfoApi.manageApi
    }
  },
  data () {
    const { factoringBusinessInfo } = this.$i18n.messages[this.$i18n.locale]
    return {
      visible: false,
      item: {},
      fileID: '',
      inlineName: '',
      componentList: [
        // 核心企业
        {
          label: factoringBusinessInfo.core_enterprise,
          value: 'coreEnterpriseId',
          componentName: SelectCoreEnterprise,
          labelWidth: '110px'
        },
        // 计划管理人/再保理银行
        {
          label: factoringBusinessInfo.planning_manager,
          value: 'planOrBankId',
          componentName: SelectFinancialInstitutions,
          labelWidth: '160px'
        },
        // 期数
        {
          label: factoringBusinessInfo.periods,
          value: 'specialPlanPeriods',
          placeholder: '请输入',
          componentName: 'el-input',
          labelWidth: '110px'
        },
        // 付款确认书编号
        {
          label: factoringBusinessInfo.pay_code,
          value: 'assetCode',
          placeholder: '请输入',
          componentName: 'el-input',
          labelWidth: '110px'
        },
        // 供应商名称
        {
          label: factoringBusinessInfo.creditor,
          value: 'creditor',
          placeholder: '请输入',
          componentName: 'el-input',
          labelWidth: '160px'
        },
        // 项目公司名称
        {
          label: factoringBusinessInfo.project_company,
          value: 'debtor',
          placeholder: '请输入',
          componentName: 'el-input',
          labelWidth: '110px'
        }
      ],
      columns: [
        // 产品名称
        {
          prop: 'projectName',
          align: 'left',
          width: '150px',
          label: factoringBusinessInfo.product_name
        },
        // 核心企业
        {
          prop: 'coreEnterpriseName',
          align: 'left',
          width: '140px',
          label: factoringBusinessInfo.core_enterprise
        },
        // 计划管理人/再保理银行
        {
          prop: 'planOrBankName',
          align: 'left',
          width: '170px',
          label: factoringBusinessInfo.planning_manager
        },
        // 期数
        {
          prop: 'specialPlanPeriods',
          align: 'right',
          width: '70px',
          label: factoringBusinessInfo.periods
        },
        // 付款确认书编号
        {
          prop: 'assetCode',
          align: 'left',
          width: '240px',
          label: factoringBusinessInfo.pay_code
        },
        // 供应商名称
        {
          prop: 'creditor',
          align: 'left',
          width: '240px',
          label: factoringBusinessInfo.creditor
        },
        // 项目公司名称
        {
          prop: 'debtor',
          align: 'left',
          width: '240px',
          label: factoringBusinessInfo.project_company
        },
        // 应收账款金额
        {
          prop: 'factoringMoney',
          align: 'right',
          width: '150px',
          label: factoringBusinessInfo.account_receivable,
          format: row => format.thousandFilter(row.factoringMoney, 2)
        },
        // 区域
        {
          prop: 'coreAreaName',
          align: 'left',
          width: '150px',
          label: factoringBusinessInfo.area
        },
        // 账款到期日
        {
          prop: 'dueDate',
          align: 'center',
          width: '140px',
          label: factoringBusinessInfo.amount_due_date,
          format: row => format.formatDateNull(row.dueDate)
        },
        // 原始权益人
        {
          prop: 'initialOwnersName',
          align: 'left',
          width: '130px',
          label: factoringBusinessInfo.initial_equity_owner
        }
      ]
    }
  },
  methods: {
    // 弹窗
    showEdit (row) {
      this.item.assetInfoId = row.assetInfoId
      this.item.assetPoolId = row.assetPoolId
      this.visible = true
    },
    // 查询
    handleFilters () {
      if (!regExp.number.test(this.queryParas.specialPlanPeriods) && this.queryParas.specialPlanPeriods !== '' && this.queryParas.specialPlanPeriods !== undefined) {
        this.warning('期数应为数字，请重新输入')
        this.queryParas.specialPlanPeriods = ''
        return
      }
      this.handleFilter()
    },
    // 重新生成
    regenerate (row) {
      let _this = this
      factoringBusinessInfoApi.assetApi.generateInfo({ assetInfoId: row.assetInfoId }).then(res => {
        if (res && res.data) {
          _this.success(this.$i18n.messages[this.$i18n.locale].factoringBusinessInfo.regenerateSuc)
          _this.handleFilters()
        } else {
          _this.error(this.$i18n.messages[this.$i18n.locale].factoringBusinessInfo.regenerateFail)
        }
      })
    }
  }
}
</script>
