/* 
添加等级
  author: zsy
  date: 2024-6-9
*/
<template>
  <div class="add-level">
    <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="80px">
      <el-form-item label="等级名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addLevel } from "@/api/level";
export default {
  data() {
    return {
      ruleForm: {
        name: "",
      },
      rules: {
        name: [{ required: true, message: "请输入等级名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addLevel({
            levelname: this.ruleForm.name,
          })
            .then((res) => {
              this.$message({
                message: "提交成功",
                type: "success",
              });
              this.$refs[formName].resetFields();
              this.$router.push('/level/levelList')
            })
            .catch((err) => {
              this.$message.error('提交失败');
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.add-level {
  width: 70%;
  margin: 120px auto 0;
}
</style>