<script setup>
	import { formatSeconds } from '../libs/time.js'
	import CategoryChooser from './category_chooser.vue'
	import { onMounted, ref, watch, defineProps } from 'vue'
	import { currentWorkingCategory, currentWorkingInstrument } from '../libs/state.js'

	const props = defineProps(['categories', 'instruments'])

	const categorySessions = ref([])

	const copy = (event) => {
		navigator.clipboard.writeText(event.target.innerText)
	}

	const loadCategory = async () => {
		const response = await fetch(import.meta.env.VITE_PRACTICE_TRACKER_API_URL+'show-category/'+currentWorkingCategory.value.id)
		categorySessions.value = await response.json()
	}

	onMounted(loadCategory)

	watch(currentWorkingCategory, loadCategory)
	defineExpose({loadCategory})
</script>

<template>
	<h2>Show Category</h2>
	<CategoryChooser
		:instruments="props.instruments"
		:categories="props.categories" />
	<table>
		<thead>
			<tr>
				<th>id</th>
				<th>description</th>
				<th>seconds</th>
				<th>comments</th>
				<th>created at</th>
				<th>category id</th>
				<th>category</th>
				<th>instrument</th>
			</tr>
		</thead>
		<tbody>
			<tr v-for="session in categorySessions">
				<td>{{session.id}}</td>
				<td @click="copy">{{session.description}}</td>
				<td @click="copy">{{formatSeconds(session.seconds)}}</td>
				<td @click="copy">{{session.comments}}</td>
				<td>{{session.created_at}}</td>
				<td>{{session.category_id}}</td>
				<td>{{session.category}}</td>
				<td>{{session.instrument}}</td>
			</tr>
		</tbody>
	</table>
</template>
