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

<script>
import { listOrgTree } from '@/api/oa/editWorkApi'
import { getCommunityId } from '@/api/community/communityApi'

export default {
  name: 'OrgTreeShow',
  props: {
    callBackListener: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      orgTreeShowInfo: {
        orgs: [],
        orgId: '',
        curOrg: {}
      },
      defaultProps: {
        children: 'children',
        label: 'text'
      }
    }
  },
  mounted() {
    this._loadOrgsShow()
  },
  methods: {
    refreshTree() {
      this._loadOrgsShow()
    },
    async _loadOrgsShow() {
      try {
        const params = {
          communityId: getCommunityId()
        }
        const { data } = await listOrgTree(params)
        this.orgTreeShowInfo.orgs = data
      } catch (error) {
        console.error('加载组织树失败:', error)
      }
    },
    handleNodeClick(data) {
      this.orgTreeShowInfo.curOrg = data
      this.orgTreeShowInfo.curOrg.orgId = data.id
      this.$emit(this.callBackListener, 'switchOrg', {
        orgId: data.id,
        orgName: data.text
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.org-tree-show {
  ::v-deep .el-tree {
    background: transparent;
    .el-tree-node__content {
      height: 36px;
      &:hover {
        background-color: #f5f7fa;
      }
    }
    .is-current > .el-tree-node__content {
      background-color: #ecf5ff;
      color: #409eff;
    }
  }
}
</style>