<template>
  <div class="def-shape-edit" v-if="editComponent">
    <el-icon class="def-close-shape-edit" @click="emit('update:shape', null)">
      <Close />
    </el-icon>
    <el-form class="def-shape-edit-form" label-width="60px">
      <component
        :key="props.shape"
        v-if="editComponent"
        :is="editComponent"
        :shape="props.shape"
        @delete="delHandler"
        @inputIng="(bol) => (inputIng = bol)"
        @blur="emit('update:shape', null)"
      />
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from "vue";
import { BoardShape, compass, title } from "./board";
import { components } from "./edit-shape";

const props = defineProps<{ shape: BoardShape }>();
const emit = defineEmits<{
  (e: "update:shape", value: BoardShape | null): void;
}>();
const editComponent = computed(() => {
  const type = props.shape.data.type;
  console.log(type);
  if (type && components[type]) {
    return components[type];
  }
});
const inputIng = ref(false);

const delHandler = () => {
  props.shape.delete();
  emit("update:shape", null);
};
// del快捷键删除
const keydownHandler = (ev: KeyboardEvent) => {
  if (
    !inputIng.value &&
    ["Backspace", "Delete"].includes(ev.key) &&
    ![title, compass].includes(props.shape.data.type)
  ) {
    delHandler();
  }
};
onMounted(() => document.documentElement.addEventListener("keydown", keydownHandler));
onUnmounted(() =>
  document.documentElement.removeEventListener("keydown", keydownHandler)
);
</script>

<style lang="scss" scoped>
.def-shape-edit {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  right: 0;
  background: #fff;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 15px 25px 0 10px;
  width: fit-content;
}
.def-close-shape-edit {
  align-self: start;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.85);
  position: absolute;
  right: 5px;
  top: 5px;
  cursor: pointer;
}
</style>

<style lang="scss">
.def-shape-edit {
  .el-form--inline .el-form-item {
    margin-bottom: 0;

    &:not(:last-child) {
      margin-right: 15px;
    }
  }
}

.def-shape-edit-form {
  max-width: 500px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  .el-form-item {
    margin-left: 10px;
    margin-right: 10px;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
  }

  // .el-form-item:nth-child(2n - 1) {
  //   width: 70%;
  // }
  // .el-form-item:nth-child(2n) {
  //   width: 30%;
  // }
}
</style>
