<template>
  <!-- Fullscreen панель выбора колонки -->
  <Teleport to="body">
    <Transition name="slide-up">
      <div v-if="open" class="move-card-overlay" @click.self="close">
        <div class="move-card-panel">
          <!-- Header панели -->
          <div class="panel-header">
            <button class="panel-back" @click="close">
              <i data-lucide="x"></i>
            </button>
            <h3 class="panel-title">Переместить в</h3>
          </div>

          <!-- Информация о карточке -->
          <div class="card-info" v-if="cardTitle">
            <span class="card-title">{{ cardTitle }}</span>
          </div>

          <!-- Список колонок -->
          <div class="panel-options">
            <button 
              v-for="column in columns" 
              :key="column.id"
              class="option-item"
              :class="{ active: currentColumnId === column.id }"
              @click="selectColumn(column.id)"
            >
              <span class="column-dot" :style="{ background: column.color }"></span>
              <span class="option-label">{{ column.title }}</span>
              <span v-if="currentColumnId === column.id" class="current-badge">текущая</span>
              <i v-else data-lucide="arrow-right" class="option-arrow"></i>
            </button>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { watch, onMounted, onUpdated, onUnmounted } from 'vue'

const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  cardId: {
    type: [Number, String],
    default: null
  },
  cardTitle: {
    type: String,
    default: ''
  },
  currentColumnId: {
    type: [Number, String],
    default: null
  },
  columns: {
    type: Array,
    default: () => []
    // [{ id: 1, title: 'Бэклог', color: '#fff' }, ...]
  }
})

const emit = defineEmits(['close', 'move'])

const close = () => {
  emit('close')
}

const selectColumn = (columnId) => {
  if (columnId !== props.currentColumnId) {
    emit('move', {
      cardId: props.cardId,
      fromColumnId: props.currentColumnId,
      toColumnId: columnId,
      toIndex: 0 // В начало колонки
    })
  }
  close()
}

// Refresh icons
const refreshIcons = () => {
  if (window.lucide) {
    setTimeout(() => window.lucide.createIcons(), 10)
  }
}

watch(() => props.open, (val) => {
  if (val) {
    document.body.style.overflow = 'hidden'
    refreshIcons()
  } else {
    document.body.style.overflow = ''
  }
})

// Cleanup при unmount — гарантируем сброс overflow
onUnmounted(() => {
  if (props.open) {
    document.body.style.overflow = ''
  }
})

onMounted(refreshIcons)
onUpdated(refreshIcons)
</script>

<style scoped>
/* Overlay */
.move-card-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 1100;
  display: flex;
  align-items: flex-end;
}

/* Панель */
.move-card-panel {
  width: 100%;
  max-height: 70vh;
  background: var(--bg-body);
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Предотвращаем выделение/копирование текста при long-press */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.panel-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.panel-back {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 8px;
  transition: all 0.15s;
}

.panel-back:active {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-primary);
}

.panel-back i {
  width: 20px;
  height: 20px;
}

.panel-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

/* Информация о карточке */
.card-info {
  padding: 12px 20px;
  background: rgba(255, 255, 255, 0.03);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.card-title {
  font-size: 14px;
  color: var(--text-secondary);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Опции */
.panel-options {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

.option-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: calc(100% + 16px);
  margin-left: -8px;
  margin-right: -8px;
  padding: 16px 16px;
  background: none;
  border: none;
  border-radius: 0;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 16px;
  text-align: left;
  cursor: pointer;
  transition: all 0.15s;
}

.option-item:active {
  background: rgba(255, 255, 255, 0.08);
}

.option-item.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.column-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.option-label {
  flex: 1;
}

.current-badge {
  font-size: 12px;
  color: var(--text-muted);
  background: rgba(255, 255, 255, 0.1);
  padding: 4px 8px;
  border-radius: 6px;
}

.option-arrow {
  width: 18px;
  height: 18px;
  color: var(--text-muted);
  opacity: 0.5;
}

/* Анимация */
.slide-up-enter-active,
.slide-up-leave-active {
  transition: all 0.3s ease;
}

.slide-up-enter-active .move-card-panel,
.slide-up-leave-active .move-card-panel {
  transition: transform 0.3s ease;
}

.slide-up-enter-from,
.slide-up-leave-to {
  opacity: 0;
}

.slide-up-enter-from .move-card-panel,
.slide-up-leave-to .move-card-panel {
  transform: translateY(100%);
}
</style>
