<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="list" v-for="(item, index) in list" :key="index" @tap="toInfo(item)">
						<view class="list_1">
							<view class="left">
								<image class="image" mode="aspectFill" :src="item.logo||config.logoUrl"></image>
							</view>
							<view class="right">
								<view class="name">
									{{item.name||'暂无名称'}}
									<text class="label"
										:class="[item.userId==user.id?'label_1':'label_2']">{{item.userId==user.id?'队长':'队员'}}</text>
								</view>
								<view class="other">
									{{item.activity||'暂无活动'}}
								</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 { ref, toRefs, getCurrentInstance } from 'vue';
	//该依赖已内置不需要单独安装
	import { onShow } from "@dcloudio/uni-app";
	// 请求接口
	const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
	interface PropsItem {
		id ?: number,
		name ?: string,
		activity ?: string,
		logo ?: string,
		label ?: string,
		user_type ?: string,
	};
	// 用户信息
	const user = ref({});
	// 查询
	const searchInfo = ref({});
	// 列表
	const list = ref<PropsItem[]>([{ id: 1, name: '测试球队' }, { id: 2, name: '肝帝集团队' }]);
	// 分页
	const pageNum = ref(1);
	const pageSize = ref(10);
	const total = ref(0);
	// 数据是否触底
	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');
	};
	// 查询列表
	const search = async () => {
		const info = {
			pageNum: pageNum.value,
			pageSize: pageSize.value,
			userId: user.value.id,
			isUse: 'Y'
		}
		const res = await $api('team/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
		else searchInfo.value = {}
		clearPage();
		search();
	};
	// 球队详情
	const toInfo = (item : any) => {
		uni.navigateTo({
			url: `/pagesHome/team/info?id=${item._id || item.id}`,
		})
	};
	// 分页
	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
		page.value = 0
	};
</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;

			.list {
				border-bottom: 1px solid var(--f5Color);
				padding: 3vw;

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

					.left {
						width: 15vw;

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

					.right {
						width: 75vw;
						margin: 0 0 0 2vw;

						.name {
							font-size: var(--font16Size);

							.label {
								padding: 3px 6px;
								font-size: 10px;
								color: var(--mainColor);
								border-radius: 2px;
							}

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

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

						.other {
							margin: 1vw 0 0 0;
							font-size: var(--font14Size);
							color: var(--f85Color);
						}
					}
				}
			}
		}
	}

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