<template>
	<fragment>
		<p>
			<span>{{ t('Discover') }}</span>
		</p>
		<p>
			<NcLoadingIcon v-if="loading" />
			<ul v-if="!loading" class="tops">
				<li v-for="top in tops" :key="top.link">
					<TopItem :author="top.author"
						:image-url="top.imageUrl"
						:link="top.link"
						:title="top.title" />
				</li>
			</ul>
			<span class="caption">{{ t('Suggests by fyyd') }}</span>
		</p>
	</fragment>
</template>

<script>
import { NcLoadingIcon } from '@nextcloud/vue'
import TopItem from './TopItem.vue'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'

export default {
	name: 'TopList',
	components: {
		NcLoadingIcon,
		TopItem,
	},
	data() {
		return {
			tops: [],
			loading: true,
		}
	},
	async mounted() {
		try {
			this.loading = true
			const toplist = await axios.get(generateUrl('/apps/repod/toplist'))
			this.tops = toplist.data
		} catch (e) {
			console.error(e)
			showError(t('Could not fetch tops'))
		} finally {
			this.loading = false
		}
	},
}
</script>

<style scoped>
	.caption {
		float: right;
		font-size: small;
		margin: .5rem;
	}

	.tops {
		display: flex;
		flex-wrap: wrap;
		gap: 2rem 5%;
		justify-content: center;
	}

	.tops li {
		flex-basis: 15%;
	}
</style>
