<!-- 
锦标赛
http://localhost:5173/card/#/pages/jbs/index
https://oss-mbh5.colormaprun.com/card/#/pages/jbs/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">
				<view :class="cssLogo"></view>
				<text class="type">{{type}}</text>
				<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,
		apiCardConfigQuery,
		apiCardDetailQuery
	} from '../../common/api';

	export default {
		data() {
			return {
				pageName: "index",
				queryString: "",
				token: "",
				
				ecId: 0, // 卡片id
				ecName: '', // 卡片名称
				ecDesc: '', // 卡片简介
				beginSecond: null, // 卡片开始时间戳，单位秒
				endSecond: null, // 卡片结束时间戳，单位秒
				
				// mcId: 0, // 赛事id
				// mcType: 0, // 赛事类型 1 普通活动 2 线下赛 3 线上赛
				// mcName: "", // 赛事名称

				countdown: "", // 倒计时
				interval: null,
				
				cssContentBg: "",
				cssLogo: "",
				type: "",
				btnText: "",
			}
		},
		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.type = event["type"] ?? "锦标赛";
			this.btnText = event["btnText"] ?? "开始比赛";
			
			tools.removeCssCode();

			this.getCardBaseQuery();
			this.getCardConfigQuery();
		},
		onUnload() {
			this.clear();
		},
		methods: {
			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
					},
					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: 120px;
								height: 120px;
								background-image: url('static/logo/xfl.png');
								background-size: contain;
							}
						`
						}; */
						
						this.loadConfig(config);
					},
					fail: (err) => {
						console.log("getCardConfigQuery err", err)
					},
				});
			},
			// 卡片信息查询
			// getCardDetailQuery() {
			// 	uni.request({
			// 		url: apiCardDetailQuery,
			// 		header: {
			// 			"Content-Type": "application/x-www-form-urlencoded",
			// 			"token": this.token,
			// 		},
			// 		method: "POST",
			// 		data: {
			// 			ecId: this.ecId
			// 		},
			// 		success: (res) => {
			// 			console.log("getCardDetailQuery", res)
			// 			const data = res.data.data;
			// 			this.mcType = data.mcType;
			// 			this.mcId = data.mcId;
			// 			this.mcName = data.mcName;
			// 			this.endSecond = data.endSecond;

			// 			this.getCountdown();

			// 			this.clear();
			// 			this.interval = setInterval(this.getCountdown, 60000);
			// 		},
			// 		fail: (err) => {
			// 			console.log("getCardDetailQuery err", err)
			// 		},
			// 	});
			// },
			btnClick() {
				// uni.reLaunch({
				// uni.navigateTo({
				// 	url: '/pages/default/rankList?' + this.queryString
				// });
				// window.location.href = `https://oss-mbh5.colormaprun.com/card/#/pages/default/rankList?${this.queryString}`;
				window.location.href = `${ossUrl}#/pages/jbs/rankList?${this.queryString}`;
			}
		}
	}
</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: 120px;
		height: 120px;
		background-image: url('/static/logo/jbs.png');
		background-repeat: no-repeat;
		background-position-x: center;
		background-position-y: center;
		background-size: contain;
	}

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

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

	.button {
		width: 320rpx;
		height: 86rpx;
		background: #ffffff;
		border-radius: 56rpx;
		font-size: 46rpx;
		line-height: 80rpx;
	}

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