<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="title">
        <el-input
          v-model.trim="form.title"
          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: '',
        title: '',
        name: '',
        projectId: '',
      },
      areaData: [],
      cityData: [],
      rules: {
        title: [{ required: true, trigger: 'blur', message: '请输入项目名称' }],
      },
      title: '',
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    handleCountryChange(value) {
      if (value) {
        const len = value.length - 1;
        this.form.parentId = value[len];
      } else {
        this.form.parentId = 0;
      }
    },
    showEdit(projectId) {
      this.title = '添加组';
      this.form.projectId = projectId;
      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) {
          this.form.name = this.form.title;
          const { msg } = await doAdd(this.form);
          this.$notify({
            title: msg,
            type: 'success',
          });
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$emit('RefreshGroup', this.form.projectId);
        } else {
          return false;
        }
      });
    },
  },
};
</script>
