<template>
	<div>
		<div v-if="!loading">
			<FloatingNavbar logo-href="/admin" main-nav-class="admin-nav-main">
				<template #left>
					<router-link v-if="adminRole !== 3" :to="{ name: 'Admin Home' }" tag="li" exact>
						<a href="#"><b>Undian</b></a>
					</router-link>

					<li class="dropdown" v-if="adminRole === 1">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
							aria-expanded="false">
							<b>Pengurusan</b> <span class="caret"></span>
						</a>
						<ul class="dropdown-menu">
							<router-link :to="{ name: 'Kemaskini Sesi' }" tag="li">
								<a href="#">Sesi</a>
							</router-link>
							<router-link :to="{ name: 'Maklumat Calon' }" tag="li">
								<a href="#">Calon</a>
							</router-link>
							<router-link :to="{ name: 'Manage Voter' }" tag="li" exact>
								<a href="#">Anggota Koperasi</a>
							</router-link>
						</ul>
					</li>

					<li class="dropdown" v-if="adminRole === 1 || adminRole === 3">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
							aria-expanded="false">
							<b>Operasi</b> <span class="caret"></span>
						</a>
						<ul class="dropdown-menu">
							<router-link :to="{ name: 'Kehadiran Calon' }" tag="li">
								<a href="#">Kehadiran</a>
							</router-link>
						</ul>
					</li>

					<li class="dropdown" v-if="adminRole === 1">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
							aria-expanded="false">
							<b>Kewangan</b> <span class="caret"></span>
						</a>
						<ul class="dropdown-menu">
							<router-link :to="{ name: 'Ringkasan Elaun' }" tag="li">
								<a href="#">Ringkasan Elaun</a>
							</router-link>
							<router-link :to="{ name: 'Pembiayaan' }" tag="li">
								<a href="#">Pembiayaan Anggota</a>
							</router-link>
							<router-link :to="{ name: 'Penyata Anggota' }" tag="li">
								<a href="#">Penyata Anggota</a>
							</router-link>
						</ul>
					</li>

					<li class="dropdown" v-if="adminRole === 1">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
							aria-expanded="false">
							<b>Sistem</b> <span class="caret"></span>
						</a>
						<ul class="dropdown-menu">
							<router-link :to="{ name: 'Activity Log' }" tag="li">
								<a href="#">Log Aktiviti</a>
							</router-link>
							<router-link :to="{ name: 'Tetapan Portal' }" tag="li">
								<a href="#">Tetapan Portal</a>
							</router-link>
						</ul>
					</li>
				</template>

				<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">
							<router-link v-if="Number(data.user.role) !== 3" :to="{ name: 'Update Account' }" tag="li"
								exact>
								<a href="#">Kemaskini Akaun</a>
							</router-link>

							<li v-if="isImpersonating()" @click="leaveImpersonation()">
								<a>Kembali Akaun Asal</a>
							</li>

							<router-link :to="{ name: 'Manage Account' }" tag="li" v-if="data.user.id == 1">
								<a href="#">Pengurusan Akaun</a>
							</router-link>

							<li @click="isImpersonating() ? null : logout()" :class="{ disabled: isImpersonating() }"
								:style="isImpersonating() ? 'opacity:0.5; cursor:not-allowed; pointer-events:none;' : ''">
								<a>Log Keluar</a>
							</li>
						</ul>
					</li>
				</template>
			</FloatingNavbar>

			<div class="admin-main">
				<router-view></router-view>
			</div>
		</div>
		<div v-if="loading" 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,
	// 	users:{'roles' : 1},
	// }),

	created: function () {
		this.util.setTitle('Voting System - Administrator');
		var vm = this;
		if (this.util.isLogin()) {
			this.util.setAuthorization();
			axios.get(config.API + 'admin/information')
				.then(response => {
					console.log(response);
					localStorage.setItem('admin_session', '1');
					vm.data.user = response.data.user;
					vm.data.election = response.data.election;
					vm.data.partylists = response.data.partylist;
					vm.data.positions = response.data.position;
					if (response.data.user && response.data.user.role !== undefined && response.data.user.role !== null) {
						localStorage.setItem('admin_role', String(response.data.user.role));
					}
					if (Number(response.data.user.role) === 3 && vm.$route.name !== 'Kehadiran Calon') {
						vm.$router.replace({ name: 'Kehadiran Calon' });
					}
					vm.loading = false;
				})
				.catch(error => {
					$.notifyClose();
					if (this.util.showResult(error) == 401) {
						vm.logout();
					}
				})
		} else {
			this.$router.push({ name: 'Admin Login' });
		}
	},

	computed: {
		adminRole: function () {
			// `data` is populated asynchronously; keep this defensive.
			try {
				return Number(this.data && this.data.user ? this.data.user.role : null);
			} catch (e) {
				return NaN;
			}
		}
	},

	methods: {
		isImpersonating: function () {
			try {
				return localStorage.getItem('is_impersonating') === '1';
			} catch (e) {
				return false;
			}
		},

		leaveImpersonation: function () {
			var vm = this;
			vm.util.notify('Leaving impersonation', 'loading');
			axios.post(config.API + 'admin/impersonate/leave')
				.then(function (response) {
					$.notifyClose();
					if (!response || !response.data || response.data.status !== 'success') {
						vm.util.showResult(response, 'error');
						return;
					}
					if (response.data.token) {
						localStorage['Access Token'] = 'Bearer ' + response.data.token;
						if (vm.util && vm.util.setAuthorization) vm.util.setAuthorization();
					}
					localStorage.removeItem('is_impersonating');
					if (response.data.user) {
						vm.data.user = response.data.user;
						if (response.data.user.role !== undefined && response.data.user.role !== null) {
							localStorage.setItem('admin_role', String(response.data.user.role));
						}
					}
					vm.util.notify('Berjaya kembali', 'success');
					if (vm.$router) vm.$router.go(0);
				})
				.catch(function (error) {
					$.notifyClose();
					vm.util.showResult(error, 'error');
				});
		},

		logout: function () {
			localStorage.removeItem('admin_role');
			localStorage.removeItem('is_impersonating');
			localStorage.clear();
			this.$router.push({ name: 'Admin Login' });
		}
	}
}
</script>

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

@media (max-width: 767px) {
	.admin-main {
		padding-top: 102px;
	}
}
</style>