<template>
  <div
    class="select_box w-full"
    :class="{ 'js-select_box_active': select }"
    @click.stop="$emit('click')"
    @click.capture="$emit('click')"
    draggable="true"
    @dragstart.stop="dragstart"
    @dragend.stop="dragend"
    @dragover.stop="dragover"
  >
    <i
      class="iconfont iconshezhi"
      :class="{ iconshezhi_right: check === 3 }"
      v-show="select"
      @click="showMenu"
    ></i>
    <ul class="menu-change" v-show="select && isMenuShowing" @mousedown.stop>
      <li
        class="menu-item menu-item__one"
        v-if="check !== 3 && check !== 12 && parent_type !== 12"
        @click.stop="modifyForms"
      >
        修改表现形式
      </li>
      <li class="menu-item menu-item__two" @click.stop="modifyTitle">
        修改标题
      </li>
      <li
        class="menu-item menu-item__two"
        v-if="show !== 3"
        @click.stop="addNextlevel"
      >
        添加下一级
      </li>
      <li class="menu-item menu-item__two" @click.stop="del">删除</li>
    </ul>
    <div class="flex item-center w-full">
      <slot></slot>
    </div>
    <div v-show="select">
      <div class="dash dash_1"></div>
      <div class="dash dash_2"></div>
      <div class="dash dash_3"></div>
      <div class="dash dash_4"></div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const isMenuShowing = ref(false);

const props = withDefaults(
  defineProps<{
    select?: boolean;
    check?: number;
    show?: number;
    parent_type?: number;
  }>(),
  {}
);

const emit = defineEmits([
  "dragstart",
  "dragend",
  "dragover",
  "modifyForms",
  "modifyTitle",
  "addNextlevel",
  "del",
  "click",
]);

const dragstart = ($event: any) => {
  $event.dataTransfer.setData("imgInfo", $event.target.id);
  emit("dragstart");
};
const dragend = ($event: any) => {
  emit("dragend");
};
const dragover = ($event: any) => {
  emit("dragover");
};
// 修改表现形式
const modifyForms = () => {
  onMenuBlur();
  emit("modifyForms");
};
// 修改标题
const modifyTitle = () => {
  onMenuBlur();
  emit("modifyTitle");
};
// 添加下一级
const addNextlevel = () => {
  onMenuBlur();
  emit("addNextlevel");
};
// 执行删除
const del = () => {
  onMenuBlur();
  emit("del");
};
// 显示操作栏
const showMenu = () => {
  isMenuShowing.value = true;
  document.addEventListener("mousedown", onMenuBlur);
};
//隐藏操作栏
const onMenuBlur = () => {
  isMenuShowing.value = false;
  document.removeEventListener("mousedown", onMenuBlur);
};
</script>

<style scoped lang="scss">
.menu-change {
  z-index: 100; /* 避免被底部挡住 */
  position: absolute;
  top: 100%;
  right: -63px;
  margin: 0;
  padding: 0;
  width: 124px;
  list-style: none;
  background: #fff;
  text-align: center;
  border: 1px solid #e7e7e7;
  border-radius: 2px;
}
.menu-item__one {
  &::after {
    content: "";
    display: block;
    position: absolute;
    width: 10px;
    height: 10px;
    border-top: 1px solid #e7e7e7;
    border-left: 1px solid #e7e7e7;
    background: white;
    top: -6px;
    left: 87px;
    transform: rotate(45deg);
  }
  &:hover {
    background: rgba(242, 244, 246, 1);
    &::after {
      background: rgba(242, 244, 246, 1);
    }
  }
}
.menu-item {
  line-height: 40px;
  color: #797d84;
  font-size: 14px;
  cursor: pointer;
}
.menu-item__two {
  &:hover {
    background: rgba(242, 244, 246, 1);
  }
}
.select_box {
  position: relative;
  box-sizing: border-box;
  border: 1px solid transparent;
}

.js-select_box_active {
  border: 1px dashed gray;
}
.iconshezhi {
  color: #e6e6e6;
  position: absolute;
  right: -46px;
  font-size: 24px;
  margin-left: 22px;
  cursor: pointer;
  z-index: 98;
  top: 50%;
  transform: translateY(-50%);
}

.check {
  background: rgba(63, 121, 254, 0.5);
}
.dash {
  position: absolute;
  width: 8px;
  height: 8px;
  border: 1px solid #d8d9da;
  background: #fff;
}

.dash_1 {
  top: -5px;
  left: -5px;
}

.dash_2 {
  top: -5px;
  right: -5px;
}

.dash_3 {
  right: -5px;
  bottom: -5px;
}

.dash_4 {
  left: -5px;
  bottom: -5px;
}

.pre-tag {
  position: absolute;
  left: -30px;
  top: 23px;
  display: none;
}
</style>
