<template>
  <div style="margin-top: 2px">
    <el-form ref="form_obj" :model="finance" class="formaddcopy02">
      <div class="title titleOne">
        <div>金融方案</div>
        <div>
          <el-button type="primary" size="mini" :disabled="submitdisabled" class="btntopblueline" @click="handleConfirm">确定</el-button>
          <el-button type="info" size="mini" @click="handleColse">关闭</el-button>
        </div>
      </div>
      <div v-if="isShowMore">
        <el-row style="border-top: 1px solid #E0E3EB">
          <el-col :span="6">
            <div class="span-sty">是否打包</div>
            <el-form-item>
              <el-radio-group style="font-size: 1px" class="addinputw" @input="isPackChange" v-model="finance.isPack">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <div class="span-sty">产品政策</div>
            <el-form-item>
              <el-select v-model="finance.policyName" placeholder="请选择" @change="changePolicy" clearable class="addinputInfo">
                <el-option v-for="item in policy_list" :key="item.sid" :label="item.name" :value="item.name"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="span-sty">融资项目</div>
            <el-form-item prop="mainVehicleAmount">
              <div class="addinputInfo">
                <span><span class="icon">*</span>主车发票价<el-input v-model="finance.mainVehicleAmount" @input="financingProject" clearable style="width: 100px" placeholder="" @keyup.native="finance.mainVehicleAmount = oninput(finance.mainVehicleAmount, 0)"/>元</span>
                <span style="padding-left: 5px"><el-checkbox @change="financingProject" v-model="finance.accessoriesAmountCb" style="padding-right: 5px"/>配件<el-input v-model="finance.accessoriesAmount" @input="financingProject" clearable style="width: 100px" placeholder="" @keyup.native="finance.accessoriesAmount = oninput(finance.accessoriesAmount, 0)"/>元</span>
                <span style="padding-left: 5px"><el-checkbox @change="financingProject" v-model="finance.trailerAmountCb" style="padding-right: 5px"/>挂车<el-input v-model="finance.trailerAmount" @input="financingProject" clearable style="width: 100px" placeholder="" @keyup.native="finance.trailerAmount = oninput(finance.trailerAmount, 0)"/>元</span>
                <span style="padding-left: 5px"><el-checkbox @change="financingProject" v-model="finance.premiumCb" :disabled="finance.isPack == '0'" style="padding-right: 5px"/>保险<el-input v-model="finance.premium" @input="financingProject" clearable style="width: 100px" placeholder="" @keyup.native="finance.premium = oninput(finance.premium, 0)"/>元</span>
                <span style="padding-left: 5px"><el-checkbox @change="financingProject" v-model="finance.purchaseTaxCb" :disabled="finance.isPack == '0'" style="padding-right: 5px"/>购置税<el-input v-model="finance.purchaseTax" @input="financingProject" clearable style="width: 100px" placeholder="" @keyup.native="finance.purchaseTax = oninput(finance.purchaseTax, 0)"/>元</span>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <div class="span-sty">融资项目总额</div>
            <el-form-item>
              <span class="addinputInfo">{{ finance.loanTotal }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <div class="span-sty">首付比例(%)</div>
            <el-form-item><span class="addinputInfo">{{ finance.downPayRatio }}</span></el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">首付金额</div>
            <el-form-item>
              <el-input class="addinputw addinputInfo" v-model="finance.downPayAmount" @input="downPayAmountInput" placeholder="" clearable @keyup.native="finance.downPayAmount = oninput(finance.downPayAmount, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">产品贷款金额</div>
            <el-form-item>
              <span class="addinputInfo">{{ finance.loanAmount }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">产品期数</div>
            <el-form-item>
              <span class="addinputInfo">{{ finance.loanPeriod }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <div class="span-sty">贷款保证金比例(%)</div>
            <el-form-item>
              <span class="addinputInfo">{{ finance.bondRatio }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">贷款保证金</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.bondAmount" placeholder="" @input="bondAmountInput" clearable @keyup.native="finance.bondAmount = oninput(finance.bondAmount, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">预计首款还款日</div>
            <el-form-item>
              <el-date-picker value-format="yyyy-MM-dd" v-model="finance.returnTime" clearable class="addinputw" type="date" placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :span="6"></el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <div class="span-sty">厂家贴息</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.factoryDiscount" placeholder="" clearable @keyup.native="finance.factoryDiscount = oninput(finance.factoryDiscount, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">贴息用途</div>
            <el-form-item>
              <span class="addinputInfo">{{ finance.factoryDiscountUse }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty" v-show="finance.factoryDiscountUseKey == '01'">贴息抵车款</div>
            <el-form-item v-show="finance.factoryDiscountUseKey == '01'">
              <el-input v-model="finance.discountCar" clearable placeholder="" class="addinputInfo addinputw" @keyup.native="finance.discountCar = oninput(finance.discountCar, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6"></el-col>
        </el-row>
        <div class="title">
          <el-checkbox v-model="finance.otherPolicyState" style="padding-right: 5px"/>
          其它融
        </div>
        <div v-if="finance.otherPolicyState">
          <el-row>
            <el-col :span="6">
              <div class="span-sty">其它融名称</div>
              <el-form-item>
                <el-select v-model="finance.otherPolicyName" placeholder="请选择" @change="changeOtherPolicy" clearable class="addinputInfo">
                  <el-option v-for="item in other_list" :key="item.sid" :label="item.name" :value="item.name"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="span-sty">贷款金额</div>
              <el-form-item>
                <el-input class="addinputw" v-model="finance.otherPolicyAmount" placeholder="" clearable @keyup.native="finance.otherPolicyAmount = oninput(finance.otherPolicyAmount, 0)"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="span-sty">期数</div>
              <el-form-item>
                <span class="addinputInfo">{{ finance.otherPolicyPeriod }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="span-sty">年利率(%)</div>
              <el-form-item>
                <span class="addinputInfo">{{ finance.otherPolicyYearRatio }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div class="title">应收明细</div>
        <el-row>
          <el-col :span="6">
            <div class="span-sty">贷款保证金</div>
            <el-form-item><span class="addinputInfo">{{ bondAmounts() }}</span></el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">保险保证金</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.depositPremium" placeholder="" clearable @keyup.native="finance.depositPremium = oninput(finance.depositPremium, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">落户保证金</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.depositSettle" placeholder="" clearable @keyup.native="finance.depositSettle = oninput(finance.depositSettle, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">服务费</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.serviceAmount" placeholder="" clearable @keyup.native="finance.serviceAmount = oninput(finance.serviceAmount, 0)"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <div class="span-sty">代收意外险</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.proxyAccidentPremium" placeholder="" clearable @keyup.native="finance.proxyAccidentPremium = oninput(finance.proxyAccidentPremium, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">上牌费</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.registerAmount" placeholder="" clearable @keyup.native="finance.registerAmount = oninput(finance.registerAmount, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">运管费</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.operationAmount" placeholder="" clearable @keyup.native="finance.operationAmount = oninput(finance.operationAmount, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty">补车价</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.vehOtherPrice" placeholder="" clearable @keyup.native="finance.vehOtherPrice = oninput(finance.vehOtherPrice, 0)"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <div class="span-sty">其它费用</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.otherAmount" placeholder="" clearable @keyup.native="finance.otherAmount = oninput(finance.otherAmount, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <div class="span-sty">其它费用说明</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.otherAmountRemark" placeholder="" clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <div class="span-sty">办理方式选择</div>
            <el-form-item>
              <el-select v-model="finance.dealWay" placeholder="请选择" @change="changeDealWay" clearable class="addinputInfo">
                <el-option v-for="item in dealWay_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <div v-if="finance.dealWayKey == '01' && finance.isPack == '0' ">
              <div class="span-sty">代收首年保险费</div>
              <el-form-item>
                <el-input class="addinputw" v-model="finance.proxyPremium" placeholder="" clearable @keyup.native="finance.proxyPremium = oninput(finance.proxyPremium, 0)"/>
              </el-form-item>
            </div>
            <div v-if="finance.dealWayKey == '02' && finance.isPack == '1'">
              <div class="span-sty">抵顶首年保险费</div>
              <el-form-item>
                <el-input class="addinputw" v-model="finance.offsetPremium" :disabled="!finance.premiumCb || this.finance.premium == ''" placeholder="" clearable @keyup.native="finance.offsetPremium = oninput(finance.offsetPremium, 0)"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6">
            <div v-if="finance.dealWayKey == '01' && finance.isPack == '0' ">
              <div class="span-sty">代收购置税</div>
              <el-form-item>
                <el-input class="addinputw" v-model="finance.proxyPurchasetax" placeholder="" clearable @keyup.native="finance.proxyPurchasetax = oninput(finance.proxyPurchasetax, 0)"/>
              </el-form-item>
            </div>
            <div v-if="finance.dealWayKey == '02' && finance.isPack == '1'">
              <div class="span-sty">抵顶购置税</div>
              <el-form-item>
                <el-input class="addinputw" v-model="finance.offsetPurchasetax" :disabled="!finance.purchaseTaxCb || this.finance.purchaseTax == ''" placeholder="" clearable @keyup.native="finance.offsetPurchasetax = oninput(finance.offsetPurchasetax, 0)"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6"></el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <div class="span-sty">名义(留购)价</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.nominalPrice" placeholder="" clearable @keyup.native="finance.nominalPrice = oninput(finance.nominalPrice, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item><span class="addinputInfo">注：结清时需缴纳此费用</span></el-form-item>
          </el-col>
        </el-row>
      </div>
      <div v-else>
        <el-row style="border-top: 1px solid #E0E3EB">
          <el-col :span="24">
            <div class="span-sty">产品政策</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.policyName" placeholder="" clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <div class="span-sty"><span class="icon">*</span>厂家贴息</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.factoryDiscount" placeholder="" clearable @keyup.native="finance.factoryDiscount = oninput(finance.factoryDiscount, 0)"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty"><span class="icon">*</span>贴息用途</div>
            <el-form-item>
              <span class="addinputInfo">{{ finance.factoryDiscountUse }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty"><span class="icon">*</span>贴息抵车款</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.discountCar" placeholder="" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div class="span-sty"><span class="icon">*</span>服务费</div>
            <el-form-item>
              <el-input class="addinputw" v-model="finance.serviceAmount" placeholder="" clearable @keyup.native="finance.serviceAmount = oninput(finance.serviceAmount, 0)"/>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </div>
</template>

<script>
import { typeValues, selectList, selectListByOther } from '@/api/dictcommons/dictcommons'
import { calculate } from '@/api/salesManagement/orderManagement'

export default {
  name: 'JinRongFangAn',
  data() {
    return {
      viewTitle: '',
      submitdisabled: false,
      isShowMore: false,
      dealWay_list: [],
      policy_list: [],
      other_list: [],
      finance: {
        accessoriesAmount: '',
        accessoriesAmountCb: false,
        bondAmount: '',
        bondAmounts: '',
        bondRatio: '',
        dealWay: '',
        dealWayKey: '',
        depositPremium: '',
        depositSettle: '',
        downPayAmount: '',
        downPayAmounts: '',
        downPayAmountsRatio: '',
        downPayRatio: '',
        factoryDiscount: '',
        factoryDiscountUse: '',
        interest: '',
        isPack: '',
        isShowMore: false,
        loanAmount: '',
        loanAmountTotal: '',
        loanDownPay: '',
        loanInterest: '',
        loanPayMoney: '',
        loanPeriod: '',
        loanTotal: '',
        mainVehicleAmount: '',
        monthlyRepay: '',
        nominalPrice: '',
        offsetPremium: '',
        offsetPurchasetax: '',
        offsetTotal: '',
        operationAmount: '',
        otherAmount: '',
        otherAmountRemark: '',
        otherPolicyAmount: '',
        otherPolicyInterest: '',
        otherPolicyMonthlyRepay: '',
        otherPolicyName: '',
        otherPolicyPeriod: '',
        otherPolicySid: '',
        otherPolicyState: false,
        otherPolicyYearRatio: '',
        period: '',
        policyName: '',
        policySid: '',
        policyYearRatio: '',
        premium: '',
        premiumCb: false,
        proxyAccidentPremium: '',
        proxyPremium: '',
        proxyPurchasetax: '',
        purchaseTax: '',
        purchaseTaxCb: false,
        realTotal: '',
        receivableTotal: '',
        registerAmount: '',
        returnTime: '',
        serviceAmount: '',
        saleOrderSid: '',
        trailerAmount: '',
        trailerAmountCb: false,
        vehOtherPrice: '',
        vehTotalPrice: ''
      },
      rules: {
      }
    }
  },
  methods: {
    init() {
      typeValues({ type: 'dealWay' }).then((resp) => {
        if (resp.success) {
          this.dealWay_list = resp.data
        }
      })
    },
    // 输入数字正则
    oninput(val, limit = 0) {
      val = val.replace(/[^\d]/g, '') // 保留数字
      val = val.replace(/^00/, '0'); // 开头不能有两个0
      /^0\d+/.test(val) ? val = val.slice(1) : '' // 两位以上数字开头不能为0
      const str = '^(\\d+)\\.(\\d{' + limit + '}).*$'
      const reg = new RegExp(str)
      if (limit === 0) {
        // 不需要小数点
        val = val.replace(reg, '$1')
      } else {
        // 通过正则保留小数点后指定的位数
        val = val.replace(reg, '$1.$2')
      }
      return val
    },
    showAdd(value) {
      this.finance = JSON.parse(JSON.stringify(value))
      this.isShowMore = this.finance.isShowMore
      this.init()
    },
    showEdit(value) {
      this.finance = JSON.parse(JSON.stringify(value))
      this.init()
      selectList({ orgPath: window.sessionStorage.getItem('defaultOrgPath'), userSid: window.sessionStorage.getItem('userSid'), isPack: this.finance.isPack }).then((resp) => {
        if (resp.success) {
          this.policy_list = resp.data
        }
      })
      selectListByOther({ orgPath: window.sessionStorage.getItem('defaultOrgPath'), userSid: window.sessionStorage.getItem('userSid'), sid: this.finance.policySid }).then((res) => {
        if (res.success) {
          this.other_list = res.data
        }
      })
    },
    // 是否打包
    isPackChange(val) {
      this.finance = {
        accessoriesAmount: '',
        accessoriesAmountCb: false,
        bondAmount: '',
        bondAmounts: '',
        bondRatio: '',
        dealWay: '',
        dealWayKey: '',
        depositPremium: '',
        depositSettle: '',
        downPayAmount: '',
        downPayAmounts: '',
        downPayAmountsRatio: '',
        downPayRatio: '',
        factoryDiscount: '',
        factoryDiscountUse: '',
        interest: '',
        isPack: val,
        isShowMore: false,
        loanAmount: '',
        loanAmountTotal: '',
        loanDownPay: '',
        loanInterest: '',
        loanPayMoney: '',
        loanPeriod: '',
        loanTotal: '',
        mainVehicleAmount: '',
        monthlyRepay: '',
        nominalPrice: '',
        offsetPremium: '',
        offsetPurchasetax: '',
        offsetTotal: '',
        operationAmount: '',
        otherAmount: '',
        otherAmountRemark: '',
        otherPolicyAmount: '',
        otherPolicyInterest: '',
        otherPolicyMonthlyRepay: '',
        otherPolicyName: '',
        otherPolicyPeriod: '',
        otherPolicySid: '',
        otherPolicyState: false,
        otherPolicyYearRatio: '',
        period: '',
        policyName: '',
        policySid: '',
        policyYearRatio: '',
        premium: '',
        premiumCb: false,
        proxyAccidentPremium: '',
        proxyPremium: '',
        proxyPurchasetax: '',
        purchaseTax: '',
        purchaseTaxCb: false,
        realTotal: '',
        receivableTotal: '',
        registerAmount: '',
        returnTime: '',
        serviceAmount: '',
        saleOrderSid: '',
        trailerAmount: '',
        trailerAmountCb: false,
        vehOtherPrice: '',
        vehTotalPrice: ''
      }
      selectList({ orgPath: window.sessionStorage.getItem('defaultOrgPath'), userSid: window.sessionStorage.getItem('userSid'), isPack: this.finance.isPack }).then((resp) => {
        if (resp.success) {
          this.policy_list = resp.data
        }
      })
    },
    // 资方产品政策
    changePolicy(value) {
      const choose = this.policy_list.filter((item) => item.name === value)
      if (choose.length > 0 && choose !== null) {
        this.finance.policySid = choose[0].sid
        // 从金融产品政策中-- 获取标准年利率
        this.finance.policyYearRatio = choose[0].yearRatio
        // 从金融产品政策中-- 获取首付比例
        this.finance.downPayRatio = choose[0].downPayRatio
        // 从金融产品政策中-- 获取期数
        this.finance.loanPeriod = choose[0].period
        // 从金融产品政策中-- 获取贷款保证金比例
        this.finance.bondRatio = choose[0].bondRatio
        // 贴息用途赋值
        this.finance.factoryDiscountUse = '抵利息'
        this.finance.factoryDiscountUseKey = '02'
        selectListByOther({ orgPath: window.sessionStorage.getItem('defaultOrgPath'), userSid: window.sessionStorage.getItem('userSid'), sid: choose[0].sid }).then((res) => {
          if (res.success) {
            this.other_list = res.data
          }
        })
      } else {
        this.finance.policySid = ''
      }
    },
    // 主方案 -- 融资项目总额
    financingProject() {
      if (!this.finance.accessoriesAmountCb && !this.finance.trailerAmountCb && !this.finance.premiumCb && !this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount))
      } else if (this.finance.accessoriesAmountCb && !this.finance.trailerAmountCb && !this.finance.premiumCb && !this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 配件
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.accessoriesAmount))
      } else if (this.finance.accessoriesAmountCb && this.finance.trailerAmountCb && !this.finance.premiumCb && !this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 配件 + 挂车
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.accessoriesAmount)) + parseInt(this.arguments(this.finance.trailerAmount))
      } else if (this.finance.accessoriesAmountCb && !this.finance.trailerAmountCb && this.finance.premiumCb && !this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 配件 + 保险
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.accessoriesAmount)) + parseInt(this.arguments(this.finance.premium))
      } else if (this.finance.accessoriesAmountCb && !this.finance.trailerAmountCb && !this.finance.premiumCb && this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 配件 + 购置税
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.accessoriesAmount)) + parseInt(this.arguments(this.finance.purchaseTax))
      } else if (this.finance.accessoriesAmountCb && this.finance.trailerAmountCb && this.finance.premiumCb && !this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 配件 + 挂车 + 保险
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.accessoriesAmount)) + parseInt(this.arguments(this.finance.trailerAmount)) + parseInt(this.arguments(this.finance.premium))
      } else if (this.finance.accessoriesAmountCb && this.finance.trailerAmountCb && !this.finance.premiumCb && this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 配件 + 挂车 + 购置税
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.accessoriesAmount)) + parseInt(this.arguments(this.finance.trailerAmount)) + parseInt(this.arguments(this.finance.purchaseTax))
      } else if (this.finance.accessoriesAmountCb && !this.finance.trailerAmountCb && this.finance.premiumCb && this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 配件 + 保险 + 购置税
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.accessoriesAmount)) + parseInt(this.arguments(this.finance.premium)) + parseInt(this.arguments(this.finance.purchaseTax))
      } else if (!this.finance.accessoriesAmountCb && this.finance.trailerAmountCb && !this.finance.premiumCb && !this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 挂车
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.trailerAmount))
      } else if (!this.finance.accessoriesAmountCb && this.finance.trailerAmountCb && this.finance.premiumCb && !this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 挂车 + 保险
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.trailerAmount)) + parseInt(this.arguments(this.finance.premium))
      } else if (!this.finance.accessoriesAmountCb && this.finance.trailerAmountCb && !this.finance.premiumCb && this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 挂车 + 购置税
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.trailerAmount)) + parseInt(this.arguments(this.finance.purchaseTax))
      } else if (!this.finance.accessoriesAmountCb && this.finance.trailerAmountCb && this.finance.premiumCb && this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 挂车 + 保险 + 购置税
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.trailerAmount)) + parseInt(this.arguments(this.finance.premium)) + parseInt(this.arguments(this.finance.purchaseTax))
      } else if (!this.finance.accessoriesAmountCb && !this.finance.trailerAmountCb && this.finance.premiumCb && !this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 保险
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.premium))
      } else if (!this.finance.accessoriesAmountCb && !this.finance.trailerAmountCb && this.finance.premiumCb && this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 保险 + 购置税
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.premium)) + parseInt(this.arguments(this.finance.purchaseTax))
      } else if (!this.finance.accessoriesAmountCb && !this.finance.trailerAmountCb && !this.finance.premiumCb && this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 购置税
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.purchaseTax))
      } else if (this.finance.accessoriesAmountCb && this.finance.trailerAmountCb && this.finance.premiumCb && this.finance.purchaseTaxCb) {
        // 融资项目总额 = 主车发票价 + 配件 + 挂车 + 保险 + 购置税
        this.finance.loanTotal = parseInt(this.arguments(this.finance.mainVehicleAmount)) + parseInt(this.arguments(this.finance.accessoriesAmount)) + parseInt(this.arguments(this.finance.trailerAmount)) + parseInt(this.arguments(this.finance.premium)) + parseInt(this.arguments(this.finance.purchaseTax))
      }
      // 首次计算出或修改融资项目总额
      if (this.finance.loanTotal !== '') {
        // 主方案 -- 计算首付金额
        this.finance.downPayAmount = Math.round(parseInt(this.arguments(this.finance.loanTotal)) * parseFloat(this.arguments(this.finance.downPayRatio)) / 100)
        // 主方案 -- 计算产品贷款金额
        this.finance.loanAmount = Math.round(parseInt(this.arguments(this.finance.loanTotal)) - parseInt(this.arguments(this.finance.downPayAmount)))
        // 主方案 -- 计算贷款保证金
        this.finance.bondAmount = Math.round(parseFloat(this.arguments(this.finance.bondRatio)) * parseInt(this.arguments(this.finance.loanAmount)) / 100)
      }
      if (this.finance.premiumCb && this.finance.dealWayKey === '02') {
        this.finance.offsetPremium = this.finance.premium
      } else {
        this.finance.offsetPremium = ''
      }
      if (this.finance.purchaseTaxCb && this.finance.dealWayKey === '02') {
        this.finance.offsetPurchasetax = this.finance.purchaseTax
      } else {
        this.finance.offsetPurchasetax = ''
      }
    },
    // 主方案 -- 修改首付金额
    downPayAmountInput() {
      if (this.finance.loanTotal !== '') {
        // 重新计算首付比例
        this.finance.downPayRatio = (parseInt(this.arguments(this.finance.downPayAmount)) / parseInt(this.arguments(this.finance.loanTotal)) * 100).toPrecision(2)
        // 重新计算产品贷款金额
        this.finance.loanAmount = Math.round(parseInt(this.arguments(this.finance.loanTotal)) - parseInt(this.arguments(this.finance.downPayAmount)))
        // 主方案 -- 贷款保证金
        this.finance.bondAmount = Math.round(parseFloat(this.arguments(this.finance.bondRatio)) * parseInt(this.arguments(this.finance.loanAmount)) / 100)
      }
    },
    // 主方案 -- 修改贷款保证金，重新计算贷款保证金比例
    bondAmountInput() {
      if (this.finance.bondAmount !== '') {
        this.finance.bondRatio = (parseInt(this.arguments(this.finance.bondAmount)) / parseInt(this.arguments(this.finance.loanAmount)) * 100).toPrecision(2)
      }
    },
    // 其它融--名称
    changeOtherPolicy(value) {
      const choose = this.other_list.filter((item) => item.name === value)
      if (choose.length > 0 && choose !== null) {
        this.finance.otherPolicySid = choose[0].sid
        this.finance.otherPolicyYearRatio = choose[0].yearRatio
        this.finance.otherPolicyPeriod = choose[0].period
      } else {
        this.finance.otherPolicySid = ''
      }
    },
    // 应收明细 —— 贷款保证金
    bondAmounts() {
      if (this.finance.bondAmount !== '') {
        this.finance.bondAmounts = this.finance.bondAmount
      }
      return this.finance.bondAmount
    },
    changeDealWay(value) {
      const choose = this.dealWay_list.filter((item) => item.dictValue === value)
      if (choose.length > 0 && choose !== null) {
        this.finance.dealWayKey = choose[0].dictKey
      } else {
        this.finance.dealWayKey = ''
      }
      if (this.finance.dealWayKey === '02') {
        this.finance.proxyPremium = '0'
        this.finance.proxyPurchasetax = '0'
        if (this.finance.premiumCb) {
          this.finance.offsetPremium = this.finance.premium
        } else {
          this.finance.offsetPremium = ''
        }
        if (this.finance.purchaseTaxCb) {
          this.finance.offsetPurchasetax = this.finance.purchaseTax
        } else {
          this.finance.offsetPurchasetax = ''
        }
      } else if (this.finance.dealWayKey === '01') {
        this.finance.offsetPremium = '0'
        this.finance.offsetPurchasetax = '0'
        this.finance.proxyPremium = ''
        this.finance.proxyPurchasetax = ''
      } else {
        this.finance.offsetPremium = '0'
        this.finance.offsetPurchasetax = '0'
        this.finance.proxyPremium = '0'
        this.finance.proxyPurchasetax = '0'
      }
    },
    arguments(val) {
      if (val === '' || val === undefined) {
        val = 0
        return val
      } else {
        return val
      }
    },
    handleConfirm() {
      if (this.finance.dealWayKey === '01' && this.finance.isPack === '0') {
        if (this.arguments(this.finance.premium) !== this.arguments(this.finance.proxyPremium)) {
          this.$message({ showClose: true, type: 'error', message: '代收首年保险费数额与融资项目中的保险数额应保持一致' })
          return
        }
        if (this.arguments(this.finance.purchaseTax) !== this.arguments(this.finance.proxyPurchasetax)) {
          this.$message({ showClose: true, type: 'error', message: '代收购置税数额与融资项目中的购置税数额应保持一致' })
          return
        }
      }
      if (this.finance.dealWayKey === '02' && this.finance.isPack === '1') {
        if (parseInt(this.arguments(this.finance.premium)) < parseInt(this.arguments(this.finance.offsetPremium))) {
          this.$message({ showClose: true, type: 'error', message: '抵顶首年保险费数额不能大于融资项目中的保险数额' })
          return
        }
        if (parseInt(this.arguments(this.finance.purchaseTax)) < parseInt(this.arguments(this.finance.offsetPurchasetax))) {
          this.$message({ showClose: true, type: 'error', message: '抵顶购置税数额不能大于融资项目中的购置税数额' })
          return
        }
      }
      this.submitdisabled = true
      if (this.isShowMore) {
        calculate(this.finance).then((resp) => {
          if (resp.success) {
            this.submitdisabled = false
            console.log(resp.data)
            this.finance.policyYearRatio = resp.data.policyYearRatio
            this.finance.loanPayMoney = resp.data.loanPayMoney
            this.finance.loanInterest = resp.data.loanInterest
            this.finance.otherPolicyMonthlyRepay = resp.data.otherPolicyMonthlyRepay
            this.finance.otherPolicyInterest = resp.data.otherPolicyInterest
            this.finance.loanDownPay = resp.data.loanDownPay
            this.finance.downPayAmountsRatio = resp.data.downPayAmountsRatio
            this.finance.loanAmountTotal = resp.data.loanAmountTotal
            this.finance.period = resp.data.period
            this.finance.monthlyRepay = resp.data.monthlyRepay
            this.finance.interest = resp.data.interest
            this.finance.downPayAmounts = resp.data.downPayAmounts
            this.finance.proxyTotal = resp.data.proxyTotal
            this.finance.receivableTotal = resp.data.receivableTotal
            this.finance.offsetTotal = resp.data.offsetTotal
            this.finance.realTotal = resp.data.realTotal
            this.finance.vehTotalPrice = resp.data.vehTotalPrice
            this.$emit('backData', this.finance)
            this.handleColse()
          } else {
            this.submitdisabled = false
          }
        }).catch(() => {
          this.submitdisabled = false
        })
      } else {
        this.$emit('backData', this.finance)
        this.submitdisabled = false
        this.handleColse()
      }
    },
    handleColse() {
      this.finance = {
        saleOrderSid: '',
        type: '',
        isPack: '',
        policyName: '',
        policySid: '',
        mainVehicleAmount: '',
        accessoriesAmount: '',
        trailerAmount: '',
        purchaseTax: '',
        premium: '',
        loanTotal: '',

        accessoriesAmountCb: false,
        trailerAmountCb: false,
        purchaseTaxCb: false,
        premiumCb: false,

        downPayRatio: '',
        downPayAmount: '',
        loanAmount: '',
        loanPeriod: '',
        bondRatio: '',
        bondAmount: '',
        factoryDiscount: '',
        factoryDiscountUse: '',
        factoryDiscountUseKey: '',
        policyYearRatio: '',
        loanPayMoney: '',
        loanInterest: '',
        discountCar: '',
        otherPolicyState: false,

        otherPolicyName: '',
        otherPolicySid: '',
        otherPolicyAmount: '',
        otherPolicyPeriod: '',

        otherPolicyMonthlyRepay: '',
        otherPolicyInterest: '',
        otherPolicyYearRatio: '',

        loanDownPay: '',
        downPayAmountsRatio: '',
        loanAmountTotal: '',
        period: '',
        monthlyRepay: '',
        interest: '',
        returnTime: '',

        downPayAmounts: '',
        bondAmounts: '',
        depositPremium: '',
        depositSettle: '',
        serviceAmount: '',
        proxyAccidentPremium: '',
        registerAmount: '',
        operationAmount: '',
        vehOtherPrice: '',
        otherAmount: '',
        otherAmountRemark: '',
        dealWay: '',
        dealWayKey: '',
        proxyPremium: '',
        proxyPurchasetax: '',
        proxyTotal: '',
        offsetPremium: '',
        offsetPurchasetax: '',
        offsetTotal: '',
        receivableTotal: '',
        realTotal: '',
        vehTotalPrice: '',
        nominalPrice: ''
      }
      this.isShowMore = false
      this.$emit('doback')
    }
  }
}
</script>

<style scoped>
.formaddcopy02 {
  padding: 0px;
}
.span-sty {
  width: 140px !important;
}

.addinputInfo {
  margin-left: 130px !important;
}
.titleOne {
  padding: 7px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
.addinputw {
  margin-left: 130px !important;
}

.formaddcopy02 .el-row .el-col /deep/ .el-form-item .addinputw {
  margin-left: 130px !important;
  width: calc(100% - 125px);
}

/deep/ .el-form-item__error {
  margin-left: 120px !important;
}
</style>
