<template>
  <div class="fixed inset-0 z-[120] flex items-center justify-center bg-slate-950/70 px-6 py-10 backdrop-blur" @click.self="$emit('close')">
    <div class="w-full max-w-md rounded-[2.5rem] border border-white/10 bg-gradient-to-b from-slate-900 via-slate-900/90 to-slate-950 p-8 text-white shadow-[0_35px_80px_rgba(2,6,23,0.65)]">
      <div class="flex items-start justify-between gap-4">
        <div>
          <p class="text-xs uppercase tracking-[0.5em] text-slate-400">{{ __('api.print_dialog.title') }}</p>
          <h2 class="mt-2 text-3xl font-semibold text-white">{{ __('api.print_dialog.quantity_prompt') }}</h2>
          <p class="mt-2 text-sm text-slate-300">
            Maximale Ausgabe pro Auftrag:
            <span class="font-semibold text-emerald-300">{{ maxCopies }}</span>
          </p>
        </div>
        <button
          type="button"
          class="rounded-full border border-white/10 p-2 text-slate-200 transition hover:border-rose-300 hover:text-rose-300"
          @click="$emit('close')"
          :aria-label="__('api.print_dialog.cancel_button')"
        >
          <svg class="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
            <path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6m-12 0 12 12" />
          </svg>
        </button>
      </div>

      <div class="mt-8 flex flex-col gap-6">
        <div class="rounded-3xl border border-white/10 bg-white/5 p-6 text-center text-slate-200">
          <p class="text-sm uppercase tracking-[0.4em] text-slate-400">Anzahl</p>
          <div class="mt-4 flex items-center justify-center gap-6">
            <button
              type="button"
              class="flex h-14 w-14 items-center justify-center rounded-2xl border border-white/20 bg-white/10 text-3xl font-semibold text-white transition hover:border-emerald-300 hover:text-emerald-200"
              @click="decrementQuantity"
              aria-label="-1"
            >
              −
            </button>
            <div class="min-w-[4rem] text-6xl font-bold tracking-tight text-white">{{ quantity }}</div>
            <button
              type="button"
              class="flex h-14 w-14 items-center justify-center rounded-2xl border border-white/20 bg-white/10 text-3xl font-semibold text-white transition hover:border-emerald-300 hover:text-emerald-200"
              @click="incrementQuantity"
              aria-label="+1"
            >
              +
            </button>
          </div>
        </div>

        <p class="text-center text-xs uppercase tracking-[0.4em] text-slate-400">
          Du steuerst direkt den Sofortdruck.
        </p>
      </div>

      <div class="mt-8 flex flex-wrap gap-3">
        <button
          type="button"
          class="flex-1 rounded-full border border-white/20 bg-white/5 px-5 py-3 text-center text-sm font-semibold uppercase tracking-[0.2em] text-slate-200 transition hover:border-rose-300 hover:text-rose-200"
          @click="$emit('close')"
        >
          {{ __('api.print_dialog.cancel_button') }}
        </button>
        <button
          type="button"
          class="flex-1 rounded-full border border-emerald-300 bg-emerald-400/20 px-5 py-3 text-center text-sm font-semibold uppercase tracking-[0.2em] text-emerald-100 transition hover:bg-emerald-400/40"
          @click="confirmPrint"
        >
          {{ __('api.print_dialog.print_button') }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';

const props = defineProps({
  maxCopies: {
    type: Number,
    default: 10,
  },
});

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

const quantity = ref(1);

const incrementQuantity = () => {
  if (quantity.value < props.maxCopies) {
    quantity.value++;
  }
};

const decrementQuantity = () => {
  if (quantity.value > 1) {
    quantity.value--;
  }
};

const confirmPrint = () => {
  emit('printConfirmed', quantity.value);
};

watch(
  () => props.maxCopies,
  (newMax) => {
    if (quantity.value > newMax) {
      quantity.value = newMax;
    }
  }
);
</script>
