<!-- 
[报名] 样式3 - 排名总览
http://localhost:5173/card/#/pages/bm/style3/rankOverview
https://oss-mbh5.colormaprun.com/card/#/pages/bm/style3/rankOverview
 -->
<template>
	<view class="body">
		<view v-if="pageReady" class="content uni-column">
			<view class="uni-column" :class="cssTop">
				<my-topbar :mcName="mcName" :class="cssTopbarColor"
					@btnBackClick="btnBack" @btnInfoClick="btnInfo"></my-topbar>
			</view>
			
			<view class="mid uni-column">
				<view class="mid-1 uni-row uni-jcsb">
					<text class="mid-1-name">{{nickName}}</text>
					<text>总场次：{{totalNum}}</text>
				</view>
				<view class="mid-2 uni-row uni-jcsa">
					<view class="uni-column">
						<text class="mid-2-value" style="color: #ff0045;">{{totalSysPoint}}</text>
						<text class="mid-2-text">百味豆(个)</text>
					</view>

					<view class="mid-line"></view>

					<view class="uni-column">
						<text class="mid-2-value">{{fmtDistanct(totalDistanct)}}</text>
						<text class="mid-2-text">总里程km</text>
					</view>

					<view class="mid-line"></view>
					<view class="uni-column">
						<text class="mid-2-value">{{totalCp}}</text>
						<text class="mid-2-text">打点数(个)</text>
					</view>

					<view class="mid-line"></view>

					<view class="uni-column">
						<text class="mid-2-value">{{fmtPace(fastPace)}}</text>
						<text class="mid-2-text">最快配速</text>
					</view>
				</view>
			</view>

			<view class="main uni-column">
				<text class="main-title">选择比赛路线</text>
				<my-pathList :style="pathListStyle.style" :pathList="pathList" :mcState="mcState" :showLine="pathListStyle.showLine"></my-pathList>
			</view>

			<my-popup ref="mypopup" :config="popupRuleConfig" :dataList="popupDataList" :acttime="acttime"></my-popup>
			<!-- <my-popup-map ref="mypopupmap" :point="navPoint"></my-popup-map> -->
		</view>
	</view>
</template>

