<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">导出</el-button>
          <el-button type="primary" size="small">下载</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">本月上传返利核对情况(按返利类型统计)</div>
          <el-table :key="tableKey" :data="formobj.scmWhenSpecialrebateCheckapplydetailDetails" :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="100" />
            <el-table-column prop="rebateTypeValue" label="返利类型" align="center" />
            <el-table-column prop="num" label="数量" align="center" />
            <el-table-column prop="withRebate" label="预提金额" align="center" />
            <el-table-column prop="withholdCost" label="其中预提费用" align="center" />
            <el-table-column prop="uploadMoney" label="上传金额" align="center" />
            <el-table-column prop="stayDetermineMoney" label="其中待确认金额" align="center" />
            <el-table-column prop="money" label="其中费用" align="center" />
            <el-table-column prop="adjustmentMoney" label="返利调整金额" align="center" />
            <el-table-column prop="fyAdjustmentMoney" label="其中费用调整金额" align="center" />
            <el-table-column label="明细" align="center">
              <template>
                <el-button type="primary" size="mini" @click="handleLook()">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="title">本月上传返利核对情况(按预提年月统计)</div>
          <el-table :key="lastSpecialrebateKey" :data="formobj.scmLastSpecialrebateCheckapplydetailDetails" :index="index" 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="100" />
            <el-table-column prop="withholdingDate" label="预提年月" align="center" />
            <el-table-column prop="num" label="预提数量" align="center" />
            <el-table-column prop="withRebate" label="预提金额" align="center" />
            <el-table-column prop="withholdCost" label="其中预提费用" align="center" />
            <el-table-column prop="uploadMoney" label="上传金额" align="center" />
            <el-table-column prop="stayDetermineMoney" label="其中待确认金额" align="center" />
            <el-table-column prop="money" label="其中费用" align="center" />
            <el-table-column prop="adjustmentMoney" label="返利调整金额" align="center" />
            <el-table-column prop="fyAdjustmentMoney" label="其中费用调整金额" align="center" />
            <el-table-column label="明细" align="center">
              <template>
                <el-button type="primary" size="mini" @click="handleLook()">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
          <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>
        </el-form>
      </div>
    </div>
    <!--End 添加修改部分-->
    <specialrebatetobecheckedInfo v-show="viewState == 2" ref="divInfo" @doback="resetState"/>
  </div>
</template>

<script>
import req from '@/api/specialrebate/specialrebatecheck'
import specialrebatetobecheckedInfo from './relation/specialrebatetobecheckedInfo'

export default {
  name: 'SpecialRebateCheckInfo',
  components: {
    specialrebatetobecheckedInfo
  },
  data() {
    return {
      viewTitle: '',
      index: 0,
      tableKey: 0,
      lastSpecialrebateKey: 1,
      viewState: 1,
      // 表单数据
      formobj: {
        sid: '',
        createTime: '',
        deptSid: '',
        deptName: '',
        createByName: '',
        createBySid: '',
        checkApply: '',
        createOrgName: '',
        createOrgSid: '',
        useOrgName: '',
        useOrgSid: '',
        remarks: '',
        instanceId: '', // 流程实例ID
        taskId: '', // 任务ID
        orgPath: '',
        scmWhenSpecialrebateCheckapplydetailDetails: [],
        scmLastSpecialrebateCheckapplydetailDetails: [],
        scmSpecialrebateCheckapplydetailDetails: []
      },
      rules: {},
      submitdisabled: false
    }
  },
  methods: {
    showInfo(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
        }
      }).catch((e) => {
        this.formobj = row
      })
    },
    handleLook() {
      this.viewState = 2
      this.$refs['divInfo'].showInfo(this.formobj.sid)
    },
    resetState() {
      this.viewState = 1
    },
    // 合计
    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 !== 'rebateTypeValue' || column.property !== 'num') {
          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() {
      // 表单数据
      this.formobj = {
        sid: '',
        createTime: '',
        deptSid: '',
        deptName: '',
        createByName: '',
        createBySid: '',
        checkApply: '',
        createOrgName: '',
        createOrgSid: '',
        useOrgName: '',
        useOrgSid: '',
        remarks: '',
        instanceId: '', // 流程实例ID
        taskId: '', // 任务ID
        orgPath: '',
        scmWhenSpecialrebateCheckapplydetailDetails: [],
        scmLastSpecialrebateCheckapplydetailDetails: [],
        scmSpecialrebateCheckapplydetailDetails: []
      }
      this.$refs['form_obj'].resetFields()
      this.$emit('doback')
    }
  }
}
</script>

<style scoped>
.span-sty {
  width: 130px !important;
}
.addinputInfo {
  margin-left: 120px !important;
}
</style>

