<template>
  <div class="set-merchant-page">
    <Card>
      <div class="save-content">
        <Button type="info" @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].shopRefund,
                  clearable: true,
                  maxlength: 8
                },
                on: {
                  input: (val) => {
                    vm.tableData[params.index].shopRefund = val
                  }
                }
              })
            ])
          }
        },
        { title: '评价返现',
          key: 'totalRecommend',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Input', {
                props: {
                  // type: 'number',
                  value: vm.tableData[params.index].evaluationRefund,
                  clearable: true
                },
                on: {
                  input: (val) => {
                    vm.tableData[params.index].evaluationRefund = val
                  }
                }
              })
            ])
          }
        },
        {
          title: '签到返现',
          key: 'fee',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Input', {
                props: {
                  // type: 'number',
                  value: vm.tableData[params.index].signInRefund,
                  clearable: true
                },
                on: {
                  input: (val) => {
                    vm.tableData[params.index].signInRefund = val
                  }
                }
              })
            ])
          }
        }
      ]
    }
  },
  methods: {
    requestData () {
      this.loading = true
      this.api({
        apiName: 'recurrencePermissionList',
        complete: () => {
          this.loading = false
        },
        success: (res) => {
          this.tableData = res.data
        }
      })
    },
    checkForm () {
      let errMsg = ''
      this.tableData.forEach((row) => {
        if (!/^\d+$/.test(row.shopRefund)) {
          errMsg = '购物返现格式输入有误！'
        }
        if (row.shopRefund < 0 || row.shopRefund > 99999999) {
          errMsg = '请输入合法购物返现额（0--99999999）！'
        }
        if (!row.shopRefund && row.shopRefund !== 0) {
          errMsg = '请输入购物返现额！'
        }
        if (!/^\d+$/.test(row.signInRefund)) {
          errMsg = '签到返现格式输入有误！'
        }
        if (row.signInRefund < 0 || row.signInRefund > 99999999) {
          errMsg = '请输入合法签到返现费（0--99999999）！'
        }
        if (!row.signInRefund && row.signInRefund !== 0) {
          errMsg = '请输入签到返现费！'
        }
        if (row.evaluationRefund < 0 || !/^\d+$/.test(row.evaluationRefund) || row.evaluationRefund > 99999999) {
          errMsg = '评价返现——请输入有效数值（0--99999999）！'
        }
        if (!row.evaluationRefund && row.evaluationRefund !== 0) {
          errMsg = '请输入评价返现值！'
        }
      })
      return { errMsg }
    },
    handleSave () {
      let { errMsg } = this.checkForm()
      if (errMsg) {
        return this.$Notice.warning({ title: errMsg })
      }
      let tableData = this.tableData
      console.log('tableData', tableData)
      this.api({
        apiName: 'updateCouponUpdate',
        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>
