<template>
	<view class="main">
		<!-- nav -->
		<view class="nav-head">
			<view class="title">{{ i18n.HOME }}</view>
			<u-button class="button" color="#323045">{{ i18n.customerService }}</u-button>
		</view>
		<!-- 轮播图 -->
		<u-swiper class="swiper" :list="swiperList" previousMargin="48rpx" nextMargin="80rpx" circular :autoplay="true"
			radius="32rpx" bgColor="rgba(0,0,0,0)" height="360rpx"></u-swiper>
		<!-- 消息 -->
		<view class="message" @click="popupShow = true">
			<u-icon class="icon" name="../../static/home/ic_proclamation.png" size="60rpx" width="60rpx"></u-icon>
			<view class="text">{{ i18n.homeMessage }}</view>
		</view>
		<!-- 三个按钮 -->
		<view class="btnDiv">
			<view class="item">
				<u--image class="img" :showLoading="true" src="../../static/home/img_recharge.png" width="144rpx"
					height="144rpx"></u--image>
				<view class="text">{{ i18n.Recharge }}</view>
			</view>
			<view class="item">
				<u--image class="img" :showLoading="true" src="../../static/home/img_Mine.png" width="144rpx"
					height="144rpx"></u--image>
				<view class="text">{{ i18n.Mine }}</view>
			</view>
			<view class="item">
				<u--image class="img" :showLoading="true" src="../../static/home/img_Subcription.png" width="144rpx"
					height="144rpx"></u--image>
				<view class="text">{{ i18n.Subcription }}</view>
			</view>
		</view>
		<!-- 步数 -->
		<view class="steps">
			<view class="content">
				<view class="title">{{ i18n.TodaySteps }}</view>
				<canvas id="chart" ref="chart" style="width: 280rpx;height:280rpx; font-size: 40rpx;"></canvas>
				<u-button class="button" color="#00E8A2">{{ i18n.ReceiveEarnings }}</u-button>
			</view>
		</view>
		<!-- 播放器 -->
		<view class="videoList" v-for="(item, index) in videoList" :key="index">
			<video class="video" :id="`video-${index}`" :enable-progress-gesture="false" :src="item.url"
				:poster="item.poster" :title="item.title" controls @play="stopOtherVideoPlay" :key="index"></video>
		</view>
		<!-- 通知弹出层 -->
		<u-popup class="popup" :show="popupShow" round="32rpx" mode="center" @close="popupShow = false"
			@open="popupShow = true" bgColor="#211F32" :customStyle="{
				margin: 'auto 102rpx auto 118rpx',
			}">
			<view class="content">
				<scroll-view scroll-y="true" style="height: 288rpx;" scroll-with-animation="true" @touchmove.stop>
					{{ i18n.homeMessage }}
				</scroll-view>
				<u-button class="button" color="#00E8A2" @click="popupShow = false">OK</u-button>
			</view>
		</u-popup>
		<!-- tabBar -->
		<tab-bar :selectActive="1"></tab-bar>
	</view>
</template>

<script>
// 引入echarts
import * as echarts from 'echarts';

let videoPlayerObj = {};
export default {
	name: 'home',
	data() {
		return {
			popupShow: false,
			swiperList: [
				'https://cdn.uviewui.com/uview/swiper/swiper1.png',
				'https://cdn.uviewui.com/uview/swiper/swiper2.png',
				'https://cdn.uviewui.com/uview/swiper/swiper3.png',
			],
			videoList: [
				{
					url: 'https://cdn.uviewui.com/uview/resources/video.mp4',
					title: '昨夜星辰昨夜风，画楼西畔桂堂东',
					poster: 'https://cdn.uviewui.com/uview/swiper/swiper1.png'
				},
				{
					url: 'https://cdn.uviewui.com/uview/resources/video.mp4',
					title: '昨夜星辰昨夜风，画楼西畔桂堂东',
					poster: 'https://cdn.uviewui.com/uview/swiper/swiper1.png'
				},
				{
					url: 'https://cdn.uviewui.com/uview/resources/video.mp4',
					title: '昨夜星辰昨夜风，画楼西畔桂堂东',
					poster: 'https://cdn.uviewui.com/uview/swiper/swiper1.png'
				},
				{
					url: 'https://cdn.uviewui.com/uview/resources/video.mp4',
					title: '昨夜星辰昨夜风，画楼西畔桂堂东',
					poster: 'https://cdn.uviewui.com/uview/swiper/swiper1.png'
				},
			],
			option: null,
			stepsNum: 5000, // 当前步数
			stepTarget: 10000,// 目标步数
			myChart: null,
		}
	},
	onLoad() {


	},
	onShow() {
		// 生成播放器队列对象
		this.videoList.forEach((item, index) => {
			const VideoPlayer = uni.createVideoContext(`video-${index}`, this)
			videoPlayerObj[`video-${index}`] = VideoPlayer
		});

		this.$nextTick(() => {
			this.initChart()
		})

	},
	onHide() {
		this.stopAllVideoPlay()
	},
	computed: {
		i18n() {
			return this.$t('home')
		}
	},
	methods: {
		/**
		 *  在播放视频是暂停其他视频
		 * @param {*} e 
		 */
		stopOtherVideoPlay(e) {
			const keyArr = Object.keys(videoPlayerObj)
			keyArr.forEach(item => {
				if (item !== e.target.id) {
					videoPlayerObj[item].pause()
				}
			});
		},
		/**
		 *  在跳转其他页面时是暂停全部视频
		 * @param {*} e 
		 */
		stopAllVideoPlay() {
			const keyArr = Object.keys(videoPlayerObj)
			keyArr.forEach(item => {
				videoPlayerObj[item].pause()
			});
		},

		initChart() {
			// let myChart = echarts.init(this.$refs.chart);
			// console.log(document);
			// console.log(this.$refs.chart);
			this.myChart = echarts.init(document.getElementById('chart'));
			// this.myChart = echarts.init(this.$refs.chart);

			this.option = {
				title: {
					text: this.stepsNum,
					// 副标题
					subtext: "",
					// 主副标题间距
					itemGap: 0,
					left: 'center',
					top: 'center',
					// 主标题样式
					textStyle: {
						fontSize: '20',
						color: '#fff'
					},
				},
				series: [{
					name: '环形饼图',
					type: 'pie',
					radius: ['48%', '70%'],
					avoidLabelOverlap: false,
					label: {
						show: false,
						position: 'center'
					},
					itemStyle: {
						// 设置扇形的阴影
						shadowBlur: 30,
						shadowColor: '#15141F',
						shadowOffsetX: 'center',
						shadowOffsetY: 'center'

					},
					data: [{
						value: +this.stepsNum / this.stepTarget
						,
						name: '步数',
						itemStyle: {
							color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
								offset: .2,
								color: '#47BDFF'
							}, {
								offset: .5,
								color: '#34AEFF'
							}, {
								offset: 1,
								color: '#0589FF'
							}
							]),
							borderRadius: 800,


						}
					},
					{
						value: (this.stepTarget - this.stepsNum) / this.stepTarget,
						name: '剩余步数',
						itemStyle: {
							color: '#15141F',

						}
					}
					]
				}]
			}

			this.myChart.setOption(this.option, true);

			window.addEventListener('resize', () => {
				this.myChart.resize();
			});
		}
	}
}

