<template>
  <div class="tree_root">
    <div class="level_root" v-for="levelItem in list" :key="levelItem.treeid">
      <div
        :class="[
          'level_item',
          selectedTreeItem.treeid === levelItem.treeid ? 'selected' : ''
        ]"
        @click="toChangeSelected(levelItem)"
      >
        <div class="level_item_main">
          <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="1"
        @toChangeSelected="toChangeSelected"
      ></CustomTreeItem>
    </div>
  </div>
</template>

<script>
import CustomTreeItem from "./CustomTreeItem.vue";
export default {
  name: "CustomTree",
  components: {
    CustomTreeItem
  },
  props: {
    // 列表数据
    list: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    return {
      // 选中树
      selectedTreeItem: {}
    };
  },
  watch: {
    list: {
      handler() {
        // 如果已选中就回显
        let selectedTreeItem = this.originSelectedTreeItem(this.list);
        if (selectedTreeItem) {
          this.toChangeSelected(selectedTreeItem);
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    // 已选中就回显
    originSelectedTreeItem(list) {
      if (list.length > 0 && this.selectedTreeItem.treeid) {
        for (let treeItem of list) {
          if (treeItem.treeid === this.selectedTreeItem.treeid) {
            this.selectedTreeItem = treeItem;
            return treeItem;
          }
          if (treeItem.childList && treeItem.childList.length > 0) {
            let selectedTreeItem = this.originSelectedTreeItem(
              treeItem.childList
            );
            if (selectedTreeItem) {
              return selectedTreeItem;
            }
          }
        }
      }
    },
    // 选中树
    toChangeSelected(levelItem) {
      if (
        this.selectedTreeItem.treeid === levelItem.treeid ||
        !levelItem.treeid
      ) {
        return;
      }
      this.selectedTreeItem = levelItem;
      this.$emit("selectedTreeItem", this.selectedTreeItem);
    },
    // 展开树
    toChangeOpenChildren(levelItem) {
      levelItem.open = !levelItem.open;
    }
  }
};
</script>

<style lang="less" scoped>
.tree_root {
  position: relative;
  padding: 10px 0;
  width: 100%;
  .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>
