<template>
  <el-card shadow="hover">
    <template #header>
      <span>信息录入</span>
    </template>

    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" style="max-width: 600px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名" />
      </el-form-item>

      <el-form-item label="年龄" prop="age">
        <el-input-number v-model="form.age" :min="1" :max="150" />
      </el-form-item>

      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="form.gender">
          <el-radio value="male">男</el-radio>
          <el-radio value="female">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="生日" prop="birthday">
        <el-date-picker v-model="form.birthday" type="date" placeholder="选择日期" style="width: 100%" />
      </el-form-item>

      <el-form-item label="城市" prop="city">
        <el-select v-model="form.city" placeholder="请选择城市" style="width: 100%">
          <el-option label="北京" value="beijing" />
          <el-option label="上海" value="shanghai" />
          <el-option label="广州" value="guangzhou" />
          <el-option label="深圳" value="shenzhen" />
        </el-select>
      </el-form-item>

      <el-form-item label="爱好" prop="hobbies">
        <el-checkbox-group v-model="form.hobbies">
          <el-checkbox value="reading" label="阅读" />
          <el-checkbox value="sports" label="运动" />
          <el-checkbox value="music" label="音乐" />
          <el-checkbox value="travel" label="旅行" />
        </el-checkbox-group>
      </el-form-item>

      <el-form-item label="评分" prop="rating">
        <el-rate v-model="form.rating" />
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'

const formRef = ref<FormInstance>()

const form = reactive({
  name: '',
  age: 18,
  gender: '',
  birthday: '',
  city: '',
  hobbies: [] as string[],
  rating: 0,
  remark: '',
})

const rules = reactive<FormRules>({
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  city: [{ required: true, message: '请选择城市', trigger: 'change' }],
})

const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('提交成功！')
    }
  })
}

const handleReset = () => {
  formRef.value?.resetFields()
}
</script>
