<template>
	<view class="container main">
		<view class="one">
			<view class="one_1">
				<input type="text" v-model="searchInfo.name" placeholder="请输入名称" @blur="toInput">
			</view>
			<view class="one_2">
				<button size="mini" class="button" type="primary" @click="toAdd">添加</button>
			</view>
		</view>
		<view class="two">
			<tabs :tabs="tabs" @tabsChange="tabsChange">
				<view class="tabsList">
					<scroll-view :scroll-top="scrollTop" 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">
								<view class="value">
									<view class="title">流程名称：</view>
									<view class="label">{{item.matchPath_name||'暂无'}}</view>
								</view>
								<view class="value">
									<view class="title">选手名称：</view>
									<view class="label">{{item.sign_name||'暂无'}}</view>
								</view>
								<view class="value">
									<view class="title">分数：</view>
									<view class="label">{{item.score||'暂无'}}</view>
								</view>
								<view class="value">
									<view class="title">时间：</view>
									<view class="label">{{item.time||'暂无'}}</view>
								</view>
								<view class="bottom">
									<button class="button button_1" type="default" size="mini"
										@tap.stop="toEdit(item)">修改</button>
									<button class="button button_2" type="default" size="mini"
										@tap.stop="toDelete(item)">删除</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 {
				id: '',
				user: {},
				tabs: {
					active: '',
					bgColor: '#ffffff',
					menu: []
				},
				config: {},
				searchInfo: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			await that.searchToken();
			await that.searchConfig();
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.searchOther();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.searchOther();
			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) {}
			},
			async searchOther() {
				const that = this;
				const res = await that.$api(`/matchPath`, 'GET', {
					match: that.id
				})
				if (res.errcode == '0' && res.total > 0) {
					that.$set(that.tabs, `active`, res.data[0].id)
					const menu = []
					for (let val of res.data) {
						menu.push({
							title: val.name,
							active: val.id
						})
					}
					that.$set(that.tabs, `menu`, menu)
				}
			},
			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,
					match: that.id,
					matchPath: that.tabs.active
				}
				const res = await that.$api(`/score/list`, '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();
			},
			// 添加
			toAdd() {
				const that = this;
				uni.navigateTo({
					url: `/pagesMy/match/add?match=${that.id}&matchPath=${that.tabs.active}`
				})
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.clearPage();
				that.search()
			},
			// 修改
			toEdit(item) {
				const that = this;
				uni.navigateTo({
					url: `/pagesMy/match/add?id=${item.id || item._id}&match=${that.id}`
				})
			},
			// 删除
			async toDelete(item) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除该打分吗?',
					success: async function(res) {
						if (res.confirm) {
							const arr = await that.$api(`/score/${item.id}`, 'DELETE');
							if (arr.errcode == '0') {
								uni.showToast({
									title: '删除信息成功',
									icon: 'none'
								})
								await that.clearPage()
								await that.search()
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 分页
			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 {
			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(--f1Color);

			.tabsList {
				position: relative;
				flex-grow: 1;
				width: 100vw;
				height: 82vh;
				margin: 10px 0 0 0;

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