<script setup>
/**
 * Big red warning shown when a shipment is scanned/loaded while the commission
 * system is offline. Purely informational: it auto-closes after 5 seconds, any
 * click dismisses it immediately, and NOTHING behind it is disabled — scanner
 * input keeps focus, so commissioning continues even while the warning is shown.
 * Plain in-page overlay (no Teleport — scoped styles stay reliable).
 */
const props = defineProps({
  modelValue: { type: Boolean, default: false }
})
const emit = defineEmits(['update:modelValue'])

const { t } = useTranslation()

const AUTO_CLOSE_MS = 5000
let autoCloseTimer = null

const close = () => {
  if (autoCloseTimer) {
    clearTimeout(autoCloseTimer)
    autoCloseTimer = null
  }
  emit('update:modelValue', false)
}

watch(() => props.modelValue, (open) => {
  if (autoCloseTimer) {
    clearTimeout(autoCloseTimer)
    autoCloseTimer = null
  }
  if (open) {
    autoCloseTimer = setTimeout(close, AUTO_CLOSE_MS)
  }
})

onBeforeUnmount(() => {
  if (autoCloseTimer) clearTimeout(autoCloseTimer)
})
</script>

<template>
  <Transition name="offline-warning-fade">
    <div v-if="modelValue" class="offline-warning-overlay" @click="close">
      <div class="offline-warning-panel" role="alertdialog" aria-live="assertive">
        <div class="offline-warning-icon">
          <i class="ti ti-alert-triangle"></i>
        </div>
        <h2 class="offline-warning-title">{{ t.commission_system_offline_warning_title }}</h2>
        <p class="offline-warning-text">{{ t.commission_system_offline_warning_text }}</p>
        <div class="offline-warning-progress"></div>
      </div>
    </div>
  </Transition>
</template>

<style scoped>
.offline-warning-overlay {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 0, 5, 0.55);
  padding: 1rem;
  cursor: pointer;
}
.offline-warning-panel {
  position: relative;
  overflow: hidden;
  max-width: 34rem;
  width: 100%;
  background: #cc0f35;
  color: #fff;
  border-radius: 1rem;
  padding: 2.25rem 2rem 2.5rem;
  text-align: center;
  box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.45);
  animation: offline-warning-shake 0.5s ease;
}
.offline-warning-icon {
  font-size: 3.5rem;
  line-height: 1;
  margin-bottom: 0.75rem;
}
.offline-warning-title {
  font-size: 1.6rem;
  font-weight: 800;
  margin-bottom: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.offline-warning-text {
  font-size: 1.05rem;
  line-height: 1.5;
  opacity: 0.95;
}
/* auto-close countdown bar (matches AUTO_CLOSE_MS) */
.offline-warning-progress {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 0.35rem;
  width: 100%;
  background: rgba(255, 255, 255, 0.85);
  transform-origin: left;
  animation: offline-warning-countdown 5s linear forwards;
}
@keyframes offline-warning-countdown {
  from { transform: scaleX(1); }
  to { transform: scaleX(0); }
}
@keyframes offline-warning-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}
.offline-warning-fade-enter-active,
.offline-warning-fade-leave-active {
  transition: opacity 0.25s ease;
}
.offline-warning-fade-enter-from,
.offline-warning-fade-leave-to {
  opacity: 0;
}
</style>
