<!-- 
[报名] 样式1
http://localhost:5173/card/#/pages/bm/style1/index
https://oss-mbh5.colormaprun.com/card/#/pages/bm/style1/index
 -->
<template>
	<view class="body body-radius">
		<view class="content" :class="cssContentBg" @click="btnClick">
			<view class="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="main uni-column">
				<!-- <image mode="aspectFit" :class="cssLogo" :src="logoSrc"></image> -->
				<view :class="cssLogo"></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">{{type}}</text>
				</view>
				<text class="name">{{ecName}}</text>
				<button class="button button-txtcolor">{{btnText}}</button>
			</view>
		</view>
	</view>
</template>

<script>
	import tools from '../../../common/tools';
	import {
		token,
		ossUrl,
		apiCardBaseQuery,
		apiUserJoinCardQuery,
		apiCardConfigQuery,
		apiUserCurrentRankNumQuery
	} from '../../../common/api';
	
	export default {
		data() {
			return {
				pageName: "index",
				rankKey: "rank-bm-style1",
				queryString: "",
				token: "",
				
				ecId: 0, // 卡片id
				ecName: '', // 卡片名称
				ecDesc: '', // 卡片简介
				beginSecond: null, // 卡片开始时间戳，单位秒
				endSecond: null, // 卡片结束时间戳，单位秒
				
				isJoin: null, // 是否报名
				
				// mcId: 0, // 赛事id
				// mcType: 0, // 赛事类型 1 普通活动 2 线下赛 3 线上赛
				// mcName: "", // 赛事名称
				
				countdown: "", // 倒计时
				interval: null,
				
				cssContentBg: "",
				cssLogo: "",
				// logoSrc: "",
				type: "",
				btnText: "",
				notice: false,	// 是否显示(小红点)通知图标
			}
		},
		computed: {},
		onLoad(event) { // 类型非必填，可自动推导
			// console.log(event);
			this.queryString = tools.objectToQueryString(event);
			// console.log(queryString);
			this.token = event["token"] ?? token;
			this.ecId = event["id"] ?? 0;
			
			// this.contentBg = "content-bg-" + (event["bg"] ?? "green");
			// this.logoSrc = "/static/logo/" + (event["logo"] ?? "jbs") + '.png';
			this.type = event["type"] ?? "锦标赛";
			this.btnText = event["btnText"] ?? "开始比赛";
			
			this.rankKey += "-" + this.ecId;
			console.log("rankKey:", this.rankKey);
			
			tools.removeCssCode();
			
			this.getCardBaseQuery();
			this.getCardConfigQuery();
			this.getUserJoinCardQuery();
			this.getUserCurrentRankNumQuery();
		},
		onUnload() {
			this.clear();
		},
		methods: {
			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 = 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;
				}
			},
			loadConfig(config) {
				// console.log("config", config);
				const css = config.css;
				if (css != undefined && css.length > 0) {
					tools.loadCssCode(css);
					
					if (css.indexOf(".content-bg{") >= 0) {
						this.cssContentBg = "content-bg";
					}
					if (css.indexOf(".logo{") >= 0) {
						this.cssLogo = "logo";
					}
				}
				
				if (this.cssContentBg == "") {
					this.cssContentBg = "content-bg-default";
				}
				if (this.cssLogo == "") {
					this.cssLogo = "logo-default";
				}
				console.log("[loadConfig] cssContentBg:", this.cssContentBg);
				console.log("[loadConfig] cssLogo:", this.cssLogo);
			},
			// 获取倒计时
			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 = "--天--小时";
				}
			},
			// 卡片基本信息查询
			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.getCountdown();
						
						this.clear();
						this.interval = setInterval(this.getCountdown, 60000);
					},
					fail: (err) => {
						console.log("getCardBaseQuery err", err)
					},
				});
			},
			// 卡片配置信息查询
			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;
						const config = data.configJson != "" ? JSON.parse(data.configJson) : "";
						// console.log("configJson", config);
						/* const config = {
						"css": `
							.content-bg {
								background: url('static/cardbg/xfl.png');
								background-size: cover;
							}
							
							.logo{
								width: 50vw;
								height: 50vw;
								background: url('static/logo/sqsj.png') no-repeat center;
								background-size: contain;
							}
						`
						}; */
						
						this.loadConfig(config);
					},
					fail: (err) => {
						console.log("getCardConfigQuery 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)
					},
				});
			},
			// 卡片用户当前排名查询
			getUserCurrentRankNumQuery() {
				uni.request({
					url: apiUserCurrentRankNumQuery,
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"token": this.token,
					},
					method: "POST",
					data: {
						ecId: this.ecId
					},
					success: (res) => {
						// console.log("getUserCurrentRankNumQuery", res)
						if (res.data.code == 0) {
							const data = res.data.data;
							const rankNum = data.rankNum;
							this.dealNotice(rankNum);
						}
					},
					fail: (err) => {
						console.log("getUserCurrentRankNumQuery err", err)
					},
				});
			},
			btnClick() {
				if (this.isJoin) {	// 已报名
					// uni.reLaunch({
					// 	url: '/pages/bm/style1/rankList?full=true&' + this.queryString
					// });
					window.location.href = `${ossUrl}#/pages/bm/style1/rankList?${this.queryString}&full=true`;
				}
				else {	// 未报名
					// uni.reLaunch({
					// 	url: '/pages/bm/style1/signup?full=true&' + this.queryString
					// });
					window.location.href = `${ossUrl}#/pages/bm/style1/signup?${this.queryString}&full=true`;
				}
				
			}
		}
	}
</script>

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

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

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

	.logo-default {
		width: 50vw;
		height: 50vw;
		background-image: url('/static/logo/jbs.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;
		background: #ffffff;
		border-radius: 56rpx;
		font-size: 46rpx;
		line-height: 80rpx;
	}

	.button-txtcolor {
		color: #000000;
	}
	
</style>

