<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="form.deptId" placeholder="请选择部门">
          <el-option
            v-for="item in deptData"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('staff.yhm')" prop="userName">
        <el-input
          v-model.trim="form.userName"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model.trim="form.password"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('staff.xm')" prop="nickName">
        <el-input
          v-model.trim="form.nickName"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model.trim="form.phone"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('staff.yx')" prop="email">
        <el-input
          v-model.trim="form.email"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input
          v-model.trim="form.wechat"
          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 { doEdit, doAdd } from '@/api/manage';
import { getList } from '@/api/role';
import { getDeptList } from '@/api/dept';
export default {
  name: 'ManageEdit',
  data() {
    return {
      form: {
        id: '',
        userName: '',
        nickName: '',
        password: '',
        phone: '',
        email: '',
        wechat: '',
      },
      rules: {
        userName: [
          { required: true, trigger: 'blur', message: '请输入用户名' },
        ],
        nickName: [{ required: true, trigger: 'blur', message: '请输入姓名' }],
        phone: [{ required: true, trigger: 'blur', message: '请输入手机号' }],
      },
      roleData: [],
      clearable: true,
      roleId: '',
      parentData: [],
      title: '',
      dialogFormVisible: false,
      diffData: {},
      ruleForm: [],
      deptData: [],
    };
  },
  created() {},
  methods: {
    // 部门列表
    async getDeptList() {
      const { data } = await getDeptList();
      this.deptData = data;
    },
    async getRoleData() {
      const params = {
        page: 1,
        size: 10,
      };
      const { data } = await getList(params);
      this.roleData = data.items;
    },
    manageEdit(row) {
      this.getRoleData();
      this.getDeptList();
      if (!row) {
        this.title = '添加用户';
      } else {
        this.title = '编辑用户';
        this.roleId = row.id;
        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) {
          this.form.name = this.form.title;
          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 {
            delete this.form.id;
            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>
