<template>
  <div
    class="list-item"
    :class="{
      'list-item-active': projectStore.selectedElementKeys.includes(data.key),
    }"
    @mouseenter="isHover = true"
    @mouseleave="isHover = false"
    @click="handleActive"
  >
    <template v-if="!isEditing">
      <span class="list-item-visible">
        <EyeOutlined v-if="data.visible && isHover" @click="handleVisible(false)" />
        <EyeInvisibleOutlined v-if="!data.visible" @click="handleVisible(true)" />
      </span>
      <span class="layer-name">
        <span v-if="type === 'group'" class="collapse-icon" @click="handleCollapse">
          <CaretUpOutlined v-if="data.collapsed" />
          <CaretDownOutlined v-else />
        </span>
        <span class="comp-icon" v-if="type === 'group'">
          <FolderOutlined />
        </span>
        <span v-else class="comp-icon" :class="{'child-icon': type === 'child'}">
          <PieChartOutlined />
        </span>
        <Tooltip :title="data.name">
          <span @dblclick="isEditing = true">{{ data.name }}</span>
        </Tooltip>
      </span>
      <span class="layer-action">
        <span v-show="isHover">
          <Dropdown :trigger="['click']">
            <Tooltip title="更多">
              <MoreOutlined />
            </Tooltip>
            <template #overlay>
              <Menu @click="handleMenuClick">
                <MenuItem key="rename"><EditOutlined />重命名</MenuItem>
                <MenuItem key="del"><DeleteOutlined />删除</MenuItem>
              </Menu>
            </template>
          </Dropdown>
        </span>

        <Tooltip title="解锁" v-if="data.locked">
          <LockOutlined @click="handleLock(false)"/>
        </Tooltip>
        <span v-else v-show="isHover">
          <Tooltip title="锁定">
            <UnlockOutlined @click="handleLock(true)"/>
          </Tooltip>
        </span>
        
      </span>
    </template>

    <Input
      v-else
      v-model:value="layerName"
      placeholder="请输入图层名称"
      size="small"
      :status="!layerName ? 'error' : undefined"
      @blur="handleChangeName"
    />
  </div>
</template>

<script setup lang="ts">
import type { CustomElement } from "#/project";
import { defineProps, ref } from "vue";
import { Tooltip, Dropdown, Menu, MenuItem, Input } from "ant-design-vue";
import {
  EyeOutlined,
  EyeInvisibleOutlined,
  MoreOutlined,
  LockOutlined,
  UnlockOutlined,
  EditOutlined,
  DeleteOutlined,
  PieChartOutlined,
  FolderOutlined,
  CaretUpOutlined,
  CaretDownOutlined
} from "@ant-design/icons-vue";
import { useProjectStore } from "@/store/modules/project";
import { useAcionStore } from "@/store/modules/action";

const props = defineProps<{
  data: CustomElement;
  // 组件 | 组 | 子组件
  type: "layer" | "group" | "child";
  // 子组件所在的索引
  index?: number;
}>();

const projectStore = useProjectStore();
const actionStore = useAcionStore();

const layerName = ref<string>(props.data.name);

const isEditing = ref(false);
const isHover = ref(false);

const handleMenuClick = ({ key }: { key: "rename" | "del" }) => {
  if (key === "rename") {
    isEditing.value = true;
  } else {
    projectStore.removeElement(props.data.key);
    actionStore.addRecord() // 添加记录
  }
};

const handleChangeName = () => {
  isEditing.value = false;
  if (!layerName.value) {
    layerName.value = props.data.name;
    return;
  };
  const prefix = props.index !== undefined ? `children[${props.index}].` : '';
  projectStore.updateElement(props.data?.parentKey ?? props.data.key, `${prefix}name`, layerName.value);
  actionStore.addRecord() // 添加记录
};

const handleActive = () => {
  projectStore.setSelectedElementKeys([props.data.key]);
};

const handleLock = (locked: boolean) => {
  const prefix = props.index !== undefined ? `children[${props.index}].` : '';
  projectStore.updateElement(props.data?.parentKey ?? props.data.key, `${prefix}locked`, locked);
  actionStore.addRecord() // 添加记录
};

const handleVisible = (visible: boolean) => {
  const prefix = props.index !== undefined ? `children[${props.index}].` : '';
  projectStore.updateElement(props.data?.parentKey ?? props.data.key, `${prefix}visible`, visible);
  actionStore.addRecord() // 添加记录
};

const handleCollapse = () => {
  projectStore.updateElement(props.data.key, "collapsed", !props.data.collapsed);
};
</script>

<style lang="less" scoped>
.list-item {
  padding: 4px 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: move;
  color: #666;
  font-size: 12px;
  line-height: 24px;
  &:hover {
    background: #e6f4ff;
  }
  &-visible {
    cursor: pointer;
    width: 0px;
  }
  .layer-name {
    width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  &-active {
    background: #e6f4ff;
    color: #1677ff;
  }
  .layer-action {
    width: 30px;
    text-align: right;
  }
  .comp-icon {
    margin-right: 4px;
  }
  .collapse-icon {
    margin-right: 4px;
    cursor: pointer;
  }
  .child-icon {
    margin-left: 20px;
  }
}
</style>
