<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <el-form-item :label="$t('grouping.zmc')" prop="name">
        <el-input
          v-model.trim="form.name"
          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 { doAdd } from '@/api/group';
export default {
  name: 'GroupEdit',
  data() {
    return {
      form: {
        id: '',
        name: '',
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入组名称' }],
      },
      roleData: [],
      clearable: true,
      roleId: '',
      parentData: [],
      title: '',
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '添加项目组';
      } else {
        this.title = '编辑项目组';
        this.form = Object.assign({}, row);
      }
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          const { msg } = await doAdd(this.form);
          this.$notify({
            title: msg,
            type: 'success',
          });
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss">
.el-dialog {
  .el-dialog__header {
    border-bottom: 1px solid #e3e3e3 !important;
    padding: 15px 20px;
  }
  .dialog-footer {
    text-align: right;
    .el button {
      height: 36px !important;
      min-height: 36px !important;
      line-height: 366px !important;
    }
  }
}
</style>
