<template>
  <div class="org-tree-container">
    <el-tree
      ref="orgTree"
      :data="orgTreeShowInfo.orgs"
      :props="defaultProps"
      node-key="id"
      default-expand-all
      highlight-current
      @node-click="handleNodeClick"
    ></el-tree>
  </div>
</template>

<script>
export default {
  name: 'OrgTreeShow',
  props: {
    callBackListener: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      orgTreeShowInfo: {
        orgs: [],
        orgId: '',
        curOrg: {}
      },
      defaultProps: {
        children: 'children',
        label: 'text'
      }
    }
  },
  created() {
    this._loadOrgsShow()
  },
  methods: {
    refreshTree() {
      this._loadOrgsShow()
    },
    async _loadOrgsShow() {
      try {
        const res = await this.$http.get('/org.listOrgTree', {
          params: {
            communityId: this.$store.getters.communityId
          }
        })
        this.orgTreeShowInfo.orgs = res.data.data
      } catch (error) {
        console.error('请求失败:', error)
      }
    },
    handleNodeClick(data) {
      this.orgTreeShowInfo.curOrg = data
      this.orgTreeShowInfo.curOrg.orgId = data.id
      this.$emit('switchOrg', {
        orgId: data.id,
        orgName: data.text
      })
    }
  }
}
</script>

<style scoped>
.org-tree-container {
  padding: 10px;
}
</style>