<template>
	<view class="container main">
		<view class="one" v-if="total>0">
			<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">
						<view class="value">
							<view class="title">编号：</view>
							<view class="label"> <text :user-select="true">{{item.no||'暂无'}}</text> </view>
						</view>
						<view class="value">
							<view class="title">赛事名称：</view>
							<view class="label">{{item.match_name||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">报名时间：</view>
							<view class="label">{{item.time||'暂无'}}</view>
						</view>
						<view class="value" v-if="item.start_time">
							<view class="title">初审时间：</view>
							<view class="label">{{item.start_time||'暂无'}}</view>
						</view>
						<view class="value" v-if="item.score">
							<view class="title">初审分数：</view>
							<view class="label">{{item.score||'暂无'}}</view>
						</view>
						<view class="value" v-if="item.final_start_time">
							<view class="title">决赛时间：</view>
							<view class="label">{{item.final_start_time||'暂无'}}</view>
						</view>
						<view class="value" v-if="item.final_score">
							<view class="title">决赛分数：</view>
							<view class="label">{{item.final_score||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">是否进入决赛：</view>
							<view class="label">{{getDict(item,'final_confirm')||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">流程状态：</view>
							<view class="label">{{getDict(item,'ext_status')||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">状态：</view>
							<view class="label">{{getDict(item,'status')||'暂无'}}</view>
						</view>
						<view class="bottom">
							<button v-if="item.ext_status=='0'" class="button button_2" type="default" size="mini"
								@tap.stop="toEdit(item)">修改信息</button>
							<button v-if="item.ext_status=='5'&&item.final_confirm=='1'" class="button button_2"
								type="primary" size="mini" @tap.stop="toFinals(item)">决赛时间确认</button>
						</view>
					</view>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottom_title||'到底了！'}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<o-empty v-else />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				config: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 禁止滚动穿透
				show: false,
				statusList: [{
						value: '0',
						label: '待审核'
					}, {
						value: '1',
						label: '已通过'
					},
					{
						value: '-1',
						label: '已退回'
					}
				],
				isList: [{
					value: '0',
					label: '是'
				}, {
					value: '1',
					label: '否'
				}],
				extList: []
			}
		},
		onLoad: async function(e) {
			const that = this;
			await that.searchToken();
			await that.searchOther();
			await that.searchConfig();
		},
		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: {
			// 禁止滚动穿透
			change(e) {
				const that = this;
				that.show = e.show
			},
			// 用户信息
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					}
				} catch (e) {}
			},
			async searchOther() {
				const that = this;
				let res;
				// 查询证件类型
				res = await that.$api(`/dictData`, 'GET', {
					code: 'extStatus',
					is_use: '0',
				})
				if (res.errcode == '0') {
					that.$set(that, `extList`, res.data)
				}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {}
			},
			// 查询
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
					user_id: that.user.id
				}
				const res = await that.$api(`/matchReg`, 'GET', info)
				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'
					});
				}
			},
			// 处理字典表
			getDict(item, model) {
				if (item) {
					const that = this;
					let res;
					if (model == 'status') res = that.statusList.find(i => i.value == item.status)
					if (model == 'ext_status') res = that.extList.find(i => i.value == item.ext_status)
					if (model == 'final_confirm') res = that.isList.find(i => i.value == item.final_confirm)
					if (res) return res.label
					else return '暂无'
				}
			},
			async toEdit(item) {
				uni.navigateTo({
					url: `/pagesMy/preliminary/edit?id=${item.id||item._id}`
				})
			},
			async toFinals(item) {
				uni.navigateTo({
					url: `/pagesMy/preliminary/finals?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;
				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 {
			position: relative;
			flex-grow: 1;
			background-color: var(--f1Color);

			.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 {
						min-width: 70px;
						font-size: var(--font14Size);
						font-weight: bold;
					}

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

				.bottom {
					margin: 2vw 0 0 0;
					text-align: center;

					.button {
						color: var(--mainColor);
						font-size: var(--font14Size);
						border-radius: 2vw;
					}

					.button_1 {
						margin: 0 2vw 0 0;
						background-color: var(--f3CColor);
					}

					.button_2 {
						background-color: var(--fF0Color);
					}
				}
			}
		}
	}

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