<template>
  <div class="custom-content-layers-group-list-item">
    <div
      class="root-item-content"
      :class="{ hover, select, 'list-mini': selectText }"
      @click="clickHandle($event)"
      @mousedown="groupMousedownHandle($event)"
      @mouseenter="mouseenterHandle(componentGroupData)"
      @mouseleave="mouseleaveHandle(componentGroupData)"
      @contextmenu="handleContextMenu($event, componentGroupData, optionsHandle)"
    >
      <div class="item-content">
        <a-space>
          <folder-open-outlined v-if="expend" class="icon" />
          <folder-outlined v-else class="icon" />
        </a-space>
        <span class="list-text">
          {{ componentGroupData.chartConfig.title }}
        </span>
        <layers-status :isGroup="false" :hover="hover" :status="status"></layers-status>
      </div>
      <div :class="{ 'select-modal': select }"></div>
    </div>
    <div style="width: 100%" v-show="expend">
      <LayersListItem
        v-for="element in componentGroupData.groupList"
        :key="element.id"
        :componentData="element"
        :layer-mode="layerMode"
        :isGroup="true"
        @mousedown="mousedownHandle($event, element, componentGroupData.id)"
        @mouseenter="mouseenterHandle(element)"
        @mouseleave="mouseleaveHandle(element)"
        @contextmenu="handleContextMenu($event, componentGroupData, optionsHandle)"
      ></LayersListItem>
    </div>
  </div>
</template>

<script setup name="LayersGroupListItem">
import { ref, computed } from 'vue';
import { useChartEditStore } from '@/store/modules/chart/chartEditStore';
import { useContextMenu, divider } from '../../hook/useContextMenu.hook';
import LayersListItem from './layers-list-item.vue';
import layersStatus from './layers-status.vue';
import { Space as aSpace } from 'ant-design-vue';

const props = defineProps({
  componentGroupData: {
    type: Object,
    required: true
  },
  layerMode: {
    type: String,
    default: 'thumbnail'
  }
});

const chartEditStore = useChartEditStore();
const { handleContextMenu, onClickOutSide } = useContextMenu();

const expend = ref(false);

// 是否选中文本
const selectText = computed(() => {
  return props.layerMode === 'text';
});

// 计算当前选中目标
const select = computed(() => {
  const id = props.componentGroupData.id;
  return chartEditStore.getTargetChart.selectId.find(e => e === id);
});

// 悬浮
const hover = computed(() => {
  return props.componentGroupData.id === chartEditStore.getTargetChart.hoverId;
});

// 组件状态 隐藏/锁定
const status = computed(() => {
  return props.componentGroupData.status;
});

// 右键
const optionsHandle = (targetList, allList, targetInstance) => {
  const filter = menulist => {
    return allList.filter(i => menulist.includes(i.key));
  };

  // 多选处理
  if (chartEditStore.getTargetChart.selectId.length > 1) {
    return filter(['group']);
  } else {
    const statusMenuEnums = [];
    if (targetInstance.status.lock) {
      statusMenuEnums.push('lock');
    } else {
      statusMenuEnums.push('unLock');
    }
    if (targetInstance.status.hide) {
      statusMenuEnums.push('hide');
    } else {
      statusMenuEnums.push('show');
    }
    return [...filter(['unGroup']), divider(), ...targetList.filter(i => !statusMenuEnums.includes(i.key))];
  }
};

// 点击
const clickHandle = e => {
  // 按下左键 + CTRL
  if (window.$KeyboardActive?.ctrl) return;
  // 判断左右键
  expend.value = !expend.value;
  mousedownHandle(e, props.componentGroupData);
};

// 组点击事件
const groupMousedownHandle = e => {
  onClickOutSide();
  // 若此时按下了 CTRL, 表示多选
  const id = props.componentGroupData.id;
  if (e.buttons === 1 && window.$KeyboardActive?.ctrl) {
    // 若已选中，则去除
    if (chartEditStore.targetChart.selectId.includes(id)) {
      const exList = chartEditStore.targetChart.selectId.filter(e => e !== id);
      chartEditStore.setTargetSelectChart(exList);
    } else {
      chartEditStore.setTargetSelectChart(id, true);
    }
    return;
  }
  chartEditStore.setTargetSelectChart(id);
};

// 公共点击事件
const mousedownHandle = (e, componentInstance, id) => {
  e.preventDefault();
  e.stopPropagation();

  onClickOutSide();
  chartEditStore.setTargetSelectChart(id || componentInstance.id);
};

// 公共进入事件
const mouseenterHandle = componentInstance => {
  chartEditStore.setTargetHoverChart(componentInstance.id);
};

// 公共移出事件
const mouseleaveHandle = componentInstance => {
  chartEditStore.setTargetHoverChart(undefined);
};
</script>

<style lang="less" scoped>
.custom-content-layers-group-list-item {
  position: relative;
  width: 90%;
  margin: 10px 5%;
  margin-bottom: 5px;
  transition: all 0.2s;

  :deep(.custom-content-layers-list-item) {
    margin-right: 0 !important;
    width: 95% !important;
  }

  &:hover {
    :deep(.icon-item) {
      opacity: 1 !important;
    }
  }

  .root-item-content {
    height: 52px;
    cursor: pointer;
    border-radius: 5px;
    border: 1px solid rgba(0, 0, 0, 0);
    &.hover,
    &:hover {
      background-color: #e3e3e4;
    }

    /* 选中 */
    &.select {
      border: 1px solid var(--primary-color);
      /* 需要设置最高级，覆盖 hover 的颜色 */
      background-color: rgba(0, 0, 0, 0);
      .list-img {
        border: 1px solid var(--primary-color) !important;
      }
    }

    // mini样式
    &.list-mini {
      height: 28px;
      .item-content {
        height: 38px !important;
      }
      .select-modal {
        height: 30px !important;
      }
    }
  }

  .select-modal,
  .item-content {
    position: absolute;
    top: 0;
    left: 0;
  }
  .item-content {
    z-index: 1;
    display: flex;
    align-items: center;
    text-align: center;
    padding: 6px 5px;
    justify-content: start !important;
    width: 100%;
    height: 52px;

    .icon {
      margin-left: 8px;
      font-size: 24px;
    }
  }
  .select-modal {
    width: 100%;
    height: calc(52px + 2px);
    opacity: 0.3;
    background-color: var(--primary-color);
  }
  .list-text {
    width: calc(100% - 61px);
    padding-left: 6px;
    text-align: left;
    font-size: 10px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .list-status-icon {
    margin-left: 3px;
  }
}
</style>
