<script setup>
import { useSignupForm } from './composables/useSignupForm.js'
import StepAge from './steps/StepAge.vue'
import StepContactPerson from './steps/StepContactPerson.vue'
import StepPersonalData from './steps/StepPersonalData.vue'
import StepRegistrationMode from './steps/StepRegistrationMode.vue'
import StepArrival from './steps/StepArrival.vue'
import StepAddons from './steps/StepAddons.vue'
import StepPhotoPermissions from './steps/StepPhotoPermissions.vue'
import StepAllergies from './steps/StepAllergies.vue'
import StepSummary from './steps/StepSummary.vue'
import SubmitSuccess from './after-submit/SubmitSuccess.vue'
import SubmitAlreadyExists from './after-submit/SubmitAlreadyExists.vue'

const props = defineProps({
    event: Object,
    participantData: Object,
    localGroups: Array,
})

const emit = defineEmits(['registrationDone'])

const {
    currentStep, goToStep, formData, selectedAddons,
    submit, submitting, submitResult, summaryLoading, summaryAmount
} = useSignupForm(props.event, props.participantData)

const steps = [
    { step: 1, label: 'Alter' },
    { step: 2, label: 'Kontaktperson' },
    { step: 3, label: 'Persönliche Daten' },
    { step: 4, label: 'An-/Abreise' },
    { step: 5, label: 'Teilnahmegruppe' },
    { step: 6, label: 'Zusatzoptionen' },
    { step: 7, label: 'Fotoerlaubnis' },
    { step: 8, label: 'Allergien' },
    { step: 9, label: 'Zusammenfassung' },
]
</script>

<template>
    <div>
        <!-- Nach Submit -->
        <SubmitSuccess
            v-if="submitResult?.status === 'success'"
            :participant="submitResult?.participant"
            :event="event"
        />
        <SubmitAlreadyExists v-else-if="submitResult?.status === 'exists'" :event="event" />

        <template v-else>
            <!-- Fortschrittsleiste (ab Step 2) -->
            <div v-if="currentStep > 1" style="margin-bottom: 28px;">
                <div style="display: flex; gap: 6px; flex-wrap: wrap; align-items: center;">
                    <template v-for="(s, index) in steps.filter(s => s.step > 1)" :key="s.step">
                        <!-- Trennlinie zwischen Pills -->
                        <div v-if="index > 0" style="flex-shrink: 0; width: 16px; height: 2px; background: #e5e7eb; border-radius: 1px;"></div>
                        <div
                            :style="{
                                padding: '5px 14px',
                                borderRadius: '999px',
                                fontSize: '0.78rem',
                                fontWeight: '600',
                                whiteSpace: 'nowrap',
                                border: '2px solid',
                                borderColor: currentStep === s.step ? '#2563eb' : currentStep > s.step ? '#bbf7d0' : '#e5e7eb',
                                background: currentStep === s.step ? '#2563eb' : currentStep > s.step ? '#f0fdf4' : '#f9fafb',
                                color: currentStep === s.step ? 'white' : currentStep > s.step ? '#15803d' : '#9ca3af',
                                cursor: currentStep > s.step ? 'pointer' : 'default',
                            }"
                            @click="currentStep > s.step ? goToStep(s.step) : null"
                        >
                            <span v-if="currentStep > s.step" style="margin-right: 4px;">✓</span>
                            {{ s.label }}
                        </div>
                    </template>
                </div>
                <!-- Fortschrittsbalken -->
                <div style="margin-top: 10px; height: 3px; background: #e5e7eb; border-radius: 2px; overflow: hidden;">
                    <div
                        :style="{
                            height: '100%',
                            background: 'linear-gradient(90deg, #2563eb, #3b82f6)',
                            borderRadius: '2px',
                            width: ((currentStep - 2) / (steps.length - 2) * 100) + '%',
                            transition: 'width 0.3s ease',
                        }"
                    ></div>
                </div>
            </div>

            <!-- Steps -->
            <form @submit.prevent="submit">
                <StepAge            v-if="currentStep === 1" :event="event" @next="goToStep" />
                <StepContactPerson  v-if="currentStep === 2" :formData="formData" :event="event" @next="goToStep" @back="goToStep" />
                <StepPersonalData   v-if="currentStep === 3" :formData="formData" :localGroups="localGroups" @next="goToStep" @back="goToStep" />
                <StepArrival        v-if="currentStep === 4" :formData="formData" :event="event" @next="goToStep" @back="goToStep" />
                <StepRegistrationMode v-if="currentStep === 5" :formData="formData" :event="event" @next="goToStep" @back="goToStep" />
                <StepAddons         v-if="currentStep === 6" :formData="formData" :event="event" :selectedAddons="selectedAddons" @next="goToStep" @back="goToStep" />
                <StepPhotoPermissions v-if="currentStep === 7" :formData="formData" :event="event" @next="goToStep" @back="goToStep" />
                <StepAllergies      v-if="currentStep === 8" :formData="formData" :event="event" @next="goToStep" @back="goToStep" />
                <StepSummary
                    v-if="currentStep === 9"
                    :formData="formData"
                    :event="event"
                    :summaryAmount="summaryAmount"
                    :summaryLoading="summaryLoading"
                    :submitting="submitting"
                    @back="goToStep"
                    @submit="submit"
                />
            </form>
        </template>
    </div>
</template>

<style>
.form-table { width: 100%; border-collapse: collapse; }
.form-table td { padding: 8px 12px 8px 0; vertical-align: top; }
.form-table td:first-child { width: 220px; color: #374151; font-weight: 500; }
.form-table input[type="text"],
.form-table input[type="date"],
.form-table select,
.form-table textarea {
    width: 100%;
    padding: 6px 10px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 0.95rem;
    box-sizing: border-box;
}
.btn-row { display: flex; gap: 10px; padding-top: 16px; }
.btn-primary {
    padding: 8px 20px;
    background: #2563eb;
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
}
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary {
    padding: 8px 20px;
    background: #f3f4f6;
    color: #374151;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
}
</style>
