<template>
	<div
		v-if="user"
		class="user"
	>
		<div class="header">
			<h2 class="username">{{ user.username }}</h2>
		</div>

		<section
			v-if="user.stashes?.length > 0"
			class="section"
		>
			<h3 class="heading">Stashes</h3>

			<ul class="stashes nolist">
				<li
					v-for="stash in user.stashes"
					:key="stash.id"
				>
					<Stash
						:stash="stash"
						:is-me="isMe"
						@publish="() => fetchUser()"
					/>
				</li>

				<li
					v-if="isMe"
					class="stashes-add"
					@click="showAddStash = true"
				>
					<Icon icon="plus2" />
				</li>
			</ul>
		</section>

		<AddStash
			v-if="showAddStash"
			@close="closeAddStash"
		/>
	</div>
</template>

<script>
import Stash from './stash.vue';
import AddStash from '../stashes/add-stash.vue';

async function fetchUser() {
	this.user = await this.$store.dispatch('fetchUser', this.$route.params.username);
	this.isMe = this.user.id === this.$store.state.auth.user?.id;

	this.pageTitle = this.user?.username;
}

async function closeAddStash(addedStash) {
	this.showAddStash = false;

	if (addedStash) {
		await this.fetchUser();
	}
}

async function mounted() {
	await this.fetchUser();
}

export default {
	components: {
		AddStash,
		Stash,
	},
	data() {
		return {
			user: this.$route.params.username === this.$store.state.auth.user?.username
				? this.$store.state.auth.user
				: null,
			isMe: false,
			pageTitle: null,
			showAddStash: false,
		};
	},
	mounted,
	methods: {
		closeAddStash,
		fetchUser,
	},
};
</script>

<style lang="scss" scoped>
@import 'breakpoints';

.header {
	padding: .5rem 1rem;
	background: var(--profile);
}

.username {
	margin: 0;
	font-size: 1.5rem;
	color: var(--text-light);
}

.section {
	padding: 1rem;
	margin: 0 0 1rem 0;
}

.stashes {
	display: grid;
	grid-template-columns: 1fr 1fr;
	grid-auto-rows: 15fr;
	grid-gap: 1rem;
}

.heading {
	color: var(--primary);
}

.stashes-add {
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	background: var(--shadow-hint);

	.icon {
		width: 1.5rem;
		height: 1.5rem;
		fill: var(--shadow-weak);
	}

	&:hover {
		background: var(--shadow-weak);
		cursor: pointer;

		.icon {
			fill: var(--shadow);
		}
	}
}

@media(max-width: $breakpoint-kilo) {
	.stashes {
		grid-template-columns: 1fr;
	}
}
</style>
