<template>
	<div class="container">
		<header class="header">
			<a href="/">
				<h1 class="title">
					<div
						class="logo"
						v-html="logo"
					/>shack
				</h1>
			</a>

			<div class="search">
				<input
					type="search"
					class="input"
					placeholder="Search the shack"
				>
			</div>

			<div class="actions">
				<VDropdown>
					<button class="button">Explore</button>

					<template #popper>
						<ul class="tooltip nolist">
							<li class="tooltip-item">
								<a
									href="/shelf/create"
									class="menu-item"
								>
									<button class="button button-submit">New shelf</button>
								</a>
							</li>

							<li
								v-for="shelf in shelves"
								:key="shelf.id"
								class="tooltip-item"
							>
								<a
									:href="`/s/${shelf.slug}`"
									class="link shelf"
								>s/{{ shelf.slug }}</a>
							</li>
						</ul>
					</template>
				</VDropdown>
			</div>

			<VDropdown v-if="me">
				<span class="userpanel">{{ me.username }}</span>

				<template #popper>
					<ul	class="tooltip nolist">
						<li>
							<a
								:href="`/user/${me.username}`"
								class="link menu-item"
							>Profile</a>
						</li>

						<li class="menu-item">
							<button
								class="button"
								@click="logout"
							>Log out</button>
						</li>
					</ul>
				</template>
			</VDropdown>

			<a
				v-else
				href="/account/login"
				class="link userpanel"
			>Log in</a>
		</header>

		<div class="content-container">
			<slot />
			<footer class="footer">shack {{ version }}</footer>
		</div>
	</div>
</template>

<script setup>
import logo from '../assets/img/logo.svg?raw'; // eslint-disable-line import/no-unresolved
import { del } from '../assets/js/api';
import { navigate } from '../assets/js/navigate';
import { usePageContext } from './usePageContext';

const { pageData, me } = usePageContext();
const { shelves } = pageData;

const version = CLIENT_VERSION;

async function logout() {
	await del('/session');
	navigate('/account/login');
}
</script>

<style>
.content {
	display: flex;
	flex-grow: 1;
	flex-direction: column;
	padding: 1rem;
}

.logo svg {
    height: 100%;
    width: auto;
}
</style>

<style scoped>
.container {
    height: 100%;
    display: flex;
    flex-direction: column;
    background: var(--background-dark-10);
}

.content-container {
    display: flex;
    flex-direction: column;
    flex-grow: 1;
	overflow-y: auto;
}

.header {
    display: flex;
	align-items: center;
	flex-shrink: 0;
    background: var(--background);
    box-shadow: 0 0 3px var(--shadow-weak-10);
	overflow: hidden;
}

.logo {
	width: auto;
    height: 1.75rem;
    fill: var(--primary);
    margin: .75rem .5rem 1rem 1rem;
}

.title {
    font-size: 0;
}

.search {
    display: flex;
    flex-grow: 1;
    align-items: center;
    margin: 0 1rem;

    .input {
	flex-grow: 1;
    }
}

.actions {
	display: flex;
	align-items: center;
}

.userpanel {
	display: flex;
	align-items: center;
	padding: 1rem;
	font-weight: bold;
	cursor: pointer;
}

.tooltip {
	width: 10rem;
	padding-bottom: .25rem;

	.button {
		width: 100%;
	}

	.shelf {
		display: block;
		box-sizing: border-box;
		padding: .25rem .5rem;
	}
}

.footer {
	padding: .5rem 1rem;
	color: var(--shadow);
	text-align: center;
}
</style>
