<template>
	<AppContent>
		<Loading v-if="loading" />
	</AppContent>
</template>

<script>
import AppContent from '../components/Atoms/AppContent.vue'
import Loading from '../components/Atoms/Loading.vue'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'

export default {
	name: 'Statistics',
	components: {
		AppContent,
		Loading,
	},
	data() {
		return {
			loading: true,
			since: 0,
			stats: [],
		}
	},
	mounted() {
		this.fetchData()
	},
	methods: {
		async fetchData() {
			try {
				this.loading = true
				const stats = []
				const episodes = await axios.get(generateUrl('/apps/gpoddersync/episode_action?since={since}', { since: this.since }))
				for (const action of episodes.data) {
					if (action.position > 0) {
						stats[action.podcast] += action.position
					}
				}
				this.stats = stats
			} catch (e) {
				console.error(e)
				showError(t('repod', 'Error while fetching the statistics'))
			} finally {
				this.loading = false
			}
		},
	},
}
</script>
