<template>
	<view class="container main">
		<view class="info">
			<scroll-view scroll-y="true" class="scroll-view">
				<view class="list-scroll-view">
					<view class="one">
						<image class="image" :src="info.file&&info.file.length>0?info.file[0].url:'/static/match.png'">
						</image>
					</view>
					<view class="two">
						<view class="two_1 name">{{info.name||'暂无活动名称'}}</view>
						<view class="two_1 text_color">
							<text class="t-icon t-icon-shizhong"></text>
							<span>{{getTime(info.start_time,info.end_time)||'暂无'}}</span>
						</view>
						<view class="two_1 text_color">
							<text class="t-icon t-icon-zuobiao"></text>
							<span>{{info.address||'在线活动'}}</span>
						</view>
						<view class="two_1 text_color">
							<text class="t-icon t-icon-dianzan"></text>
							<span>{{info.work||'暂无组织单位'}}</span>
						</view>
					</view>
					<view class="thr">
						<view class="thr_cont" v-if="info.rules&&info.rules.rules1">
							<view class="thr_1">大赛背景</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules1)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules2">
							<view class="thr_1">大赛主题和目标</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules2)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules3">
							<view class="thr_1">大赛基本情况介绍</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules3)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules4">
							<view class="thr_1">赛题任务</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules4)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules5">
							<view class="thr_1">赛程安排</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules5)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules6">
							<view class="thr_1">赛制阶段</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules6)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules7">
							<view class="thr_1">参赛资格</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules7)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules8">
							<view class="thr_1">参赛报名</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules8)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules9">
							<view class="thr_1">奖项设置与奖励办法</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules9)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules10">
							<view class="thr_1">组织单位</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules10)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules11">
							<view class="thr_1">赛事联络</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules11)"></rich-text>
							</view>
						</view>
						<view class="thr_cont" v-if="info.rules&&info.rules.rules12">
							<view class="thr_1">赛事交流</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.rules.rules12)"></rich-text>
							</view>
						</view>
					</view>
					<view class="thr">
						<view class="thr_cont" v-if="info.brief">
							<view class="thr_1">常见问题</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.brief)"></rich-text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="foot">
			<view class="foot_text foot_1" @click="toFriend()">分享给好友</view>
			<view class="foot_text foot_2" @click="toSign()">报名活动</view>
		</view>
		<uni-popup ref="share" type="share" safeArea backgroundColor="#fff">
			<view class="popup_cont">
				<view class="popup_title">
					<text>分享到</text>
				</view>
				<view class="popup_share">
					<view class="share_1">
						<button type="primary" plain="true" open-type="share" @click="toShare">
							<text class="t-icon t-icon-weixin"></text>
							<text>分享给好友</text>
						</button>
					</view>
					<view class="share_1">
						<button type="primary" plain="true" open-type="share" @click="toPoster">
							<text class="t-icon t-icon-iconfontzhizuobiaozhunbduan36"></text>
							<text>生成活动海报</text>
						</button>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				user: {},
				config: {},
				info: {},
				// 字典表
				statusList: []
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.searchConfig();
			await that.searchOther();
			await that.search();
		},
		onShow: async function() {
			const that = this;
			that.searchToken();
		},
		onUnload: function() {
			// 页面卸载,重新部署分享内容
			const that = this;
			if (that.config) {
				// 赋值默认值
				that.$config.share = {
					title: that.config.zhTitle,
					path: '/pages/index/index',
					imageUrl: that.config?.logoUrl[0]?.url
				}
			}
		},
		methods: {
			// 用户信息
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					}
				} catch (e) {}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {}
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询类型
				res = await that.$api(`/dictData`, 'GET', {
					code: 'matchStatus',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `statusList`, res.data);
			},
			// 查询
			async search() {
				const that = this;
				if (that.id) {
					let res;
					res = await that.$api(`/match/${that.id}`, 'GET', {})
					if (res.errcode == '0') {
						that.$set(that, `info`, res.data)
						uni.setNavigationBarTitle({
							title: res.data.name
						});
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 处理时间
			getTime(start_time, end_time) {
				if (start_time && end_time) {
					const start = new Date(start_time);
					const end = new Date(end_time);
					const weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
					const day = start.getDate();
					const weekday = weekdays[start.getDay()];
					const month = start.getMonth();
					const start_hours = start.getHours();
					const start_minutes = start.getMinutes();
					const end_hours = end.getHours();
					const end_minutes = end.getMinutes();
					return `${month}/${day < 10 ? '0' + day : day} ${weekday} ${start_hours.toString().padStart(2, '0')}:${start_minutes.toString().padStart(2, '0')} - ${end_hours.toString().padStart(2, '0')}:${end_minutes.toString().padStart(2, '0')} `;
				}
			},
			// 处理富文本
			formatRichText(html) {
				if (html) {
					// 富文本内容格式化
					return html && html.replace(/<img[^>]*>/gi, function(match, capture) {
						// 查找所有的 img 元素
						return match.replace(/style=".*"/gi, '').replace(/style='.*'/gi,
							'')
						// 删除找到的所有 img 元素中的 style 属性
					}).replace(/\<img/gi, '<img style="width:100%;"') // 对 img 元素增加 style 属性，并设置宽度为 100%
				}
			},
			// 分享给好友
			toFriend() {
				const that = this;
				that.$refs.share.open()
			},
			// 活动报名
			toSign() {
				const that = this;
				if (that.info.match_status == '0') {
					uni.showToast({
						title: '活动未开始 暂不支持报名！',
						icon: 'none'
					})
				} else if (that.info.match_status == '1') {
					if (that.user && that.user.id) {
						uni.navigateTo({
							url: `/pagesHome/match/sign?id=${that.id}`
						})
					} else {
						uni.navigateTo({
							url: `/pagesHome/login/index`
						})
					}
				} else {
					uni.showToast({
						title: '比赛已结束！',
						icon: 'none'
					})
				}
			},
			// 分享给好友
			toShare() {
				const that = this;
				let info = that.info;
				let imageUrl = that.config?.logoUrl[0]?.url || info.file[0]?.url;
				that.$config.share = {
					title: info.name,
					path: `/pagesHome/match/index?id=${that.id}`,
					imageUrl: imageUrl
				}
			},
			// 生成海报
			toPoster() {
				console.log('生成海报');
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.info {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);

			.one {
				.image {
					width: 100%;
					height: 50vw;
				}
			}

			.two {
				padding: 0 2vw;
				background-color: var(--mainColor);

				.two_1 {
					display: flex;
					align-items: center;
					padding: 3vw 0;
					border-bottom: 1px solid var(--f5Color);
					font-size: var(--font14Size);

					span {
						margin: 0 0 0 1vw;
					}

					.t-icon {
						width: var(--font15Size) !important;
						height: var(--font15Size) !important;
					}
				}

				.name {
					font-weight: bold;
					font-size: var(--font16Size);
				}

				.text_color {
					color: var(--f69Color);
				}

			}

			.thr {
				margin: 3vw 0 0 0;
				background-color: var(--mainColor);

				.thr_cont {
					margin: 2vw 0 0 0;

					.thr_1 {
						text-indent: 10px;
						border-left: 3px solid var(--f3CColor);
						font-weight: bold;
						font-size: var(--font16Size);
					}

					.thr_2 {
						padding: 2vw;
					}
				}
			}
		}

		.foot {
			width: 100%;
			height: 8vh;
			overflow: hidden;
			display: flex;
			align-items: center;

			.foot_text {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 50%;
				height: 100%;
				color: var(--mainColor);
			}

			.foot_1 {
				background: linear-gradient(90deg, #FFCD1E, #FF8A18);
			}

			.foot_2 {
				background: linear-gradient(90deg, #1E83FF, #1071e6);
			}
		}

		.popup_cont {
			.popup_title {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				height: 40px;
				font-size: var(--font14Size);
				color: #666;
			}

			.popup_share {
				display: flex;
				align-items: center;
				padding: 2vw 0;

				.share_1 {
					width: 50%;

					button {
						background: none;
						border: none;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;

						.t-icon {
							width: 8vw;
							height: 8vw;
						}

						text {
							margin-top: 10px;
							font-size: var(--font14Size);
							color: #3B4144;
						}
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>