<template>
  <div class="app-container">
    <div class="box" v-loading="loading">
      <el-form :inline="true" ref="formRef" :model="form" :rules="rules" class="demo-form-inline">
        <h3>每小时发放免费券（游戏时间内）</h3>
        <div class="item" style=" width: 1100px;">
          *每次获得上限
          <div>
            <el-form-item label="" prop="singleMaxCoupons">
              <el-input-number v-model="form.singleMaxCoupons" controls-position="right" :min="0" :precision="0"></el-input-number> 张
            </el-form-item>
          </div>
          *发放的是上限以内的随机整张数
        </div>
        <h3>登录摇一摇免费次数</h3>
        <div class="item" style=" width: 1100px;">
          *次数
          <div>
            <el-form-item label="" prop="loginForShakes">
              <el-input-number v-model="form.loginForShakes" controls-position="right" :min="0" :precision="0"></el-input-number> 次
            </el-form-item>
          </div>
        </div>
        <h3>拉新用户获得</h3>
        <div class="item" style=" width: 1100px;">
          *摇一摇次数
          <div>
            <el-form-item label="" prop="shakesForReferral">
              <el-input-number v-model="form.shakesForReferral" controls-position="right" :min="0" :precision="0"></el-input-number> 次
            </el-form-item>
          </div>
        </div>
        <h3>看广告获得</h3>
        <div class="item" style=" width: 1100px;">
          *摇一摇次数
          <div>
            <el-form-item label="" prop="shakesForAd">
              <el-input-number v-model="form.shakesForAd" controls-position="right" :min="0" :precision="0"></el-input-number> 次
            </el-form-item>
          </div>
        </div>
        <br />
        <div class="item" style=" width: 1100px;">
          *最多看广告
          <div>
            <el-form-item label="" prop="adsShowMax">
              <el-input-number v-model="form.adsShowMax" controls-position="right" :min="0" :precision="0"></el-input-number> 次
            </el-form-item>
          </div>
          每场超过次数看广告不获得摇一摇次数
        </div>
        <h3>摇一摇发放券数</h3>
        <div class="item" style=" width: 1100px;">
          *单次获得券算
          <div>
            <el-form-item label="" prop="unevenCouponsMin">
              <el-input-number v-model="form.unevenCouponsMin" controls-position="right" :min="0" :max="form.unevenCouponsMax?form.unevenCouponsMax:Infinity" :precision="0"></el-input-number> 到
            </el-form-item>
            <el-form-item label="" prop="unevenCouponsMax">
              <el-input-number v-model="form.unevenCouponsMax" controls-position="right" :min="form.unevenCouponsMin?form.unevenCouponsMin:0" :precision="0"></el-input-number>
            </el-form-item>
          </div>
        </div>
        <br />
        <div class="item" style=" width: 1100px;">
          *双次获得券算
          <div>
            <el-form-item label="" prop="evenCouponsMin">
              <el-input-number v-model="form.evenCouponsMin" controls-position="right" :min="0" :max="form.evenCouponsMax?form.evenCouponsMax:Infinity" :precision="0"></el-input-number> 到
            </el-form-item>
            <el-form-item label="" prop="evenCouponsMax">
              <el-input-number v-model="form.evenCouponsMax" controls-position="right" :min="form.evenCouponsMin?form.evenCouponsMin:0" :precision="0"></el-input-number>
            </el-form-item>
          </div>
        </div>
        <br />
        <div class="item" style=" width: 1100px;">
          *活动时间
          <div>
            <el-form-item label="" prop="timeArr">
              <el-time-picker is-range v-model="form.timeArr" format="HH:mm" value-format="HH:mm" range-separator="至" start-placeholder="选择开始时间" end-placeholder="选择结束时间" placeholder="选择时间范围">
              </el-time-picker>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <div class="btnBox">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">保 存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { shakeGameConfigInfo, shakeGameConfigSave } from '@/api/shake'
export default {
  data () {
    return {
      buttonLoading: false,
      loading: false,
      form: {
        singleMaxCoupons: undefined,
        loginForShakes: undefined,
        shakesForReferral: undefined,
        shakesForAd: undefined,
        adsShowMax: undefined,
        adsShowMax: undefined,
        unevenCouponsMin: undefined,
        unevenCouponsMax: undefined,
        evenCouponsMin: undefined,
        evenCouponsMax: undefined,
        timeArr: undefined
      },
      rules: {
        singleMaxCoupons: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        loginForShakes: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        shakesForReferral: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        shakesForAd: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        adsShowMax: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        unevenCouponsMin: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        unevenCouponsMax: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        evenCouponsMin: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        evenCouponsMax: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        timeArr: [
          { required: true, message: "请选择", trigger: "blur" }
        ],
      }
    }
  },
  created () {
    this.getDetails()
  },
  methods: {
    getDetails () {
      this.loading = true
      shakeGameConfigInfo().then(res => {
        if (res.data) {
          if (res.data.activityStartTime && res.data.activityEndTime) {
            res.data.timeArr = [res.data.activityStartTime, res.data.activityEndTime]
          }
          this.form = res.data
        }
      }).finally(() => {
        this.loading = false
      })
    },
    submitForm () {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          if (this.form.timeArr && this.form.timeArr.length > 1) {
            this.form.activityStartTime = this.form.timeArr[0];
            this.form.activityEndTime = this.form.timeArr[1];
          }
          this.buttonLoading = true;
          shakeGameConfigSave(this.form).then(res => {
            this.$message({
              message: '提交成功',
              type: 'success'
            })
          }).finally(() => {
            this.buttonLoading = false;
          })
        }
      })
    }
  }
}
</script>

<style lang='scss' scoped>
h3 {
  font-weight: 700;
}
.item {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  font-size: 14px;
  > div {
    margin: 0 10px;
  }
}
.hint {
  font-size: 12px;
  color: red;
}
.btnBox {
  margin: 20px 0 0 100px;
}
.el-form-item {
  margin-bottom: 0;
}
</style>