<template>
	<view class="container 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">
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-view" @scrolltolower="toPage"
				@scroll="toScroll">
				<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="item.file&&item.file.length>0?item.file[0].url:'/static/match.png'"></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-zuobiao"></text>
								<span>{{item.address||'在线活动'}}</span>
							</view>
							<view class="list_right"
								:class="[item.match_status=='0'?'active_1':item.match_status=='1'?'active_2':'active_3']">
								{{getDict(item.match_status,'status')}}
							</view>
						</view>
					</view>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottom_title||'到底了！'}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="fixd" @tap="goTop" v-if="showDiv">
			<text class="t-icon t-icon-fanhuidingbu"></text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				config: {},
				searchInfo: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				oldScrollTop: 0,
				showDiv: false,
				// 字典表
				statusList: []
			}
		},
		onLoad: async function(e) {
			const that = this;
			await that.searchToken();
			await 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);
					}
				} 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;
				let info = {
					skip: that.skip,
					limit: that.limit,
					is_use: '0',
					status: '1'
				}
				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,
						icon: 'none'
					});
				}
			},
			// 输入框
			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 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)
				if (res) return res.label
				else return '暂无'
			},
			// 回到顶部
			goTop(e) {
				const that = this;
				that.scrollTop = that.oldScrollTop
				that.$nextTick(function() {
					that.scrollTop = 0
				});
			},
			// 详情
			toView(item) {
				uni.navigateTo({
					url: `/pagesHome/match/index?id=${item.id||item._id}`
				})
			},
			// 分页
			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, `oldScrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
				// 检查滚动位置是否达到div显示的条件
				if (e.detail.scrollTop >= 300 && !that.showDiv) {
					that.showDiv = true;
				} else if (e.detail.scrollTop < 300 && that.showDiv) {
					that.showDiv = false;
				}
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 5)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.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 {
			position: relative;
			flex-grow: 1;

			.list {
				padding: 2vw 1vw;
				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, #949698, #c6c9cc);

					}
				}
			}
		}

		.fixd {
			position: fixed;
			right: 2vw;
			bottom: 5vw;

			.t-icon {
				width: 40px !important;
				height: 40px !important;
			}
		}
	}

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