<!--
 * @description: 编辑用户
 * @fileName: EditAccount
 * @author: 17076
 * @date: 2024/6/14-下午3:05
 * @version: V1.0.0
-->

<template>
  <cs-drawer
    :drawer-option="drawerOption"
    @onReset="handleReset"
    @onSubmit="handleSubmit"
  >
    <template slot="content">
      <el-form ref="form" :model="userForm" :rules="rules" label-width="80px">
        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            :action="uploadCofig.action"
            :show-file-list="false"
            :disabled="isView"
          >
            <img v-if="userForm['avatar']" :src="userForm['avatar']" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="userForm['userName']" :disabled="isView" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userForm['password']" :disabled="isView" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="userForm['mobile']" :disabled="isView" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="userForm['role']" :disabled="isView" placeholder="请选择角色" multiple>
            <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门" prop="department">
          <el-select v-model="userForm['department']" :disabled="isView" placeholder="请选择所属部门" multiple>
            <el-option v-for="item in departmentOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
    </template>
  </cs-drawer>
</template>

<script>
import { validPhone } from '@/utils/validate'
export default {
  name: 'EditAccount',
  data() {
    return {
      drawerOption: {
        show: false,
        title: '新增用户'
      },
      // 角色数据
      roleOptions: [],
      // 所属部门数据
      departmentOptions: [],
      // 用户表单
      userForm: {},
      // 规则校验
      rules: {
        avatar: [{ required: true, message: '头像不能为空!', trigger: 'blur' }],
        userName: [{ required: true, message: '用户名不能为空!', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空!', trigger: 'blur' }],
        mobile: [{ required: false, validate: validPhone, trigger: 'blur' }],
        role: [{ required: true, message: '角色不能为空!', trigger: 'blur' }],
        department: [{ required: true, message: '所属部门不能为空!', trigger: 'blur' }]
      },
      // 上传文件配置
      uploadCofig: {
        action: ''
      },
      // 是否编辑
      isEdit: false,
      // 是否是查看
      isView: false
    }
  },
  methods: {
    // 显示弹窗
    show(data, isEdit) {
      this.isEdit = isEdit
      this.isView = !!data && !isEdit
      this.drawerOption.title = this.isEdit ? '编辑用户' : (this.isView ? '查看用户' : '新增用户')
      this.userForm = !isEdit && !this.isView ? {} : data
      this.drawerOption.show = true
    },
    // 重置
    handleReset() {
      this.$refs.form.resetFields()
      this.$refs.form.clearValidate()
    },
    // 确定
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) { }
      })
    }
  }
}
</script>

<style scoped lang="scss">

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
::v-deep {
  .el-select {
    width: 100%;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
}
</style>
