<template>
	<view class="main">
		<view class="one">
			<view class="one_1">
				<input type="text" v-model="searchInfo.name" placeholder="请输入赛事名称">
			</view>
			<view class="one_2">
				<button size="mini" class="button" type="primary" @click="toInput">搜索</button>
			</view>
		</view>
		<view class="two">
			<tabs :tabs="tabs" @tabsChange="tabsChange">
				<view class="tabsList">
					<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll"
						v-if="total>0">
						<view class="list-scroll-view">
							<view class="list" v-for="(item, index) in list" :key="index" @tap="toView(item)">
								<view class="list_1">
									<image class="image" :src="getUrl(item.file)"></image>
								</view>
								<view class="list_2 textOne">{{item.name||'暂无比赛名称'}}</view>
								<view class="list_3">
									<view class="list_left">
										<text class="t-icon t-icon-shizhong"></text>
										<span>{{getTime(item.start_time,item.end_time)||'暂无'}}</span>
									</view>
									<view class="list_left">
										<text class="t-icon t-icon-gonglve"></text>
										<span>{{getDict(item.match_type,'type')||'自办'}}</span>
									</view>
									<view class="list_right"
										:class="[item.match_status=='0'?'active_1':item.match_status=='1'?'active_2':item.match_status=='2'?'active_3':'active_4']">
										{{getDict(item.match_status,'status')}}
									</view>
								</view>
								<view class="list_4">
									<button class="warning" size="default" type="warn"
										@tap.stop="toSign(item)">报名信息</button>
									<button class="warning" size="default" type="warn" @tap.stop="toScore(item)"
										v-if="item.match_status=='2'&&item.form!='4'">分数列表</button>
								</view>
							</view>
							<view class="is_bottom" v-if="is_bottom">
								<text>{{config.bottom_title||'到底了！'}}</text>
							</view>
						</view>
					</scroll-view>
					<o-empty v-else />
				</view>
			</tabs>
		</view>
	</view>
</template>

<script>
	import tabs from '../../components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				searchInfo: {},
				config: {},
				user: {},
				tabs: {
					active: '0',
					bgColor: '#ffffff',
					menu: []
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				statusList: [],
				typeList: []
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.searchToken();
			that.searchConfig();
			await that.searchOther();
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					} else {
						that.$set(that, `user`, {});
					}
				} catch (e) {}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
					status: that.tabs.active,
					user: that.user.id,
				}
				const res = await that.$api(`/match`, 'GET', {
					...info,
					...that.searchInfo
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.clearPage();
				that.search()
			},
			// 查看详情
			toView(item) {
				uni.navigateTo({
					url: `/pagesHome/match/index?id=${item.id||item._id}`
				})
			},
			// 报名信息
			toSign(item) {
				const that = this;
				if (item.form == '3') {
					uni.navigateTo({
						url: `/pagesMy/match/active?id=${item.id||item._id}`
					})
				} else if (item.form == '4') {
					uni.navigateTo({
						url: `/pagesMy/match/preliminary?id=${item.id||item._id}`
					})
				} else {
					uni.navigateTo({
						url: `/pagesMy/match/sign?id=${item.id||item._id}`
					})
				}

			},
			// 分数列表
			toScore(item) {
				uni.navigateTo({
					url: `/pagesMy/match/score?id=${item.id||item._id}`
				})
			},
			// 输入框
			toInput() {
				const that = this;
				if (!that.searchInfo?.name) that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 处理时间
			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 == 'status') res = that.statusList.find(i => i.value == item)
				else if (model == 'type') res = that.typeList.find(i => i.value == item)
				if (res) return res.label
				else return '暂无'
			},
			async searchOther() {
				const that = this;
				let res;
				//状态
				res = await that.$api('/dictData', 'GET', {
					code: 'examStatus',
					is_use: '0'
				})
				if (res.errcode == '0') {
					const menu = res.data.map((item) => {
						if (item.value != '-2') {
							return {
								title: item.label,
								active: item.value
							}
						}
					})
					that.$set(that.tabs, `menu`, menu)
				}
				// 查询状态
				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)
			},
			// 图片处理
			getUrl(e) {
				const that = this;
				if (e && e.length > 0) return that.$config.serverFile + e[0].url
				else return '/static/match.png'
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vw;
			background-color: var(--mainColor);

			.one_1 {
				padding: 0 2vw;
				width: 75vw;

				input {
					padding: 2vw;
					background-color: var(--f1Color);
					font-size: var(--font14Size);
					border-radius: 5px;
				}
			}

			.one_2 {
				.button {
					background-color: var(--f3CColor);
					color: var(--mainColor);
				}
			}
		}

		.two {
			background-color: var(--f9Color);
			margin: 2vw 0 0 0;

			.tabsList {
				position: relative;
				width: 100vw;
				height: 81vh;

				.list {
					background-color: var(--mainColor);
					padding: 2vw;
					margin: 2vw 2vw 0 2vw;
					border-radius: 10px;

					.list_1 {
						.image {
							height: 50vw;
							width: 100%;
							border-radius: 5px;
						}
					}

					.list_2 {
						margin: 1vw 0;
						font-weight: bold;
						font-size: var(--font16Size);
					}

					.list_3 {
						display: flex;
						align-items: center;
						justify-content: space-between;
						font-size: var(--font14Size);

						.list_left {
							display: flex;
							align-items: center;
							color: var(--f69Color);

							span {
								margin: 0 0 0 0.5vw;
							}

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

						.list_right {
							border-radius: 3px;
							padding: 0 1vw;
							color: var(--mainColor);
						}

						.active_1 {
							background: linear-gradient(to right, #4caf50, #a8f3b2);

						}

						.active_2 {
							background: linear-gradient(to right, #2196f3, #a2ccec);

						}

						.active_3 {
							background: linear-gradient(to right, #FF5722, #FF9800);

						}

						.active_4 {
							background: linear-gradient(to right, #949698, #c6c9cc);
						}
					}

					.list_4 {
						display: flex;
						justify-content: center;
						margin: 2vw 0 0 0;
						text-align: center;

						.warning {
							font-size: 16px;
							width: 140px;
							height: 35px;
							line-height: 35px;
							background: linear-gradient(to right, #39D5FF, #868bff);
						}

						.danger {
							background: var(--fF0Color);
						}

						button {
							margin: 0 2vw 0 0;
						}
					}
				}
			}
		}
	}

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

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		width: 100%;
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}
</style>