<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('staff.xm')" prop="name">
        <el-input v-model.trim="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model.trim="form.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item :label="$t('staff.yx')" prop="email">
        <el-input v-model.trim="form.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input v-model.trim="form.wechat" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item :label="$t('alarm.bz')" prop="remark">
        <el-input
          v-model.trim="form.remark"
          type="textarea"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { doEdit, doAdd } from '@/api/alarmuser';
export default {
  name: 'UserEdit',
  data() {
    return {
      form: {
        id: '',
        name: '',
        phone: '',
        email: '',
        wechat: '',
        remark: '',
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入姓名' }],
      },
      roleData: [],
      clearable: true,
      roleId: '',
      parentData: [],
      title: '',
      dialogFormVisible: false,
      diffData: {},
      ruleForm: [],
    };
  },
  created() {},
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '添加用户';
      } else {
        this.title = '编辑用户';
        this.form = Object.assign({}, row);
        this.ruleForm = Object.assign({}, row);
      }
      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) {
          if (this.form.id) {
            this.getDiffData();
            if (Object.keys(this.diffData).length != 0) {
              this.diffData.id = this.form.id;
              const { msg } = await doEdit(this.diffData);
              this.$notify({
                title: msg,
                type: 'success',
              });
            }
          } else {
            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>
.select {
  width: 100%;
}
</style>
