<template>
  <div
    class="select_box"
    :class="{'js-select_box_active':select}"
    @click.stop="$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" @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>
    <slot></slot>
    <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">
import { Component, Prop, Vue } from "vue-property-decorator";
@Component
export default class SelectBox extends Vue {
  isMenuShowing: boolean = false;
  @Prop({ type: Boolean, required: true }) select!: boolean;
  @Prop() check!: Number;
  @Prop() show!: Number;
  // @Prop({ type: Boolean, required: true }) show!: Boolean;
  public dragstart($event: any) {
    $event.dataTransfer.setData("imgInfo", $event.target.id);
    this.$emit("dragstart");
  }
  public dragend($event: any) {
    this.$emit("dragend");
  }
  public dragover($event: any) {
    this.$emit("dragover");
  }
  // 修改表现形式
  public modifyForms() {
    this.onMenuBlur();
    this.$emit("modifyForms");
  }
  // 修改标题
  public modifyTitle() {
    this.onMenuBlur();
    this.$emit("modifyTitle");
  }
  // 添加下一级
  public addNextlevel() {
    this.onMenuBlur();
    this.$emit("addNextlevel");
  }
  // 执行删除
  public del() {
    this.onMenuBlur();
    this.$emit("del");
  }
  // 显示操作栏
  public showMenu() {
    this.isMenuShowing = true;
    document.addEventListener("mousedown", this.onMenuBlur);
  }
  //隐藏操作栏
  public onMenuBlur() {
    this.isMenuShowing = false;
    document.removeEventListener("mousedown", this.onMenuBlur);
  }
}
</script>

<style scoped lang="less">
.menu-change {
  z-index: 100; /* 避免被底部挡住 */
  position: absolute;
  top: 100%;
  right: 50px;
  margin: 0;
  padding: 0;
  width: 124px;
  list-style: none;
  background: #fff;
  text-align: center;
  border: 1px solid #e7e7e7;
  border-radius: 2px;
  // overflow: hidden;
}
.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: 70px;
    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 rgba(63, 121, 254, 0.5);
}
.iconshezhi {
  color: #e6e6e6;
  font-size: 24px;
  margin-left: 22px;
  cursor: pointer;
  position: absolute;
  right: 88px;
  z-index: 98;
  top: 50%;
  transform: translateY(-50%);
}
// .iconshezhi_right{
//   // right: 188px;
// }
.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>
