<template>
  <div v-if="visible" class="modal-overlay" @click="handleOverlayClick">
    <div
      class="modal-container"
      :class="[sizeClass, { 'dark': themeStore.isDarkMode }]"
      @click.stop
    >
      <!-- 模态框头部 -->
      <div class="modal-header">
        <slot name="header">
          <h2 class="modal-title">{{ title }}</h2>
        </slot>
        <button class="close-btn" @click="$emit('close')" v-if="closable">
          <i class="fas fa-times"></i>
        </button>
      </div>

      <!-- 模态框内容 -->
      <div class="modal-body">
        <slot></slot>
      </div>

      <!-- 模态框底部 -->
      <div class="modal-footer" v-if="$slots.footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script setup>
import {computed, onMounted, onUnmounted} from 'vue';
import {useThemeStore} from '../stores/theme.ts';

const themeStore = useThemeStore();

const props = defineProps({
  visible: {
    type: Boolean,
    default: true
  },
  title: {
    type: String,
    default: ''
  },
  size: {
    type: String,
    default: 'medium',
    validator: (value) => ['small', 'medium', 'large', 'full'].includes(value)
  },
  closable: {
    type: Boolean,
    default: true
  },
  maskClosable: {
    type: Boolean,
    default: true
  }
});

const emit = defineEmits(['close']);

const sizeClass = computed(() => {
  return `modal-${props.size}`;
});

const handleOverlayClick = () => {
  if (props.maskClosable) {
    emit('close');
  }
};

const handleEscKey = (event) => {
  if (event.key === 'Escape' && props.closable) {
    emit('close');
  }
};

onMounted(() => {
  document.addEventListener('keydown', handleEscKey);
  document.body.style.overflow = 'hidden';
});

onUnmounted(() => {
  document.removeEventListener('keydown', handleEscKey);
  document.body.style.overflow = '';
});
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
  padding: 20px;
}

.modal-container {
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  animation: slideUp 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.modal-container.dark {
  background: #2d2d2d;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}

/* 尺寸变体 */
.modal-small {
  width: 100%;
  max-width: 400px;
}

.modal-medium {
  width: 100%;
  max-width: 600px;
}

.modal-large {
  width: 100%;
  max-width: 800px;
}

.modal-full {
  width: 95vw;
  height: 95vh;
  max-width: none;
  max-height: none;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 30px;
  border-bottom: 1px solid #e2e8f0;
  background: linear-gradient(135deg, #4361ee, #3f37c9);
  color: white;
  flex-shrink: 0;
}

.modal-container.dark .modal-header {
  border-bottom-color: #4b5563;
}

.modal-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.close-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  color: white;
  font-size: 14px;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

.modal-body {
  padding: 24px 30px;
  overflow-y: auto;
  flex: 1;
  color: #374151;
}

.modal-container.dark .modal-body {
  color: #d1d5db;
}

.modal-footer {
  padding: 20px 30px;
  border-top: 1px solid #e2e8f0;
  background: #f8fafc;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  flex-shrink: 0;
}

.modal-container.dark .modal-footer {
  background: #374151;
  border-top-color: #4b5563;
}

/* 按钮样式 */
:deep(.modal-btn) {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

:deep(.modal-btn-cancel) {
  background: #f1f5f9;
  color: #64748b;
  border: 1px solid #e2e8f0;
}

:deep(.modal-btn-cancel:hover) {
  background: #e2e8f0;
  color: #475569;
}

:deep(.modal-btn-confirm) {
  background: linear-gradient(135deg, #4361ee, #3f37c9);
  color: white;
}

:deep(.modal-btn-confirm:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(67, 97, 238, 0.3);
}

.modal-container.dark :deep(.modal-btn-cancel) {
  background: #4b5563;
  color: #d1d5db;
  border-color: #6b7280;
}

.modal-container.dark :deep(.modal-btn-cancel:hover) {
  background: #6b7280;
  color: #f3f4f6;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-overlay {
    padding: 10px;
  }

  .modal-small,
  .modal-medium,
  .modal-large {
    width: 100%;
    max-width: none;
  }

  .modal-full {
    width: 100vw;
    height: 100vh;
    border-radius: 0;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding-left: 20px;
    padding-right: 20px;
  }
}
</style>
