<template>
  <div :class="[`${prefixCls}-bottom`, '!dark:bg-dark-900']">
    <div class="datacenter-left">
      <div class="page-name">
        <p>组件目录</p>
      </div>
      <div class="ztree-container">
        <p v-for="(i, k, index) in assemblyType" :key="index" @click="scrollToSection(k, index)"
          :class="[`${action == index ? 'action' : ''}`]">{{ k }}({{ i && i.length || 0 }})</p>
      </div>
    </div>
    <div class="datacenter-right">
      <AssemblyData :list="assemblylist"></AssemblyData>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue';
import AssemblyData from './item/AssemblyData.vue';
import { onMounted } from 'vue';
import { listApproved } from '/@/api/dataAdmin/assembly';
import eventBus from '/@/utils/eventBus';


const prefixCls = 'account-center-bottom'

export default defineComponent({
  components: {
    AssemblyData,
  },
  setup() {
    var menu = ref({});
    var assemblylist = ref([]);
    const action = ref(-1)
    const actionName = ref('');
    var assemblyType = ref({
      '地图浏览工具': [],
      '服务加载工具': [],
      'GIS功能工具': [],
      '空间分析工具': [],
      '三维可视化效果工具': [],
    })
    onMounted(() => {
      getDataList('');
    })

    eventBus.on('assemblyCenter', () => getDataList(''))

    function getDataList(key) {
      listApproved(key).then(res => {
        if (res && res.length) {
          menu.value = {};
          res.map(i => {
            if (!menu.value[i.source] && i.source) menu.value[i.source] = [];
            if (i.source) menu.value[i.source].push(i);
          })
          for (var i in assemblyType.value) {
            assemblyType.value[i] = menu.value[i];
          }
          eventBus.emit('assemblylist', assemblyType.value)
        } else {
          assemblyType.value = { '地图浏览工具': [], '服务加载工具': [], 'GIS功能工具': [], '空间分析工具': [], '三维可视化效果工具': [] }
          eventBus.emit('assemblylist', assemblyType.value)
        }
      })
    }

    function scrollToSection(id, index) {
      actionName.value = actionName.value == id ? '' : id;
      action.value = action.value == index ? -1 : index;
      eventBus.emit('actionName', actionName.value)
    }

    return {
      getDataList,
      assemblyType,
      action,
      scrollToSection,
      menu,
      assemblylist,
      prefixCls: 'account-center',
    };
  },
});
</script>
<style lang="less" scoped>
.ztree-container p.action {
  background: #0671DD;
  color: #fff;
}

.account-center-bottom {
  background: #fff;
  height: calc(100vh - 80px);
  overflow: hidden;
}

.datacenter-right {
  float: left;
  width: calc(100vw - 320px);
  height: 100%;
  overflow-y: scroll;
}

.ztree-container {
  float: left;
  overflow: auto;
  width: 100%;
  height: auto;
  max-height: 976px;
}

.ztree-container p {
  padding-left: 30px;
  line-height: 40px;
  margin-left: 9px;
  width: 233px;
  height: 40px;
  text-align: left;
  height: 40px;
  margin: 0px 0px 14px 9px;
  background: #FFFFFF;
  cursor: pointer;
}

.ztree-container p:hover {
  background: #0671DD;
  color: #fff;
}

.datacenter-left .page-name {
  margin-bottom: 30px;
  border-bottom: solid 1px #DEDEDE;
}

.datacenter-left .page-name p {
  height: 42px;
  line-height: 42px;
  font-size: 16px;
  font-family: PingFang SC;
  font-weight: bold;
  color: #333333;
  text-align: center;
  margin-bottom: 0px;
}

.datacenter-left {
  float: left;
  width: 250px;
  height: auto;
  margin-right: 16px;
  border-top: none;
  background: #F8F8F8;
  overflow: auto;
  height: calc(100% - 20px);
  max-height: 1306px;
}

.account-center {
  &-bottom {
    padding: 10px;
    margin: 16px;
    // background-color: @component-background;
    border-radius: 3px;
  }
}
</style>
