<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>选择案场：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-select v-model="CaseId" placeholder="请选择">
                <el-option
                  v-for="item in cases"
                  :key="item.CaseId"
                  :label="item.CaseName"
                  :value="item.CaseId">
                </el-option>
              </el-select>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>VIP卡数量：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入VIP卡数量"
                v-model="postData.TotalCount"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>价格：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入价格"
                v-model="postData.CardAmount"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>有效期：<em>*</em></span>
          <div class="flex-item">
            <div>
              <el-date-picker
                value-format="yyyy-MM-ddT00:00:00+08:00"
                v-model="postData.BeginDate"
                type="date"
                placeholder="选择开始日期">
              </el-date-picker>
              <span style="line-height:40px;">至</span>
              <el-date-picker
                value-format="yyyy-MM-ddT23:59:59+08:00"
                v-model="postData.EndDate"
                type="date"
                placeholder="选择截止日期">
              </el-date-picker>
            </div>
          </div>
        </li>
        <li style="text-align:center">
          <el-button type="primary" size="mini" @click="submit">保存</el-button>
          <el-button type="danger" size="mini" @click="cancel">取消</el-button>
        </li>
      </ul>
    </form>
  </div>
</template>

<script>
import { createNamespacedHelpers, mapState } from 'vuex'

const { mapActions: mapVipActions } = createNamespacedHelpers('vip')

export default {
  name: '',
  data () {
    return {
      postData: {
        TotalCount: '', // 卡数量
        CardAmount: '', // 价格
        CaseId: '', // 案场id
        OrgId: '',
        BeginDate: '',
        EndDate: '',
      }
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      OrgId: x => x.app.user.OrgId,
    }),
    CaseId: {
      get () {
        return this.postData.CaseId === '' ? this.defaultCaseId || '' : this.postData.CaseId
      },
      set (val) {
        this.postData.CaseId = val
      }
    }
  },
  created () {
    this.initData()
  },
  components: {},
  methods: {
    ...mapVipActions([
      'AddVip',
    ]),
    initData () { // 数据初始化
    },
    submit () { // 提交数据
      if (this.postData.TotalCount === '') {
        this.$message({
          type: 'error',
          message: '请输入VIP卡数量!'
        })
        return
      }
      if (this.postData.CardAmount === 0) {
        this.$message({
          type: 'error',
          message: '请输入价格!'
        })
        return
      }
      if (this.postData.BeginDate === '') {
        this.$message({
          type: 'error',
          message: '请选择开始时间!'
        })
        return
      }
      if (this.postData.EndDate === '') {
        this.$message({
          type: 'error',
          message: '请选择截止时间!'
        })
        return
      }
      this.AddVip({...this.postData, CaseId: this.CaseId, OrgId: this.OrgId}).then(x => {
        this.$message({
          type: 'success',
          message: '保存成功!'
        })
        this.$router.push({ name: 'vipList' })
      })
    },
    cancel () {
      this.$router.push({ name: 'vipList' })
    }
  },
  mounted () { }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
