<script setup lang="ts">
import { watch } from 'vue'
import LanguageSelect from '../components/LanguageSelect.vue'
import { useTriageSession } from '../composables/useTriageSession'
import { useRouter } from 'vue-router'

const router = useRouter()
const { language, isLoadingConfig, loadError, loadQuestions } = useTriageSession()

watch(
  () => language.value,
  (val) => {
    document.documentElement.lang = val
  },
  { immediate: true },
)

watch(
  () => language.value,
  () => {
    void loadQuestions()
  },
  { immediate: true },
)

function goNext() {
  if (!isLoadingConfig.value && !loadError.value) {
    router.push('/complaint')
  }
}
</script>

<template>
  <section>
    <LanguageSelect v-model="language" />

    <p v-if="isLoadingConfig" class="status">Konfiguration wird geladen ...</p>
    <p v-if="loadError" class="status error">{{ loadError }}</p>

    <div class="actions">
      <button class="primary" type="button" :disabled="!!loadError || isLoadingConfig" @click="goNext">
        Weiter
      </button>
    </div>
  </section>
</template>

<style scoped>
.status {
  margin-top: 1rem;
  font-size: 0.9rem;
  color: #4b5563;
}

.status.error {
  color: #b91c1c;
}

.actions {
  margin-top: 1.5rem;
}
</style>
