<template>
  <Draggable
    tag="ul"
    item-key="type"
    ghostClass="ghost"
    :group="{ name: 'people', pull: 'clone', put: false }"
    :sort="false"
    :list="list"
    class="cg-list-container"
    :disabled="isDisabled"
  >
    <template #item="{ element }">
      <li
        v-if="fields.includes(element.type)"
        class="form-edit-widget-label cg-list"
        :class="{ 'no-put': element.type === 'divider' }"
        @click="handleClick(element)"
      >
        <a>
          <Icon :icon="element.customIcon" v-if="element.customIcon" class="mr-5px" />
          <IconFontSymbol v-else :icon="componentIcon(element)" />
          <span class="title">{{ element.label || element.typeName }}</span>
        </a>
      </li>
    </template>
  </Draggable>
  <div class="clear"></div>
</template>

<script lang="ts" setup>
  import { PropType } from 'vue';
  import Draggable from 'vuedraggable';
  import IconFontSymbol from '/@/components/IconFontSymbol/Index.vue';
  import Icon from '/@/components/Icon/src/Icon.vue';
  const props = defineProps({
    title: {
      type: String,
    },
    fields: {
      type: Array as PropType<Array<string>>,
      required: true,
    },
    list: {
      type: Array as PropType<Array<any>>,
      required: true,
    },
    isDisabled: Boolean,
  });

  const componentIcon = (element) => {
    if (element.type === 'button') {
      return element.type + (element.options.buttonType || '');
    }
    return element.type ?? element.icon;
  };
  const emit = defineEmits(['copy']);
  const handleClick = (element: any) => {
    emit('copy', element);
  };
</script>

<style scoped lang="less">
  .left-title {
    height: 14px;
    line-height: 14px;
    padding-left: 10px;
    box-sizing: border-box;
    margin: 15px 0;
    border-left: 6px solid #5e95ff;
    font-weight: bold;
    font-size: 13px;
  }

  .cg-list-container {
    width: 100%;
  }

  .cg-list {
    float: left;
    width: calc((100% - 10px) / 2);
    margin-bottom: 8px;
    text-align: center;
    background: #f8f8f8;
    border: 1px solid #f8f8f8;
    height: 40px;
    line-height: 40px;
    border-radius: 8px;

    &:nth-child(odd) {
      margin-right: 10px;
    }

    &:hover {
      border: 1px solid #1c8dff;
      background-color: #fff;
      cursor: v-bind("props.isDisabled ? 'not-allowed' : 'pointer'");

      a {
        color: #1c8dff;
        cursor: v-bind("props.isDisabled ? 'not-allowed' : 'pointer'");
      }
    }

    > a {
      display: flex;
      align-items: center;
      color: #333;
      padding: 0 5px;

      > span.title {
        position: relative;
        width: 100%;

        &::after {
          content: '';
          position: absolute;
          background: #333;
          left: 0;
          top: 14px;
          height: 12px;
          width: 1px;
        }

        &:hover::after {
          background: #1c8dff !important;
        }
      }

      svg {
        font-size: 17px;
        margin-right: 5px;
      }
    }
  }

  .clear {
    clear: both;
  }
  // @import '/@/assets/style/designer/index.css';
</style>