<script>
	import tools from '/common/tools';
	import {
		defaultPopUpDataList,
		defaultPopUpDataList2
	} from '/common/define';
	import {
		token,
		apiMatchRsDetailQuery,
		apiCardConfigQuery,
		checkResCode
	} from '/common/api';

	export default {
		data() {
			return {
				pageReady: false,
				// audioSrc: "/static/audio/2.mp3",
				// audioSrc: "https://oss-mbh5.colormaprun.com/card/static/audio/2.mp3",
				// audioSrc: "http://t-oss-mbh5.colormaprun.com/card/static/audio/2.mp3",
				pageName: "rankOverview",
				firstEnterKey: 'firstEnter-bm-style3',
				rankKey: "rank-bm-style3",
				queryObj: {},
				queryString: "",
				token: "",

				ecId: 0, // 卡片id
				mcId: 0, // 赛事id
				mcType: 0, // 赛事类型 1 普通活动 2 线下赛 3 线上赛
				mcName: "", // 赛事名称
				acttime: "", // 活动时间
				beginSecond: null, // 活动或赛事开始时间戳，单位秒
				endSecond: null, // 活动或赛事结束时间戳，单位秒
				nickName: "", // 昵称
				totalNum: null, // 总场次
				totalDistanct: null, // 总距离,单位米
				totalDistanctRankNum: null, // 总距离排名
				totalCp: null, // 总打点数
				totalCpRankNum: null, // 总打点数排名
				totalSysPoint: null, // 总百味豆
				totalSysPointRankNum: null, // 总百味豆排名
				fastPace: null, // 个人最快配速
				fastPaceRankNum: null, // 个人最快配速排名
				ocaRs: [], // 卡片对应活动集合

				interval: null,
				mcState: 0, // 赛事/活动状态 0: 未开始  1: 进行中  2: 已结束
				cssTop: "",
				cssTopbarColor: "",
				pathList: {},
				pathListStyle: {},
				popupRuleConfig: {}, // 规则弹窗配置
				popupDataList: [],
				navPoint: {},
			}
		},
		computed: {
			pathListLen() {
				return Object.keys(this.pathList).length;
			}
		},
		onLoad(query) { // 类型非必填，可自动推导
			// console.log("query:", query);
			this.queryObj = query;
			this.queryString = tools.objectToQueryString(this.queryObj);
			// console.log(queryString);
			this.token = query["token"] ?? token;
			this.ecId = query["id"] ?? 0;

			this.firstEnterKey += "-" + this.ecId;
			console.log("firstEnterKey:", this.firstEnterKey);

			this.rankKey += "-" + this.ecId;
			console.log("rankKey:", this.rankKey);

			tools.removeCssCode();

			this.getCardConfigQuery();
		},
		// 页面初次渲染完成，此时组件已挂载完成，DOM 树($el)已可用
		onReady() {
			// this.dealFirstEnter();
			// this.playMusic();
		},
		onShow() {
			// uni.showToast({
			// 	icon: "none",
			// 	title: "[rankOverview] onShow"
			// })
			// if (this.$audio.audioStatus == 2 && this.$audio.audioSrc == this.audioSrc) {
			// 	this.$audio.play();
			// }
		},
		onUnload() {
			this.clear();
		},
		methods: {
			playMusic() {
				return;
				// uni.showToast({
				// 	title: 'playMusic',
				// 	icon: 'none',
				// 	duration: 2000
				// });
				// if (this.$audio.audioStatus == 0 || this.$audio.audioSrc != this.audioSrc) {
				// 	this.$audio.playAudio(this.audioSrc);
				// }
			},
			dealNotice(rank) {
				// console.log('[dealFirstEnter]');
				let that = this;
				uni.getStorage({
					key: that.rankKey,
					success: (res) => {
						console.log('[getStorage]', that.rankKey, res.data);
						const oldRank = res.data;
						if (oldRank != rank) {
							// that.notice = true;
							that.setRankValue(rank);
						}
					},
					fail: (e) => {
						console.log('[getStorage] fail', that.rankKey, e);
						// that.notice = false;
						that.setRankValue(rank);
					},
				})
			},
			setRankValue(data) {
				let that = this;
				uni.setStorage({
					key: that.rankKey,
					data: data,
					success: () => {
						console.log('[setStorage] success', that.rankKey, data);
					},
					fail: (e) => {
						console.log('[setStorage] fail', that.rankKey, e);
					},
				})
			},
			dealFirstEnter() {
				// console.log('[dealFirstEnter]');
				let that = this;
				uni.getStorage({
					key: that.firstEnterKey,
					success: (res) => {
						console.log('[getStorage]', that.firstEnterKey, res.data);
					},
					fail: (e) => {
						console.log('[getStorage] fail', that.firstEnterKey, e);
						that.btnInfo();
						that.setFirstEnterValue(true);
					},
				})
			},
			setFirstEnterValue(data) {
				let that = this;
				uni.setStorage({
					key: that.firstEnterKey,
					data: data,
					success: () => {
						console.log('[setStorage] success', that.firstEnterKey, data);
					},
					fail: (e) => {
						console.log('[setStorage] fail', that.firstEnterKey, e);
					},
				})
			},
			clear() {
				if (this.interval != null) {
					clearInterval(this.interval);
					this.interval = null;
				}
			},
			loadConfig(config) {
				// console.log("config", config);

				// 加载CSS样式
				const css = config.css;
				if (css != undefined && css.length > 0) {
					tools.loadCssCode(css);

					if (css.indexOf(".top{") >= 0) {
						this.cssTop = "top";
					}
					if (css.indexOf(".topbar-color{") >= 0) {
						this.cssTopbarColor = "topbar-color";
					}
				}

				if (this.cssTop == "") {
					this.cssTop = "top-default";
				}
				if (this.cssTopbarColor == "") {
					this.cssTopbarColor = "topbar-color-default";
				}
				console.log("[loadConfig] cssTop:", this.cssTop);
				console.log("[loadConfig] cssTopbarColor:", this.cssTopbarColor);

				// 加载比赛路线数据
				const pathList = config.pathList;
				// console.log("[loadConfig] pathList:", pathList);
				if (pathList != undefined) {
					this.pathList = pathList;
				}
				
				// 加载比赛路线样式
				const pathListStyle = config.pathListStyle;
				// console.log("[loadConfig] pathList:", pathList);
				if (pathListStyle != undefined) {
					this.pathListStyle = pathListStyle;
				}

				// 加载规则弹窗配置
				const popupRuleConfig = config.popupRuleConfig;
				if (popupRuleConfig != undefined) {
					this.popupRuleConfig = popupRuleConfig;
				}
				// console.log("[loadConfig] popupRuleConfig:", this.popupRuleConfig);
				
				// 加载弹窗数据
				const popupDataList = config.popupDataList;
				// console.log("[loadConfig] popupDataList:", popupDataList);
				if (popupDataList != undefined && popupDataList.length > 0) {
					for (var i = 0; i < popupDataList.length; i++) {
						// console.log("[loadConfig] popupDataList", i, popupDataList[i]);
						if (popupDataList[i] == 'default') {
							for (var j = 0; j < defaultPopUpDataList.length; j++) {
								this.popupDataList.push(defaultPopUpDataList[j]);
							}
						} else if (popupDataList[i] == 'default2') {
							for (var j = 0; j < defaultPopUpDataList2.length; j++) {
								this.popupDataList.push(defaultPopUpDataList2[j]);
							}
						} else {
							this.popupDataList.push(popupDataList[i]);
						}
					}
				} else {
					this.popupDataList = defaultPopUpDataList;
					console.log("[loadConfig] popupDataList 加载默认列表");
				}
				// console.log("[loadConfig] popupDataList:", this.popupDataList);
				
				this.pageReady = true;
			},
			// 获取倒计时
			getCountdown() {
				// console.log(this.endSecond)
				if (this.endSecond > 0) {
					const now = Date.now() / 1000;
					const dif = this.endSecond - now;
					// const dif = 3600*24 - 60;
					if (dif > 0) {
						this.countdown = '距结束 ' + tools.convertSecondsToDHM(dif);
					} else {
						this.countdown = "活动已结束";
					}
					// this.countdown = tools.convertSecondsToHMS(dif);
				} else {
					this.countdown = "距结束 --天--小时";
				}
			},
			// 格式化 距离
			fmtDistanct(val) {
				if (val < 1000)
					return Math.round(val * 10 / 1000) / 10;
				else
					return Math.round(val / 1000);
			},
			// 格式化 配速
			fmtPace(val) {
				return tools.convertSecondsToHMS(val, 2);
			},
			fmtMcTime(timestamp) {
				return tools.fmtMcTime(timestamp);
			},
			// 获取活动时间
			getActtime() {
				this.acttime = tools.getActtime(this.beginSecond, this.endSecond);
			},
			getCardConfigQuery() {
				uni.request({
					url: apiCardConfigQuery,
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"token": this.token,
					},
					method: "POST",
					data: {
						ecId: this.ecId,
						pageName: this.pageName
					},
					success: (res) => {
						// console.log("getCardConfigQuery", res)
						const data = res.data.data;
						// console.log("configJson", data.configJson);
						const config = data.configJson != "" ? JSON.parse(data.configJson) : "";
						// console.log("configJson", data.configJson);
						/* const config = {
							"css": `
								.top{
									position: relative;
									z-index: 10;
									width: 100%;
									height: 270px;
									padding-top: 36px;
									justify-content: space-between;
									background-image: url('static/backgroud/top_bg_guoqing.png');
									background-repeat: no-repeat;
									background-position-x: center;
									background-position-y: -8px;
									background-size: 100% 100%;
								}
								.topbar-color{
									color: #ffffff;
								}
							`,
							"pathList": {
								"row1": [{
										"type": 3,
										"pathImg": "/static/common/领秀城A区.png",
										"path": {
											"ocaId": 1,
											"mcType": 3
										},
										"navImg": "/static/common/nav.png",
										"point": {
											"longitude": 117.022194,
											"latitude": 36.661612,
											"name": "领秀城A区起始点"
										}
									},
									{
										"type": 3,
										"pathImg": "/static/common/领秀城B区.png",
										"path": {
											"ocaId": 1,
											"mcType": 3
										},
										"navImg": "/static/common/nav.png",
										"point": {
											"longitude": 117.022194,
											"latitude": 36.661612,
											"name": "领秀城B区起始点"
										}
									}
								],
								"row2": [{
										"type": 3,
										"pathImg": "/static/common/领秀城C区.png",
										"path": {
											"ocaId": 1,
											"mcType": 3
										},
										"navImg": "/static/common/nav.png",
										"point": {
											"longitude": 117.022194,
											"latitude": 36.661612,
											"name": "领秀城C区起始点"
										}
									},
									{
										"type": 3,
										"pathImg": "/static/common/领秀城D区.png",
										"path": {
											"ocaId": 1,
											"mcType": 3
										},
										"navImg": "/static/common/nav.png",
										"point": {
											"longitude": 117.022194,
											"latitude": 36.661612,
											"name": "领秀城D区起始点"
										}
									}
								],
								"row3": [{
										"type": 3,
										"pathImg": "/static/common/领秀城E区.png",
										"path": {
											"ocaId": 1,
											"mcType": 3
										},
										"navImg": "/static/common/nav.png",
										"point": {
											"longitude": 117.022194,
											"latitude": 36.661612,
											"name": "领秀城E区起始点"
										}
									},
									{
										"type": 3,
										"pathImg": "/static/common/领秀城F区.png",
										"path": {
											"ocaId": 1,
											"mcType": 3
										},
										"navImg": "/static/common/nav.png",
										"point": {
											"longitude": 117.022194,
											"latitude": 36.661612,
											"name": "领秀城F区起始点"
										}
									}
								],
								"row4": [{
										"type": 3,
										"pathImg": "/static/common/贵和购物中心.png",
										"path": {
											"ocaId": 1,
											"mcType": 3
										},
										"navImg": "/static/common/nav.png",
										"point": {
											"longitude": 117.022194,
											"latitude": 36.661612,
											"name": "贵和购物中心起始点"
										}
									},
									{
										"type": 3,
										"pathImg": "/static/common/中心花园.png",
										"path": {
											"ocaId": 1,
											"mcType": 3
										},
										"navImg": "/static/common/nav.png",
										"point": {
											"longitude": 117.022194,
											"latitude": 36.661612,
											"name": "中心花园起始点"
										}
									}
								]
							},
							"pathListStyle" : {
								"showLine" : true,
								"style": "justify-content: flex-start;"
							},
							"popupDataList": [
								{
									"type": 2,
									"data": {
										"title": "基本图例",
										"img": "/static/common/jbtl.png"
									}
								}, {
									"type": 2,
									"data": {
										"title": "基本标识",
										"img": "/static/common/jbbs2.png"
									}
								},
								{
									"type": 7,
									"data": {
										"title": "活动规则",
										"content": "<li>赛期内随时参赛、不限完赛次数、起点任选、实时排名 <li>起点-各途经点-结束点完整打卡为1次有效完赛 <li>赛事如有疑问，请咨询微信客服",
										"imageList": [
											{
												"src": "/static/common/qrcode_wxkf.png",
												"width": "100px",
												"height": "100px"
											}
										]
									}
								},
								{
									"type": 7,
									"data": {
										"title": "活动奖励",
										"content": "<li>途经点打卡1次，获1个百味豆<br><li>每次正确答题，再获1个百味豆<br><li>20个百味豆兑换1个鸡蛋<br><li>上不封顶！随时兑换！<br><text style='font-size:12px;color:#999999;'>(限本次活动百味豆，兑换以“蛋叔券”为准)</text><br><li>健身又能“薅羊毛”~",
										"imageList": [
											{
												"src": "/static/common/jidanquan.png",
												"width": "80px",
												"height": "80px"
											}
										]
									}
								},
								{
									"type": 5,
									"data": {
										"title": "兑换地点",
										"img": "/static/common/wslgwcs.png",
										"point": {
											"longitude": 117.022194,
											"latitude": 36.661612,
											"name": "万盛隆购物超市"
										}
									}
								}
							]
						}; */

						this.loadConfig(config);
						this.matchRsDetailQuery();

						setTimeout(this.dealFirstEnter, 500);
					},
					fail: (err) => {
						console.log("getCardConfigQuery err", err)
					},
				});
			},
			// 卡片对应线上赛多个活动查询
			matchRsDetailQuery() {
				uni.request({
					url: apiMatchRsDetailQuery,
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"token": this.token,
					},
					method: "POST",
					data: {
						ecId: this.ecId
					},
					success: (res) => {
						console.log("matchRsDetailQuery", res);
						if (checkResCode(res)) {
							const data = res.data.data;
							this.mcType = data.mcType;
							this.mcId = data.mcId;
							this.mcName = data.mcName;
							this.beginSecond = data.beginSecond;
							this.endSecond = data.endSecond;
							this.nickName = data.nickName;
							this.totalNum = data.totalNum;
							this.totalDistanct = data.totalDistanct;
							this.totalDistanctRankNum = data.totalDistanctRankNum;
							this.totalCp = data.totalCp;
							this.totalCpRankNum = data.totalCpRankNum;
							this.totalSysPoint = data.totalSysPoint;
							this.totalSysPointRankNum = data.totalSysPointRankNum;
							this.fastPace = data.fastPace;
							this.fastPaceRankNum = data.fastPaceRankNum;
							this.ocaRs = data.ocaRs;

							this.mcState = tools.checkMcState(this.beginSecond, this.endSecond);

							const rank = JSON.stringify(data);
							this.dealNotice(rank);

							this.getCountdown();
							this.getActtime();

							this.clear();
							this.interval = setInterval(this.getCountdown, 60000);
						}
					},
					fail: (err) => {
						console.log("matchRsDetailQuery err", err)
					},
				});
			},
			btnBack() {
				// const url = `action://to_home/`;
				// tools.appAction(url);

				// uni.navigateTo({
				// 	url: "/pages/bm/style3/rankList?" + this.queryString
				// });
				const url = "/pages/bm/style3/rankList?" + this.queryString;
				tools.appAction(url, "uni.navigateTo");
			},
			btnInfo() {
				// console.log(this.$refs.mypopup);
				this.$refs.mypopup.popupOpen();
			},
			onOverviewClick(ovtype) {
				this.queryObj.ovtype = ovtype;
				this.queryString = tools.objectToQueryString(this.queryObj);
				// uni.navigateTo({
				// 	url: "/pages/bm/style3/rankList?" + this.queryString
				// });
				const url = "/pages/bm/style3/rankList?" + this.queryString;
				tools.appAction(url, "uni.navigateTo");
			},
		}
	}
