<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <!--标题按钮部分开始-->
      <div class="tab-header webtop">
        <!--标题-->
        <div>{{ viewTitle }}</div>
        <!--start 添加修改按钮-->
        <div>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="save()">保存</el-button>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="submit()">提交</el-button>
          <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
        </div>
      </div>
      <!--标题按钮部分结束-->
      <!--Start 新增修改部分-->
      <div class="listconadd">
        <div class="titwu"><span>{{ formobj.checkApply }}单车返利上传核对审核申请</span></div>
        <el-form ref="form_obj" :model="formobj" :rules="rules" class="formaddcopy02">
          <el-row style="border-top: 1px solid #E0E3EB">
            <el-col :span="16">
              <div class="span-sty">申请编号</div>
              <el-form-item><span class="addinputInfo">{{ formobj.billNo }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">申请日期</div>
              <el-form-item><span class="addinputInfo">{{ formobj.createTime }}</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.createOrgName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">申请部门</div>
              <el-form-item><span class="addinputInfo">{{ formobj.deptName }}</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-row>
          <div class="title titleOne">
            <div>本月上传核对情况(按返利类型统计)</div>
            <el-button size="mini" type="primary" class="btntopblueline" @click="handleSelect()">选择</el-button>
          </div>
          <el-table :key="tableKey" :data="formobj.scmWhenVehrebateCheckapplydetailDetails" :index="index" border style="width: 100%" show-summary :summary-method="getSummaries">
            <el-table-column fixed width="80px" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column prop="purchaseSystemName" label="采购系统" align="center" min-width="120" />
            <el-table-column prop="brandName" label="品牌" align="center" min-width="120" />
            <el-table-column prop="rebateTypeValue" label="返利类型" align="center" min-width="120" />
            <el-table-column prop="num" label="数量" align="center" min-width="100" />
            <el-table-column prop="withRebate" label="预提金额" align="center" min-width="120" />
            <el-table-column prop="withholdCost" label="其中预提费用" align="center" min-width="140" />
            <el-table-column prop="uploadMoney" label="上传金额" align="center" min-width="120" />
            <el-table-column prop="stayDetermineMoney" label="其中待确认金额" align="center" min-width="150" />
            <el-table-column prop="money" label="其中费用" align="center" min-width="120" />
            <el-table-column prop="adjustmentMoney" label="返利调整金额" align="center" min-width="140" />
            <el-table-column prop="fyAdjustmentMoney" label="其中费用调整金额" align="center" min-width="160" />
            <el-table-column label="明细" align="center" min-width="100">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handleLook(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="title">本月上传核对情况(按预提年月统计)</div>
          <el-table :key="lastVehrebateKey" :data="formobj.scmLastVehrebateCheckapplydetailDetails" border style="width: 100%">
            <el-table-column fixed width="80px" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column prop="purchaseSystemName" label="采购系统" align="center" min-width="120" />
            <el-table-column prop="brandName" label="品牌" align="center" min-width="120" />
            <el-table-column prop="withholdingDate" label="预提年月" align="center" min-width="120" />
            <el-table-column prop="num" label="预提数量" align="center" min-width="100" />
            <el-table-column prop="withRebate" label="预提金额" align="center" min-width="120" />
            <el-table-column prop="withholdCost" label="其中预提费用" align="center" min-width="140" />
            <el-table-column prop="uploadMoney" label="上传金额" align="center" min-width="120" />
            <el-table-column prop="stayDetermineMoney" label="其中待确认金额" align="center" min-width="150" />
            <el-table-column prop="money" label="其中费用" align="center" min-width="120" />
            <el-table-column prop="adjustmentMoney" label="返利调整金额" align="center" min-width="140" />
            <el-table-column prop="fyAdjustmentMoney" label="其中费用调整金额" align="center" min-width="160" />
            <el-table-column label="明细" align="center" min-width="100">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handleLook(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">备注</div>
              <el-form-item><el-input v-model="formobj.remarks" clearable placeholder="" class="addinputInfo addinputw"></el-input></el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <!--End 添加修改部分-->
    <yiheduidanchefanli v-show="viewState == 2" ref="divSelect" @backData="backData" @doback="resetState"/>
    <bicyclerebatetobecheckedAdd v-show="viewState == 3" ref="divAdd" @backAdd="backAdd" @doback="resetState"/>
  </div>
</template>

<script>
import req from '@/api/bikerebate/bicyclerebatecheck'
import { fetchBySid } from '@/api/cheliang/dictcommons'
import yiheduidanchefanli from './relation/yiheduidanchefanli'
import bicyclerebatetobecheckedAdd from './relation/bicyclerebatetobecheckedAdd'

export default {
  name: 'BicycleRebateCheckAdd',
  components: {
    yiheduidanchefanli,
    bicyclerebatetobecheckedAdd
  },
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      index: 0,
      tableKey: 0,
      lastVehrebateKey: 1,
      // 表单数据
      formobj: {
        sid: '',
        createByName: '',
        createBySid: '',
        checkApply: '',
        billNo: '',
        createTime: '',
        createOrgName: '',
        deptName: '',
        deptSid: '',
        createOrgSid: '',
        useOrgName: '',
        useOrgSid: '',
        remarks: '',
        instanceId: '', // 流程实例ID
        taskId: '', // 任务ID
        orgPath: '',
        scmWhenVehrebateCheckapplydetailDetails: [],
        scmLastVehrebateCheckapplydetailDetails: [],
        scmVehrebateCheckapplydetailList: []
      },
      rules: {},
      submitdisabled: false
    }
  },
  methods: {
    // 获取制单日期
    newDate() {
      let date = new Date()
      let year = date.getFullYear() // 本年
      let month = date.getMonth() // 上一月
      let day = date.getDate() // 本日
      if (month < 1 && day < 7) {
        month = 12
        year = date.getFullYear() - 1
      } else if (month < 1 && day > 6) {
        month = date.getMonth() + 1 // 本月
      } else if (month > 0 && day > 6) {
        month = date.getMonth() + 1 // 本月
      } else if (month > 0 && day < 7) {
        month = date.getMonth() // 上一月
      }
      this.formobj.checkApply = year + '年' + month + '月'
    },
    init() {
      fetchBySid(this.formobj.createOrgSid).then((res) => {
        if (res.success) {
          this.formobj.createOrgName = res.data.name
          this.formobj.useOrgName = res.data.name
        }
      })
    },
    showAdd(createOrgSid) {
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
        this.init()
        this.newDate()
      })
      this.formobj.createOrgSid = createOrgSid
      this.formobj.useOrgSid = createOrgSid
      this.formobj.createByName = window.sessionStorage.getItem('name')
      this.formobj.createBySid = window.sessionStorage.getItem('userSid')
      this.formobj.orgPath = window.sessionStorage.getItem('defaultOrgPath')
      this.formobj.deptName = window.sessionStorage.getItem('defaultOrgPathName').substring(window.sessionStorage.getItem('defaultOrgPathName').lastIndexOf('/') + 1)
      this.formobj.deptSid = window.sessionStorage.getItem('defaultOrgPath').substring(window.sessionStorage.getItem('defaultOrgPath').lastIndexOf('/') + 1)
      var nowDate = new Date()
      var date = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1,
        day: nowDate.getDate()
      }
      this.formobj.createTime = date.year + '-' + (date.month >= 10 ? date.month : '0' + date.month) + '-' + (date.day >= 10 ? date.day : '0' + date.day)
      this.viewTitle = '【新增】单车返利核对审核申请'
    },
    showEdit(row) {
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      this.viewTitle = '【编辑】单车返利核对审核申请'
      console.log('编辑回显', row.sid)
      req.fetchDetailsBySid(row.sid).then((resp) => {
        if (resp.success) {
          this.formobj = resp.data
          this.formobj.instanceId = resp.data.procInstId
        }
      }).catch((e) => {
        this.formobj = row
      })
    },
    // 明细表添加一行数据
    handleSelect() {
      this.viewState = 2
      this.$refs['divSelect'].showData(this.formobj.scmWhenVehrebateCheckapplydetailDetails, this.formobj.createOrgSid)
    },
    backData(val) {
      this.viewState = 1
      var list = JSON.parse(JSON.stringify(val))
      // 同一数据源
      list.forEach((e) => {
        this.formobj.scmVehrebateCheckapplydetailList.push(e)
      })
      // 将同一数据源进行拆分(1.按照返利类型统计 2.按照预提年月统计)
      // 按照返利类型统计
      list.forEach((e) => {
        let v = 0
        for (var i in this.formobj.scmWhenVehrebateCheckapplydetailDetails) {
          if (this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].rebateTypeValue === e.rebateTypeValue && this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].purchaseSystemName === e.purchaseSystemName && this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].brandName === e.brandName) {
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].num = parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].num) + parseFloat(1)
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withRebate = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withRebate !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withRebate : 0) + parseFloat(e.estimateRebate !== '' ? e.estimateRebate : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withholdCost = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withholdCost !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withholdCost : 0) + parseFloat(e.withholdCost !== '' ? e.withholdCost : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].uploadMoney = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].uploadMoney !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].uploadMoney : 0) + (parseFloat(e.uploadMoney !== '' ? e.uploadMoney : 0) + parseFloat(e.secondaryUploadMoney !== '' ? e.secondaryUploadMoney : 0))) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].stayDetermineMoney = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].stayDetermineMoney !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].stayDetermineMoney : 0) + parseFloat(e.stayDetermineMoney !== '' ? e.stayDetermineMoney : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].adjustmentMoney = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].adjustmentMoney !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].adjustmentMoney : 0) + parseFloat(e.adjustmentMoney !== '' ? e.adjustmentMoney : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].money = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].money !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].money : 0) + parseFloat(e.money !== '' ? e.money : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].fyAdjustmentMoney = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].fyAdjustmentMoney !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].fyAdjustmentMoney : 0) + (parseFloat(e.money !== '' ? e.money : 0) - parseFloat(e.withholdCost !== '' ? e.withholdCost : 0))) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].vehRebateSids.push(e.sid)
            v = 1
            break
          }
        }
        if (v === 0) {
          // 第一次对比没有参照，放入参照物
          this.formobj.scmWhenVehrebateCheckapplydetailDetails.push({
            rebateTypeKey: e.rebateTypeKey,
            rebateTypeValue: e.rebateTypeValue,
            num: 1,
            withRebate: e.estimateRebate,
            withholdCost: e.withholdCost,
            brandName: e.brandName,
            brandSid: e.brandSid,
            purchaseSystemName: e.purchaseSystemName,
            purchaseSystemSid: e.purchaseSystemSid,
            uploadMoney: Math.round((parseFloat(e.uploadMoney !== '' ? e.uploadMoney : 0) + parseFloat(e.secondaryUploadMoney !== '' ? e.secondaryUploadMoney : 0)) * 100) / 100,
            stayDetermineMoney: e.stayDetermineMoney,
            adjustmentMoney: e.adjustmentMoney,
            money: e.money,
            fyAdjustmentMoney: Math.round((parseFloat(e.money !== '' ? e.money : 0) - parseFloat(e.withholdCost !== '' ? e.withholdCost : 0)) * 100) / 100,
            vehRebateSids: [e.sid],
            sid: '',
            mainSid: ''
          })
        }
      })
      let k = 0
      // 按照预提年月统计
      list.forEach((s) => {
        for (var l in this.formobj.scmLastVehrebateCheckapplydetailDetails) {
          if (this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withholdingDate === s.withholdingDate && this.formobj.scmLastVehrebateCheckapplydetailDetails[l].purchaseSystemName === s.purchaseSystemName && this.formobj.scmLastVehrebateCheckapplydetailDetails[l].brandName === s.brandName) {
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].num = parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].num) + parseFloat(1)
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withRebate = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withRebate !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withRebate : 0) + parseFloat(s.estimateRebate !== '' ? s.estimateRebate : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withholdCost = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withholdCost !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withholdCost : 0) + parseFloat(s.withholdCost !== '' ? s.withholdCost : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].uploadMoney = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].uploadMoney !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].uploadMoney : 0) + (parseFloat(s.uploadMoney !== '' ? s.uploadMoney : 0) + parseFloat(s.secondaryUploadMoney !== '' ? s.secondaryUploadMoney : 0))) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].stayDetermineMoney = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].stayDetermineMoney !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].stayDetermineMoney : 0) + parseFloat(s.stayDetermineMoney !== '' ? s.stayDetermineMoney : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].adjustmentMoney = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].adjustmentMoney !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].adjustmentMoney : 0) + parseFloat(s.adjustmentMoney !== '' ? s.adjustmentMoney : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].money = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].money !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].money : 0) + parseFloat(s.money !== '' ? s.money : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].fyAdjustmentMoney = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].fyAdjustmentMoney !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].fyAdjustmentMoney : 0) + (parseFloat(s.money !== '' ? s.money : 0) - parseFloat(s.withholdCost !== '' ? s.withholdCost : 0))) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].vehRebateSids.push(s.sid)
            k = 1
            break
          }
        }
        if (k === 0) {
          this.formobj.scmLastVehrebateCheckapplydetailDetails.push({
            withholdingDate: s.withholdingDate,
            num: 1,
            withRebate: s.estimateRebate,
            withholdCost: s.withholdCost,
            brandName: s.brandName,
            brandSid: s.brandSid,
            purchaseSystemName: s.purchaseSystemName,
            purchaseSystemSid: s.purchaseSystemSid,
            uploadMoney: Math.round((parseFloat(s.uploadMoney !== '' ? s.uploadMoney : 0) + parseFloat(s.secondaryUploadMoney !== '' ? s.secondaryUploadMoney : 0)) * 100) / 100,
            stayDetermineMoney: s.stayDetermineMoney,
            adjustmentMoney: s.adjustmentMoney,
            money: s.money,
            fyAdjustmentMoney: Math.round((parseFloat(s.money !== '' ? s.money : 0) - parseFloat(s.withholdCost !== '' ? s.withholdCost : 0)) * 100) / 100,
            vehRebateSids: [s.sid],
            sid: '',
            mainSid: ''
          })
        }
      })
    },
    // 明细表删除一行数据
    dataDelete(index) {
      this.formobj.baseVehinstallModpricedetails.splice(index, 1)
    },
    handleLook(row) {
      this.viewState = 3
      this.$refs['divAdd'].showInfo(row.vehRebateSids)
    },
    backAdd(val) {
      this.viewState = 1
      // 先将数据清空
      this.formobj.scmWhenVehrebateCheckapplydetailDetails = []
      this.formobj.scmLastVehrebateCheckapplydetailDetails = []
      var list = JSON.parse(JSON.stringify(val))
      // 同一数据源
      for (var n = 0; n < list.length; n++) {
        for (var m = 0; m < this.formobj.scmVehrebateCheckapplydetailList.length; m++) {
          if (list[n] === this.formobj.scmVehrebateCheckapplydetailList[m].sid) {
            this.formobj.scmVehrebateCheckapplydetailList.splice(this.formobj.scmVehrebateCheckapplydetailList[m], 1)
          }
        }
      }
      // 将同一数据源进行拆分(1.按照返利类型统计 2.按照预提年月统计)
      // 按照返利类型统计
      this.formobj.scmVehrebateCheckapplydetailList.forEach((e) => {
        let v = 0
        for (var i in this.formobj.scmWhenVehrebateCheckapplydetailDetails) {
          if (this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].rebateTypeValue === e.rebateTypeValue && this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].purchaseSystemName === e.purchaseSystemName && this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].brandName === e.brandName) {
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].num = parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].num) + parseFloat(1)
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withRebate = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withRebate !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withRebate : 0) + parseFloat(e.estimateRebate !== '' ? e.estimateRebate : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withholdCost = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withholdCost !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].withholdCost : 0) + parseFloat(e.withholdCost !== '' ? e.withholdCost : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].uploadMoney = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].uploadMoney !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].uploadMoney : 0) + (parseFloat(e.uploadMoney !== '' ? e.uploadMoney : 0) + parseFloat(e.secondaryUploadMoney !== '' ? e.secondaryUploadMoney : 0))) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].stayDetermineMoney = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].stayDetermineMoney !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].stayDetermineMoney : 0) + parseFloat(e.stayDetermineMoney !== '' ? e.stayDetermineMoney : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].adjustmentMoney = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].adjustmentMoney !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].adjustmentMoney : 0) + parseFloat(e.adjustmentMoney !== '' ? e.adjustmentMoney : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].money = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].money !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].money : 0) + parseFloat(e.money !== '' ? e.money : 0)) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].fyAdjustmentMoney = Math.round((parseFloat(this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].fyAdjustmentMoney !== '' ? this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].fyAdjustmentMoney : 0) + (parseFloat(e.money !== '' ? e.money : 0) - parseFloat(e.withholdCost !== '' ? e.withholdCost : 0))) * 100) / 100
            this.formobj.scmWhenVehrebateCheckapplydetailDetails[i].vehRebateSids.push(e.sid)
            v = 1
            break
          }
        }
        if (v === 0) {
          // 第一次对比没有参照，放入参照物
          this.formobj.scmWhenVehrebateCheckapplydetailDetails.push({
            rebateTypeKey: e.rebateTypeKey,
            rebateTypeValue: e.rebateTypeValue,
            num: 1,
            withRebate: e.estimateRebate,
            withholdCost: e.withholdCost,
            brandName: e.brandName,
            brandSid: e.brandSid,
            purchaseSystemName: e.purchaseSystemName,
            purchaseSystemSid: e.purchaseSystemSid,
            uploadMoney: Math.round((parseFloat(e.uploadMoney !== '' ? e.uploadMoney : 0) + parseFloat(e.secondaryUploadMoney !== '' ? e.secondaryUploadMoney : 0)) * 100) / 100,
            stayDetermineMoney: e.stayDetermineMoney,
            adjustmentMoney: e.adjustmentMoney,
            money: e.money,
            fyAdjustmentMoney: Math.round((parseFloat(e.money !== '' ? e.money : 0) - parseFloat(e.withholdCost !== '' ? e.withholdCost : 0)) * 100) / 100,
            vehRebateSids: [e.sid],
            sid: '',
            mainSid: ''
          })
        }
      })
      let k = 0
      // 按照预提年月统计
      this.formobj.scmVehrebateCheckapplydetailList.forEach((s) => {
        for (var l in this.formobj.scmLastVehrebateCheckapplydetailDetails) {
          if (this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withholdingDate === s.withholdingDate && this.formobj.scmLastVehrebateCheckapplydetailDetails[l].purchaseSystemName === s.purchaseSystemName && this.formobj.scmLastVehrebateCheckapplydetailDetails[l].brandName === s.brandName) {
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].num = parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].num) + parseFloat(1)
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withRebate = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withRebate !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withRebate : 0) + parseFloat(s.estimateRebate !== '' ? s.estimateRebate : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withholdCost = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withholdCost !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].withholdCost : 0) + parseFloat(s.withholdCost !== '' ? s.withholdCost : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].uploadMoney = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].uploadMoney !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].uploadMoney : 0) + (parseFloat(s.uploadMoney !== '' ? s.uploadMoney : 0) + parseFloat(s.secondaryUploadMoney !== '' ? s.secondaryUploadMoney : 0))) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].stayDetermineMoney = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].stayDetermineMoney !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].stayDetermineMoney : 0) + parseFloat(s.stayDetermineMoney !== '' ? s.stayDetermineMoney : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].adjustmentMoney = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].adjustmentMoney !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].adjustmentMoney : 0) + parseFloat(s.adjustmentMoney !== '' ? s.adjustmentMoney : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].money = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].money !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].money : 0) + parseFloat(s.money !== '' ? s.money : 0)) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].fyAdjustmentMoney = Math.round((parseFloat(this.formobj.scmLastVehrebateCheckapplydetailDetails[l].fyAdjustmentMoney !== '' ? this.formobj.scmLastVehrebateCheckapplydetailDetails[l].fyAdjustmentMoney : 0) + (parseFloat(s.money !== '' ? s.money : 0) - parseFloat(s.withholdCost !== '' ? s.withholdCost : 0))) * 100) / 100
            this.formobj.scmLastVehrebateCheckapplydetailDetails[l].vehRebateSids.push(s.sid)
            k = 1
            break
          }
        }
        if (k === 0) {
          this.formobj.scmLastVehrebateCheckapplydetailDetails.push({
            withholdingDate: s.withholdingDate,
            num: 1,
            withRebate: s.estimateRebate,
            withholdCost: s.withholdCost,
            brandName: s.brandName,
            brandSid: s.brandSid,
            purchaseSystemName: s.purchaseSystemName,
            purchaseSystemSid: s.purchaseSystemSid,
            uploadMoney: Math.round((parseFloat(s.uploadMoney !== '' ? s.uploadMoney : 0) + parseFloat(s.secondaryUploadMoney !== '' ? s.secondaryUploadMoney : 0)) * 100) / 100,
            stayDetermineMoney: s.stayDetermineMoney,
            adjustmentMoney: s.adjustmentMoney,
            money: s.money,
            fyAdjustmentMoney: Math.round((parseFloat(s.money !== '' ? s.money : 0) - parseFloat(s.withholdCost !== '' ? s.withholdCost : 0)) * 100) / 100,
            vehRebateSids: [s.sid],
            sid: '',
            mainSid: ''
          })
        }
      })
    },
    resetState() {
      this.viewState = 1
    },
    save() {
      console.log(this.formobj)
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          req.save(this.formobj).then((resp) => {
            this.submitdisabled = false
            if (resp.success) {
              this.$message({ showClose: true, type: 'success', message: resp.msg })
              this.handleReturn('true')
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        } else {
          return false
        }
      })
    },
    submit() {
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          req.submitVehicleApply(this.formobj).then((resp) => {
            this.submitdisabled = false
            if (resp.success) {
              this.$message({ showClose: true, type: 'success', message: resp.msg })
              this.handleReturn('true')
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        } else {
          return false
        }
      })
    },
    // 合计
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (column.property !== 'purchaseSystemName' && column.property !== 'brandName' && column.property !== 'rebateTypeValue' && column.property !== undefined) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return Math.round((parseFloat(prev) + parseFloat(curr)) * 100) / 100
            } else {
              return Math.round((parseFloat(prev)) * 100) / 100
            }
          }, 0)
          sums[index] += ''
        }
      })
      return sums
    },
    // 返回（===既判断）
    handleReturn(isreload) {
      if (isreload === 'true') this.$emit('reloadlist')
      // 表单数据
      this.formobj = {
        sid: '',
        createByName: '',
        createBySid: '',
        checkApply: '',
        billNo: '',
        createTime: '',
        deptName: '',
        deptSid: '',
        createOrgName: '',
        createOrgSid: '',
        useOrgName: '',
        useOrgSid: '',
        remarks: '',
        instanceId: '', // 流程实例ID
        taskId: '', // 任务ID
        orgPath: '',
        scmWhenVehrebateCheckapplydetailDetails: [],
        scmLastVehrebateCheckapplydetailDetails: [],
        scmVehrebateCheckapplydetailList: []
      }
      this.$refs['form_obj'].resetFields()
      this.$emit('doback')
    }
  }
}
</script>
<style scoped>
.span-sty {
  width: 130px !important;
}
.addinputInfo {
  margin-left: 120px !important;
}
.formaddcopy02 .el-row .el-col /deep/ .el-form-item .addinputw {
  margin-left: 120px !important;
  width: calc(100% - 115px);
}
.titleOne {
  padding: 7px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
</style>

