<template>
  <div>
    <el-button type="primary" size="mini" icon="el-icon-plus" @click="dialogFormVisible = true">新建群组</el-button>
    <el-dialog title="新建群组" append-to-body :visible.sync="dialogFormVisible" width="600px">
      <el-form :model="form" ref="form" label-position="right" label-width="80px" size="small">
        <el-form-item label="群组名称" prop="groupname" :rules="[
          { required: true, message: '群组名称不能为空'},
        ]">
          <el-input v-model="form.groupname" autocomplete="on" placeholder="输入群组名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" style="width:120px" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" style="width:120px" type="primary" @click="addGroup()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      dialogFormVisible:false,
      form:{}
    }
  },
  watch: {
    dialogFormVisible (val) {
     !val && this.$refs.form.resetFields()
    }
  },
  methods:{
    async addGroup () {
      this.$refs['form'].validate(async (valid) => {
        if (!valid)  return false;
        const res = await this.$api.requested({
          "id": "20220831164203",
          "version":1,
          "content": {
            "sys_phonebookgroupid":0,     //sys_phonebookgroupid<=0时 为新增
            "groupname":this.form.groupname
          }
        })
        this.tool.showMessage(res,()=>{
          setTimeout(() => {
            this.$emit('onSuccess')
          }, 500);
          this.dialogFormVisible = false
        })
      });
    }
  }
}

</script>
<style scoped>
/deep/.el-dialog__body {
  padding-bottom: 0;
}
</style>