<template>

    <basic-container v-loading="loading">

      <el-card>
        <div slot="header" class="clearfix">
            <span style="font-weight: 800;">任务员等级</span>
        </div>
        <el-form
          label-position="left"
          inline size="small"
          :rules="rules"
          :model="form"
          ref="normalForm"
          label-width="200px"
          class="layout_v">

            <div class="layout_h_v_c" v-for="(level, idx) in levels" :key="idx" :style="{marginTop: idx == 0 ? '0px' : '16px'}">
              <span>成长值范围在</span>
              <el-form-item style="margin: 0;margin-left: 10px;margin-right: 10px;">
                <el-input v-model="level.min" :min="0" :max="99999" type="number" autocomplete="off"></el-input>
              </el-form-item>
              <span>-</span>
              <el-form-item style="margin: 0;margin-left: 10px;margin-right: 10px;">
                <el-input v-model="level.max" :min="0" :max="99999" type="number" autocomplete="off"></el-input>
              </el-form-item>
              <span>之间的用户任务员等级为{{level.levelName}}</span>
            </div>

        </el-form>
      </el-card>

      <el-card>
        <div slot="header" class="clearfix">
            <span style="font-weight: 800;">成长值配置</span>
        </div>
        <el-form
          label-position="left"
          inline size="small"
          :rules="rules"
          :model="form"
          ref="normalForm"
          label-width="200px"
          class="layout_v">

          <div class="layout_h_v_c">
            <span>任务员每日签到可获得</span>
            <el-form-item style="margin: 0;margin-left: 10px;margin-right: 10px;">
              <el-input v-model="form.signReward" autocomplete="off"></el-input>
            </el-form-item>
            <span>成长值</span>
          </div>

          <div class="layout_h_v_c" style="margin-top: 16px;">
            <span>任务员每完成一次任务可获得</span>
            <el-form-item style="margin: 0;margin-left: 10px;margin-right: 10px;">
              <el-input v-model="form.taskReward" autocomplete="off"></el-input>
            </el-form-item>
            <span>成长值</span>
          </div>

          <div class="layout_h_v_c" style="margin-top: 16px;">
            <span>邀请人员注册可获得</span>
            <el-form-item style="margin: 0;margin-left: 10px;margin-right: 10px;">
              <el-input v-model="form.inviteReward" autocomplete="off"></el-input>
            </el-form-item>
            <span>成长值</span>
          </div>

        </el-form>
      </el-card>

      <div class="layout_v" style="width: 100%;margin-bottom: 16px;">
        <el-button class="self_center" type="primary" :loading="saving" @click="submitForm">保存</el-button>
      </div>

    </basic-container>

</template>

<script>

  import { getSystemSet, configSystemSet } from '@/api/systemSet/systemSet.js'
  import { ZmMixin } from '@/mixins/ZmMixin.js'

  export default {
    mixins: [ZmMixin],
    data() {
      return {
        key: 'SYSTEM_SET_TASK_SET',
        loading: false,
        saving: false,
        levels: [],
        rules: {},
        form: {
            signReward: '',
            taskReward: '',
            inviteReward: ''
        }
      }
    },
    created() {
      this.load()
    },
    methods: {
      findParent(r) {
        let find = undefined
        for(let v of this.levels) {
          if(v.vip == r.vip) {
            return v
          }
        }
        return undefined
      },
      load() {
        this.loading = true
        getSystemSet({key: this.key})
        .then(r => {
          this.loading = false
          let z = this.zmRes(r)
          if(z) {
            this.form.signReward = z.signReward
            this.form.taskReward = z.taskReward
            this.form.inviteReward = z.inviteReward
            this.levels = z.taskLevels
          }
        })
        .catch(e => {
          this.loading = false
        })
      },
      submitForm() {
        let collect = []
        for(let v of this.levels) {
          if(!this.checkEmpty(v.min, v.levelName + "的最小值")) {
            return
          }
          if(!this.checkEmpty(v.max, v.levelName + "的最大值")) {
            return
          }
          collect.push(v)
        }
        if(collect.length == 0) {
          this.warn("未设置任何等级")
          return
        }
        if(!this.checkEmpty(this.form.signReward, "签到可获得成长值")) {
          return
        }
        if(!this.checkEmpty(this.form.taskReward, "完成任务可获得成长值")) {
          return
        }
        if(!this.checkEmpty(this.form.inviteReward, "邀请人员注册可获得成长值")) {
          return
        }
        this.saving = true
        configSystemSet({
          key: this.key,
          data: JSON.stringify({
            signReward: this.form.signReward,
            taskReward: this.form.taskReward,
            inviteReward: this.form.inviteReward,
            taskLevels: this.levels
          })
        })
        .then(r => {
          this.saving = false
          this.success("保存成功！")
        })
        .catch(e => {
          this.saving = false
        })
      }
    }
  }

</script>

<style scoped>


</style>
