<template>
  <div class="wrap">
    <div class="chunk">
      <div class="title">
        <i class="el-icon-monitor"></i>
        <div class="text">首页组件模块</div>
      </div>
      <div class="content">
        <div
          class="item"
          v-for="(item, index) in dropElement"
          :key="index"
          draggable="true"
          @click="dropIn(item)"
          @dragstart="dragstart($event, item)"
        >
          <i :class="item.icon"></i>
          {{ item.name }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import _ from "lodash";
import { mapGetters, mapActions } from "vuex";
import { v4 as uuidv4 } from "uuid";
export default {
  props: {
    dropElement: {
      type: Array,
      default: () => {
        return [];
      },
    },
    littleModule: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  computed: {
    ...mapGetters(["elementList"]),
  },
  methods: {
    //点击模块录用数据
    dropIn(item) {
      const drop_element = _.cloneDeep(item);
      if (!drop_element) return;
      let tempFlag = this.elementList.some((item) => {
        if (drop_element.componentName === item.componentName) {
          return true;
        } else {
          return false;
        }
      });
      if (tempFlag) {
        this.$message({
          type: "warning",
          message: "禁止加入相同组件",
        });
        return;
      }
      const dropElement = drop_element;
      dropElement.id = uuidv4();
      dropElement.key = uuidv4();
      dropElement.component =
        this.$options.components[dropElement.componentName];
      this.updateElementList(dropElement);
    },
    //拖拽开始
    dragstart(e, item) {
      e.dataTransfer.setData("drop_element", JSON.stringify(_.cloneDeep(item)));
    },
    ...mapActions("dropIndex", ["updateElementList", "=-"]),
  },
};
</script>

<style scoped lang='scss'>
.wrap {
  width: 100%;
  height: 100%;
  background-color: #fff;
  padding: 5px;
  > .chunk {
    > .title {
      margin-top: 10px;
      display: flex;
      align-content: center;
      font-size: 14px;
      > .text {
        margin-left: 10px;
      }
    }
    > .content {
      display: flex;
      margin-top: 10px;
      justify-content: space-between;
      align-items: center;
      flex-wrap: wrap;
      > .item {
        cursor: pointer;
        margin-top: 5px;
        width: 49%;
        font-size: 12px;
        padding: 15px 10px;
        background-color: #f6f7fe;
        border: 1px transparent dashed;
        &:hover {
          color: #797be1;
          border: 1px #797be1 dashed;
        }
      }
    }
  }
}
</style>
