<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="证件照" prop="avatar"><cropper v-model="form.avatar" :fixed_number="[1, 1.5]"></cropper></el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item v-if="form.userId == undefined" label="账号" prop="userName">
            <el-input v-model="form.userName" placeholder="请输入账号" maxlength="30" auto-complete="456" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.userId == undefined" label="密码">
            <el-input placeholder="默认密码是123456" maxlength="20" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名" prop="nickName"><el-input v-model="form.nickName" placeholder="请输入姓名" maxlength="30" /></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机" prop="phonenumber"><el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11" /></el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="科室" prop="deptId">
            <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择所属科室" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色">
            <el-select v-model="form.roleIds" multiple placeholder="请选择角色">
              <el-option v-for="item in roleOptions" :key="item.roleId" :label="item.roleName" :value="item.roleId" :disabled="item.status == 1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="简介" prop="introduce">
        <editor v-model="form.introduce" placeholder="请输入"></editor>
      </el-form-item>
    </el-form>
    <div class="mfooter">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus, deptTreeSelect } from '@/api/system/user';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
export default {
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      form: {},
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      deptOptions: [],
      // 表单校验
      rules: {
        userName: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          { min: 2, max: 20, message: '账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        nickName: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        deptId: [{ required: true, message: '科室不能为空', trigger: 'blur' }],
        phonenumber: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  mounted() {
    if (this.param.id) {
      getUser(this.param.id).then((response) => {
        this.form = response.data;
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.form.postIds = response.postIds;
        this.form.roleIds = response.roleIds;
        this.form.password = '';
      });
    } else {
      getUser().then((response) => {
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.form.password = 123456;
      });
    }
    this.getDeptTree();
  },
  methods: {
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            addUser(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  }
};
</script>
