<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="id">
        <el-select v-model="form.roleId" size="small" placeholder="请选择">
          <el-option
            v-for="item in roleData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </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 { assignRole } from '@/api/manage';
import { getList } from '@/api/role';
import { ElMessage, ElLoading } from 'element-plus';
import router from '@/router';
export default {
  name: 'ManageRole',
  data() {
    return {
      form: {
        userId: '',
        roleId: '',
      },
      roleData: [],
      clearable: true,
      parentData: [],
      title: '',
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    async getRoleData() {
      const params = {
        page: 1,
        size: 20,
      };
      const { data } = await getList(params);
      this.roleData = data.items;
    },
    manageRole(row) {
      this.title = '分配角色';
      this.form.userId = row.id;
      if (row.role != undefined) {
        this.form.roleId = row.role.id;
      }
      this.dialogFormVisible = true;
      this.getRoleData();
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          const { msg } = await assignRole(this.form);
          // var magess = '该账号已在别处登录';
          // ElMessage.error(magess);
          // Promise.reject(magess);
          // router.push({ path: '/login', replace: true });
          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>
