<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="getUrl(info.file)"></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-gonglve"></text>
							<span>{{getDict(info.match_type,'type')||'自办'}}</span>
						</view>
						<view class="two_1 text_color">
							<text class="t-icon t-icon-gongsi"></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" v-if="info.brief">
						<view class="thr_cont">
							<view class="thr_1">常见问题</view>
							<view class="thr_2">
								<rich-text :nodes="formatRichText(info.brief)"></rich-text>
							</view>
						</view>
					</view>
					<view class="thr" v-if="info.form == '3'&&info.match_status == '3' && signList.length > 0">
						<view class="thr_cont">
							<uni-section title="参演方信息" type="line">
								<uni-group mode="card" v-for="(item, index) in signList" :key="index">
									<view class="card_title" style="font-weight: bold;">{{ item.name || '暂无' }} </view>
									<view class="card_title"> <span>路演名称：</span>{{ item.project_name || '暂无' }} </view>
									<view class="card_title"> <span>路演介绍：</span>{{ item.project_brief || '暂无' }} </view>
									<view class="card_title" v-if="info.is_show == '0'">
										<span>联系方式：</span>{{ item.phone || '暂无' }}
									</view>
									<view class="card_button" v-else style="text-align: center;margin: 10px 0 0 0;">
										<button class="button" size="mini" type="primary"
											@tap.stop="toChat(item)">获取联系方式</button>
									</view>
								</uni-group>
							</uni-section>
						</view>
					</view>
					<view class="thr" v-if="info.form == '4' && info.ext_status == '3'">
						<view class="thr_cont">
							<uni-section title="初审公示名单" type="line">
								<uni-group mode="card" v-for="(item, index) in preliminaryList" :key="index">
									<view class="card_title" style="font-weight: bold;">
										{{ item.user_name || '暂无' }}
									</view>
									<view class="card_title">
										<span>编号：</span>{{ item.no || '暂无' }}
									</view>
									<view class="card_title">
										<span>项目名称：</span>{{ item.project_name || '暂无' }}
									</view>
									<view class="card_title">
										<span>单位名称：</span>{{ item.person_unit || '暂无' }}
									</view>
									<view class="card_title">
										<span>初审时间：</span>{{ item.start_time || '暂无' }}
									</view>
								</uni-group>
							</uni-section>
						</view>
					</view>
					<view class="thr" v-if="info.form == '4' && info.ext_status == '6'">
						<view class="thr_cont">
							<uni-section title="决赛公示名单" type="line">
								<uni-group mode="card" v-for="(item, index) in finalsList" :key="index">
									<view class="card_title" style="font-weight: bold;">
										{{ item.user_name || '暂无' }}
									</view>
									<view class="card_title">
										<span>编号：</span>{{ item.no || '暂无' }}
									</view>
									<view class="card_title">
										<span>项目名称：</span>{{ item.project_name || '暂无' }}
									</view>
									<view class="card_title">
										<span>单位名称：</span>{{ item.person_unit || '暂无' }}
									</view>
									<view class="card_title">
										<span>初审时间：</span>{{ item.start_time || '暂无' }}
									</view>
									<view class="card_title">
										<span>决赛时间：</span>{{ item.final_start_time || '暂无' }}
									</view>
									<view class="card_title">
										<span>顺序：</span>{{ item.final_order_no || '暂无' }}
									</view>
								</uni-group>
							</uni-section>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="foot">
			<view class="foot_text foot_1" @tap="toFriend()">分享给好友</view>
			<view class="foot_text foot_2" @tap="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: [],
				typeList: [],
				// 参演方
				signList: [],
				// 初审结果公示
				preliminaryList: [],
				// 决赛结果公示
				finalsList: [],
			}
		},
		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);
				// 赛事类型
				res = await that.$api(`/dictData`, 'GET', {
					code: 'activeType',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `typeList`, 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)
						if (res.data.form == '3') {
							const arr = await that.$api(`/sign`, 'GET', {
								match: res.data.id,
								status: '1',
								type: '1'
							})
							if (arr.errcode == '0') that.$set(that, `signList`, arr.data)
						}
						if (res.data.form == '4' && res.data.ext_status == '3') {
							await that.searchStep3()
						}
						if (res.data.form == '4' && res.data.ext_status == '6') {
							await that.searchStep5()
						}
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 查询初审公示名单
			async searchStep3() {
				const that = this;
				const res = await that.$api(`/matchExt/step3/nameList/${that.id}`, 'GET', {})
				if (res.errcode == '0') that.$set(that, `preliminaryList`, res.data)
			},
			// 查询决赛公示名单
			async searchStep5() {
				const that = this;
				const res = await that.$api(`/matchExt/step5/list/${that.id}`, 'GET', {
					final_confirm: '0'
				})
				if (res.errcode == '0') that.$set(that, `finalsList`, res.data)
			},
			// 处理时间
			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 year = start.getFullYear();
					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 `${year}/${month+1}/${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')} `;
				}
			},
			// 处理字典表
			getDict(item, model) {
				const that = this;
				let res
				if (model == 'type') res = that.typeList.find(i => i.value == item)
				if (res) return res.label
				else return '暂无'
			},
			// 图片处理
			getUrl(e) {
				const that = this;
				if (e && e.length > 0) return that.$config.serverFile + e[0].url
				else return '/static/match.png'
			},
			// 处理富文本
			formatRichText(html) {
				if (html) {
					html = html.replace(/<table[^>]*>/gi, match => {
						// 如果已有 style 属性，替换为新的样式；如果没有，添加新的 style 属性
						return match.replace(/style="[^"]+"/gi,
								`style="border-collapse: collapse; width: 100%;text-align: center;"`)
							.replace(/<table/gi,
								`<table style="border-collapse: collapse; width: 100%;text-align: center;"`);
					});
					html = html.replace(/<th[^>]*>/gi, match => {
						// 如果已有 style 属性，替换为新的样式；如果没有，添加新的 style 属性
						return match.replace(/style="[^"]+"/gi,
								`style="border: 1px solid #ddd; padding: 8px; background-color: #f2f2f2;text-align: center;"`
							)
							.replace(/<table/gi,
								`<table style="border: 1px solid #ddd; padding: 8px; background-color: #f2f2f2;text-align: center;"`
							);
					});
					html = html.replace(/<td[^>]*>/gi, match => {
						// 如果已有 style 属性，替换为新的样式；如果没有，添加新的 style 属性
						return match.replace(/style="[^"]+"/gi,
								`style="border: 1px solid #ddd; padding: 8px;text-align: center;"`)
							.replace(/<table/gi,
								`<table style="border: 1px solid #ddd; padding: 8px;text-align: center;"`);
					});
					// 富文本内容格式化
					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) {
						if (that.info.form == '3') {
							uni.navigateTo({
								url: `/pagesHome/match/active?id=${that.id}`
							})
						} else if (that.info.form == '4') {
							uni.navigateTo({
								url: `/pagesHome/investigate/index?id=${that.id}`
							})
						} else {
							uni.navigateTo({
								url: `/pagesHome/match/sign?id=${that.id}`
							})
						}
					} else {
						if (that.info.form == '4') {
							uni.navigateTo({
								url: `/pagesHome/investigate/index?id=${that.id}`
							})
						} else {
							uni.navigateTo({
								url: `/pagesHome/login/index`
							})
						}
					}
				} else if (that.info.match_status == '2') {
					uni.showToast({
						title: '活动进行中 暂不支持报名！',
						icon: 'none'
					})
				} 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('生成海报');
			},
			// 获取联系方式
			toChat(item) {
				const that = this;
				if (that.user.id) {
					uni.showModal({
						title: '提示',
						content: '您确认要获取联系方式？',
						success: async function(res) {
							if (res.confirm) {
								let source_id = item.id
								let source = 'sign'
								let apply_user = that.user.id
								const res = await that.$api(`/contactApply/sign`, 'POST', {
									source_id,
									source,
									apply_user
								})
								if (res.errcode == 0) {
									uni.showToast({
										title: '申请获取联系方式成功，等待发送消息',
										icon: 'none'
									})
								} else {
									uni.showToast({
										title: res.errmsg,
										icon: 'none'
									})
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					uni.navigateTo({
						url: `/pagesHome/login/index`
					})
				}
			}
		}
	}
</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>