<template>
  <div class="set-merchant-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: 'level', minWidth: 200, align: 'center' },
        { title: '会员',
          key: 'name',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            let obj = UserLevelObject[params.row.level]
            return h('div', obj.name)
          }
        },
        { title: '累计消费额（元）',
          key: 'totalConsume',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Input', {
                props: {
                  // type: 'number',
                  value: vm.tableData[params.index].totalConsume,
                  clearable: true,
                  maxlength: 8
                },
                on: {
                  input: (val) => {
                    vm.tableData[params.index].totalConsume = val
                  }
                }
              })
            ])
          }
        },
        { title: '累计推荐人数（位）',
          key: 'totalRecommend',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Input', {
                props: {
                  // type: 'number',
                  value: vm.tableData[params.index].totalRecommend,
                  clearable: true
                },
                on: {
                  input: (val) => {
                    vm.tableData[params.index].totalRecommend = val
                  }
                }
              })
            ])
          }
        },
        {
          title: '会员费（元/年）',
          key: 'fee',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Input', {
                props: {
                  // type: 'number',
                  value: vm.tableData[params.index].fee,
                  clearable: true
                },
                on: {
                  input: (val) => {
                    vm.tableData[params.index].fee = val
                  }
                }
              })
            ])
          }
        }
      ]
    }
  },
  methods: {
    requestData () {
      this.loading = true
      this.api({
        apiName: 'getMemberList',
        complete: () => {
          this.loading = false
        },
        success: (res) => {
          this.tableData = res.data
        }
      })
    },
    checkForm () {
      let errMsg = ''
      this.tableData.forEach((row) => {
        if (!/^\d+$/.test(row.totalConsume)) {
          errMsg = '累计消费额格式输入有误！'
        }
        if (row.totalConsume < 0 || row.totalConsume > 99999999) {
          errMsg = '请输入合法消费额（0--99999999）！'
        }
        if (!row.totalConsume && row.totalConsume !== 0) {
          errMsg = '请输入累计消费额！'
        }
        if (!/^\d+$/.test(row.fee)) {
          errMsg = '会员费格式输入有误！'
        }
        if (row.fee < 0 || row.fee > 99999999) {
          errMsg = '请输入合法会员费（0--99999999）！'
        }
        if (!row.fee && row.fee !== 0) {
          errMsg = '请输入会员费！'
        }
        if (row.totalRecommend < 0 || !/^\d+$/.test(row.totalRecommend) || row.totalRecommend > 99999999) {
          errMsg = '累计推荐人数——请输入有效数值（0--99999999）！'
        }
        if (!row.totalRecommend && row.totalRecommend !== 0) {
          errMsg = '请输入累计推荐人数！'
        }
      })
      return { errMsg }
    },
    handleSave () {
      let { errMsg } = this.checkForm()
      if (errMsg) {
        return this.$Notice.warning({ title: errMsg })
      }
      let tableData = this.tableData
      this.api({
        apiName: 'editMemberConfig',
        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>
