<template>
	<view class="container main">
		<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>
		<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-if="tabs.active=='1'">
					<view class="thr">
						<view class="thr_cont">
							<view class="thr_1">报名信息</view>
							<view class="thr_2" v-if="info.form!='3'">
								<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 class="thr_2" v-else>
								<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>{{sign.phone||'暂无联系电话'}}</text>
									</view>
								</view>
								<view class="thr_label">
									<view class="left">报名类型</view>
									<view class="right textOne">
										<text>{{getDict(sign.type,'sign')||'暂无报名类型'}}</text>
									</view>
								</view>
								<view class="thr_label">
									<view class="left">公司名称</view>
									<view class="right textOne">
										<text>{{sign.company||'暂无公司名称'}}</text>
									</view>
								</view>
								<view class="thr_label" v-if="sign.type=='1'">
									<view class="left">路演名称</view>
									<view class="right textOne">
										<text>{{sign.project_name||'暂无路演名称'}}</text>
									</view>
								</view>
								<view class="thr_label" v-if="sign.type=='1'">
									<view class="left">路演简介</view>
									<view class="right textOne">
										<text>{{sign.project_brief||'暂无路演简介'}}</text>
									</view>
								</view>
								<view class="thr_label" v-if="sign.type=='1'">
									<view class="left">路演附件</view>
									<view class="right textOne">
										<text v-if="sign.project_file&&sign.project_file.length>0"
											@click="viewFile(sign.project_file[0].url)">
											{{sign.project_file[0].name||'暂无路演附件'}}
										</text>
										<text v-else>暂无路演附件</text>
									</view>
								</view>
								<view class="thr_label">
									<view class="left">报名时间</view>
									<view class="right textOne">
										<text>{{sign.time||'暂无报名时间'}}</text>
									</view>
								</view>
							</view>
						</view>
						<view class="button">
							<button class="danger" size="mini" type="warn" @tap.stop="toEdit(sign)"
								v-if="sign.status!=='1'">修改报名信息</button>
						</view>
					</view>
				</view>
				<view v-else>
					<view class="list" v-for="(item, index) in score" :key="index">
						<view class="value">
							<view class="title">流程名称：</view>
							<view class="label">{{item.matchPath_name||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">分数：</view>
							<view class="label">{{item.score||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">时间：</view>
							<view class="label">{{item.time||'暂无'}}</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: {},
				// 分数信息
				score: {},
				// 字典表
				statusList: [],
				cTypeList: [],
				typeList: [],
				signTypeList: []
			}
		},
		onShow: 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, `cTypeList`, res.data);
				// 赛事类型
				res = await that.$api(`/dictData`, 'GET', {
					code: 'activeType',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `typeList`, res.data)
				// 报名类型
				res = await that.$api(`/dictData`, 'GET', {
					code: 'signType',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `signTypeList`, 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)
								if (arr.data.match_status == '2' || arr.data.match_status == '3') {
									await that.searchPath()
								}
							}
						}
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			async searchPath() {
				const that = this;
				that.$set(that.tabs, `menu`, [...that.tabs.menu, {
					title: '分数信息',
					active: '2'
				}])
				const arr = await that.$api(`/score/list`, 'GET', {
					match: that.info.id,
					sign: that.sign.id
				})
				if (arr.errcode == '0') that.$set(that, `score`, arr.data)
			},
			// 选择选项卡
			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')} `;
				}
			},
			// 修改报名信息
			toEdit(item) {
				const that = this;
				if (that.info.form == '3') {
					uni.navigateTo({
						url: `/pagesMy/activity/active?id=${item.id}`
					})
				} else {
					uni.navigateTo({
						url: `/pagesMy/activity/add?id=${item.id}`
					})
				}
			},
			// 查看文件
			viewFile(e) {
				const that = this;
				uni.downloadFile({
					url: that.$config.serverFile + e,
					success: function(res) {
						var filePath = res.tempFilePath;
						uni.openDocument({
							filePath: filePath,
							showMenu: true,
							success: function(res) {
								console.log('打开文档成功');
							}
						});
					}
				});
			},
			// 图片处理
			getUrl(e) {
				const that = this;
				if (e && e.length > 0) return that.$config.serverFile + e[0].url
				else return '/static/match.png'
			},
			// 处理字典表
			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.cTypeList.find(i => i.value == item)
				else if (model == 'type') res = that.typeList.find(i => i.value == item)
				else if (model == 'sign') res = that.signTypeList.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 {
			.list {
				background-color: var(--mainColor);
				border-bottom: 1px solid var(--f5Color);
				margin: 2vw 2vw 0 2vw;
				border-radius: 4px;
				padding: 3vw;


				.value {
					display: flex;
					align-items: center;

					.title {
						font-size: var(--font14Size);
						font-weight: bold;
					}

					.label {
						font-size: var(--font12Size);
						color: var(--f85Color);
					}
				}


			}

			.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;
							}
						}
					}
				}

				.button {
					text-align: center;
				}
			}
		}
	}
</style>