</script>

<style lang="scss" scoped>
.main {
	padding-bottom: 198rpx; // 避免底部TabBar盖住内容

	.nav-head {
		overflow: hidden;
		position: fixed;
		top: 0rpx;
		height: 112rpx;
		font-size: 36rpx;
		color: #fff;
		padding-top: 88rpx;
		display: flex;
		align-items: center;
		width: 100%;
		background-color: #15141F;
		z-index: 8;
		border-bottom: 4rpx solid #323045;

		.title {
			margin-left: 48rpx;
		}

		.button {
			width: 286rpx;
			height: 70rpx;
			line-height: 70rpx;
			border-radius: 90rpx;
			font-size: 28rpx;
			color: #F2FE8D !important;
			padding: 0;
			margin-right: 24rpx;
		}
	}

	.swiper {
		margin-top: 236rpx;
	}

	.message {
		position: relative;
		margin: 24rpx 24rpx 32rpx;
		height: 96rpx;
		background: #211F32;
		border-radius: 16rpx;

		.icon {
			position: absolute;
			top: 18rpx;
			left: 20rpx;
		}

		.text {
			position: absolute;
			top: 18rpx;
			left: 92rpx;
			height: 68rpx;
			line-height: 32rpx;
			width: 574rpx;
			font-size: 24rpx;
			// 超出字符省略成...
			overflow: hidden;
			text-overflow: ellipsis;
			word-wrap: break-word;
			display: -webkit-box; //兼容
			white-space: normal !important;
			-webkit-line-clamp: 2; //显示行数
			-webkit-box-orient: vertical;
		}
	}

	.btnDiv {
		margin: 32rpx 48rpx;

		.item {
			display: inline-block;
			width: 218rpx;
			text-align: center;

			.img {
				margin: 0 36rpx 16rpx;
			}
		}
	}

	.steps {
		margin: 0 32rpx;
		width: 686rpx;
		height: 472rpx;
		background-color: #211F32;
		background-image: url(../../static/home/img_People.png);
		background-repeat: no-repeat;
		background-size: contain;
		border-radius: 16px;

		.content {
			margin-left: 246rpx;
			height: 100%;
			display: flex;
			justify-content: space-evenly;
			align-items: center;

			flex-direction: column;

			.title {
				font-size: 48rpx;
				height: 64rpx;
				line-height: 64rpx;
			}

			.chart {
				width: 180rpx;
				height: 180rpx;
			}

			.button {
				width: 386rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 40rpx;
				font-size: 24rpx;
				color: #15141F !important;
				padding: 0;
				margin-right: 24rpx;
			}
		}
	}

	.videoList {
		z-index: 1;

		.video {
			margin: 32rpx 32rpx 0;
			width: 686rpx;
			height: 360rpx;
			border-radius: 32rpx;
		}
	}


	.popup {

		.content {
			padding: 60rpx 48rpx;

			&::before {
				display: block;
				position: absolute;
				content: '';
				background-image: url(../../static/home/img_Speaker.png);
				background-repeat: no-repeat;
				background-size: contain;
				width: 112rpx;
				height: 112rpx;
				top: -32rpx;
				left: -16rpx;
			}

			.button {
				width: 386rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 40rpx;
				font-size: 24rpx;
				color: #15141F !important;
				padding: 0;
				margin: 20rpx auto 0;
			}
		}



	}

}
</style>
