<template>
  <div
    :class="['column', selected ? 'selected' : hovered ? 'hovered' : '']"
    :title="column.df.fieldname"
    @click.stop="handleColumnClick"
    @mouseover.stop="hovered = true"
    @mouseout.stop="hovered = false"
  >
    <div
      v-if="column.df.label"
      class="column-header"
      :hidden="!column.df.label && store.readOnly"
    >
      <div class="column-label">
        <span>{{ column.df.label }}</span>
      </div>
    </div>
    <div v-if="column.df.description" class="column-description">
      {{ column.df.description }}
    </div>
    <draggable
      class="column-container"
      v-model="column.fields"
      group="fields"
      :delay="isTouchScreenDevice() ? 200 : 0"
      :animation="200"
      :easing="store.getAnimation"
      item-key="id"
      :disabled="store.readOnly"
      @click="handleContainerClick"
    >
      <template #item="{ element }">
        <Field
          :column="column"
          :field="element"
          :data-is-user-generated="store.isUserGeneratedField(element)"
        />
      </template>
    </draggable>
    <div class="empty-column" :hidden="store.readOnly">
      <AddFieldButton :column="column" />
    </div>
    <div v-if="column.fields.length" class="add-new-field-btn">
      <AddFieldButton :field="column.fields[column.fields.length - 1]" :column="column" />
    </div>
  </div>
</template>

<script setup>
import draggable from "vuedraggable";
import Field from "./Field.vue";
import AddFieldButton from "./AddFieldButton.vue";
import { computed, ref } from "vue";
import { useFormBuilderStore } from "../store";
import { isTouchScreenDevice } from "../utils";
import { useMagicKeys, whenever } from "@vueuse/core";

const props = defineProps(["section", "column"]);
const store = useFormBuilderStore();

// delete/backspace to delete the field
const { Backspace } = useMagicKeys();
whenever(Backspace, (value) => {
  if (value && selected.value && store.notUsingInput) {
    // Column delete handled by section
  }
});

const hovered = ref(false);
const selected = computed(() => store.selected(props.column.df.name));

function handleColumnClick(event) {
  // 如果点击的是 AddFieldButton，不选中列
  if (event.target.closest('.add-field-btn, .add-field-wrapper')) {
    return;
  }
  store.form.selected_field = props.column.df;
}

function handleContainerClick(event) {
  // 如果点击的是按钮，不阻止事件传播
  if (event.target.closest('.add-field-btn, .add-field-wrapper')) {
    return;
  }
  // 阻止事件冒泡到列容器，避免触发 handleColumnClick
  event.stopPropagation();
}
</script>

<style lang="scss" scoped>
.column {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  background-color: #f9fafb;
  border-radius: 8px;
  border: 1px dashed #9ca3af;
  padding: 0.5rem;
  margin-left: 4px;
  margin-right: 4px;

  &.hovered,
  &.selected {
    border-color: #3b82f6;
    border-style: solid;
  }

  .column-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 0.5rem;
    padding-left: 0.3rem;

    .column-label {
      :deep(span) {
        font-weight: 600;
        color: #111827;
      }
    }

    .column-actions {
      display: flex;
      justify-content: flex-end;

      .btn.btn-icon {
        padding: 2px;
        box-shadow: none;
        opacity: 0;

        &:hover {
          opacity: 1;
          background-color: #fff;
        }
      }
    }
  }

  .column-description {
    margin-bottom: 10px;
    margin-left: 0.3rem;
    font-size: 12px;
    color: #6b7280;
  }

  &:first-child {
    margin-left: 0px;
  }

  &:last-child {
    margin-right: 0px;
  }

  .column-container {
    min-height: 2rem;
    border-radius: 8px;
    z-index: 1;

    &:empty {
      flex: 1;
      & + .empty-column {
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        gap: 5px;
        width: 100%;
        padding: 15px;
        z-index: 5; // 确保在 column-container 上面

        button {
          background-color: #f9fafb;
          z-index: 10;
          position: relative;

          &:hover {
            background-color: #e5e7eb;
          }
        }
      }
    }

    & + .empty-column {
      display: none;
    }
  }

  .add-new-field-btn {
    padding: 10px 6px 5px;

    button {
      background-color: #fff;

      &:hover {
        background-color: #e5e7eb;
      }
    }
  }
}
</style>
