<script setup lang="ts">
import type { SlotInfo } from '@/stores/gameStore'
import { useI18n } from '@/composables/useI18n'

const { t } = useI18n()

defineProps<{
  saves: SlotInfo[]
}>()

const emit = defineEmits<{
  save: [slot: number]
  load: [slot: number]
  close: []
}>()

const maxSlots = 5
</script>

<template>
  <div class="save-overlay" @click.self="emit('close')" @keydown.escape="emit('close')">
    <div class="save-panel">
      <h2 class="save-title">{{ t('ui.save') }} / {{ t('ui.load') }}</h2>

      <div class="slot-list">
        <div class="save-slot auto-save-slot">
          <div class="slot-thumb">
            <img
              v-if="saves.find(s => s.slot === 0)?.thumbnail"
              :src="saves.find(s => s.slot === 0)!.thumbnail"
              class="thumb-img"
            />
            <span v-else class="thumb-empty">自动</span>
          </div>
          <div class="slot-meta">
            <div class="slot-label auto-save-label">{{ t('ui.autoSave') }}</div>
            <div class="slot-info" v-if="saves.find(s => s.slot === 0)">
              {{ saves.find(s => s.slot === 0)!.sceneLabel }}
            </div>
            <div class="slot-info empty" v-else>{{ t('ui.noAutoSave') }}</div>
          </div>
          <div class="slot-actions">
            <button
              class="slot-btn load-btn"
              :disabled="!saves.find(s => s.slot === 0)"
              @click="emit('load', 0)"
            >
              {{ t('ui.load') }}
            </button>
          </div>
        </div>

        <div
          v-for="slot in maxSlots"
          :key="slot"
          class="save-slot"
        >
          <div class="slot-thumb">
            <img
              v-if="saves.find(s => s.slot === slot)?.thumbnail"
              :src="saves.find(s => s.slot === slot)!.thumbnail"
              class="thumb-img"
            />
            <span v-else class="thumb-empty">{{ t('ui.empty') }}</span>
          </div>
          <div class="slot-meta">
            <div class="slot-label">{{ t('ui.save') }} {{ slot }}</div>
            <div class="slot-info" v-if="saves.find(s => s.slot === slot)">
              {{ saves.find(s => s.slot === slot)!.sceneLabel }}
            </div>
            <div class="slot-info empty" v-else>{{ t('ui.empty') }}</div>
          </div>
          <div class="slot-actions">
            <button class="slot-btn save-btn" @click="emit('save', slot)">{{ t('ui.save') }}</button>
            <button
              class="slot-btn load-btn"
              :disabled="!saves.find(s => s.slot === slot)"
              @click="emit('load', slot)"
            >
              {{ t('ui.load') }}
            </button>
          </div>
        </div>
      </div>

      <div class="save-hint">{{ t('ui.autoSaveHint') }}</div>
      <button class="close-btn" @click="emit('close')">{{ t('ui.close') }}</button>
    </div>
  </div>
</template>

<style scoped>
.save-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.save-panel {
  background: rgba(12, 12, 18, 0.85);
  border: none;
  border-radius: 12px;
  padding: 32px;
  min-width: 480px;
  max-width: 560px;
}

.save-title {
  text-align: center;
  font-size: 20px;
  font-weight: 400;
  letter-spacing: 2px;
  margin-bottom: 24px;
  color: #c9a84c;
}

.slot-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.save-slot {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 4px;
}

.auto-save-slot {
  border-color: rgba(201, 168, 76, 0.3);
  background: rgba(201, 168, 76, 0.06);
}

.auto-save-label {
  color: #c9a84c;
}

.slot-thumb {
  width: 64px;
  height: 36px;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 3px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.thumb-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.thumb-empty {
  font-size: 12px;
  color: #444;
}

.slot-meta {
  flex: 1;
  min-width: 0;
}

.slot-label {
  font-size: 12px;
  color: #888;
}

.slot-info {
  font-size: 13px;
  color: #ccc;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.slot-info.empty {
  color: #555;
}

.slot-actions {
  display: flex;
  gap: 6px;
}

.slot-btn {
  padding: 6px 14px;
  font-size: 13px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 3px;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.08);
  color: #ddd;
  transition: background 0.2s;
}

.slot-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.18);
}

.slot-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.save-hint {
  text-align: center;
  font-size: 12px;
  color: #555;
  margin-top: 16px;
}

.close-btn {
  display: block;
  margin: 12px auto 0;
  padding: 10px 32px;
  font-size: 14px;
  color: #888;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 4px;
  cursor: pointer;
}

.close-btn:hover {
  color: #ccc;
  background: rgba(255, 255, 255, 0.1);
}
</style>
