<!--
 * @Author: your name
 * @Date: 2024-06-11 09:36:36
 * @LastEditTime: 2024-07-09 15:38:47
 * @LastEditors: bogon
 * @Description: In User Settings Edit
 * @FilePath: /dashengmag/performance-test/src/views/system/roleMag/components/dataAuth.vue
-->
<template>
  <div>
    <el-tree
      :data="menuList"
      show-checkbox
      default-expand-all
      node-key="codeNumber"
      ref="tree"
      highlight-current
      :props="defaultProps"
    >
    </el-tree>
  </div>
</template>

<script>
import { getRoleOrganization } from "@/api/system.js";
import {
  checkDataAuthTreeFn,
  getCheckDataAuthTreeFn,
} from "@/utils/getMenu.js";

export default {
  props: {
    treeObj: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      menuList: [],
      defaultProps: {
        children: "children",
        label: "codeName",
      },
    };
  },
  watch: {
    treeObj: {
      handler() {
        this.getMenuList();
      },
      deep: true,
      immediate: true,
    },
  },
  created() {
    // this.getMenuList();
  },
  methods: {
    getMenuList() {
      getRoleOrganization({ roleId: this.treeObj.roleId }).then((res) => {
        this.menuList = res.data;
        this.updateTreeCheckState(this.menuList);
      });
    },
    getCheckTreeKey() {
      const result = getCheckDataAuthTreeFn(
        this.$refs.tree.getCheckedKeys(),
        this.menuList
      );
      const uniqueArray = Array.from(
        new Set(result.map((item) => item.codeNumber))
      ).map((id) => {
        return result.find((item) => item.codeNumber === id);
      });
      return {
        organizationCodes: uniqueArray,
        roleIdAuth: this.treeObj.roleId,
      };
    },
    updateTreeCheckState(checkList) {
      if (this.$refs.tree) {
        this.$refs.tree.setCheckedKeys(checkDataAuthTreeFn(checkList));
      }
    },
  },
};
</script>

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