<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="报警用户" prop="userIds">
        <el-select
          v-model="form.userIds"
          size="small"
          filterable
          multiple
          placeholder="选择报警用户"
        >
          <el-option
            v-for="item in areaData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </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 { getAlarmUser, addAlarmUsers } from '@/api/project';
export default {
  data() {
    return {
      form: {
        userIds: '',
        projectId: '',
      },
      areaData: [],
      rules: {
        userIds: [
          { 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;
      }
    },
    async getAlarmUser() {
      const { data } = await getAlarmUser(this.form);
      this.areaData = data.all;
      this.form.userIds = data.select;
    },
    showEdit(projectId) {
      this.title = '绑定报警用户';
      this.form.projectId = projectId;
      this.dialogFormVisible = true;
      this.getAlarmUser();
    },
    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 addAlarmUsers(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>
