<script setup>
	import { onMounted, ref, defineExpose } from 'vue'
	const emit = defineEmits(['update'])

	const categories = ref([])

	const loadData = async () => {
		const response = await fetch(import.meta.env.VITE_PRACTICE_TRACKER_API_URL+'summary')
		categories.value = await response.json()
	}

	onMounted(loadData)

	const setWorkingCategory = (category) => {
		emit('update', category.category_id)
	}

	defineExpose({loadData})
</script>

<template>
	<h2 @click="loadData">Summary</h2>
	<table>
		<thead>
			<tr>
				<th>category id</th>
				<th>category</th>
				<th>chunks practiced</th>
				<th>instrument</th>
				<th>weight</th>
			</tr>
		</thead>
		<tbody>
			<tr v-for="category in categories" @click="setWorkingCategory(category)">
				<td>{{category.category_id}}</td>
				<td>{{category.category}}</td>
				<td>{{category.chunks_practiced}}</td>
				<td>{{category.instrument}}</td>
				<td>{{category.weight}}</td>
			</tr>
		</tbody>
	</table>
</template>
