<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="键" prop="dataCode">
        <el-input
          v-model.trim="form.dataCode"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="值" prop="dataValue">
        <el-input
          v-model.trim="form.dataValue"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button type="primary" size="small" @click="save">确 定</el-button>
      <el-button size="small" @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { insertDictData, updateDictData } from '@/api/sysdict';
export default {
  name: 'Add',
  data() {
    return {
      form: {
        id: '',
        dictTypeId: '',
        dataCode: '',
        dataValue: '',
      },
      rules: {
        dataCode: [{ required: true, trigger: 'blur', message: '请输入键' }],
        dataValue: [{ required: true, trigger: 'blur', message: '请输入值' }],
      },
      dictId: '',
      roleData: [],
      clearable: true,
      parentData: [],
      title: '',
      dialogFormVisible: false,
      diffData: {},
      ruleForm: [],
    };
  },
  created() {},
  methods: {
    showEdit(row, status) {
      if (!status) {
        this.title = '添加字典数据';
      } else {
        this.title = '编辑字典数据';
        this.form = Object.assign({}, row.row);
        this.ruleForm = Object.assign({}, row.row);
      }
      this.form.dictTypeId = row.dictId;
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    getDiffData() {
      this.diffData = {};
      Object.keys(this.form).forEach(item => {
        if (this.form[item] != this.ruleForm[item]) {
          this.diffData[item] = this.form[item];
        }
      });
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          this.form.name = this.form.title;
          if (this.form.id) {
            this.getDiffData();
            if (Object.keys(this.diffData).length != 0) {
              this.diffData.id = this.form.id;
              const { msg } = await updateDictData(this.diffData);
              this.$notify({
                title: msg,
                type: 'success',
              });
            }
          } else {
            delete this.form.id;
            const { msg } = await insertDictData(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          }
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
