<template>
  <div class="promotion-commission-page">
    <Card>
      <div class="save-content">
        <Button type="primary" @click="handleSave">+ 保存</Button>
      </div>
      <tables
        ref="tables"
        editable
        v-model="tableData"
        :columns="columns"
        :loading="loading"
      />
    </Card>
  </div>
</template>
<script>
import { UserLevelObject } from '@/enums'
import Tables from '_c/tables'
export default {
  components: {
    Tables
  },
  data () {
    let vm = this
    return {
      loading: false,
      tableData: [],
      columns: [
        { title: 'ID', key: 'sort', minWidth: 100, align: 'center' },
        { title: '会员等级',
          key: 'memberLevel',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            let obj = UserLevelObject[params.row.memberLevel]
            return h('div', obj.name)
          }
        },
        { title: '新人注册奖励',
          key: 'directFb',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Input', {
                props: {
                  value: vm.tableData[params.index].registerAward,
                  clearable: true
                },
                on: {
                  input: (val) => {
                    vm.tableData[params.index].registerAward = val
                  }
                }
              })
            ])
          }
        },
        { title: '推荐新人注册奖励',
          key: 'indirectFb',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Input', {
                props: {
                  value: vm.tableData[params.index].recommendRegisterAward,
                  clearable: true
                },
                on: {
                  input: (val) => {
                    vm.tableData[params.index].recommendRegisterAward = val
                  }
                }
              })
            ])
          }
        }
      //   { title: '直推新人平台奖励（元）',
      //     key: 'directRmb',
      //     minWidth: 200,
      //     align: 'center',
      //     render: (h, params) => {
      //       return h('div', [
      //         h('Input', {
      //           props: {
      //             value: vm.tableData[params.index].directRmb,
      //             clearable: true
      //           },
      //           on: {
      //             input: (val) => {
      //               vm.tableData[params.index].directRmb = val
      //             }
      //           }
      //         })
      //       ])
      //     }
      //   },
      //   { title: '间推新人平台奖励（元）',
      //     key: 'indirectRmb',
      //     minWidth: 200,
      //     align: 'center',
      //     render: (h, params) => {
      //       return h('div', [
      //         h('Input', {
      //           props: {
      //             value: vm.tableData[params.index].indirectRmb,
      //             clearable: true
      //           },
      //           on: {
      //             input: (val) => {
      //               vm.tableData[params.index].indirectRmb = val
      //             }
      //           }
      //         })
      //       ])
      //     }
      //   }
      ]
    }
  },
  methods: {
    requestData () {
      this.loading = true
      this.api({
        apiName: 'popularizeCommissionList',
        complete: () => {
          this.loading = false
        },
        success: (res) => {
          this.tableData = res.data
        }
      })
    },
    checkForm () {
      let errMsg = ''
      this.tableData.forEach((row) => {
        // if (row.directFb == '' || row.indirectFb == '' || row.directRmb == '' || row.indirectRmb == '') {
        //   errMsg = '所有项均必填哦！'
        // }
        if (!/^\d+$/.test(row.registerAward)) {
          errMsg = '新人注册奖励格式输入有误！'
        }
        if (!/^\d+$/.test(row.recommendRegisterAward)) {
          errMsg = '推荐新人注册奖励格式输入有误！'
        }
        // if (!/^\d+$/.test(row.directRmb)) {
        //   errMsg = '直推新人-平台奖励格式输入有误！'
        // }
        // if (!/^\d+$/.test(row.indirectFb)) {
        //   errMsg = '间推新人-平台奖励格式输入有误！'
        // }
      })
      return { errMsg }
    },
    handleSave () {
      let { errMsg } = this.checkForm()
      if (errMsg) {
        return this.$Notice.warning({ title: errMsg })
      }
      let tableData = this.tableData
      this.api({
        apiName: 'popularizeCommissionSave',
        postdata: tableData,
        success: (res) => {
          this.requestData()
          this.$Notice.success({ title: `保存成功！` })
        }
      })
    }
  },
  created () {
    this.requestData()
  }
}
</script>
<style scoped lang="less">
  .save-content {
    padding-bottom: 10px;
    text-align: right;
  }
</style>
