<template>
  <VaCard class="orgTree">
    <VaCardTitle>
      <div class="title-box">
        <span>组织架构</span>
        <addUserBtn ref="addUserBtn" @after-op="afterOp" v-if="isAddUserBtn"></addUserBtn>
        <addOrgBtn :parentId="selectNode" @after-op="getOrgListTree" v-if="isAddOrgBtn"></addOrgBtn>
      </div>
    </VaCardTitle>
    <VaTreeView :nodes="nodes" :textBy="'name'" :expandAll="true">
      <template #content="node">
        <div class="flex items-center" style="cursor: pointer" @click="goSelect(node)">
          <span>{{ node.orgName }}</span>
          <VaBadge v-if="selectNode == node.id" text="选中" color="success" class="mr-2 marginl-10" />
        </div>
      </template>
    </VaTreeView>
  </VaCard>
</template>

<script>
  import addUserBtn from '../userManage/module/addUserBtn.vue'
  import addOrgBtn from '../orgManage/module/addOrgBtn.vue'
  export default {
    name: 'orgTree',
    props: {
      isAddUserBtn: {
        default: false,
      },
      isAddOrgBtn: {
        default: false,
      },
    },
    components: {
      addUserBtn,
      addOrgBtn,
    },
    methods: {
      afterOp() {
        this.$emit('after-op')
      },
      goSelect(node) {
        if (node.id == this.selectNode) {
          this.selectNode = 0
          this.$emit('node-select', null)
        } else {
          this.selectNode = node.id
          this.$emit('node-select', node)
        }
        this.$refs.addUserBtn.orgId = this.selectNode
      },
      getOrgListTree() {
        this.$api.getOrgListTree().then((res) => {
          this.nodes = res.result.records
        })
      },
    },
    mounted() {
      this.getOrgListTree()
    },
    data() {
      return {
        selectNode: 0,
        nodes: [],
      }
    },
  }
</script>

<style scoped lang="less">
  .orgTree {
    min-width: 200px;
    .title-box {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
  .marginl-10{
    margin-left: 10px;
  }
</style>
