<!-- 
[模板] 样式1 - 卡片页
http://localhost:5173/card/#/pages/tpl/style1/index
https://oss-mbh5.colormaprun.com/card/#/pages/tpl/style1/index
 -->
<template>
	<view class="body body-radius">
		<view v-if="pageReady" class="content content-bg" @click="btnClick">
			<view class="card-top uni-row">
				<view class="top-right uni-row">
					<image mode="aspectFit" class="clock" src="/static/default/clock.png"></image>
					<view class="countdown">{{countdown}}</view>
				</view>
			</view>
			<view class="card-main uni-column">
				<view class="logo"></view>
				<view class="uni-row" style="position: relative;">
					<image v-if="notice" mode="aspectFit" src="/static/common/notice.png" class="notice"></image>
					<text class="type mod-text">{{type}}</text>
				</view>
				<view class="name mod-text">{{ecName}}</view>
				<button class="button mod-button">{{btnText}}</button>
			</view>
		</view>
	</view>
</template>

<script>
	import tools from '../../../common/tools';
	import cardfunc from '../../../common/cardfunc';
	// import { localCardConfig } from "./cardconfig/test.js";
	import {
		localCardConfig
	} from "./cardconfig/pattern1.js";
	import {
		localUserConfig
	} from "./cardconfig/test_user.js";
	import {
		token,
		ossUrl,
		apiCardBaseQuery,
		apiUserJoinCardQuery,
		apiMatchRsDetailQuery
	} from '../../../common/api';

	export default {
		data() {
			return {
				cardConfigData: cardfunc.cardConfigData,
				pageReady: false,
				pageName: "index",
				rankKey: "rank-tpl-style1",
				queryObj: {},
				queryString: "",
				token: "",
				cardconfig: {}, // 卡片配置
				userconfig: {}, // 用户配置

				ecId: 0, // 卡片id
				ecName: '', // 卡片名称
				ecDesc: '', // 卡片简介
				beginSecond: null, // 卡片开始时间戳，单位秒
				endSecond: null, // 卡片结束时间戳，单位秒
				secondCardName: '', // 跳转页面名称

				isJoin: null, // 是否报名
				isFinished: false, // 赛事是否结束

				// mcId: 0, // 赛事id
				// mcType: 0, // 赛事类型 1 普通活动 2 线下赛 3 线上赛
				// mcName: "", // 赛事名称

				countdown: "", // 倒计时
				interval: null,

				type: "",
				btnText: "",
				notice: false, // 是否显示(小红点)通知图标
			}
		},
		computed: {},
		onLoad(query) { // 类型非必填，可自动推导
			// console.log(query);
			this.queryObj = query;
			this.queryString = tools.objectToQueryString(this.queryObj);
			// console.log(queryString);
			this.token = query["token"] ?? token;
			this.ecId = query["id"] ?? 0;

			this.type = query["type"] ?? "锦标赛";
			this.btnText = query["btnText"] ?? "开始比赛";

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

			cardfunc.init(this, this.token, this.ecId);
			cardfunc.getCardConfig(this.cardConfigQueryCallback, localCardConfig);

			this.getCardBaseQuery();
			this.matchRsDetailQuery();
		},
		onShow() {
			this.getUserJoinCardQuery();
		},
		onUnload() {
			this.clear();
		},
		methods: {
			dealNotice(rank) {
				// console.log('[dealNotice]');
				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 = true;
						// 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);
					},
				})
			},
			clear() {
				if (this.interval != null) {
					clearInterval(this.interval);
					this.interval = null;
				}
			},
			cardConfigQueryCallback(cardconfig) {
				this.cardconfig = cardconfig;
				cardfunc.getUserConfig(this.userConfigQueryCallback, localUserConfig);
			},
			userConfigQueryCallback(userconfig) {
				this.userconfig = userconfig;
				this.loadConfig();
			},
			loadConfig() {
				this.loadCardConfig(this.cardconfig);
				this.loadUserConfig(this.userconfig);
				this.pageReady = true;
			},
			loadCardConfig(cardconfig) {
				cardconfig = cardfunc.parseCardConfig(cardconfig);
				// console.log("[loadCardConfig] cardconfig:", cardconfig);

				// 加载卡片通用配置
				if (cardconfig.common != undefined) {
					cardfunc.loadCardCommonConfig(cardconfig.common);
				}

				// -------- 加载当前页面的配置 --------

				const config = cardfunc.parseCardConfig(cardconfig[this.pageName]);
				// console.log("[loadConfig] config_page:", config);
				if (config == undefined || config == null) {
					return;
				}

				// 加载CSS样式
				const css = config.css;
				if (css != undefined && css.length > 0) {
					tools.loadCssCode(css);
				}
			},
			loadUserConfig(userconfig) {
				if (!userconfig) {
					console.log("[loadUserConfig] userconfig 为空");
					return;
				}
				
				const config = cardfunc.parseCardConfig(userconfig);
				console.log("[loadUserConfig] userconfig:", config);

				// 加载CSS样式
				let css = "";
				const tplInfo = config.tplInfo;
				if (tplInfo.matchLogo != '') {
					css = `.logo{
						width: 80vw !important;
						height: 40vw !important;
						background: url('${tplInfo.matchLogo}') no-repeat center !important;
						background-size: contain !important;
					}`;
				}
				if (css.length > 0) {
					tools.loadCssCode(css, "css-user");
				}
			},
			// 获取倒计时
			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.isFinished = true;
					}
					// this.countdown = tools.convertSecondsToHMS(dif);
				} else {
					this.countdown = "距结束 --天--小时";
				}
			},
			// 卡片基本信息查询
			getCardBaseQuery() {
				uni.request({
					url: apiCardBaseQuery,
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"token": this.token,
					},
					method: "POST",
					data: {
						ecId: this.ecId,
						pageName: this.pageName
					},
					success: (res) => {
						// console.log("getCardBaseQuery", res)
						const data = res.data.data;

						this.ecName = data.ecName;
						this.ecDesc = data.ecDesc;
						this.beginSecond = data.beginSecond;
						this.endSecond = data.endSecond;
						this.secondCardName = data.secondCardName;

						this.getCountdown();

						this.clear();
						this.interval = setInterval(this.getCountdown, 60000);
					},
					fail: (err) => {
						console.log("getCardBaseQuery err", err)
					},
				});
			},
			// 用户是否已经报名卡片对应赛事查询
			getUserJoinCardQuery() {
				uni.request({
					url: apiUserJoinCardQuery,
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"token": this.token
					},
					method: "POST",
					data: {
						ecId: this.ecId
					},
					success: (res) => {
						// console.log("getUserJoinCardQuery", res)
						const code = res.data.code;
						const data = res.data.data;
						if (code == 0) {
							this.isJoin = data.isJoin;
							/*if (this.isJoin) {
								this.btnText = "已报名";
							} else {
								this.btnText = "未报名";
							} */
						}
					},
					fail: (err) => {
						console.log("getUserJoinCardQuery 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 (res.data.code == 0) {
							const data = res.data.data;
							const rank = JSON.stringify(data);
							this.dealNotice(rank);
						}
					},
					fail: (err) => {
						console.log("matchRsDetailQuery err", err)
					},
				});
			},
			btnClick() {
				if (this.isJoin) { // 已报名
					const url = `${ossUrl}#/pages/tpl/style1/rankList?${this.queryString}&full=true`;
					tools.appAction(url);
				} else { // 未报名
					if (!this.isFinished) { // 赛事未结束
						if (this.secondCardName == 'rankList') {
							const url = `${ossUrl}#/pages/tpl/style1/rankList?${this.queryString}&full=true`;
							tools.appAction(url);
						} else {
							const url = `${ossUrl}#/pages/tpl/style1/signup?${this.queryString}&full=true`;
							tools.appAction(url);
						}
					} else { // 赛事已结束
						const url = `${ossUrl}#/pages/tpl/style1/rankList?${this.queryString}&full=true`;
						tools.appAction(url);
					}
				}
			}
		}
	}
