<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <div class="tab-header webtop">
        <div>{{ viewTitle }}</div>
        <div>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="saveOrUpdate()">保存</el-button>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="submit()">提交</el-button>
        </div>
      </div>
      <div class="">
        <el-form ref="form_obj" :model="formobj" :rules="rules" class="formaddcopy02">
          <div style="font-size: 16px;text-align: right">金额单位：元</div>
          <el-row style="border-top: 1px solid #e0e3eb">
            <el-col :span="8">
              <div class="span-sty">申请部门</div>
              <el-form-item><span class="addinputInfo">{{ formobj.createDept }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">申请人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.createByName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">申请日期</div>
              <el-form-item><span class="addinputInfo">{{ formobj.applyDate }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">车架号</div>
              <el-form-item><span class="addinputInfo">{{ formobj.vinNo }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">车牌号</div>
              <el-form-item><span class="addinputInfo">{{ formobj.vehMark }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item>
                <span style="margin-left: 10px;color: #20a0ff" @click="lookCSJL(formobj.csjlSid)">催收记录</span><span style="margin-left: 10px;color: #20a0ff" @click="lookJRFA(formobj.jrfaSid)">金融方案</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">客户名称</div>
              <el-form-item><span class="addinputInfo">{{ formobj.customerName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">贷款人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.loanName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">贷款合同编号</div>
              <el-form-item><span class="addinputInfo">{{ formobj.loanContractNo }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">资方</div>
              <el-form-item><span class="addinputInfo">{{ formobj.bankName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">资方合同编号</div>
              <el-form-item><span class="addinputInfo">{{ formobj.bankContractNo }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">预计结清日期</div>
              <el-form-item><el-date-picker class="addinputInfo" @change="changeDate" v-model="formobj.prepareDate" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placeholder="选择日期" /></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">公司当前逾期月还</div>
              <el-form-item><span class="addinputInfo">{{ formobj.overdueMonthPrice }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">公司当期未到期月还</div>
              <el-form-item><span class="addinputInfo">{{ formobj.currentNotDuePrice }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">公司未到期金额</div>
              <el-form-item><span class="addinputInfo">{{ formobj.notDuePrice }}</span></el-form-item>
            </el-col>
          </el-row>
          <div class="title">资方结清费用</div>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">资方逾期月还</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" v-model="formobj.bankCostVo.price" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">资方当期未到期月还</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" v-model="formobj.bankCostVo.currentNotDuePrice" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">资方未到期金额</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" @input="notDuePriceInput" v-model="formobj.bankCostVo.notDuePrice" clearable placeholder=""/></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">资方逾期利息</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" @input="bankInterestInput" v-model="formobj.bankCostVo.bankInterest" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">资方名义价</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" @input="bankNominalPriceInput" v-model="formobj.bankCostVo.bankNominalPrice" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">资方合同违约金</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" @input="bankContractLiquidatedInput" v-model="formobj.bankCostVo.bankContractLiquidated" clearable placeholder=""/></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">资方其他应收</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" v-model="formobj.bankCostVo.bankOtherReceivable" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="16">
              <div class="span-sty">其他应收说明</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" v-model="formobj.bankCostVo.otherReceivableRemarks" clearable placeholder=""/></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">资方结清金额</div>
              <el-form-item><span class="addinputInfo">{{ calculateBankSettlePrice }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">附件</div>
              <el-form-item>
                <uploadImg ref="uploadImg" class="addinputInfo" v-model="formobj.bankCostVo.bankfiles" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="title" style="display: flex;flex-direction: row;justify-content: space-between;align-items: center">
            <div>公司结清费用</div>
            <div>贷款保整金：<span>{{ formobj.companyCostVo.loanDeposit }}</span></div>
          </div>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">公司逾期月还</div>
              <el-form-item><span class="addinputInfo">{{ formobj.companyCostVo.overduePrice }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">垫资方逾期利息</div>
              <el-form-item><span class="addinputInfo">{{ formobj.companyCostVo.putBankInterest }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">资金占用费</div>
              <el-form-item><span class="addinputInfo">{{ formobj.companyCostVo.funfCost }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">资方逾期利息</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" v-model="formobj.companyCostVo.bankOverInterest" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">公司当期未到期月还</div>
              <el-form-item><span class="addinputInfo">{{ formobj.companyCostVo.companyCurrentNotPrice }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">资方未到期金额</div>
              <el-form-item><span class="addinputInfo">{{ formobj.companyCostVo.bankNotPrice }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">贷款保证金扣罚金额</div>
              <el-form-item><span class="addinputInfo">{{ formobj.companyCostVo.deductionAmount }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">名义价</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" v-model="formobj.companyCostVo.companyNominalPrice" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">合同违约金</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" v-model="formobj.companyCostVo.contractLiquidated" clearable placeholder=""/></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">公司其他费用</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" v-model="formobj.companyCostVo.companyOtherPrice" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="16">
              <div class="span-sty">其他费用说明</div>
              <el-form-item><el-input class="addinputInfo addinputw" @keyup.native="UpNumber" v-model="formobj.companyCostVo.otherCostRemarks" clearable placeholder=""/></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">公司正常结清合计</div>
              <el-form-item><span class="addinputInfo">{{ calculateSettleAll }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">结清利润</div>
              <el-form-item><span class="addinputInfo">{{ calculateProfit }}</span></el-form-item>
            </el-col>
          </el-row>
          <div class="title">回购情况</div>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">回购金额</div>
              <el-form-item><span class="addinputInfo">{{ calculateBankSettlePrice }}</span></el-form-item>
            </el-col>
            <el-col :span="16">
              <div class="span-sty">回购原因</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.buybacksReason" clearable placeholder=""/></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">资方收款账户名称</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.bankReceivingAccount" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">资方收款账号</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.bankAccountNo" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">付款时需备注内容</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.payRemarks" clearable placeholder=""/></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">附件</div>
              <el-form-item>
                <uploadImg ref="uploadImg" class="addinputInfo" v-model="formobj.buyfiles" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <!-- 查看催收记录 -->
    <vehiclecollectionrecords v-show="viewState == 2" ref="divCollection" @doback="resetState"/>
    <!-- 查看金融方案 -->
    <financialschemeInfo v-show="viewState == 3" ref="divFinancial" @doback="resetState"/>
  </div>
</template>

<script>
import req from '@/api/buyback/buyback'
import uploadImg from '@/components/uploadFile/uploadImg'
import vehiclecollectionrecords from '../cuishoucuoshiFlow/relation/vehiclecollectionrecords'
import financialschemeInfo from '../publicPage/financialschemeInfo'

export default {
  name: 'BuyBackEdit',
  components: {
    uploadImg,
    vehiclecollectionrecords,
    financialschemeInfo
  },
  computed: {
    // 资方结清费用模块--资方结清金额计算
    calculateBankSettlePrice() {
      return parseFloat(this.formobj.bankCostVo.price === '' ? 0 : this.formobj.bankCostVo.price) + parseFloat(this.formobj.bankCostVo.currentNotDuePrice === '' ? 0 : this.formobj.bankCostVo.currentNotDuePrice) + parseFloat(this.formobj.bankCostVo.notDuePrice === '' ? 0 : this.formobj.bankCostVo.notDuePrice) + parseFloat(this.formobj.bankCostVo.bankInterest === '' ? 0 : this.formobj.bankCostVo.bankInterest) + parseFloat(this.formobj.bankCostVo.bankNominalPrice === '' ? 0 : this.formobj.bankCostVo.bankNominalPrice) + parseFloat(this.formobj.bankCostVo.bankContractLiquidated === '' ? 0 : this.formobj.bankCostVo.bankContractLiquidated) + parseFloat(this.formobj.bankCostVo.bankOtherReceivable === '' ? 0 : this.formobj.bankCostVo.bankOtherReceivable) || 0
    },
    // 公司结清费用模块--公司正常结清合计计算
    calculateSettleAll() {
      return parseFloat(this.formobj.companyCostVo.overduePrice === '' ? 0 : this.formobj.companyCostVo.overduePrice) + parseFloat(this.formobj.companyCostVo.funfCost === '' ? 0 : this.formobj.companyCostVo.funfCost) + parseFloat(this.formobj.companyCostVo.bankOverInterest === '' ? 0 : this.formobj.companyCostVo.bankOverInterest) + parseFloat(this.formobj.companyCostVo.companyCurrentNotPrice === '' ? 0 : this.formobj.companyCostVo.companyCurrentNotPrice) + parseFloat(this.formobj.companyCostVo.bankNotPrice === '' ? 0 : this.formobj.companyCostVo.bankNotPrice) + parseFloat(this.formobj.companyCostVo.deductionAmount === '' ? 0 : this.formobj.companyCostVo.deductionAmount) + parseFloat(this.formobj.companyCostVo.contractLiquidated === '' ? 0 : this.formobj.companyCostVo.contractLiquidated) + parseFloat(this.formobj.companyCostVo.companyOtherPrice === '' ? 0 : this.formobj.companyCostVo.companyOtherPrice) - parseFloat(this.formobj.companyCostVo.loanDeposit === '' ? 0 : this.formobj.companyCostVo.loanDeposit) || 0
    },
    // 结清利润计算
    calculateProfit() {
      return (parseFloat(this.formobj.companyCostVo.bankOverInterest === '' ? 0 : this.formobj.companyCostVo.bankOverInterest) + parseFloat(this.formobj.companyCostVo.companyNominalPrice === '' ? 0 : this.formobj.companyCostVo.companyNominalPrice) + parseFloat(this.formobj.companyCostVo.contractLiquidated === '' ? 0 : this.formobj.companyCostVo.contractLiquidated) + parseFloat(this.formobj.companyCostVo.companyOtherPrice === '' ? 0 : this.formobj.companyCostVo.companyOtherPrice)) - (parseFloat(this.formobj.bankCostVo.bankInterest === '' ? 0 : this.formobj.bankCostVo.bankInterest) + parseFloat(this.formobj.bankCostVo.bankNominalPrice === '' ? 0 : this.formobj.bankCostVo.bankNominalPrice) + parseFloat(this.formobj.bankCostVo.bankContractLiquidated === '' ? 0 : this.formobj.bankCostVo.bankContractLiquidated) + parseFloat(this.formobj.bankCostVo.bankOtherReceivable === '' ? 0 : this.formobj.bankCostVo.bankOtherReceivable)) || 0
    }
  },
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      submitdisabled: false,
      formobj: {
        amount: '',
        applyDate: '',
        bankAccountNo: '',
        bankContractNo: '',
        bankCostVo: {
          bankContractLiquidated: '',
          bankInterest: '',
          bankNominalPrice: '',
          bankOtherReceivable: '',
          bankSettlePrice: '',
          bankfiles: [],
          currentNotDuePrice: '',
          notDuePrice: '',
          otherReceivableRemarks: '',
          price: ''
        },
        bankName: '',
        bankReceivingAccount: '',
        busVinSid: '',
        buybacksReason: '',
        buyfiles: [],
        companyCostVo: {
          bankNotPrice: '',
          bankOverInterest: '',
          companyCurrentNotPrice: '',
          companyNominalPrice: '',
          companyOtherPrice: '',
          contractLiquidated: '',
          deductionAmount: '',
          funfCost: '',
          loanDeposit: '',
          otherCostRemarks: '',
          overduePrice: '',
          profit: '',
          putBankInterest: '',
          settleAll: ''
        },
        createByName: '',
        createDept: '',
        createDeptSid: '',
        currentNotDuePrice: '',
        customerName: '',
        customerSid: '',
        instanceId: '',
        loanContractNo: '',
        loanName: '',
        loanSid: '',
        notDuePrice: '',
        orgPath: '',
        overdueMonthPrice: '',
        payRemarks: '',
        prepareDate: '',
        salesOrderSid: '',
        sid: '',
        taskId: '',
        userSid: '',
        vehMark: '',
        vinNo: ''
      },
      rules: {}
    }
  },
  created() {
    console.log('url:' + window.location.href)
    var one = window.location.href.indexOf('&data') + 6
    const data = window.location.href.substr(one) // url解码unescape()已从web中移除,尽量不使用
    const obj = JSON.parse(decodeURIComponent(data))
    this.showInfo(obj.businessSid)
  },
  mounted() {
    window.parent.postMessage({
      cmd: 'returnHeight',
      params: {
        // 告诉父级页面，子页面的弹框高度。
        code: 2,
        data: 500 + 'px'
      }
    }, '*')
  },
  methods: {
    UpNumber(e) {
      e.target.value = e.target.value.replace(/[^0-9.]/g, '') // 清除“数字”和“.”以外的字符
      e.target.value = e.target.value.replace(/^00/, '0.') // 开头不能有两个0
      e.target.value = e.target.value.replace(/\.{2,}/g, '.') // 只保留第一个. 清除多余的
      e.target.value = e.target.value.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
      e.target.value = e.target.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') // 只能输入两个小数
      if (e.target.value.indexOf('.') < 0 && e.target.value !== '' && e.target.value !== '-') {
        // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        e.target.value = parseFloat(e.target.value)
      }
    },
    showInfo(sid) {
      this.viewTitle = '【编辑】回购申请'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      req.fetchBySid(sid).then((res) => {
        if (res.success) {
          this.formobj = res.data
        }
      })
    },
    // 查看催收记录
    lookCSJL(sid) {
      if (sid !== '') {
        this.viewState = 2
        this.$refs['divCollection'].showInfo({ saleVehSid: this.formobj.busVinSid })
      } else {
        this.$message({ showClose: true, type: 'error', message: '暂无催收记录' })
      }
    },
    lookJRFA(sid) {
      if (sid !== '') {
        req.selectDetailss({ businessSid: this.formobj.salesOrderSid }).then((res) => {
          if (res.success) {
            this.viewState = 3
            this.$refs['divFinancial'].showInfo(res.data, '03')
          }
        })
      } else {
        this.$message({ showClose: true, type: 'error', message: '暂无金融方案' })
      }
    },
    resetState() {
      this.viewState = 1
    },
    // 修改预计结清日期时需重新获取计算部分参数值
    changeDate(val) {
      req.repurchaseInit({ busVinSid: this.formobj.busVinSid, orgPath: this.formobj.orgPath, prepareDate: val, userSid: this.formobj.userSid }).then((res) => {
        if (res.success) {
          this.formobj.overdueMonthPrice = res.data.overdueMonthPrice
          this.formobj.currentNotDuePrice = res.data.currentNotDuePrice
          this.formobj.notDuePrice = res.data.notDuePrice
          this.formobj.companyCostVo.loanDeposit = res.data.companyCostVo.loanDeposit
          this.formobj.companyCostVo.overduePrice = res.data.companyCostVo.overduePrice
          this.formobj.companyCostVo.putBankInterest = res.data.companyCostVo.putBankInterest
          this.formobj.companyCostVo.funfCost = res.data.companyCostVo.funfCost
          this.formobj.companyCostVo.companyCurrentNotPrice = res.data.companyCostVo.companyCurrentNotPrice
          this.formobj.companyCostVo.deductionAmount = res.data.companyCostVo.deductionAmount
        }
      })
    },
    notDuePriceInput(val) {
      this.formobj.companyCostVo.bankNotPrice = val
    },
    bankInterestInput(val) {
      this.formobj.companyCostVo.bankOverInterest = val
    },
    bankNominalPriceInput(val) {
      this.formobj.companyCostVo.companyNominalPrice = val
    },
    bankContractLiquidatedInput(val) {
      this.formobj.companyCostVo.contractLiquidated = val
    },
    saveOrUpdate() {
      this.formobj.bankCostVo.bankSettlePrice = this.calculateBankSettlePrice
      this.formobj.amount = this.calculateBankSettlePrice
      this.formobj.companyCostVo.settleAll = this.calculateSettleAll
      this.formobj.companyCostVo.profit = this.calculateProfit
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          req.saveOrUpdate(this.formobj).then((res) => {
            if (res.success) {
              this.$message({ showClose: true, type: 'success', message: '保存成功' })
              // 子页面向父级页面传递值(关闭弹框)
              window.parent.postMessage({
                cmd: 'returnHeight',
                params: {
                  // 操作成功，告诉父级页面关闭弹框
                  code: 1
                }
              }, '*')
            } else {
              this.submitdisabled = false
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        }
      })
    },
    submit() {
      this.formobj.bankCostVo.bankSettlePrice = this.calculateBankSettlePrice
      this.formobj.amount = this.calculateBankSettlePrice
      this.formobj.companyCostVo.settleAll = this.calculateSettleAll
      this.formobj.companyCostVo.profit = this.calculateProfit
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          req.submit(this.formobj).then((res) => {
            if (res.success) {
              this.$message({ showClose: true, type: 'success', message: '操作成功' })
              // 子页面向父级页面传递值(关闭弹框)
              window.parent.postMessage({
                cmd: 'returnHeight',
                params: {
                  // 操作成功，告诉父级页面关闭弹框
                  code: 1
                }
              }, '*')
            } else {
              this.submitdisabled = false
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.span-sty {
  width: 160px !important;
}
.addinputInfo {
  margin-left: 150px !important;
}
.formaddcopy02 .el-row .el-col /deep/ .el-form-item .addinputw {
  margin-left: 150px !important;
  width: calc(100% - 145px);
}
</style>
