<template>
  <div style="display:flex; flex-direction:column; height:100%; overflow:hidden;">
    <!-- Tabs Header -->
    <div class="cw-lp-tabs">
      <button type="button" class="cw-lp-tab" :class="{ 'is-active': activeTab === 'blocks' }" @click="activeTab = 'blocks'">Blocks</button>
      <button type="button" class="cw-lp-tab" :class="{ 'is-active': activeTab === 'design' }" @click="activeTab = 'design'">Design</button>
    </div>

    <!-- TAB 1: BLOCKS -->
    <div v-show="activeTab === 'blocks'" class="cw-lp-content" style="flex: 1; overflow-y: auto; overflow-x: hidden; padding: 14px 12px;">
      <div class="fb-palette-label" style="margin-bottom:8px">Content</div>
      <draggable
        :list="CONTENT_PALETTE_ITEMS"
        :group="{ name: 'form-blocks', pull: 'clone', put: false }"
        :clone="makeBlock"
        item-key="type"
        class="cw-block-grid"
        @start="onDragStart"
        @end="onDragEnd"
      >
        <template #item="{ element: item }">
          <div class="cw-block-card" @click="addBlock(item)">
            <div class="cw-block-card-dots">⠿</div>
            <div class="cw-block-card-icon" v-html="item.icon"></div>
            <div class="cw-block-card-label">{{ item.label }}</div>
          </div>
        </template>
      </draggable>

      <div class="fb-palette-label" style="margin:16px 0 8px">Form Fields</div>
      <draggable
        :list="FIELD_PALETTE_ITEMS"
        :group="{ name: 'form-blocks', pull: 'clone', put: false }"
        :clone="makeBlock"
        item-key="type"
        class="cw-block-grid"
        @start="onDragStart"
        @end="onDragEnd"
      >
        <template #item="{ element: item }">
          <div class="cw-block-card" :class="{ 'is-disabled': item.type === 'submit-button' && hasSubmitButton }" :style="item.type === 'submit-button' && hasSubmitButton ? { opacity: 0.4, pointerEvents: 'none' } : {}" @click="addBlock(item)">
            <div class="cw-block-card-dots">⠿</div>
            <div class="cw-block-card-icon" v-html="item.icon"></div>
            <div class="cw-block-card-label">{{ item.label }}</div>
          </div>
        </template>
      </draggable>
    </div>

    <!-- TAB 2: DESIGN -->
    <div v-show="activeTab === 'design'" class="cw-lp-content" style="flex: 1; overflow-y: auto; overflow-x: hidden; padding: 14px 12px;">
      <div v-if="bodyBlock" class="cw-design-settings">
        <div class="cw-lp-section-title" style="margin-bottom:10px">Form Container</div>
        <div class="cw-settings-row">
          <span class="cw-settings-lbl">Background</span>
          <CwColorPicker v-model="bodyBlock.bgColor" />
        </div>
        <div class="cw-settings-row" style="margin-top:10px">
          <span class="cw-settings-lbl">Corner Radius</span>
          <div class="cw-unit-input">
            <input type="number" class="cw-field-input" v-model.number="bodyBlock.borderRadius" min="0" max="40" style="width:60px" />
            <span class="cw-unit-tag">px</span>
          </div>
        </div>
        <div style="margin-top:14px">
          <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:6px">
            <span class="cw-settings-lbl">Width</span>
            <span style="font-size:12px;font-weight:600;color:#4f46e5">{{ bodyBlock.contentWidth || 480 }}px</span>
          </div>
          <input type="range" min="320" max="700" step="10" v-model.number="bodyBlock.contentWidth" style="width:100%;accent-color:#6366f1" @change="saveHistory" />
        </div>
        <div class="cw-settings-row" style="margin-top:14px">
          <span class="cw-settings-lbl">Font Family</span>
          <select class="cw-field-select" v-model="bodyBlock.fontFamily">
            <option value="Inter">Inter</option>
            <option value="Georgia">Georgia</option>
            <option value="Verdana">Verdana</option>
            <option value="Arial">Arial</option>
          </select>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, inject } from 'vue';
import draggable from 'vuedraggable';
import CwColorPicker from '@/Components/CwColorPicker.vue';
import { CONTENT_PALETTE_ITEMS, FIELD_PALETTE_ITEMS } from './formEditorUtils';

const {
  blocks,
  selectedBlockId,
  bodyBlock,
  onDragStart,
  onDragEnd,
  saveHistory,
  makeBlock,
} = inject('formEditorState');

const activeTab = ref('blocks');

const hasSubmitButton = computed(() => blocks.value.some((b) => b.type === 'submit-button'));

function addBlock(item) {
  if (item.type === 'submit-button' && hasSubmitButton.value) return;
  const block = makeBlock(item);
  blocks.value.push(block);
  selectedBlockId.value = block.id;
  saveHistory();
}
</script>

<style scoped>
.cw-lp-tabs {
    display: flex;
    border-bottom: 2px solid #e5e7eb;
    flex-shrink: 0;
}
.cw-lp-tab {
    flex: 1;
    padding: 12px 4px;
    font-size: 13px;
    font-weight: 500;
    color: #6b7280;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s;
}
.cw-lp-tab.is-active {
    color: var(--active-text);
    border-bottom-color: var(--active-text);
    font-weight: 600;
}

.cw-block-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.cw-block-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 10px 6px;
    background: var(--bg-surface);
    border: 1.5px solid var(--border-color);
    border-radius: 10px;
    cursor: grab;
    user-select: none;
    transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
    position: relative;
    width: calc(33.333% - 6px);
    box-sizing: border-box;
}
.cw-block-card:hover {
    border-color: #10b981;
    background: #f0fdf4;
    box-shadow: 0 2px 8px rgba(16,185,129,0.10);
}
.cw-block-card:active { cursor: grabbing; }
.cw-block-card-dots {
    position: absolute;
    top: 4px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 11px;
    color: #d1d5db;
    letter-spacing: 1px;
    line-height: 1;
}
.cw-block-card-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #10b981;
    margin-top: 8px;
}
.cw-block-card-label {
    font-size: 11px;
    font-weight: 500;
    color: var(--text-primary);
    text-align: center;
    line-height: 1.2;
}
</style>