</script>

<style scoped>
	.content {
		width: 100vw;
		height: 100vh;
	}

	.content-bg {
		background: linear-gradient(180deg, #7aedff 0%, #047200 100%);
		/* background: linear-gradient(180deg, #178bff 0%, #004d9b 100%); */
		/* background: linear-gradient(180deg, #7aedff 0%, #8d2219 100%); */
	}

	.card-top {
		width: 100%;
		justify-content: flex-end;
	}

	.top-right {
		min-width: 180rpx;
		height: 80rpx;
		margin-top: 30rpx;
		margin-right: 30rpx;
		padding-left: 16rpx;
		padding-right: 16rpx;
		background-color: rgba(0, 0, 0, 0.3);
		border-radius: 12px;
	}

	.clock {
		width: 50rpx;
		height: 50rpx;
		margin-right: 12rpx;
	}

	.countdown {
		min-width: 120rpx;
		text-align: center;
		font-family: Roboto;
		color: #ffffff;
		font-size: 46rpx;
		/* letter-spacing: 2rpx; */
	}

	.card-main {
		width: 100%;
		/* height: 700rpx; */
		height: 660rpx;
		margin-top: 20rpx;
		justify-content: space-evenly;
	}

	.logo {
		width: 50vw;
		height: 50vw;
		background-image: url('https://oss-mbh5.colormaprun.com/static/logo/default.png');
		background-repeat: no-repeat;
		background-position-x: center;
		background-position-y: center;
		background-size: contain;
	}

	.notice {
		width: 30rpx;
		height: 30rpx;
		/* margin-right: 30rpx; */
		position: absolute;
		left: -60rpx;
	}

	.type {
		opacity: 60%;
		/* line-height: 25px; */
		font-family: Roboto;
		color: #ffffff;
		font-size: 40rpx;
		text-align: center;
	}

	.name {
		font-family: Roboto;
		color: #ffffff;
		font-size: 50rpx;
		text-align: center;
	}

	.button {
		width: 320rpx;
		height: 86rpx;
		margin-top: 30rpx;
		color: #000000;
		background: #ffffff;
		border-radius: 56rpx;
		font-size: 46rpx;
		line-height: 80rpx;
	}
</style>