<template>
  <div>
    <el-dialog
      title="分配角色"
      :destroy-on-close="true"
      :visible.sync="Visible"
      width="30%"
      @close="handleClose"
      :close-on-click-modal="false"
      top="10vh"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="small"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="roleIds">
          <el-select
            multiple
            clearable
            style="width: 100%"
            v-model="form.roleIds"
            placeholder="请选择角色"
          >
            <el-option
              v-for="v in roleList"
              :key="v.id"
              :label="v.name"
              :value="v.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleRest">重 置</el-button>
        <el-button size="small" type="primary" @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, saveUser } from "@/api/system";
export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      roleList: [],
      form: {
        roleIds: [],
      },
      rules: {
        roleIds: [{ required: true, message: "请选择角色", trigger: "change" }],
      },
    };
  },
  computed: {
    Visible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit("update:show", val);
      },
    },
  },
  created() {
    this.getRoleList();
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      let res = await getRoleList({
        page: 1,
        size: -1,
      });
      if (res.data.code == 1) {
        let { data } = res.data.data;
        this.roleList = data;
      }
    },
    // 确定
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let { id, roleIds, lastLoginAddress, mobile } = this.form;
          let res = await saveUser({
            id,
            lastLoginAddress,
            mobile,
            roleIds: roleIds.join(","),
          });
          let { code, msg } = res.data;
          if (code == 1) {
            this.$message.success(msg);
            this.$emit("success");
            this.handleClose();
          }
          this.loading = false;
        }
      });
    },
    // 新增
    onAdd() {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, "id");
    },
    // 编辑
    onEdit(row) {
      setTimeout(() => {
        this.form = { ...row };
        if (this.form.roleIds) {
          this.form.roleIds = this.form.roleIds.split(",").map(Number);
        } else {
          this.form.roleIds = [];
        }
      }, 10);
    },
    // 重置
    handleRest() {
      this.$resetForm("form");
    },
    // 关闭
    handleClose() {
      this.$resetForm("form");
      this.Visible = false;
    },
  },
};
</script>

<style lang="less" scoped></style>
