
<template>
  <PageWrapLayout>
    <div style="max-width: 800px">
      <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
          :size="formSize"
      >
        <el-form-item label="礼包码" prop="code">
          <el-input v-model="ruleForm.code" placeholder="3 到 8 个字符"/>
        </el-form-item>
        <el-form-item label="礼包类型" prop="codeType">
          <el-radio-group v-model="ruleForm.codeType">
            <el-radio :label="0">一次性(一个玩家只使用一次)</el-radio>
            <el-radio :label="1">通用(每个玩家可使用一次)</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="礼包道具" prop="itemInfo">
          <el-input v-model="ruleForm.itemInfo" placeholder="可填列表，逗号间隔，格式：1001-100,1002-200" />
        </el-form-item>
        <el-form-item label="礼包分组" prop="codeGroup">
          <el-input v-model="ruleForm.codeGroup" placeholder="可选项，在一个分组里面的礼包码，每个玩家只能用一个" />
        </el-form-item>
        <el-form-item label="礼包生效时间" required>
          <el-col :span="7">
            <el-form-item prop="validTime">
              <el-date-picker
                  v-model="ruleForm.validTime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  type="datetime"
                  placeholder="选择开始时间"
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col class="text-center" :span="1" style="text-align: center">
            <span class="text-gray-500">-</span>
          </el-col>
          <el-col :span="7">
            <el-form-item prop="invalidTime">
              <el-date-picker v-model="ruleForm.invalidTime" value-format="YYYY-MM-DD HH:mm:ss" type="datetime" placeholder="选择结束时间" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col class="text-enpty" :span="1"/>
          <el-button type="primary" @click="valid7Day">7天</el-button>
          <el-button type="primary" @click="valid15Day">15天</el-button>
          <el-button type="primary" @click="valid30Day">30天</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">立即生成</el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </PageWrapLayout>
</template>

<script lang="ts" setup >
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
// import Upload from './components/Upload.vue'
import giftCodeApi from '@/api/giftCode'
import dateUtils from '@/common/dateUtils'

const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  code: '',
  codeType: 0,
  validTime: '',
  invalidTime: '',
  giftItems: '',
  codeGroup:'',
  itemInfo:[]
})

const rules = reactive({
  code: [
    {
      required: true,
      message: '请输入礼包码',
      trigger: 'change',
    },
    { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
  ],
  codeType: [
    {
      required: true,
      message: '请选择礼包类型',
      trigger: 'change',
    },
  ],
  validTime: [
    {
      type: 'date',
      required: true,
      message: '请选择时间',
      trigger: 'change',
    },
  ],
  invalidTime: [
    {
      type: 'date',
      required: true,
      message: '请选择时间',
      trigger: 'change',
    },
  ],
  itemInfo: [
    {
      required: true,
      message: '请输入礼包道具',
      trigger: 'change',
    },
  ]
})

const submitForm = async (done: () => void) => {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      giftCodeApi.fixedGenerate(ruleForm).then(res => {
        if (res.data.code = 200) {
            return ElMessage.success(res.data.msg)
          } else {
            return ElMessage.error(res.data.msg)
          }
      })
      console.log('submit!', ruleForm)
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const valid7Day = () => {
  let now = new Date();
  ruleForm.validTime = dateUtils.getDate(now);
  let invalidDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7, 
    now.getHours(), now.getMinutes(), now.getSeconds())
  ruleForm.invalidTime = dateUtils.getDate(invalidDate)
}

const valid15Day = () => {
  let now = new Date();
  ruleForm.validTime = dateUtils.getDate(now);
  let invalidDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 15, 
    now.getHours(), now.getMinutes(), now.getSeconds())
  ruleForm.invalidTime = dateUtils.getDate(invalidDate)
}

const valid30Day = () => {
  let now = new Date();
  ruleForm.validTime = dateUtils.getDate(now);
  let invalidDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 30, 
    now.getHours(), now.getMinutes(), now.getSeconds())
  ruleForm.invalidTime = dateUtils.getDate(invalidDate)
}

</script>
