<template>
	<view class="main">
		<view class="first">
			<input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索活动名称">
		</view>
		<view class="second">
			<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
				<view class="list-scroll-view">
					<view class="other">
						<view class="other_1">
							<view class="drop" v-for="(item, index) in list" :key="index">
								.
								<view class="line"></view>
							</view>
						</view>
						<view class="other_2">
							<view class="list" v-for="(item, index) in list" :key="index" @tap="toInfo(item)">
								<view class="list_1">
									<view class="date">
										{{moment(item.startTime).format('MM-DD')||'暂无日期'}}
										{{getDayOfWeek(item.startTime)}}
									</view>
									<view class="status"
										:class="[item.status=='0'?'status0':item.status=='1'?'status1':item.status=='2'?'status2':'status3']">
										{{getStatus(item.status)}}
									</view>
								</view>
								<view class="list_2">
									<view class="name" v-if="item.type=='0'">{{item?.matchInfo?.title||'友谊赛'}}</view>
									<view class="name" v-else>{{item?.leagueInfo?.title||'联赛'}}</view>
									<view class="score">
										<view class="red">
											<view class="red_name">{{item?.redInfo?.name||'暂无红方名称'}}</view>
											<view class="red_image">
												<image class="image" mode="aspectFill"
													:src="item?.redInfo?.logo||config.logoUrl">
												</image>
											</view>
											<view class="red_score">{{item.redScore||0}}</view>
										</view>
										<view class="center">:</view>
										<view class="blue">
											<view class="blue_score">{{item.blueScore||0}}</view>
											<view class="blue_image">
												<image class="image" mode="aspectFill"
													:src="item?.blueInfo?.logo||config.logoUrl">
												</image>
											</view>
											<view class="blue_name">{{item?.blueInfo?.name||'暂无蓝方名称'}}</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottomTitle||'到底了！'}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import moment from 'moment';
	import { ref, toRefs, getCurrentInstance } from 'vue';
	//该依赖已内置不需要单独安装
	import { onShow } from "@dcloudio/uni-app";
	// 请求接口
	const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
	interface PropsItem {
		id ?: number,
		name ?: string,
		date ?: string,
		red_name ?: string,
		red_score ?: number,
		red_logo ?: string,
		blue_name ?: string,
		blue_score ?: number,
		blue_logo ?: string,
		status ?: string,
		status_name ?: string,
	};
	// 用户信息
	const user = ref({});
	// 查询
	const searchInfo = ref({});
	// 列表
	const list = ref<PropsItem[]>([{ id: 1, name: '测试球队' }, { id: 1, name: '肝帝集团队' }]);
	// 分页
	const pageNum = ref(1);
	const pageSize = ref(10);
	const total = ref(0);
	const statusList = ref([]);
	// 数据是否触底
	const scrollTop = ref(0);
	const is_bottom = ref(false);
	const props = defineProps({
		config: { type: Object, default: () => { } },
	});
	const { config } = toRefs(props);
	onShow(async () => {
		await searchUser();
		await search();
	})
	// 用户信息
	const searchUser = async () => {
		user.value = uni.getStorageSync('user');
		let res;
		// 比赛状态
		res = await $api(`dict/data/list`, 'GET', { dictType: 'sys_game_status', status: '0' });
		if (res.code === 200 && res.total > 0) statusList.value = res.rows
	};
	// 查询列表
	const search = async () => {
		const info = {
			pageNum: pageNum.value,
			pageSize: pageSize.value,
			userId: user.value.id
		}
		const res = await $api('game/list', 'GET', {
			...info,
			...searchInfo.value
		});
		if (res.code === 200) {
			list.value = res.rows
			total.value = res.total
		} else {
			uni.showToast({
				title: res.msg || '',
				icon: 'error',
			});
		}
	};
	//查询
	const toInput = (e : any) => {
		if (searchInfo.value.name) searchInfo.value.name = e.detail.value
		searchInfo.value = {}
		clearPage();
		search();
	};
	// 活动详情
	const toInfo = (item : any) => {
		uni.navigateTo({
			url: `/pagesHome/activity/info?id=${item._id || item.id}&name=${item.name || ''}`,
		})
	};
	// 日期换算星期几
	const getDayOfWeek = (dateString) => {
		if (dateString) {
			const date = new Date(dateString);
			const dayOfWeek = date.getDay();
			const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
			return weekdays[dayOfWeek];
		} else return '暂无日期';
	};
	// 换算状态
	const getStatus = (value) => {
		if (value) {
			const data = statusList.value.find(i => i.dictValue == value)
			if (data) return data.dictLabel
		} else return '暂无状态';
	};
	// 分页
	const toPage = () => {
		if (total.value > list.value.length) {
			uni.showLoading({
				title: '加载中',
				mask: true
			})
			pageNum.value = pageNum.value + 1
			pageSize.value = pageNum.value * 10;
			search();
			uni.hideLoading();
		} else is_bottom.value = true
	};
	const toScroll = (e : any) => {
		let up = scrollTop.value;
		scrollTop.value = e.detail.scrollTop
		let num = Math.sign(up - e.detail.scrollTop);
		if (num == 1) is_bottom.value = false
	};
	// 清空数据
	const clearPage = () => {
		list.value = []
		pageNum.value = 1
		pageSize.value = 10
	};
</script>
<style lang="scss" scoped>
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 66vh;

		.first {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vw;
			border-bottom: 1px solid var(--f5Color);

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

		.second {
			position: relative;
			flex-grow: 1;

			.other {
				display: flex;

				.other_1 {
					padding: 2vw 2vw 2vw 5vw;
					font-size: 20px;
					font-weight: bold;

					.line {
						height: 18vh;
						margin: 0 0 0 2px;
						border-left: 1px dashed var(--f99Color);
					}

				}

				.other_2 {
					width: 90%;

					.list {
						.list_1 {
							display: flex;
							align-items: center;
							padding: 4vw 2vw 2vw 2vw;

							.date {
								font-size: var(--font16Size);
								font-weight: bold;
							}

							.status {
								font-size: 10px;
								margin: 0 1vw;
								padding: 1px 5px;
							}

							.status0 {
								color: var(--mainColor);
								background-color: var(--f35BColor);
							}

							.status1 {
								color: var(--mainColor);
								background-color: var(--fFFColor);
							}

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

							.status3 {
								color: var(--f99Color);
								background-color: var(--f9Color);
							}
						}

						.list_2 {
							border: 1px solid var(--f5Color);
							padding: 2vw;
							border-radius: 2px;

							.name {
								font-size: var(--font16Size);
								font-weight: bold;
								margin: 2vw;
							}

							.score {
								display: flex;
								align-items: center;
								justify-content: center;
								font-size: var(--font14Size);
								background-color: var(--f9Color);
								border-radius: 5vw;
								padding: 1vw 0;

								.red {
									display: flex;
									align-items: center;
									margin: 0 1vw 0 0;

									.red_image {
										margin: 0 1vw;

										.image {
											width: 8vw;
											height: 8vw;
											border-radius: 8vw;
										}
									}

									.red_score {
										font-size: var(--font16Size);
										color: var(--fF0Color);
									}
								}

								.center {
									font-size: var(--font16Size);
									color: var(--fF0Color);
								}

								.blue {
									display: flex;
									align-items: center;
									margin: 0 0 0 1vw;

									.blue_image {
										margin: 0 1vw;

										.image {
											width: 8vw;
											height: 8vw;
											border-radius: 8vw;
										}
									}

									.blue_score {
										font-size: var(--font16Size);
										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>