<script setup lang="ts">
import { computed, ref, onMounted, onUnmounted } from 'vue';
import PostcardItem from './PostcardItem.vue';
import type { VotingProgram, AwardConfig } from '@gala/shared/types';

export interface Props {
  programs: VotingProgram[];
  awards?: AwardConfig[];
  columns?: number;
  rows?: number;
}

const props = withDefaults(defineProps<Props>(), {
  columns: 4,
  rows: 2,
});

// Calculate visible programs based on grid size
const maxVisible = computed(() => props.columns * props.rows);

const visiblePrograms = computed(() => {
  return props.programs.slice(0, maxVisible.value);
});

// Refs for postcard items (for stamp animation targeting)
const postcardRefs = ref<Map<string, InstanceType<typeof PostcardItem>>>(new Map());

function setPostcardRef(id: string, el: any) {
  if (el) {
    postcardRefs.value.set(id, el);
  } else {
    postcardRefs.value.delete(id);
  }
}

// Get stamp target element for a specific program
function getStampTarget(programId: string): HTMLElement | null {
  const postcard = postcardRefs.value.get(programId);
  return postcard?.stampTargetRef || null;
}

// Expose for parent component (animation system)
defineExpose({
  getStampTarget,
  postcardRefs,
});
</script>

<template>
  <div
    class="postcard-grid"
    :style="{
      '--columns': columns,
      '--rows': rows,
    }"
  >
    <PostcardItem
      v-for="program in visiblePrograms"
      :key="program.id"
      :ref="(el) => setPostcardRef(program.id, el)"
      :id="program.id"
      :name="program.name"
      :team-name="program.teamName"
      :performer="(program as any).performer"
      :remark="(program as any).remark"
      :order="program.order"
      :votes="program.votes"
      :stamps="program.stamps"
      :awards="awards"
      class="grid-item"
    />

    <!-- Empty slots for incomplete grid -->
    <div
      v-for="n in Math.max(0, maxVisible - visiblePrograms.length)"
      :key="`empty-${n}`"
      class="grid-item empty-slot"
    >
      <div class="empty-placeholder">
        <span class="empty-icon">📮</span>
        <span class="empty-text">待添加节目</span>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.postcard-grid {
  display: grid;
  grid-template-columns: repeat(var(--columns, 4), 1fr);
  grid-template-rows: repeat(var(--rows, 2), 1fr);
  gap: 40px;
  padding: 40px 80px 80px;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background: transparent;
  overflow: visible; // Allow shadows and rotation to bleed out if needed

  // Responsive breakpoints
  @media (max-width: 1200px) {
    grid-template-columns: repeat(3, 1fr);
    gap: 30px;
    padding: 30px;
  }

  @media (max-width: 900px) {
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: auto;
    gap: 20px;
    padding: 20px;
    overflow-y: auto;
    height: auto;
    min-height: 100%;
  }

  @media (max-width: 500px) {
    grid-template-columns: 1fr;
    gap: 16px;
    padding: 16px;
  }
}

.grid-item {
  min-width: 0;
  min-height: 0;
}

.empty-slot {
  background: rgba(255, 255, 255, 0.03);
  border: 2px dashed rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;

  &:hover {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.2);
  }
}

.empty-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  color: rgba(255, 255, 255, 0.25);

  .empty-icon {
    font-size: 48px;
    opacity: 0.6;
    filter: grayscale(1) brightness(0.8);
  }

  .empty-text {
    font-size: 16px;
    font-family: 'Kaiti', 'STKaiti', serif;
    letter-spacing: 2px;
  }
}
</style>
