<template>
    <div>
        <div v-if="isOpen" class="dialog-overlay">
            <div class="dialog-content">
                <h3>Gottesdienst-Konfiguration</h3>
                <div>
                    <label for="location-select">Bitte wählen Sie den Ort für den Gottestdienst aus:</label>
                </div>
                <div>
                    <select id="location-select" v-model="selectedLocation">
                        <option :value="-1">Alle Orte</option>
                        <option v-for="location in locations" :key="location.id" :value="location.id">
                            {{ location.name }}
                        </option>
                    </select>
                </div>
                <div>
                    <button @click="confirmWorshipConfiguration">Bestätigen</button>
                    <button @click="closeWorshipDialog">Schließen</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { ref } from 'vue';
import axios from '@/axios';

export default {
    name: 'WorshipDialog',
    emits: ['confirm'],
    setup(props, { emit }) {
        const isOpen = ref(false);
        const locations = ref([]);
        const selectedLocation = ref(-1);

        const openWorshipDialog = () => {
            isOpen.value = true;
            fetchLocations();
        };

        const closeWorshipDialog = () => {
            isOpen.value = false;
        };

        const fetchLocations = async () => {
            try {
                const response = await axios.get('/event-places');
                locations.value = response.data;
            } catch (error) {
                console.error('Fehler beim Laden der Orte:', error);
            }
        };

        const confirmWorshipConfiguration = () => {
            emit('confirm', selectedLocation.value);
            closeWorshipDialog();
        };

        return {
            isOpen,
            locations,
            selectedLocation,
            openWorshipDialog,
            closeWorshipDialog,
            confirmWorshipConfiguration,
        };
    },
};
</script>

<style scoped>
.dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
}

.dialog-content {
    background: white;
    padding: 20px;
    border-radius: 8px;
}
</style>