<template>
  <el-dialog
    :visible="dialogVisible"
    width="40%"
    style="height: 98%"
    :before-close="handleClose"
    top="5vh"
    lock-scroll
  >
    <div class="title" slot="title">
      <div class="divider"></div>
      <div class="label">赋予角色</div>
    </div>
    <div class="dialog-content">
      <el-transfer
        v-model="own"
        :data="roles"
        :titles="['赋予角色', '拥有角色']"
      ></el-transfer>
    </div>
    <div class="dialog-footer">
      <el-button @click="cancelForm">取 消</el-button>
      <el-button type="primary" @click="handleSubmit"
        >确定<i class="el-icon-right" style="margin-left: 5px"></i
      ></el-button>
    </div>
  </el-dialog>
</template>
  
  <script>
import { updateUserOwnRoles } from "@/api/user";
export default {
  name: "EditRole",
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    roleData: {
      type: Object,
      default: () => ({}),
    },
    id: {
      type: String,
      default: null,
    },
  },
  computed: {},
  watch: {
    visible: {
      handler: function (value) {
        this.dialogVisible = value;
      },
      deep: true,
      immediate: true,
    },
    roleData(value) {
      if (Object.keys(value).length > 0) {
        const { allRole, ownRoles } = value;
        this.roles = allRole.map((item) => {
          return {
            key: item.id,
            label: item.name,
          };
        });
        this.own = ownRoles;
      }
    },
    id(value) {
      if (value) {
        this.userId = value;
      }
    },
  },
  data() {
    return {
      dialogForm: {},
      formLabelWidth: "100px",
      dialogVisible: false,
      data: [],
      roles: [],
      own: [],
      userId: null,
    };
  },
  methods: {
    // 取消编辑
    cancelForm() {
      this.$emit("handleClose");
    },

    async handleSubmit() {
      console.log("this.own", this.own);
      console.log("this.roles", this.roles);

      let params = this.own;
      //   console.log(params);
      //   console.log(this.userId);
      let res = await updateUserOwnRoles(this.userId, params);
      console.log("res", res);
      if (res.code == 0) {
        this.$message.success("修改成功！");
        this.roles = [];
        this.own = [];
        this.userId = null;
        this.$emit("handleClose");
      }
    },
    reload() {
      this.$emit("refresh");
      this.$emit("handleClose");
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.$emit("handleClose");
          this.files = [];
        })
        .catch((_) => {});
    },
  },
};
</script>
  
  <style lang='scss' scoped>
.title {
  display: flex;
  margin-bottom: 16px;
  .divider {
    width: 8px;
    border-left: 4px solid #409eff;
    margin-right: 8px;
  }
  .label {
    font-weight: bold;
  }
}
.dialog-content {
  padding: 0 32px;
  display: flex;
  .basic-info {
    flex: 1;
    margin-right: 48px;
  }
}

.dialog-footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>