
<template>
  <el-dialog
    v-dialog-drag
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    :visible.sync="dialogVisible"
    width="620px"
    append-to-body
    @close="handleCancel"
  >
    <el-form ref="form" class="standard-form" :model="formState" :rules="rules" :inline="true" label-width="80px">
      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="formState.nickName" placeholder="请输入用户昵称" maxlength="30" />
      </el-form-item>
      <!--      <el-form-item label="归属部门" prop="deptId">-->
      <!--        <treeselect v-model="formState.deptId" style="width: 200px" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />-->
      <!--      </el-form-item>-->
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input v-model="formState.phonenumber" placeholder="请输入手机号码" maxlength="11" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formState.email" placeholder="请输入邮箱" maxlength="50" />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="formState.userName" :disabled="dialogStatus === 'edit'" placeholder="请输入用户名称" :maxlength="20" :minlength="2" />
      </el-form-item>
      <el-form-item v-if="dialogStatus !== 'edit'" label="用户密码" prop="password">
        <el-input v-model="formState.password" placeholder="请输入用户密码" type="password" maxlength="20" show-password auto-complete="new-password" />
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formState.status" style="width: 200px">
          <el-radio
            v-for="dict in dict.type[DICT_TYPE.SYS_NORMAL_DISABLE]"
            :key="dict.value"
            :label="dict.value"
          >{{ dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="用户性别">
        <el-select v-model="formState.sex" placeholder="请选择性别" filterable>
          <el-option
            v-for="dict in dict.type[DICT_TYPE.SYS_USER_SEX]"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="岗位">
        <el-select v-model="formState.postIds" multiple placeholder="请选择岗位" filterable>
          <el-option
            v-for="item in postOptions"
            :key="item.postId"
            :label="item.postName"
            :value="item.postId"
            :disabled="item.status == 1"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="formState.roleIds" multiple placeholder="请选择角色" filterable>
          <el-option
            v-for="item in roleOptions"
            :key="item.roleId"
            :label="item.roleName"
            :value="item.roleId"
            :disabled="item.status == 1"
          />
        </el-select>
      </el-form-item>
      <el-form-item style="width: 100%" label="备注">
        <el-input v-model="formState.remark" style="width: 490px" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>

import regex from '@/utils/regex'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { addUser, deptTreeSelect, getUser, updateUser } from '@/api/system/user'
import { DICT_TYPE } from '@/utils/dict/config'

const initForm = {
  nickName: null,
  userName: null,
  password: null,
  phonenumber: null,
  email: null,
  sex: null,
  postIds: [],
  roleIds: []
}
export default {
  name: 'ExpertDialog',
  dicts: [DICT_TYPE.SYS_USER_SEX, DICT_TYPE.SYS_NORMAL_DISABLE],
  components: { Treeselect },
  data() {
    return {
      DICT_TYPE,
      dialogVisible: false,
      dialogStatus: 'create',
      loading: false,
      deptOptions: [],
      postOptions: [],
      roleOptions: [],
      formState: Object.assign({}, initForm),
      rules: {
        userName: { required: true, message: '用户名称不能为空', trigger: 'blur' },
        nickName: { required: true, message: '用户昵称不能为空', trigger: 'blur' },
        password: [
          { required: true, message: '用户密码不能为空', trigger: 'blur' },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱地址不能为空', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        phonenumber: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' },
          { pattern: regex.mobile, message: '请输入正确的手机号码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getDeptTree()
  },
  methods: {
    getDeptTree() {
      deptTreeSelect().then(({ data }) => {
        this.deptOptions = data
      })
    },
    getExpertInfo(userId) {
      getUser(userId).then(({ posts, roles, data, postIds, roleIds }) => {
        if (this.dialogStatus === 'edit') {
          this.formState = Object.assign({}, initForm, data)
          this.formState.postIds = postIds
          this.formState.roleIds = roleIds
          this.formState.password = ''
        }
        this.postOptions = posts
        this.roleOptions = roles
      })
    },
    handleOpen() {
      this.getExpertInfo()
      this.dialogVisible = true
      this.dialogStatus = 'create'
    },
    handleEdit(user) {
      this.getExpertInfo(user.userId)
      this.dialogStatus = 'edit'
      this.dialogVisible = true
    },
    handleCancel() {
      this.dialogVisible = false
      this.$refs.form.clearValidate()
      this.formState = Object.assign({}, initForm)
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const fn = this.dialogStatus === 'edit' ? updateUser : addUser
          fn(Object.assign({}, this.formState, { isExpert: '1' }))
            .then(() => {
              this.$modal.msgSuccess('保存成功')
              this.handleCancel()
              this.$emit('ok')
            }).finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
