<template>
  <div class="drawer_info">
    <div class="drawer_title">{{ dataForm.id ? "修改" : "新增" }}</div>
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      @keyup.enter.native="validateForm('dataForm')"
      class="drawer_list"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item prop="dictName" label="字典名称">
            <el-input
              v-model="dataForm.dictName"
              maxlength="20"
              placeholder="请输入字典名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="dictType" label="字典类型">
            <el-input
              v-model="dataForm.dictType"
              maxlength="30"
              placeholder="请输入字典类型"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="sort" label="排序">
            <el-input-number
              v-model="dataForm.sort"
              controls-position="right"
              :precision="0"
              :step-strictly="true"
              :step="1"
              :min="0"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="remark" label="备注">
            <el-input
              v-model="dataForm.remark"
              type="textarea"
              show-word-limit="1000"
              maxlength="1000"
              :rows="4"
              placeholder="请输入备注"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="save_btn">
      <el-button type="primary" @click="validateForm('dataForm')"
        >提 交</el-button
      >
      <el-button plain @click="resetForm('dataForm')">重 置</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: "",
      dataForm: {
        id: "",
        dictName: "",
        dictType: "",
        sort: 0,
        remark: "",
      },
    };
  },
  computed: {
    dataRule() {
      return {
        dictName: [
          {
            required: true,
            message: "请输入字典名称",
            trigger: "blur",
          },
        ],
        dictType: [
          {
            required: true,
            message: "请输入字典类型",
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: "请输入排序",
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    init() {
      this.resetForm("dataForm");
      this.$nextTick(() => {
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/sys/dict/type/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        });
    },
    //表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataForm.id = this.id;
    },
    //表单验证
    validateForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitForm();
        } else {
          return false;
        }
      });
    },
    // 表单提交
    submitForm() {
      this.$http[!this.dataForm.id ? "post" : "put"](
        "/sys/dict/type",
        this.dataForm
      ).then((res) => {
        if (res.data.code == 0) {
          this.$message({
            type: "success",
            message: "保存成功",
          });
          this.$emit("refreshDataList");
        } else {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
  },
};
</script>
<style lang="scss">
.customWidth.alert {
  width: 50%;
  .el-dialog__title {
    border-left: 4px solid #ddd;
    padding-left: 8px;
    border-color: #5776e6;
  }
}
</style>