<template>
  <div class="layout-root">
    <header class="topbar">
      <div class="back-btn-container">
        <button v-show="showBack" class="back-btn" @click="handleBack" tabindex="0">← Back</button>
      </div>
      <div class="spacer"></div>
      <div class="login-btn-container">
        <LoginButton />
      </div>
    </header>
    <main class="main-content">
      <router-view />
    </main>
  </div>
</template>

<script setup lang="ts">
import { useRouter, useRoute } from 'vue-router'
import { computed } from 'vue'
import LoginButton from '../components/shared/LoginButton.vue'

const router = useRouter()
const route = useRoute()

const handleBack = () => {
  if (window.history.length > 1) {
    router.back()
  } else {
    router.push('/child')
  }
}

const showBack = computed(() => route.path !== '/child')
</script>

<style scoped>
/* Only keep styles unique to ChildLayout */

.topbar > .spacer {
  height: 100%;
  display: flex;
  align-items: center;
}

.spacer {
  flex: 1 1 auto;
}
</style>
