<template>
  <div class="node-wrapper">
    <div v-for="node in nodes" :key="node.id" :class="['node', { active: getCurChapterId() === node.id }]">
      <div
        :class="['node-name', 'nowrap-ellipsis', { content: node.is_leaf_chapter === 'true' }]"
        :title="node.name"
        @click="handleSelectNode(node.is_leaf_chapter, node.id)"
      >
        <span>{{ node.name }}</span>
      </div>
      <CatalogueTree v-if="node.nodes && node.is_leaf_chapter === 'false'" :nodes="node.nodes" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'CatalogueTree',
  inject: ['selectNode', 'getCurChapterId'],
  props: {
    nodes: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {};
  },
  methods: {
    handleSelectNode(is_leaf_chapter, id) {
      if (is_leaf_chapter === 'false') return;
      this.selectNode(id);
    },
  },
};
</script>

<style lang="scss" scoped>
.node {
  &.active {
    background-color: #f3f3f3;
  }

  &-name {
    padding-bottom: 4px;

    &.content {
      cursor: pointer;

      &:hover {
        color: #409eff;
      }
    }

    &:not(.content) {
      font-weight: bold;
    }
  }

  .node {
    margin-left: 20px;
  }
}
</style>
