
<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="codeType">
          <el-select v-model="ruleForm.codeType" placeholder="礼包类型">
            <el-option label="一次性" value="0" />
            <el-option label="通用" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="礼包道具" prop="itemInfo">
          <el-input v-model="ruleForm.itemInfo" placeholder="可填列表，逗号间隔" />
        </el-form-item>
        <el-form-item label="礼包生效时间" required>
          <el-col :span="11">
            <el-form-item prop="validTime">
              <el-date-picker
                  v-model="ruleForm.validTime"
                  type="datetime"
                  placeholder="选择开始时间"
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col class="text-center" :span="2" style="text-align: center">
            <span class="text-gray-500">-</span>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="invalidTime">
              <el-date-picker v-model="ruleForm.invalidTime"  type="datetime" placeholder="选择结束时间" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="随机生成个数" prop="randomCount">
          <el-input v-model="ruleForm.randomCount" placeholder="随机生成个数"/>
        </el-form-item>
        <el-form-item label="生成的礼包码" prop="result">
          <el-input readonly v-model="ruleForm.result" :rows="10" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">立即生成</el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
          <el-button type="warning" @click="copyResult(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 Upload from './components/Upload.vue'

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

const rules = reactive({
  codeType: [
    {
      required: true,
      message: '请选择礼包类型',
      trigger: 'change',
    },
  ],
  validTime: [
    {
      type: 'date',
      required: true,
      message: '请选择时间',
      trigger: 'change',
    },
  ],
  invalidTime: [
    {
      type: 'date',
      required: true,
      message: '请选择时间',
      trigger: 'change',
    },
  ],
  itemInfo: [
    {
      type: 'array',
      required: true,
      message: '请输入礼包道具',
      trigger: 'change',
    },
  ],
  randomCount: [
    {
      type: 'number',
      required: true,
      message: '请输入随机生成个数',
      trigger: 'change',
    },
    { pattern: /^(?!0)(?:[0-9]{1,4}|10000)$/, message: '范围在1-10000',trigger: 'blur'}
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  console.log('--FORM---', ruleForm)
  if (!formEl) return

}

const copyResult = async (formEl: FormInstance | undefined) => {
  console.log('--COPY---', ruleForm)
  if (!formEl) return

}

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