<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <!--标题按钮部分开始-->
      <div class="tab-header webtop">
        <!--标题-->
        <div>{{ viewTitle }}</div>
        <!--start 添加修改按钮-->
        <div>
          <el-button type="danger" size="small" @click="handleDelete">删除</el-button>
          <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
        </div>
      </div>
      <!--标题按钮部分结束-->
      <!--Start 新增修改部分-->
      <div class="listconadd">
        <el-form ref="form_obj" :rules="rules" class="formadd">
          <el-table :key="tableKey" :data="list" :index="index" border style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column fixed type="selection" align="center" width="50px"/>
            <el-table-column fixed width="80px" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column fixed label="操作" align="center" width="80">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handleLook(scope.row)">查看</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="createOrgName" label="分公司" align="center" width="150" />
            <el-table-column prop="purchaseSystemName" label="采购系统" align="center" width="140" />
            <el-table-column prop="brandName" label="品牌" align="center" width="120" />
            <el-table-column prop="collectionTypeValue" label="回款方式" align="center" width="150" />
            <el-table-column prop="manufaSaleChannelValue" label="厂家回款通路" align="center" width="160" />
            <el-table-column prop="collectionMoney" label="回款金额" align="center" width="130" />
            <el-table-column prop="collectionDate" label="回款日期" align="center" width="120" />
            <el-table-column prop="freight" label="运费" align="center" width="130" />
            <el-table-column prop="topAmount" label="上装金额" align="center" width="130" />
            <el-table-column prop="rebateTypeValue" label="返利类型" align="center" width="150" />
            <el-table-column prop="rebateName" label="返利名称" align="center" min-width="150" />
            <el-table-column prop="calculationStandard" label="计算标准" align="center" width="120" />
            <el-table-column prop="estimateRebate" label="预计返利" align="center" width="130" />
            <el-table-column prop="remarks" label="备注" align="center" min-width="200" />
          </el-table>
        </el-form>
      </div>
    </div>
    <!--End 添加修改部分-->
    <collectionrebatemanagementInfo v-show="viewState == 2" ref="divInfo" @doback="resetState"/>
  </div>
</template>

<script>
import req from '@/api/manufacturerrebates/collectionrebatewithholding'
import collectionrebatemanagementInfo from '../../../manufacturerrebates/huikuanguanli/collectionrebatemanagementInfo'

export default {
  name: 'HuiKuanFanLiAdd',
  components: {
    collectionrebatemanagementInfo
  },
  data() {
    return {
      viewTitle: '',
      index: 0,
      tableKey: 0,
      viewState: 1,
      purchaseSystemName: '',
      brandName: '',
      rebateTypeValue: '',
      rebateName: '',
      sids: [],
      // 表单数据
      list: [],
      rules: {},
      submitdisabled: false
    }
  },
  methods: {
    handleSelectionChange(row) {
      const aa = []
      row.forEach((element) => {
        aa.push(element.sid)
      })
      this.sids = aa
      console.log('sids', this.sids)
    },
    showInfo(row) {
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      this.viewTitle = '待预提回款返利明细'
      this.purchaseSystemName = row.purchaseSystemName
      this.brandName = row.brandName
      this.rebateTypeValue = row.rebateTypeValue
      this.rebateName = row.rebateName
      console.log('编辑回显', row.sid)
      req.collectionRebateWithDetails(row.collectionRebateSids).then((resp) => {
        if (resp.success) {
          this.list = resp.data
        }
      }).catch((e) => {
        this.formobj = row
      })
    },
    handleDelete() {
      if (this.sids.length === 0) {
        this.$message({
          showClose: true,
          type: 'error',
          message: '请选择至少一条记录进行删除操作'
        })
        return
      }
      for (var i = 0; i < this.list.length; i++) {
        for (var j = 0; j < this.sids.length; j++) {
          if (this.list[i].sid === this.sids[j]) {
            this.list.splice(i, 1)
          }
        }
      }
    },
    handleLook(row) {
      this.viewState = 2
      this.$refs['divInfo'].showInfo(row)
    },
    resetState() {
      this.viewState = 1
    },
    // 返回（===既判断）
    handleReturn() {
      const data_list = []
      for (var i = 0; i < this.list.length; i++) {
        data_list.push({
          sid: this.list[i].sid,
          purchaseSystemName: this.list[i].purchaseSystemName,
          purchaseSystemSid: this.list[i].purchaseSystemSid,
          brandName: this.list[i].brandName,
          brandSid: this.list[i].brandSid,
          rebateTypeKey: this.list[i].rebateTypeKey,
          rebateTypeValue: this.list[i].rebateTypeValue,
          rebateName: this.list[i].rebateName,
          estimateRebate: this.list[i].estimateRebate
        })
      }
      this.$emit('backAdd', data_list, this.purchaseSystemName, this.brandName, this.rebateTypeValue, this.rebateName)
    }
  }
}
</script>
<style scoped>
</style>

