<template>
	<view class="home">
		<!-- 校园 -->
		<campus ref="campus" :scroll="scrollTop" :active="index" :campusRandom="options.campusRandom" v-show="index === 0"></campus>
		<!-- 小队 -->
		<team ref="team" :scroll="scrollTop" :active="index" :teamRandom="options.teamRandom" v-show="index === 1"></team>
		<!-- 我 -->
		<me ref="me" :scroll="scrollTop" :active="index" :meRandom="options.meRandom" v-show="index === 2"></me>
		<!-- tabs -->
		<view class="tabs" :style="'padding-bottom:' + bottom + 'px;'">
			<view class="tabs_white" :class="{tabs_block: index !== 2}"></view>
			<view class="item" v-for="(item, key) in list" :key="key" :class="{active: index === key}" @click="change(item, key)">
				<view class="ico">
					<template v-if="item.spot && options.spot !== '0'">
						<view class="spot"></view>
					</template>	
					<image class="pic" mode="aspectFill" :src="index === key ? item.icoActive : item.ico"></image>
				</view>
				<view class="txt">{{ item.name }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import mixins from '@/utils/mixins'
	import Campus from '@/wxcomponents/tabBar/Campus.vue'
	import Team from '@/wxcomponents/tabBar/Team.vue'
	import Me from '@/wxcomponents/tabBar/Me.vue'
	import imgDict from '@/utils/img'
	
	export default {
		components: {
			Campus, // 0校园
			Team, // 1小队
			Me // 2我
		},
		mixins: [mixins],
		data() {
			return {
				scrollTop: 0,
				index: 0,
				options: {
					imgDict,
					campusRandom: 0,
					teamRandom: 0,
					meRandom: 0,
					spot: '0' , // 用于判断页面底部“我”是否展示红点提示，前端每1分钟调用一次 data为1时页面底部“我”展示红点提示，data为0时目前页面状态不变
					spotTimer: null
				},
				list: [
					{ name: this.$t('tabs.campus'), ico: imgDict.campus, icoActive: imgDict.campus_active, spot: false },
					{ name: this.$t('tabs.team'), ico: imgDict.team, icoActive: imgDict.team_active, spot: false },
					{ name: this.$t('tabs.me'), ico: imgDict.me, icoActive: imgDict.me_active, spot: true }
				]
			}
		},
		onReady() {
			this.setNavigationBarColor()
		},
		onLoad() {
			this.bottom = this.System.safeAreaInsets.bottom
			if (uni.getStorageSync('index')) {
				this.index = uni.getStorageSync('index')
			} else {
				uni.setStorageSync('index', this.index)
			}
			uni.$on('home', data => {
				this.index = uni.setStorageSync('index', data)
				this.index = data
				this.hasNewReminder()
				this.setSpot()
			})
			// 校园
			uni.$on('campus', data => {
				this.options.campusRandom = Math.random()
				this.hasNewReminder()
				this.setSpot()
			})
			// 小队
			uni.$on('team', data => {
				this.options.teamRandom = Math.random()
				this.hasNewReminder()
				this.setSpot()
			})
			// 我的
			uni.$on('me', data => {
				this.options.meRandom = Math.random()
				this.hasNewReminder()
				this.setSpot()
			})
		},
		onUnload() {
			// 移除监听事件
			uni.$off('home')
			uni.$off('campus')
			uni.$off('team')
			uni.$off('me')
		},
		onHide () {
			this.clearTimer()
		},
		onPageScroll (e) {
			this.scrollTop = e.scrollTop
		},
		methods: {
			// 切换底部导航
			change (item, key) {
				this.index = key
				uni.setStorageSync('index', key)
				this.setNavigationBarColor()
				this.hasNewReminder()
				this.setSpot()
			},
			// 设置状态条
			setNavigationBarColor () {
				uni.setNavigationBarColor({
					frontColor: this.index !== 2 ? '#ffffff' : '#000000',
					backgroundColor: this.index !== 2 ? '#000000' : '#F6F6F6',
				})
			},
			// 设置底部me红点
			setSpot () {
				this.clearTimer()
				this.options.spotTimer = setInterval(() => {
					this.hasNewReminder()
				}, 5000)
			},
			// 清除定时器
			clearTimer () {
				clearInterval(this.options.spotTimer)
				this.options.spotTimer = null
			},
			// 查询用户是否有新提醒
			async hasNewReminder () {
				this.options.spot = '0'
				const result = await this.$axios.get(this.API.hasNewReminder)
				if (result) {
					this.options.spot = result
				}
			},
		},
		// 分享
		onShareAppMessage (e) {
			let title = '同学们都在这里抱团，等你加入！'
			let path = '/pages/index/index'
			// 菜单分享
			if (e.from === 'menu') {
				const index = uni.getStorageSync('index')
				const userInfo = uni.getStorageSync('userInfo')
				if (index === 0) {
					title = `在这里，和校友一起分享互助`
				}
				if (index === 2) {
					title = `${userInfo.nickname}的带飞主页`
					path = `/pages/share/he?uid=${userInfo.uid}`
				}
			}
			// 按钮分享
			if (e.from === 'button') {
				const team = uni.getStorageSync('team')
				title = `邀你加入「${team.teamName}」抱团一起飞！`
				path = `/pages/share/team?id=${team.id}`
			}
			return {
				title: title, // 分享标题
				path: path, // 页面 path
				imageUrl: 'https://daifeiclub.com/image/pics/team_share.png'
			}
		},
		onReachBottom (e) {
			// 个人主页
			if (this.index === 2) {
				uni.$emit('getReceivedGiftsFresh')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.home {
		box-sizing: border-box;
		min-height: calc(100vh);
		.tabs {
			overflow: hidden;
			position: fixed;
			z-index: 98;
			width: 100%;
			height: 60px;
			bottom: 0;
			align-items: center;
			display: flex;
			
			.tabs_white {
				position: absolute;
				top: 0;
				right: 0;
				left: 0;
				bottom: 0;
				z-index: 2;
				width: 100%;
				height: 60px;
				padding-bottom: 34px;
				background-color: rgba($color: $white, $alpha: 0.85);
				// -webkit-tap-highlight-color: transparent;
				// -wekit-filter: blur(10rpx);
				// transform: scale(1.2);
				// filter: blur(10rpx);
				&.tabs_block {
					background-color: rgba($color: $bg-0, $alpha: 0.85);
				}
			}
			
			.item {
				position: relative;
				z-index: 2;
				width: 100%;
				&.active {
					.txt {
						color: $mc;
					}
				}
				.ico {
					position: relative;
					margin: 0 auto;
					height: 44rpx;
					width: 44rpx;
				
					.spot {
						position: absolute;
						top: 0;
						right: -8rpx;
						width: 16rpx;
						height: 16rpx;
						background-color: $FF0000;
						border-radius: 50%;
					}
				}
				.txt {
					margin-top: 10rpx;
					text-align: center;
					font-size: 24rpx;
					font-weight: 400;
					line-height: 1;
					color: $J49494;
				}
			}
		}
	}
</style>
