<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    custom-class="el-dialog-s"
    append-to-body
    :modal="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      label-width="120px"
    >
      <el-form-item prop="dictValue" label="字典值">
        <el-input
          v-model="dataForm.dictValue"
          placeholder="请输入字典值"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="dictLabel" label="字典标签">
        <el-input
          v-model="dataForm.dictLabel"
          placeholder="请输入字典标签"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="color" label="字典颜色">
        <el-input
          v-model="dataForm.color"
          placeholder="请输入字典颜色"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="icon" label="字典图标">
        <el-input
          v-model="dataForm.icon"
          placeholder="请输入字典图标"
          clearable
        ></el-input>
      </el-form-item>
      <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"
          label="排序"
        ></el-input-number>
      </el-form-item>
      <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-form>
    <template slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()"
        >确 定</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        dictTypeId: "",
        dictLabel: "",
        dictValue: "",
        sort: 0,
        remark: "",
        icon: "",
        color: "",
      },
    };
  },
  computed: {
    dataRule() {
      return {
        dictLabel: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        dictValue: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/sys/dict/data/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/sys/dict/data",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
