<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.id">
					<TopItem :xml-url="top.xmlURL" :img-url="top.imgURL" :author="top.author" />
				</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 top = await axios.get(generateUrl('/apps/repod/top'))
			this.tops = top.data.data
		} catch (e) {
			console.error(e)
			showError(t('Could not fetch tops'))
		} finally {
			this.loading = false
		}
	},
}
</script>

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

	.tops li {
		flex-basis: 15%;
	}

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