<template>
	<view class="container main">
		<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>
		<tabs :tabs="tabs" @tabsChange="tabsChange">
			<view class="tabsList">
				<view v-if="tabs.active=='0'">
					<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>
				<view v-else>
					<view class="thr">
						<view class="thr_cont">
							<view class="thr_1">报名信息</view>
							<view class="thr_2">
								<view class="thr_label">
									<view class="left">姓名</view>
									<view class="right textOne">
										<text>{{sign.name||'暂无姓名'}}</text>
									</view>
								</view>
								<view class="thr_label">
									<view class="left">证件类型</view>
									<view class="right textOne">
										<text>{{getDict(sign.cardType,'cardType')||'暂无证件类型'}}</text>
									</view>
								</view>
								<view class="thr_label">
									<view class="left">证件号码</view>
									<view class="right textOne">
										<text>{{sign.card||'暂无证件号码'}}</text>
									</view>
								</view>
								<view class="thr_label">
									<view class="left">手机号</view>
									<view class="right textOne">
										<text>{{sign.phone||'暂无手机号'}}</text>
									</view>
								</view>
								<view class="thr_label">
									<view class="left">电子邮箱</view>
									<view class="right textOne">
										<text>{{sign.email||'暂无电子邮箱'}}</text>
									</view>
								</view>
								<view class="thr_label">
									<view class="left">微信/QQ</view>
									<view class="right textOne">
										<text>{{sign.communication||'暂无微信/QQ'}}</text>
									</view>
								</view>
								<view class="thr_label">
									<view class="left">报名时间</view>
									<view class="right textOne">
										<text>{{sign.time||'暂无报名时间'}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</tabs>
	</view>
</template>

<script>
	import tabs from '../../components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				id: '',
				tabs: {
					active: '0',
					bgColor: '#ffffff',
					menu: [{
						title: '活动内容',
						active: '0'
					}, {
						title: '报名信息',
						active: '1'
					}]
				},
				user: {},
				config: {},
				info: {},
				sign: {},
				// 字典表
				statusList: [],
				typeList: [],
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.searchConfig();
			that.searchToken();
			await that.searchOther();
			await that.search();
		},
		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: 'cardType',
					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(`/sign/${that.id}`, 'GET', {})
					if (res.errcode == '0') {
						that.$set(that, `sign`, res.data)
						if (res.data.match) {
							const arr = await that.$api(`/match/${res.data.match}`, 'GET', {})
							if (res.errcode == '0') that.$set(that, `info`, arr.data)
						}
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
			},
			// 处理时间
			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')} `;
				}
			},
			// 处理字典表
			getDict(item, model) {
				const that = this;
				let res
				if (model == 'status') res = that.statusList.find(i => i.value == item)
				else if (model == 'cardType') res = that.typeList.find(i => i.value == item)
				if (res) return res.label
				else return '暂无'
			},
			// 处理富文本
			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%
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		background-color: var(--f9Color);

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

		.two {
			padding: 0 2vw;
			margin: 0 0 3vw 0;
			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);
			}

		}

		.tabsList {
			.thr {
				background-color: var(--mainColor);

				.thr_cont {
					padding: 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;

						.thr_label {
							display: flex;
							justify-content: space-between;
							padding: 4vw;
							border-bottom: 1px solid var(--f9Color);
							font-size: var(--font14Size);
							color: var(--f69Color);

							.right {
								text-align: right;
								width: 60vw;
							}
						}
					}
				}
			}
		}
	}
</style>