<template>
	<Submenu :value="menu" />
	<div class="container-fluid" style="padding-top: 5rem;">
		<Navbar v-model:server="server" :user="user" />
		<Throbber :client="client" />
		<div class="container">
			<Login :secret="secret" v-model:client="client" v-model:server="server" v-model:user="user" />
			<router-view v-if="user" :client="client"></router-view>
		</div>
	</div>
</template>

<script>
	import Submenu from './Submenu.vue';
	import Navbar from './Navbar.vue';
	import Throbber from './Throbber.vue';
	import Login from './Login.vue';
	import RouteSchedule from './RouteSchedule.vue';
	import RouteLookup from './RouteLookup.vue';
	import RoutePatient from './RoutePatient.vue';
	import RoutePatientDetail from './RoutePatientDetail.vue';
	import RoutePatientVisits from './RoutePatientVisits.vue';
	import RoutePatientOrders from './RoutePatientOrders.vue';
	import RoutePatientReports from './RoutePatientReports.vue';
	import RoutePlanner from './RoutePlanner.vue';
	import RouteRecall from './RouteRecall.vue';
	import RouteInbox from './RouteInbox.vue';

	export default {
		components: {
			Submenu, Navbar, Throbber, Login
		},
		props: {
			secret: String
		},
		data() {
			return {
				client: null,
				server: null,
				user: null,
				heartbeat: null,
				banner: '',
				authenticated: false,
				menu: {
					name: 'Main',
					items: [
						{ name: 'Schedule', href: '/' },
						{ name: 'Lookup', href: '/lookup' },
						{ name: 'Planner', href: '/planner' },
						{ name: 'Recall', href: '/recall' },
						{ name: 'Inbox', href: '/inbox' },
					]
				}
			};
		},
		watch: {
			async client(value, oldvalue) {
				if(this.heartbeat) window.clearInterval(this.heartbeat);
				else {
					[
						{ path: '/', component: RouteSchedule },
						{ path: '/lookup', component: RouteLookup },
						{ path: '/patient/:id', component: RoutePatient, children: [
							{ path: '', component: RoutePatientDetail },
							{ path: 'visits', component: RoutePatientVisits },
							{ path: 'orders', component: RoutePatientOrders },
							{ path: 'reports', component: RoutePatientReports },
							{ path: 'reports/bloodbank', component: RoutePatientReports, props: { report_name: 'Blood Bank' } },
							{ path: 'reports/microbiology', component: RoutePatientReports, props: { report_name: 'Microbiology' } },
							{ path: 'reports/pathology', component: RoutePatientReports, props: { report_name: 'Pathology' } },
							{ path: 'reports/radiology', component: RoutePatientReports, props: { report_name: 'Radiology' } },
							{ path: 'reports/notes', component: RoutePatientReports, props: { report_name: 'Notes' } },
						] },
						{ path: '/planner', component: RoutePlanner },
						{ path: '/recall', component: RouteRecall },
						{ path: '/inbox', component: RouteInbox },
					].forEach(route => this.$root.$router.addRoute(route));
					await this.$root.$router.replace(this.$route);
				}
				if(value) this.heartbeat = await value.heartbeat();
			}
		}
	};
</script>