</script>

<style scoped>
	.content {
		width: 100vw;
		/* height: 100vh; */
		overflow-x: scroll;
	}

	.top-default {
		position: relative;
		z-index: 10;
		width: 100%;
		height: 270px;
		padding-top: 36px;
		justify-content: space-between;
		background-image: url("/static/backgroud/top_bg_egg2.png");
		background-repeat: no-repeat;
		background-position-x: center;
		background-position-y: -8px;
		background-size: 100% 100%;
		/* background-size: cover; */
	}

	/* .top-oval {
		position: relative;
		z-index: 0;
		margin-top: -180px;
		margin-left: -10%;
		width: 120%;
		height: 280px;
		background: linear-gradient(180deg, #ffffff 12.18%, #eeeeee 100%);
		border-radius: 50%;
		text-align: center;
	}

	.top-oval-text {
		color: #333333;
		font-size: 13px;
		line-height: 386px;
		pointer-events: auto;
	}
 */
	.topbar-color-default {
		color: #333333;
	}

	.topbtm {
		width: 100%;
		/* height: 60rpx; */
		margin-bottom: 40px;
		justify-content: space-evenly;
	}

	.topbtm-name {
		/* width: 320rpx; */
		/* height: 28px; */
		padding: 5px 12px;
		background-color: #9fda39;
		border-radius: 5px;
		/* backdrop-filter: blur(30px); */
		text-align: center;
		font-weight: 500;
		color: #497400;
		font-size: 14px;
	}

	.mid {
		width: 90%;
		height: 120px;
		position: relative;
		z-index: 20;
		margin-top: -100px;
		background: #ffffff;
		border-radius: 9px;
		box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.13);
		font-family: Source Han Sans CN;
	}

	.mid-1 {
		width: 90%;
		margin: 12px;
		font-weight: 500;
		color: #8e8e8e;
		font-size: 14px;
	}
	
	.mid-1-name {
		max-width: 360rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.mid-2 {
		width: 92%;
		/* margin: 0 10px; */
	}

	.mid-2-value {
		font-weight: 900;
		font-size: 22px;
	}

	.mid-2-text {
		color: #989898;
		font-size: 12px;
	}

	.mid-line {
		width: 0px;
		height: 45.04px;
		border: 1px solid;
		border-color: #e6e6e6;
	}

	.overview-1 {
		width: 111px;
		height: 54px;
		background: #ffb40b;
		border-radius: 50%;
		box-shadow: 3px 3px 0px rgba(140, 140, 140, 1);
		pointer-events: auto;
	}

	.overview-2 {
		margin-top: -43px;
		color: #ffffff;
		font-size: 18px;
		pointer-events: auto;
	}

	.overview-3 {
		width: 111px;
		height: 54px;
		background: #f39509;
		border-radius: 50%;
		box-shadow: 3px 3px 0px rgba(140, 140, 140, 1);
		pointer-events: auto;
	}

	.overview-4 {
		width: 111px;
		height: 54px;
		background: #81cd00;
		border-radius: 50%;
		box-shadow: 3px 3px 0px rgba(140, 140, 140, 1);
		pointer-events: auto;
	}

	.overview-5 {
		width: 111px;
		height: 54px;
		background: #64cbb0;
		border-radius: 50%;
		box-shadow: 3px 3px 0px rgba(140, 140, 140, 1);
		pointer-events: auto;
	}

	.ovline1 {
		margin-top: 9px;
		color: #ffffff;
		font-size: 12px;
	}

	.ovline2 {
		color: #ffffff;
		font-size: 16px;
	}

	.main {
		width: 100%;
		margin-top: 20px;
		margin-bottom: 10px;
		/* height: 70vh; */
		justify-content: space-around;
		/* justify-content: space-between; */
	}

	.main-title {
		margin-bottom: 10px;
		font-weight: 550;
		color: #333333;
		font-size: 16px;
	}
</style>