<template>
  <div class="mapLegendView">
    <div class="legendTitle">
      图例
      <span class="legendBtn" @click="isOpen = !isOpen">
        <i class="icon iconfont" :class="isOpen ? 'icon-fold' : 'icon-unFold'"></i>
      </span>
    </div>

    <div class="legendContent" v-show="isOpen">
      <a-spin :spinning="loadLegendData">
        <div class="legendGroup" v-for="i in legendData">
          <div class="groupTitle">{{ i.name }} {{ i.layerCode }}</div>
          <div class="groupContent">
            <template v-for="j in i.childrenList" :key="j.name">
              <LegendItem v-if="j.layerCode" :item="j" @click="legendItemClick(j)" />
              <div
                v-else-if="j.childrenList && j.childrenList.length > 0"
                class="legendGroup"
              >
                <div class="groupTitle">{{ j.name }}</div>
                <div class="groupContent">
                  <LegendItem
                    v-for="item in j.childrenList"
                    :key="item.layerCode"
                    :item="item"
                    @click="legendItemClick(item)"
                  />
                </div>
              </div>
            </template>
          </div>
        </div>
      </a-spin>
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import { useMapStore } from "@/store/modules/map";
import LegendItem from "@/components/mapLegend/LegendItem.vue";
const mapStore: any = useMapStore();
// 图层数据
const legendData = ref(mapStore.legendDataSelected);
const loadLegendData = ref(false);
const legendItemClick = (item: any) => {
  if (item.canBeChecked == 0) {
    return;
  }
  item.checked = item.checked == 0 ? 1 : 0;
};
watch(
  () => mapStore.legendDataSelected,
  (newValue) => {
    // layerConfigs.value = newValue;
    console.log(newValue);
    legendData.value = newValue;
    // for (let i = 0; i < newValue.length; i++) {
    //   console.log(mapStore.layerData[i]);
    //   if (newValue[i].key != "-") {
    //     if (newValue[i].checked == 1) {
    //       if (newValue[i].children?.length > 0) {
    //         legendData.value.push(newValue[i]);
    //       }
    //     }
    //   }
    // }
  },
  { immediate: true, deep: true }
);
onMounted(() => {
  // for (let i = 0; i < mapStore.layerData.length; i++) {
  //   console.log(mapStore.layerData[i]);
  //   if (mapStore.layerData[i].key != "-") {
  //     if (mapStore.layerData[i].checked == 1) {
  //     }
  //   }
  // }
  // console.log(layerConfigs.value)
  // setTimeout(() => {
  //   data.value = 10;
  // }, 1000);
  // console.log(data.value);
});
const isOpen = ref(true);
</script>

<style lang="scss" scoped>
.mapLegendView {
  position: absolute;
  left: 0;
  bottom: 0;
  min-width: 72px;
  margin: 24px 0 16px 16px;
  border: 1px solid #ccdae7;
  padding: 6px;
  z-index: 9;
  background-color: #ffffff;
}
.legendTitle {
  font-size: 16px;
  font-weight: 700;
  display: flex;
  justify-content: space-between;
  cursor: pointer;
  .legendBtn {
    cursor: pointer;
  }
}
.legendContent {
  white-space: nowrap;
  max-width: 450px;
  max-height: 392px;
  overflow-x: scroll;
  overflow-y: scroll;
  border-top: 1px solid #eeeeee;
  .legendGroup {
    display: inline-block;
    vertical-align: top;
    padding: 0 4px;
    .groupTitle {
      text-align: left;
      font-weight: 700;
      font-size: 14px;
      cursor: pointer;
    }
    .groupContent {
      font-size: 14px;
      padding: 10px 0;
      margin-bottom: 0;
      .gray {
        filter: grayscale(100%);
        color: #00000073;
      }
      .disabled {
        cursor: not-allowed !important;
      }
    }
  }
}
</style>
