<template>
	<div>
		<div v-if="!loading">
			<FloatingNavbar :logo-href="data.baseURL" main-nav-class="voter-nav-main">
				<template #right>
					<li class="dropdown">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
							aria-expanded="false">
							<span class="fa fa-user"></span> {{ data.user.name }} <span class="caret"></span>
						</a>
						<ul class="dropdown-menu">
							<li @click="logout()"><a>Log Keluar</a></li>
						</ul>
					</li>
				</template>
			</FloatingNavbar>

			<div class="voter-main">
				<div class="voter-breadcrumb-wrap">
					<div class="container-fluid">
						<ol class="breadcrumb voter-breadcrumb">
							<li v-for="(crumb, idx) in breadcrumbs" :key="idx" :class="{ active: crumb.active }">
								<router-link v-if="crumb.to" :to="crumb.to">{{ crumb.label }}</router-link>
								<span v-else>{{ crumb.label }}</span>
							</li>
						</ol>
					</div>
				</div>
				<router-view></router-view>
			</div>
		</div>
		<div v-else class="container">
			<div class="jumbotron">
				<h1>Loading <i class="fa fa-refresh fa-spin"></i></h1>
			</div>
		</div>
	</div>
</template>

<script>
import FloatingNavbar from '../../common/FloatingNavbar.vue';

export default {
	components: { FloatingNavbar },
	data: () => ({
		loading: true
	}),

	created: function () {
		this.refreshInfo();
	},

	computed: {
		breadcrumbs: function () {
			var routeName = this.$route.name;
			var labels = {
				'Voter Home': 'Laman Utama',
				'Voter Calon': 'Maklumat Calon',
				'Mat': 'MAT',
				'Vote': 'Undian',
				'Result': 'Keputusan',
				'Attendance': 'Daftar MAT',
				'Zoom': 'Pautan Mesyuarat',
				'penyata': 'Penyata Ahli'
			};
			var current = labels[routeName] || routeName || 'Halaman';
			if (routeName === 'Voter Home') {
				return [{ label: 'Laman Utama', to: null, active: true }];
			}
			return [
				{ label: 'Laman Utama', to: { name: 'Voter Home' }, active: false },
				{ label: current, to: null, active: true }
			];
		}
	},

	methods: {
		logout: function () {
			var vm = this;
			this.util.setAuthorization();
			axios.post(config.API + 'voter/logout')
				.catch(function () { })
				.finally(function () {
					localStorage.clear();
					vm.$router.push({ name: 'Voter Login' });
				});
		},

		hasVoted: function () {
			return this.data.result.length > 0;
		},

		refreshInfo: function () {
			var vm = this;
			this.util.setAuthorization();
			axios.get(config.API + 'election/information')
				.then(response => {
					console.log(response);
					vm.loading = false;
					vm.data.election = response.data.election;
					vm.data.nominees = response.data.nominee;
					vm.data.partylists = response.data.partylist;
					vm.data.positions = response.data.position;
					vm.data.user = response.data.voter;
					vm.data.result = response.data.result;
				})
				.catch(error => {
					vm.loading = false;
					if (vm.util.showResult(error, 'error') == 401) {
						vm.$router.push({ name: 'Voter Login' });
					}
				});
		}
	}
}
</script>



<style scoped>
.voter-main {
	padding-top: 112px;
}

.voter-breadcrumb-wrap {
	margin: 0 16px 8px;
	padding: 0;
	position: relative;
	z-index: 5;
}

.voter-breadcrumb-wrap .container-fluid {
	padding-left: 15px;
	padding-right: 15px;
}

.voter-breadcrumb {
	margin-bottom: 16px;
	padding: 14px 20px;
	border-radius: 14px;
	background: rgba(255, 255, 255, 0.92);
	border: 1px solid #e5ebf3;
	box-shadow: 0 8px 22px rgba(35, 64, 97, 0.1);
	font-size: 1.7rem;
	line-height: 1.45;
}

.voter-breadcrumb>li {
	padding-top: 2px;
	padding-bottom: 2px;
}

.voter-breadcrumb>li+li:before {
	color: #94a3b8;
	padding: 0 12px;
	font-size: 1.35rem;
	font-weight: 600;
}

.voter-breadcrumb>li>a {
	color: #2563eb;
	font-weight: 600;
}

.voter-breadcrumb>li>a:hover {
	color: #1d4ed8;
	text-decoration: underline;
}

.voter-breadcrumb>li.active {
	color: #17324d;
	font-weight: 700;
	font-size: 1.7rem;
}

@media (max-width: 767px) {
	.voter-main {
		padding-top: 102px;
	}

	.voter-breadcrumb-wrap {
		margin-left: 10px;
		margin-right: 10px;
	}

	.voter-breadcrumb {
		font-size: 1.4rem;
		line-height: 1.4;
		padding: 12px 16px;
		border-radius: 12px;
	}

	.voter-breadcrumb>li.active {
		font-size: 1.4rem;
	}

	.voter-breadcrumb>li+li:before {
		padding: 0 8px;
		font-size: 1.2rem;
	}
}
</style>
