<template>
  <Teleport to="body">
    <Transition name="alert">
      <div v-if="alert.show" class="global-alert" :class="alertClass">
        <div class="alert-icon">
          <svg v-if="alert.type === 'success'" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" />
            <polyline points="22 4 12 14.01 9 11.01" />
          </svg>
          <svg v-else-if="alert.type === 'error'" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <circle cx="12" cy="12" r="10" />
            <line x1="15" y1="9" x2="9" y2="15" />
            <line x1="9" y1="9" x2="15" y2="15" />
          </svg>
          <svg v-else-if="alert.type === 'warning'" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z" />
            <line x1="12" y1="9" x2="12" y2="13" />
            <line x1="12" y1="17" x2="12.01" y2="17" />
          </svg>
          <svg v-else viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <circle cx="12" cy="12" r="10" />
            <line x1="12" y1="16" x2="12" y2="12" />
            <line x1="12" y1="8" x2="12.01" y2="8" />
          </svg>
        </div>
        <div class="alert-content">
          <div v-if="alert.title" class="alert-title">{{ alert.title }}</div>
          <div class="alert-message">{{ alert.message }}</div>
        </div>
        <button class="alert-close" @click="handleClose">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <line x1="18" y1="6" x2="6" y2="18" />
            <line x1="6" y1="6" x2="18" y2="18" />
          </svg>
        </button>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { computed } from 'vue'
import { useScreenStore } from '@/stores/screen'

const screenStore = useScreenStore()

const alert = computed(() => screenStore.globalAlert)

const alertClass = computed(() => {
  return `alert-${alert.value.type || 'info'}`
})

const handleClose = () => {
  screenStore.hideAlert()
}
</script>

<style scoped>
.global-alert {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 20px 24px;
  background: var(--bg-card);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  z-index: 9999;
  max-width: 500px;
  min-width: 320px;
}

.alert-success {
  border-left: 6px solid var(--success);
}

.alert-error {
  border-left: 6px solid var(--danger);
}

.alert-warning {
  border-left: 6px solid var(--warning);
}

.alert-info {
  border-left: 6px solid var(--primary);
}

.alert-icon {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
}

.alert-success .alert-icon {
  color: var(--success);
}

.alert-error .alert-icon {
  color: var(--danger);
}

.alert-warning .alert-icon {
  color: var(--warning);
}

.alert-info .alert-icon {
  color: var(--primary);
}

.alert-icon svg {
  width: 100%;
  height: 100%;
}

.alert-content {
  flex: 1;
}

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

.alert-message {
  font-size: 16px;
  color: var(--text-secondary);
  line-height: 1.5;
}

.alert-close {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.alert-close:hover {
  background: var(--bg-page);
  color: var(--text-primary);
}

.alert-close svg {
  width: 20px;
  height: 20px;
}

/* 动画 */
.alert-enter-active,
.alert-leave-active {
  transition: all 0.3s ease;
}

.alert-enter-from,
.alert-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}
</style>
