<template>
	<view class="main">
		<!-- nav -->
		<view class="nav-head">
			<u-button class="serviceButton" color="#323045">{{ i18n.customerService }}</u-button>
			<view class="languageBtn" @click.stop="languageShow = true">
				{{ langTrue }}
			</view>
			<!-- popups弹窗 -->
			<u-overlay :show="languageShow" @click="languageShow = false" :opacity="0">
				<language-popups v-model="languageShow" :popData="languageData" @tapPopup="tapPopup">
				</language-popups>
			</u-overlay>

		</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" @click="goto('recharge')">
				<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" @click="goto('mine')">
				<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" @click="goto('subcription')">
				<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>
				<view id="chart" :stepsNum="stepsNum" :stepTarget="stepTarget"
					:change:stepTarget="echarts.receive" :change:stepsNum="echarts.receive"
					style="width: 280rpx;height:280rpx; font-size: 40rpx;"></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" @click="playVideo(item)">
			<view class="videoImage" :style="`background-image: url(${item.poster});background-repeat: no-repeat;
			background-size: cover;`">
				<view class="VideoBg">
					<view class="VideoLogo"></view>
					<view class="VideoTitle">{{ item.title }}</view>
				</view>
			</view>
		</view>
		<video class="video" v-show="videoShow" id="video" :enable-progress-gesture="false" autoplay="true"
			:src="videoUrl" controls x5-video-player-type="h5-page" @fullscreenchange="ifStopVideo"></video>
		<!-- 通知弹出层 -->
		<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>
	import languagePopups from '@/components/language-popups/language-popups.vue'
	import languageData from '@/utils/language/config'
	import api from '@/utils/api'
	// 引入echarts
	// import * as echarts from 'echarts';

	// let videoPlayerObj = {};
	let videoPlayer;
	let myChart;
	export default {
		name: 'home',
		components: {
			languagePopups
		},
		data() {
			return {
				languageShow: false,
				langTrue: languageData[0].title || 'English',
				languageData,
				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',
				],
				videoShow: false,
				videoUrl: '',
				videoList: [{
						url: 'https://cdn.uviewui.com/uview/resources/video.mp4',
						title: 'Video name 1',
						poster: 'https://cdn.uviewui.com/uview/swiper/swiper1.png'
					},
					{
						url: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
						title: 'Video name 2',
						poster: 'https://cdn.uviewui.com/uview/swiper/swiper2.png'
					},
					{
						url: 'http://vjs.zencdn.net/v/oceans.mp4',
						title: 'Video name 3',
						poster: 'https://cdn.uviewui.com/uview/swiper/swiper3.png'
					},
					{
						url: 'http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4',
						title: 'Video name 4',
						poster: 'https://cdn.uviewui.com/uview/swiper/swiper2.png'
					},
				],
				option: null,
				stepsNum: 5000, // 当前步数
				stepTarget: 10000, // 目标步数
				myChart: null,
			}
		},
		onLoad() {
			this.stepsNum = 2000

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

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

			if (uni.getStorageSync('langTrue')) {
				this.langTrue = uni.getStorageSync('langTrue')
			}

		},
		onHide() {
			this.stopAllVideoPlay()
		},
		computed: {
			i18n() {
				return this.$t('home')
			}
		},
		methods: {
			getBanner(){
				const returnData=api.noticeList({'mark':'APP_INDEX'});
			},
			// 弹出语言选择框
			laug() {
				this.laugValue = true;
			},
			// 切换语言
			tapPopup(e) {
				this._i18n.locale = e.language
				this.$store.commit('setLanguage', e.language)
				this.langTrue = e.title
				uni.setStorageSync('langTrue', e.title)
				uni.redirectTo({
					url: '/pages/home/index'
				})
			},
			/**
			 *  在跳转其他页面时是暂停全部视频
			 * @param {*} e 
			 */
			stopAllVideoPlay() {
				videoPlayer.pause()
			},
			/**
			 * 根据全屏状态与暂停视频
			 * @param {*} e 
			 */
			ifStopVideo(e) {
				if (e.detail.fullScreen) {
					videoPlayer.play()
				} else {
					this.videoShow = false
					videoPlayer.pause()
				}
			},
			/**
			 * 播放视频
			 * @param {*} data 
			 */
			playVideo(data) {
				this.videoUrl = data.url;
				this.videoShow = true;
				videoPlayer.requestFullScreen({
					direction: 0
				})
			},
			/**
			 * 跳转
			 */
			goto(id) {
				// console.log(id);
				let url = '';
				switch (id) {
					case 'recharge':
						url = '/pages/recharge/recharge'
						break;
					case 'mine':
						url = '/pages/mine/mine'
						break;
					case 'subcription':
						url = '/pages/subscription/index';
						break;
					default:
						break;
				}
				if (url) {
					this.stopAllVideoPlay()
					// console.log(url);
					if (id === 'subcription') {
						uni.reLaunch({
							url,
						})
						return;
					}
					uni.navigateTo({
						url,
					});
				}

			}
		}
	}
</script>

<script module="echarts" lang="renderjs">
	let myChart
	export default {

		mounted() {
			console.log(0);
			console.log(window);
			console.log(typeof window.echarts);
			// this.initEcharts();
			if (typeof window.echarts === 'object') {
				this.initEcharts()
			} else if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				console.log(1);
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script');
				console.log(2);
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = './static/echarts.js';
				console.log(3);
				script.onload = this.initEcharts.bind(this);
				console.log(4);
				document.head.appendChild(script);
				console.log(5);
			}
		},

		methods: {

			// 接收逻辑层发送的数据
			receive(newValue, oldValue, ownerVm, vm) {
				console.log('msg变化了newValue', newValue)
				console.log('msg变化了oldValue', oldValue)
				
			},
			
			initEcharts() {
				console.log(31);
				// let t1=null;
				this.initChart()
				console.log(32);
				myChart = echarts.init(document.getElementById('chart'))
				console.log(33);
				// 观测更新的数据在 view 层可以直接访问到
				myChart.setOption(this.option)
				console.log(34);
				// clearInterval(t1)
				// t1= setInterval(() => {
				// 	console.log(1);
				// 	myChart.setOption(this.option)
				// }, 1000);
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				myChart.setOption(newValue)
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			},
			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);

				// myChart = echarts.init(document.getElementById('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',

								}
							}
						]
					}]
				}

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

				// window.addEventListener('resize', () => {
				// 	myChart.resize();
				// });
			}
		},
		watch: {
			// 如果 `question` 发生改变，这个函数就会运行
			columData: function(newQuestion, oldQuestion) {

				// this.updateEcharts(this.option)
			}
		},
	}
</script>

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

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


			.serviceButton {
				display: inline-block;
				width: 286rpx;
				height: 70rpx;
				line-height: 70rpx;
				border-radius: 90rpx;
				font-size: 28rpx;
				color: #F2FE8D !important;
				padding: 0;
				margin: 20rpx 180rpx 0 48rpx;
				vertical-align: top;
			}

			.languageBtn {
				display: inline-block;
				box-sizing: border-box;
				width: 180rpx;
				position: relative;
				margin: 38rpx 48rpx 0 0;
				font-size: 24rpx;
				height: 36rpx;
				line-height: 36rpx;
				text-align: center;
				padding-left: 12rpx;

				&::before {
					display: block;
					position: absolute;
					content: '';
					background-image: url(../../static/home/map.png);
					background-repeat: no-repeat;
					background-size: 36rpx;
					width: 36rpx;
					height: 36rpx;
					top: 0rpx;
					left: 0rpx;
				}

				&::after {
					display: block;
					position: absolute;
					content: '';
					background-image: url(../../static/home/pull-down.png);
					background-repeat: no-repeat;
					background-size: 24rpx;
					width: 24rpx;
					height: 24rpx;
					top: 12rpx;
					right: 0rpx;
				}

			}
		}



		.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 {

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

				.VideoBg {
					box-sizing: border-box;
					padding-top: 96rpx;
					width: 100%;
					height: 100%;
					background-color: rgba(0, 0, 0, 0.6) !important;

					.VideoLogo {
						width: 96rpx;
						height: 96rpx;
						margin: 0 auto;
						background-image: url(../../static/home/ic_play.png);
						background-repeat: no-repeat;
						background-size: 96rpx;
					}

					.VideoTitle {
						font-size: 48rpx;
						text-align: center;
					}
				}
			}
		}

		.video {
			// position: fixed;
			top: 0;
			z-index: 9;
		}


		.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>
