<template>
  <el-scrollbar>
    <div class="com-collapse" :style="{ height: allowScroll ? scrollHeight : 'auto' }">
      <div class="collapse-box" v-for="(menu, i) in list" :key="menu" :class="{ active: menuIndex == i }">
        <div class="box-text" @click="menuClick(menu,i)">
          {{ menu.text }}
        </div>
        <div class="collapse-items">
          <div class="item" v-for="(item, j) in menu.children" :key="item" @click.stop="itemClick(item, j)" :class="{ active: itemIndex == j }">
            <span class="dot" :class="'bg-' + item.color"></span>
            <span class="value"> {{ item.text }}</span>
          </div>
        </div>
      </div>
    </div>
  </el-scrollbar>
</template>

<script>
export default {
  // 名称
  name: "List",
  // 使用组件
  components: {},
  // 传入参数
  props: {
    // 标题 ""不显示
    title: {
      type: String,
      default: "",
    },
    // 列表面板高度 fill auto 27.778vh
    height: {
      type: String,
      default: "fill",
    },
    /**
     {
       id:'',
        text: "某某风电场",
        children: [{
          id:'',
          text:'2E01',
          color:'green'
        }]
      }
    */
    list: {
      type: Array,
      default: () => [
        {
          id: "1",
          text: "某某风电场",
          children: [
            {
              id: "1",
              text: "2E01",
              color: "green",
            },
            {
              id: "2",
              text: "2E01",
              color: "green",
            },
            {
              id: "3",
              text: "2E01",
              color: "green",
            },
          ],
        },
        {
          id: "2",
          text: "某某风电场",
          children: [
            {
              id: "1",
              text: "2E01",
              color: "green",
            },
            {
              id: "2",
              text: "2E01",
              color: "green",
            },
            {
              id: "3",
              text: "2E01",
              color: "green",
            },
          ],
        },
      ],
    },
    allowScroll: {
      type: Boolean,
      default: false,
    },
    scrollHeight: {
      type: String,
      default: "100%",
    },
  },
  // 自定义事件
  emits: {
    // 选中事件
    click: null,
  },
  computed: {},
  // 数据
  data () {
    return {
      menuIndex: 0,
      itemIndex: 0,
    };
  },
  // 函数
  methods: {
    menuClick (item,index) {
      if (this.menuIndex == index) {
        this.menuIndex = -1;
      } else {
        this.menuIndex = index;
      }
      if(index === 0){
        this.$emit("click", item);
      }
      this.itemIndex = -1;
    },
    itemClick (item, index) {
      this.itemIndex = index;
      this.wpId = item.wpId;
      this.wtId = item.id;
      this.$emit("click", item);
    },
    setDefaultActiveMenu (menu) {
      menu.forEach((pEle, pIndex) => {

        let findResult = pEle.children.find(cEle => {
          return cEle.wpId === this.wpId;
        });

        if (findResult) this.menuIndex = pIndex;

        pEle.children.forEach((cEle, cIndex) => {
          if (cEle.id === this.wtId) {
            this.itemIndex = cIndex
          }
        });

      });

    }
  },
  // 生命周期钩子
  beforeCreate () {
    // 创建前
  },
  created () {
    // 创建后
  },
  beforeMount () {
    // 渲染前
  },
  mounted () {
    this.wpId = this.$route.params.wpId || "";
    this.wtId = this.$route.params.wtId || "";
    this.setDefaultActiveMenu(this.list);
  },
  beforeUpdate () {
    // 数据更新前
  },
  updated () {
    // 数据更新后
  },

  watch: {
    list (res) {
      this.setDefaultActiveMenu(res)
    }
  }
};
</script>

<style lang="less">
.com-collapse {
  .collapse-box {
    cursor: pointer;

    .box-text {
      padding: 0.741vh 1.481vh;
      background: fade(@white, 10);

      &:hover {
        color: #fff;
        background: fade(@purple, 60);
      }
    }

    .collapse-items {
      display: none;

      .item {
        padding: 0.741vh 1.481vh;
        display: flex;
        align-items: center;

        .dot {
          display: inline-block;
          width: 0.741vh;
          height: 0.741vh;
          margin-right: 1.111vh;
        }

        .value {
          flex: auto;
        }

        &.active {
          color: #fff;
          cursor: pointer;
        }
      }
    }

    &.active {
      .box-text {
        color: @white;
        background: fade(@purple, 60);
      }
      & > .collapse-items {
        display: block;
      }
    }
  }
}
</style>
