<template>
  <el-form :model="formData" :rules="formRules" ref="formRef" label-width="100px" @submit.native.prevent="handleSubmit">
    <!-- 用户名 -->
    <el-form-item label="用户名" prop="username">
      <el-input v-model="formData.username" placeholder="请输入用户名" />
    </el-form-item>
    
    <!-- 密码 -->
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="formData.password" placeholder="请输入密码" />
    </el-form-item>
    
    <!-- 邮箱 -->
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="formData.email" 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>
</template>

<script>
export default {
  name: 'CustomForm',
  data() {
    return {
      // 表单数据
      formData: {
        username: '',
        password: '',
        email: ''
      },
      // 表单校验规则
      formRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      }
    };
  },
  methods: {
    // 提交表单
    handleSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          console.log('表单数据:', this.formData);
          // 在这里处理表单提交逻辑，比如调用 API
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    
    // 重置表单
    handleReset() {
      this.$refs.formRef.resetFields();
    }
  }
};
</script>

<style scoped>
/* 可以根据需要自定义样式 */
</style>
