<template>
  <div class="app-container">
    <div>
      <!--标题按钮部分开始-->
      <div class="tab-header webtop">
        <!--标题-->
        <div>{{ viewTitle }}</div>
        <!--start 添加修改按钮-->
        <div>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="handleSave()">保存</el-button>
          <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
        </div>
      </div>
      <!--标题按钮部分结束-->
      <!--Start 新增修改部分-->
      <div class="listconadd">
        <div class="titwu"><span>专项返利</span></div>
        <el-form ref="form_obj" :model="formobj" :rules="rules" class="formaddcopy02">
          <el-row style="border-top: 1px solid #e0e3eb">
            <el-col :span="8">
              <div class="span-sty">分公司</div>
              <el-form-item><span class="addinputInfo">{{ formobj.useOrgName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">采购系统</div>
              <el-form-item><span class="addinputInfo">{{ formobj.purchaseSystemName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">品牌</div>
              <el-form-item><span class="addinputInfo">{{ formobj.brandName }}</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.rebateTypeValue }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">返利名称</div>
              <el-form-item><span class="addinputInfo">{{ formobj.rebateName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">所属年月</div>
              <el-form-item><span class="addinputInfo">{{ formobj.palceGenDate }}</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.estimateRebate }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">预提日期</div>
              <el-form-item><span class="addinputInfo">{{ formobj.withholdingDate }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">状态</div>
              <el-form-item><span class="addinputInfo">{{ formobj.state == '0' ? '未计提' : formobj.state == '1' ? '计提中' : formobj.state == '2' ? '待核对' : formobj.state == '3' ? '核对中' : formobj.state == '4' ? '已核对' : formobj.state == '5' ? '分配中' : formobj.state == '6' ? '已分配' : '' }}</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.expectItureCost }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">其中待支付费用</div>
              <el-form-item><span class="addinputInfo">{{ formobj.expectTreatCost }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">其中抵顶费用</div>
              <el-form-item><span class="addinputInfo">{{ formobj.expectSuppCost }}</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">{{ formobj.expectSuppRemark }}</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">{{ formobj.remarks }}</span></el-form-item>
            </el-col>
          </el-row>
          <div class="title">一次上传</div>
          <el-row>
            <el-col :span="8">
              <div class="span-sty"><span class="icon">*</span>上传日期</div>
              <el-form-item prop="uploadDate"><el-date-picker class="addinputInfo" v-model="formobj.uploadDate" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty"><span class="icon">*</span>上传金额</div>
              <el-form-item prop="uploadMoney"><el-input class="addinputInfo addinputw" @input="isAdjustmentInput(formobj)" v-model="formobj.uploadMoney" @keyup.native="formobj.uploadMoney = getSpecialNumber(formobj.uploadMoney, 2)" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">其中待确定金额</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="isAdjustmentInput(formobj)" v-model="formobj.stayDetermineMoney" @keyup.native="formobj.stayDetermineMoney = getNumber(formobj.stayDetermineMoney, 2)" 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" @input="isAdjustmentInput(formobj)" v-model="formobj.onceItureCost" @keyup.native="formobj.onceItureCost = getNumber(formobj.onceItureCost, 2)" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">其中待支付费用</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="isAdjustmentInput(formobj)" v-model="formobj.onceTreatCost" @keyup.native="formobj.onceTreatCost = getNumber(formobj.onceTreatCost, 2)" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">其中抵顶费用</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="isAdjustmentInput(formobj)" v-model="formobj.onceSuppCost" @keyup.native="formobj.onceSuppCost = getNumber(formobj.onceSuppCost, 2)" clearable placeholder="" /></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">抵顶费用说明</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.onceSuppRemark" clearable placeholder="" /></el-form-item>
            </el-col>
          </el-row>
          <div class="title"><el-checkbox disabled v-model="checked"><span style="color: #FFFFFF">返利二次上传</span></el-checkbox></div>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">上传日期</div>
              <el-form-item><el-date-picker class="addinputInfo" v-model="formobj.secondaryUploadDate" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">上传金额</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="isAdjustmentInput(formobj)" v-model="formobj.secondaryUploadMoney" @keyup.native="formobj.secondaryUploadMoney = getNumber(formobj.secondaryUploadMoney, 2)" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">其中支出费用</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="isAdjustmentInput(formobj)" v-model="formobj.secondItureCost" @keyup.native="formobj.secondItureCost = getNumber(formobj.secondItureCost, 2)" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">其中待支付费用</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="isAdjustmentInput(formobj)" v-model="formobj.secondTreatCost" @keyup.native="formobj.secondTreatCost = getNumber(formobj.secondTreatCost, 2)" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">其中抵顶费用</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="isAdjustmentInput(formobj)" v-model="formobj.secondSuppCost" @keyup.native="formobj.secondSuppCost = getNumber(formobj.secondSuppCost, 2)" clearable placeholder="" /></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">抵顶费用说明</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.secondSuppRemark" clearable placeholder="" /></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">{{ computeDiffAmount }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">是否调整</div>
              <el-form-item>
                <el-select class="addinputInfo" :disabled="adjustOrNot" v-model="isAdjustment" placeholder="请选择" @change="changeIsAdjustment">
                  <el-option v-for="item in state_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictValue"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">调整金额</div>
              <el-form-item><span class="addinputInfo">{{ formobj.adjustmentMoney }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">调整说明</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.adjustmentRemarks" clearable placeholder="" /></el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <!--End 添加修改部分-->
  </div>
</template>

<script>
import req from '@/api/specialrebate/specialrebatemanagement'
import specialrebatecheck from '@/api/specialrebate/specialrebatecheck'

export default {
  name: 'SpecialRebateToBeCheckedByUpdate',
  data() {
    return {
      viewTitle: '',
      submitdisabled: false,
      checked: true,
      isAdjustment: '',
      adjustOrNot: false,
      state_list: [
        {
          dictKey: '0',
          dictValue: '否'
        },
        {
          dictKey: '1',
          dictValue: '是'
        },
        {
          dictKey: '2',
          dictValue: '无需调整'
        }
      ],
      // 表单数据
      formobj: {},
      rules: {
        uploadDate: [{ required: true, message: '一次上传日期不能为空', trigger: 'change' }],
        uploadMoney: [{ required: true, message: '一次上传金额不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 计算差异金额 = 上传返利金额 - (预提返利 - 其中预提费用); 上传返利金额 = 上传金额(一次上传金额 + 二次上传金额) - 待确定金额 - 费用(一次支出费用 + 一次待支付费用 + 一次抵顶费用 + 二次支出费用 + 二次待支付费用 + 二次抵顶费用)
    computeDiffAmount() {
      let diffAmount = '0'
      diffAmount = Math.round(((parseFloat(this.formobj.uploadMoney !== '' ? this.formobj.uploadMoney : 0) + parseFloat(this.formobj.secondaryUploadMoney !== '' ? this.formobj.secondaryUploadMoney : 0) - parseFloat(this.formobj.stayDetermineMoney !== '' ? this.formobj.stayDetermineMoney : 0) - (parseFloat(this.formobj.onceItureCost !== '' ? this.formobj.onceItureCost : 0) + parseFloat(this.formobj.onceTreatCost !== '' ? this.formobj.onceTreatCost : 0) + parseFloat(this.formobj.onceSuppCost !== '' ? this.formobj.onceSuppCost : 0) + parseFloat(this.formobj.secondItureCost !== '' ? this.formobj.secondItureCost : 0) + parseFloat(this.formobj.secondTreatCost !== '' ? this.formobj.secondTreatCost : 0) + parseFloat(this.formobj.secondSuppCost !== '' ? this.formobj.secondSuppCost : 0))) - (parseFloat(this.formobj.estimateRebate !== '' ? this.formobj.estimateRebate : 0) - (parseFloat(this.formobj.expectItureCost !== '' ? this.formobj.expectItureCost : 0) + parseFloat(this.formobj.expectTreatCost !== '' ? this.formobj.expectTreatCost : 0) + parseFloat(this.formobj.expectSuppCost !== '' ? this.formobj.expectSuppCost : 0)))) * 100) / 100
      return diffAmount
    }
  },
  methods: {
    showInfo(row) {
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      this.viewTitle = '修改'
      req.fetchBySid(row.sid).then((resp) => {
        if (resp.success) {
          this.formobj = resp.data
          if (parseFloat(this.formobj.secondaryUploadMoney) === 0) {
            this.checked = false
          }
          this.isAdjustmentInput(this.formobj)
        }
      }).catch((e) => {
        this.formobj = row
      })
    },
    getSpecialNumber(val, limit) {
      val = val.replace(/[^0-9.-]/g, '') // 保留数字
      val = val.replace(/^00/, '0.') // 开头不能有两个0
      val = val.replace(/^\./g, '0.') // 开头为小数点转换为0.
      val = val.replace(/\.{2,}/g, '.') // 两个以上的小数点转换成一个
      val = val.replace(/\-{2,}/g, '.') // 两个以上的“-”号转换成一个
      val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.') // 只保留一个小数点
      val = val.replace('-', '$#$').replace(/\-/g, '').replace('$#$', '-'); // 只保留一个"-"号
      /^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
    },
    getNumber(val, limit) {
      val = val.replace(/[^0-9.]/g, '') // 保留数字
      val = val.replace(/^00/, '0.') // 开头不能有两个0
      val = val.replace(/^\./g, '0.') // 开头为小数点转换为0.
      val = val.replace(/\.{2,}/g, '.') // 两个以上的小数点转换成一个
      val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.'); // 只保留一个小数点
      /^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
    },
    isAdjustmentInput(val) {
      let money = '0'
      // 上传金额(一次上传金额 + 二次上传金额) - 费用 - 待确定金额 - (预提返利 - 其中预提费用)
      money = Math.round((parseFloat(val.uploadMoney !== '' ? val.uploadMoney : 0) + parseFloat(val.secondaryUploadMoney !== '' ? val.secondaryUploadMoney : 0) - parseFloat(val.stayDetermineMoney !== '' ? val.stayDetermineMoney : 0) - (parseFloat(val.onceItureCost !== '' ? val.onceItureCost : 0) + parseFloat(val.onceTreatCost !== '' ? val.onceTreatCost : 0) + parseFloat(val.onceSuppCost !== '' ? val.onceSuppCost : 0) + parseFloat(val.secondItureCost !== '' ? val.secondItureCost : 0) + parseFloat(val.secondTreatCost !== '' ? val.secondTreatCost : 0) + parseFloat(val.secondSuppCost !== '' ? val.secondSuppCost : 0)) - (parseFloat(val.estimateRebate !== '' ? val.estimateRebate : 0) - parseFloat(val.expectItureCost !== '' ? val.expectItureCost : 0) - parseFloat(val.expectTreatCost !== '' ? val.expectTreatCost : 0) - parseFloat(val.expectSuppCost !== '' ? val.expectSuppCost : 0))) * 100) / 100
      if (parseFloat(val.stayDetermineMoney) === 0) {
        if (parseFloat(money) > 0) {
          this.isAdjustment = '是'
          val.adjustmentMoney = parseFloat(money)
          this.adjustOrNot = true
        } else if (parseFloat(money) === 0) {
          this.isAdjustment = '无需调整'
          val.adjustmentMoney = parseFloat(money)
          this.adjustOrNot = true
        } else {
          this.isAdjustment = '否'
          val.adjustmentMoney = '0'
          val.adjustmentRemarks = ''
          this.adjustOrNot = false
        }
      } else {
        this.isAdjustment = '否'
        val.adjustmentMoney = '0'
        val.adjustmentRemarks = ''
        this.adjustOrNot = true
      }
      if (parseFloat(this.formobj.secondaryUploadMoney) > 0) {
        this.checked = true
      } else {
        this.checked = false
      }
    },
    changeIsAdjustment(val) {
      if (val === '是' || val === '无需调整') {
        // 计算调整金额 = 上传金额(一次上传金额 + 二次上传金额) - 待确定金额 - 费用(一次支出费用 + 一次待支付费用 + 一次抵顶费用 + 二次支出费用 + 二次待支付费用 + 二次抵顶费用) - (预提返利 - 其中预提费用)
        this.formobj.adjustmentMoney = Math.round((parseFloat(this.formobj.uploadMoney !== '' ? this.formobj.uploadMoney : 0) + parseFloat(this.formobj.secondaryUploadMoney !== '' ? this.formobj.secondaryUploadMoney : 0) - parseFloat(this.formobj.stayDetermineMoney !== '' ? this.formobj.stayDetermineMoney : 0) - (parseFloat(this.formobj.onceItureCost !== '' ? this.formobj.onceItureCost : 0) + parseFloat(this.formobj.onceTreatCost !== '' ? this.formobj.onceTreatCost : 0) + parseFloat(this.formobj.onceSuppCost !== '' ? this.formobj.onceSuppCost : 0) + parseFloat(this.formobj.secondItureCost !== '' ? this.formobj.secondItureCost : 0) + parseFloat(this.formobj.secondTreatCost !== '' ? this.formobj.secondTreatCost : 0) + parseFloat(this.formobj.secondSuppCost !== '' ? this.formobj.secondSuppCost : 0)) - (parseFloat(this.formobj.estimateRebate !== '' ? this.formobj.estimateRebate : 0) - parseFloat(this.formobj.expectItureCost !== '' ? this.formobj.expectItureCost : 0) - parseFloat(this.formobj.expectTreatCost !== '' ? this.formobj.expectTreatCost : 0) - parseFloat(this.formobj.expectSuppCost !== '' ? this.formobj.expectSuppCost : 0))) * 100) / 100
      } else {
        this.formobj.adjustmentMoney = '0'
        this.formobj.adjustmentRemarks = ''
      }
    },
    handleSave() {
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          if (this.checked) {
            if (this.formobj.stayDetermineMoney.toString() === '' || this.formobj.stayDetermineMoney.toString() !== '0') {
              this.$message({ showClose: true, type: 'error', message: '因勾选返利二次上传，则其中待确定金额必须为零' })
              return
            }
            if (this.formobj.stayDetermineMoney.toString() === '' || this.isAdjustment === '否') {
              this.$message({ showClose: true, type: 'error', message: '因勾选返利二次上传，则是否调整必须为是' })
              return
            }
          }
          this.formobj.diffAmount = this.computeDiffAmount
          if (this.isAdjustment === '是' || this.isAdjustment === '无需调整') {
            this.formobj.isAdjustment = '是'
          } else if (this.isAdjustment === '否') {
            this.formobj.isAdjustment = '否'
          }
          this.submitdisabled = true
          specialrebatecheck.updateDate(this.formobj).then((resp) => {
            if (resp.success) {
              this.$message({ showClose: true, type: 'success', message: '操作成功' })
              this.handleReturn('true')
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        } else {
          return false
        }
      })
    },
    // 返回（===既判断）
    handleReturn(isreload) {
      if (isreload === 'true') this.$emit('reloadlist')
      // 表单数据
      this.formobj = {}
      this.checked = true
      this.isAdjustment = ''
      this.adjustOrNot = false
      this.submitdisabled = false
      this.$refs['form_obj'].resetFields()
      this.$emit('doback')
    }
  }
}
</script>
<style scoped>
.span-sty {
  width: 130px !important;
}
.addinputInfo {
  margin-left: 120px !important;
}
/deep/ .el-form-item__error {
  margin-left: 120px !important;
}
</style>

