<template>
  <el-dialog
    :visible="dialogVisible"
    width="30%"
    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-tree
        :data="allRole"
        show-checkbox
        node-key="id"
        ref="myTree"
        highlight-current
        :props="{ label: 'name' }"
        :default-checked-keys="ownRoles"
        @check="roleParentTreeCheck"
        check-strictly
        default-expand-all
      >
      </el-tree>
    </div>
    <div class="dialog-footer">
      <el-button @click="cancelForm">取 消</el-button>
      <el-button type="primary" @click="handleSubmit"
        >确定<i class="el-icon-circle-check" style="margin-left: 5px"></i
      ></el-button>
    </div>
  </el-dialog>
</template>
  
  <script>
import treeTransfer from "el-tree-transfer";
import { updateUserOwnRoles } from "@/api/user";
export default {
  name: "EditRole",
  components: {
    treeTransfer,
  },
  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) {
        let that = this;
        const { allRole, ownRoles } = value;
        this.allRole = allRole;
        this.ownRoles = ownRoles;
        // if (ownRoles.length > 0) {
        //   ownRoles.map((id) => {
        //     findObj(allRole, id);
        //   });
        //   function findObj(arr, id) {
        //     if (arr.length > 0) {
        //       arr.map((v) => {
        //         if (v.id == id) {
        //           that.toData.push(v);
        //         }
        //         if (v.children) {
        //           findObj(v.children, id);
        //         }
        //       });
        //     }
        //   }
        // }

        // this.allRolesData = JSON.parse(JSON.stringify(allRole));
        // let fromData = JSON.parse(JSON.stringify(allRole));
        // this.fromData = fromData.map((item) => {
        //   item.pid = 0;
        //   return item;
        // });
      }
    },
    id(value) {
      if (value) {
        this.userId = value;
      }
    },
  },
  data() {
    return {
      dialogForm: {},
      formLabelWidth: "100px",
      dialogVisible: false,
      userId: null,
      allRole: [],
      ownRoles: [],
      // fromData: [],
      // toData: [],
      // allRolesData: [], //留存数据，因为el-tree-transfer 左边数据需要pid为0 ，后期提交时根据该数据重新构造
    };
  },
  methods: {
    // 取消编辑
    cancelForm() {
      this.$emit("handleClose");
    },

    async handleSubmit() {
      // debugger
      let ids = this.$refs.myTree.getCheckedKeys();
      let params = ids;
      let res = await updateUserOwnRoles(this.userId, params);

      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.allRole = [];
          this.ownRoles = [];
        })
        .catch((_) => {});
    },
    roleParentTreeCheck(node, list) {
      if (list.checkedKeys.length > 1) {
        //单选实现
        this.$refs.myTree.setCheckedKeys([node.id]);
      }
    },
  },
};
</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>