<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="借款申请流水号" prop="applyNo">
          <el-input v-model="dataForm.applyNo" placeholder="借款申请流水号"></el-input>
      </el-form-item>
          <el-form-item label="客户编号" prop="custCode">
          <el-input v-model="dataForm.custCode" placeholder="客户编号"></el-input>
      </el-form-item>
          <el-form-item label="姓名" prop="custName">
          <el-input v-model="dataForm.custName" placeholder="姓名"></el-input>
      </el-form-item>
          <el-form-item label="手机号" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
          <el-form-item label="银行卡号" prop="bankCardNo">
          <el-input v-model="dataForm.bankCardNo" placeholder="银行卡号"></el-input>
      </el-form-item>
          <el-form-item label="证件号码" prop="idNo">
          <el-input v-model="dataForm.idNo" placeholder="证件号码"></el-input>
      </el-form-item>
          <el-form-item label="产品编号" prop="productCode">
          <el-input v-model="dataForm.productCode" placeholder="产品编号"></el-input>
      </el-form-item>
          <el-form-item label="借款编号" prop="agreementCode">
          <el-input v-model="dataForm.agreementCode" placeholder="借款编号"></el-input>
      </el-form-item>
          <el-form-item label="债权转让方Code" prop="loanCode">
          <el-input v-model="dataForm.loanCode" placeholder="债权转让方Code"></el-input>
      </el-form-item>
          <el-form-item label="债权转让方公司名" prop="loanName">
          <el-input v-model="dataForm.loanName" placeholder="债权转让方公司名"></el-input>
      </el-form-item>
          <el-form-item label="债权转让方银行卡" prop="loanCardNo">
          <el-input v-model="dataForm.loanCardNo" placeholder="债权转让方银行卡"></el-input>
      </el-form-item>
          <el-form-item label="资金平台Code" prop="capitalPlatformCode">
          <el-input v-model="dataForm.capitalPlatformCode" placeholder="资金平台Code"></el-input>
      </el-form-item>
          <el-form-item label="借款申请时间" prop="loanApplyTime">
          <el-input v-model="dataForm.loanApplyTime" placeholder="借款申请时间"></el-input>
      </el-form-item>
          <el-form-item label="借款金额" prop="loanAmount">
          <el-input v-model="dataForm.loanAmount" placeholder="借款金额"></el-input>
      </el-form-item>
          <el-form-item label="交易手续费" prop="loanAmountFee">
          <el-input v-model="dataForm.loanAmountFee" placeholder="交易手续费"></el-input>
      </el-form-item>
          <el-form-item label="借款期限" prop="initPeriod">
          <el-input v-model="dataForm.initPeriod" placeholder="借款期限"></el-input>
      </el-form-item>
          <el-form-item label="借款期限单位" prop="periodUnitCode">
          <el-input v-model="dataForm.periodUnitCode" placeholder="借款期限单位"></el-input>
      </el-form-item>
          <el-form-item label="借款期数" prop="loanPeriod">
          <el-input v-model="dataForm.loanPeriod" placeholder="借款期数"></el-input>
      </el-form-item>
          <el-form-item label="借款用途" prop="loanUsage">
          <el-input v-model="dataForm.loanUsage" placeholder="借款用途"></el-input>
      </el-form-item>
          <el-form-item label="借款利率" prop="loanRate">
          <el-input v-model="dataForm.loanRate" placeholder="借款利率"></el-input>
      </el-form-item>
          <el-form-item label="计息方式" prop="interestTypeCode">
          <el-input v-model="dataForm.interestTypeCode" placeholder="计息方式"></el-input>
      </el-form-item>
          <el-form-item label="还款方式" prop="repaymentTypeCode">
          <el-input v-model="dataForm.repaymentTypeCode" placeholder="还款方式"></el-input>
      </el-form-item>
          <el-form-item label="合同利率" prop="contractRate">
          <el-input v-model="dataForm.contractRate" placeholder="合同利率"></el-input>
      </el-form-item>
          <el-form-item label="逾期利率" prop="overdueRate">
          <el-input v-model="dataForm.overdueRate" placeholder="逾期利率"></el-input>
      </el-form-item>
          <el-form-item label="借款开始时间" prop="loanStartTime">
          <el-input v-model="dataForm.loanStartTime" placeholder="借款开始时间"></el-input>
      </el-form-item>
          <el-form-item label="借款结束时间" prop="loanEndTime">
          <el-input v-model="dataForm.loanEndTime" placeholder="借款结束时间"></el-input>
      </el-form-item>
          <el-form-item label="协议确认时间" prop="agreementConfirmTime">
          <el-input v-model="dataForm.agreementConfirmTime" placeholder="协议确认时间"></el-input>
      </el-form-item>
          <el-form-item label="借款人是否已网签0：未;1：已" prop="isOpen">
          <el-input v-model="dataForm.isOpen" placeholder="借款人是否已网签0：未;1：已"></el-input>
      </el-form-item>
          <el-form-item label="待签约，待放款，放款中，成功，失败" prop="applyStatus">
          <el-input v-model="dataForm.applyStatus" placeholder="待签约，待放款，放款中，成功，失败"></el-input>
      </el-form-item>
          <el-form-item label="版本号" prop="version">
          <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        applyNo: '',
        custCode: '',
        custName: '',
        mobile: '',
        bankCardNo: '',
        idNo: '',
        productCode: '',
        agreementCode: '',
        loanCode: '',
        loanName: '',
        loanCardNo: '',
        capitalPlatformCode: '',
        loanApplyTime: '',
        loanAmount: '',
        loanAmountFee: '',
        initPeriod: '',
        periodUnitCode: '',
        loanPeriod: '',
        loanUsage: '',
        loanRate: '',
        interestTypeCode: '',
        repaymentTypeCode: '',
        contractRate: '',
        overdueRate: '',
        loanStartTime: '',
        loanEndTime: '',
        agreementConfirmTime: '',
        isOpen: '',
        applyStatus: '',
        createTime: '',
        createBy: '',
        updateTime: '',
        updateBy: '',
        version: '',
        remark: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        applyNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        custCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        custName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        bankCardNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        idNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        productCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        agreementCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanCardNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        capitalPlatformCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanApplyTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanAmount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanAmountFee: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        initPeriod: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        periodUnitCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanPeriod: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanUsage: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanRate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        interestTypeCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        repaymentTypeCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        contractRate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        overdueRate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanStartTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanEndTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        agreementConfirmTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        isOpen: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        applyStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        version: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        remark: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/loan/tloanapply/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/loan/tloanapply/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
