<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="title">
        <el-input
          v-model.trim="form.title"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="菜单路径" prop="path">
        <el-input
          v-model.trim="form.path"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="菜单路由" prop="component">
        <el-input
          v-model.trim="form.component"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model.trim="form.sort"
          size="small"
          type="number"
          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 { miniMenuUpdate, miniMenuInsert } from '@/api/menu';
export default {
  name: 'MenuEdit',
  data() {
    return {
      form: {
        id: '',
        title: '',
        name: '',
        path: '',
        component: '',
        sort: 0,
      },
      rules: {
        title: [{ required: true, trigger: 'blur', message: '请输入菜单名称' }],
        path: [{ required: true, trigger: 'blur', message: '请输入菜单路径' }],
      },
      clearable: true,
      menuId: '',
      parentData: [],
      title: '',
      dialogFormVisible: false,
      diffData: {},
      ruleForm: [],
    };
  },
  created() {},
  methods: {
    handleCountryChange(value) {
      if (value) {
        const len = value.length - 1;
        this.form.parentId = value[len];
      } else {
        this.form.parentId = 0;
      }
    },
    showEdit(row) {
      if (!row) {
        this.title = '添加';
      } else {
        this.title = '编辑';
        this.menuId = row.id;
        this.form = Object.assign({}, row);
        this.ruleForm = Object.assign({}, row);
        this.form.title = row.name;
      }
      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 miniMenuUpdate(this.diffData);
              this.$notify({
                title: msg,
                type: 'success',
              });
            }
          } else {
            const { msg } = await miniMenuInsert(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          }
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style>
.select {
  width: 100%;
}
</style>
