<template>
  <div>
    <div
      class="level_root"
      v-for="levelItem in list"
      :key="levelItem.treeid"
      v-show="open"
    >
      <div
        :class="[
          'level_item',
          selectedTreeItem.treeid === levelItem.treeid ? 'selected' : ''
        ]"
        @click="toChangeSelected(levelItem)"
      >
        <div
          class="level_item_main"
          :style="{
            marginLeft: `${level * 20}px`
          }"
        >
          <div
            class="arrow_icon_box"
            @click="toChangeOpenChildren(levelItem)"
            v-if="levelItem.childList && levelItem.childList.length > 0"
          >
            <Icon
              :class="['arrow_icon', levelItem.open ? 'arrow_down' : '']"
              type="ios-arrow-forward"
              :size="16"
            />
          </div>
          <div v-else class="replace_dom"></div>
          <div class="level_title">
            {{ levelItem.treealias }}
          </div>
        </div>
      </div>
      <CustomTreeItem
        v-if="levelItem.childList && levelItem.childList.length > 0"
        :list="levelItem.childList"
        :selected-tree-item="selectedTreeItem"
        :open="levelItem.open"
        :level="level + 1"
        @toChangeSelected="toChangeSelected"
      ></CustomTreeItem>
    </div>
  </div>
</template>

<script>
export default {
  name: "CustomTreeItem",
  props: {
    // 列表数据
    list: {
      type: Array,
      default() {
        return [];
      }
    },
    // 选中项
    selectedTreeItem: {
      type: Object,
      default() {
        return {};
      }
    },
    // 是否展开
    open: {
      type: Boolean,
      default: false
    },
    // 当前层级
    level: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {};
  },
  methods: {
    toChangeSelected(levelItem) {
      this.$emit("toChangeSelected", levelItem);
    },
    toChangeOpenChildren(levelItem) {
      levelItem.open = !levelItem.open;
    }
  }
};
</script>

<style lang="less" scoped>
.level_root {
  position: relative;
  width: 100%;
  .level_item {
    margin: 0 5px;
    border-radius: 4px;
    cursor: pointer;
    &.selected,
    &:hover {
      color: var(--font_selected_color);
      background-color: var(--bg_transparent_color);
    }
    .level_item_main {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .arrow_icon_box {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        width: 20px;
        height: 32px;
        .arrow_icon {
          transition: all 0.2s ease-in-out;
          &.arrow_down {
            transform: rotate(90deg);
          }
        }
      }
      .replace_dom {
        width: 20px;
        height: 32px;
      }
      .level_title {
        width: calc(100% - 28px);
        font-size: 12px;
        line-height: 32px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
</style>
