<template>
  <div class="orders-view">
    <div class="header">
      <h1>Bestellübersicht</h1>
      <div class="header-actions">
        <div class="connection-status">
          <span 
            :class="['status-indicator', { 
              'connected': isConnected,
              'connecting': orderStore.connectionStatus === 'connecting'
            }]"
            :title="getConnectionTooltip()"
          ></span>
        </div>
      </div>
    </div>

    <div v-if="error" class="error-message">
      <p>{{ error }}</p>
      <button @click="retryConnection" class="retry-btn">
        Erneut versuchen
      </button>
    </div>

    <div v-if="isLoading && orders.length === 0" class="loading">
      <div class="loading-spinner"></div>
      <p>Lade Bestellungen...</p>
    </div>

    <div v-else-if="orders.length === 0" class="empty-state">
      <div class="empty-icon">🍹</div>
      <h3>Keine Bestellungen vorhanden</h3>
      <p>Es sind noch keine Bestellungen eingegangen.</p>
    </div>

    <div v-else class="orders-container">
      <div class="orders-stats">
        <div class="stat-item">
          <span class="stat-label">Offene Bestellungen:</span>
          <span class="stat-value">{{ totalOrders }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">Letzte Aktualisierung:</span>
          <span class="stat-value">{{ lastUpdate }}</span>
        </div>
      </div>

      <div class="orders-list">
        <OrderCard 
          v-for="order in openOrders" 
          :key="order.id" 
          :order="order"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, computed } from 'vue'
import { useOrderStore } from '@/stores/orderStore'
import OrderCard from '@/components/OrderCard.vue'

const orderStore = useOrderStore()

// Computed properties
const orders = computed(() => orderStore.orders)
const openOrders = computed(() => orderStore.openOrders)
const isLoading = computed(() => orderStore.isLoading)
const error = computed(() => orderStore.error)
const totalOrders = computed(() => orderStore.totalOrders)

const isConnected = computed(() => {
  return orderStore.connectionStatus === 'connected'
})

const lastUpdate = computed(() => {
  return new Date().toLocaleTimeString('de-DE')
})

// Methods
const retryConnection = () => {
  orderStore.connectWebSocket()
}

const getConnectionTooltip = () => {
  if (orderStore.connectionStatus === 'connected') {
    return 'WebSocket verbunden'
  } else if (orderStore.connectionStatus === 'connecting') {
    return 'Verbindung wird hergestellt...'
  } else {
    return 'WebSocket getrennt'
  }
}

// Lifecycle
onMounted(() => {
  orderStore.connectWebSocket()
})

onUnmounted(() => {
  orderStore.disconnectWebSocket()
})
</script>

<style scoped>
.orders-view {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
  padding-bottom: 16px;
  border-bottom: 2px solid #e5e7eb;
}

.header h1 {
  margin: 0;
  font-size: 2rem;
  font-weight: 700;
  color: #1f2937;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.refresh-btn {
  background: #3b82f6;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.refresh-btn:hover:not(:disabled) {
  background: #2563eb;
  transform: translateY(-1px);
}

.refresh-btn:disabled {
  background: #9ca3af;
  cursor: not-allowed;
}

.connection-status {
  display: flex;
  align-items: center;
}

.status-indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #ef4444;
  transition: background-color 0.3s ease;
}

.status-indicator.connected {
  background: #10b981;
}

.status-indicator.connecting {
  background: #f59e0b;
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.7;
    transform: scale(1.1);
  }
}

.error-message {
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 24px;
  text-align: center;
}

.error-message p {
  margin: 0 0 12px 0;
  color: #dc2626;
  font-weight: 500;
}

.retry-btn {
  background: #dc2626;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 0.875rem;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.retry-btn:hover {
  background: #b91c1c;
}

.loading {
  text-align: center;
  padding: 60px 20px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f4f6;
  border-top: 4px solid #3b82f6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading p {
  color: #6b7280;
  font-size: 1rem;
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 16px;
}

.empty-state h3 {
  margin: 0 0 8px 0;
  color: #374151;
  font-size: 1.5rem;
}

.empty-state p {
  margin: 0;
  color: #6b7280;
  font-size: 1rem;
}

.orders-stats {
  display: flex;
  gap: 32px;
  margin-bottom: 24px;
  padding: 16px;
  background: #f9fafb;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
}

.stat-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  font-size: 0.875rem;
  color: #6b7280;
  font-weight: 500;
}

.stat-value {
  font-size: 1.25rem;
  font-weight: 600;
  color: #1f2937;
}

.orders-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

@media (max-width: 768px) {
  .orders-view {
    padding: 16px;
  }
  
  .header {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }
  
  .header h1 {
    font-size: 1.5rem;
  }
  
  .orders-stats {
    flex-direction: column;
    gap: 16px;
  }
}
</style> 