<template>
	<view class="rank">
		<view class="rank_top">
			<view class="rank_bg">
				<view class="rank_title">奖学金排行榜</view>
				<view class="flexS rank_btn">
					<!-- <view @click="skipPage(0)">奖学金商城</view> -->
					<view @click="skipPage(1)">奖学金明细</view>
				</view>
			</view>
			<view class="person flexCC">
				<image :src="person.store.img" mode="" class="avatar" v-if="person.store"></image>
				<view class="person_title" v-if="person.store">{{ person.store.name }}</view>
				<view class="flexA person_rank">
					<view class="person_intr">
						<view>{{current==0?person.sort:person.yes_sort }}</view>
						<text>{{current==0?'个人总排名':'个人昨日排名'}}</text>
					</view>
					<view class="person_intr">
						<view v-if="current==0">{{person.integral?person.integral:0}}</view>
						<view v-else>{{person.yes_integral?person.yes_integral:0}}</view>
						<text>奖学金</text>
					</view>
				</view>
			</view>
			<view class="tabs">
				<u-tabs :list="list" :is-scroll="false" :current="current" @change="change" active-color="#FB231F">
				</u-tabs>
			</view>
			<view class="rank_intr">
				<view style="width: 15%;">排名</view>
				<view style="width: 15%;">头像</view>
				<view style="width: 50%;">昵称</view>
				<view>奖学金</view>
			</view>
		</view>
		<view class="rank_box">
			<scroll-view scroll-y="true" @scrolltolower="getMore">
				<view class="scroll_con">
					<view v-for="(item, idx) in rankList" :key="idx" class="flexS">
						<view style="width: 15%;text-align: center;" v-if="idx == 0 || idx == 1 || idx == 2">
							<image src="../../static/gold.png" v-if="idx == 0" class="rank_icon"></image>
							<image src="../../static/silver.png" v-if="idx == 1" class="rank_icon"></image>
							<image src="../../static/copper.png" v-if="idx == 2" class="rank_icon"></image>
						</view>
						<text style="width: 15%;font-size:32rpx;"
							v-if="idx != 0 && idx != 1 && idx != 2">{{ idx + 1 }}</text>
						<view style="width: 15%;text-align: center;">
							<image :src="item.img" class="user_avatar"></image>
						</view>
						<text style="width: 50%;font-size:32rpx;">{{ item.name | getName(10) }}</text>
						<text style="font-size:36rpx;color:#FB231F;">{{ item.integral }}</text>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		integralRank,
		getIntegral
	} from '@/apis/shop.js';
	export default {
		data() {
			return {
				rankList: [],
				list: [{
					name: '总榜单'
				}, {
					name: '昨日榜单'
				}],
				current: 0,
				params: {
					page_index: 1,
					page_size: 15,
					type: 'all'
				},
				totalPage: 0,
				person: '', //个人信息
				exc_gifts: 0,
			};
		},
		onShow() {
			this.getIntegral();
			this.getRank();
		},
		methods: {
			//切换总榜单昨日榜单
			change(index) {
				this.current = index;
				this.params.type = index == 0 ? 'all' : 'yesterday';
				this.params.page_index = 1;
				this.rankList = [];
				this.getRank();
				this.goTop();
			},

			goTop() {
				// 一键回到顶部
				if (uni.pageScrollTo) {
					uni.pageScrollTo({
						scrollTop: 0
					});
				}
			},
			//获取个人排行
			getIntegral() {
				getIntegral().then(res => {
					if (res.code == 200) {
						this.person = res.data;
						this.exc_gifts = res.data.exc_gifts;
					} else {
						uni.showModal({
							content: res.msg || '获取数据失败',
							showCancel: false
						});
					}
				});
			},
			//跳转页面
			skipPage(type) {
				// if (type == 0) {
				// 	uni.showToast({
				// 		title: '暂未开启，敬请期待~',
				// 		icon: 'none'
				// 	});
				// 	return false;
				// }
				if (this.exc_gifts == 0 && type == 0) {
					uni.showToast({
						title: '暂未开启，敬请期待~',
						icon: 'none'
					});
					return false;
				}
				let url = '';
				url = type == 0 ? '../integral-store/integral-store?integral=' + this.person.store.integral :
					'../integral-detail/integral-detail';
				uni.navigateTo({
					url
				});
			},
			//获取排行榜
			getRank(isMore) {
				uni.showLoading({
					title: '加载中...'
				});
				let params = this.params;
				integralRank(params)
					.then(res => {
						if (res.code == 200) {
							this.rankList = isMore ? this.rankList.concat(res.data.list) : res.data.list;
							this.totalPage = Math.ceil(res.data.total / this.params.page_size);
						} else {
							uni.showModal({
								content: res.msg || '获取排名失败',
								showCancel: false
							});
						}
						uni.hideLoading();
					})
					.catch(err => {
						uni.hideLoading();
					});
			},
			getMore() {
				if (this.params.page_index >= this.totalPage) {
					uni.showToast({
						title: '没有更多啦~',
						icon: 'none'
					});
					return false;
				}
				this.params.page_index++;
				this.getRank(true);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.rank {
		width: 100vw;
		height: 100vh;
		background: #f9f9fb;
		overflow: hidden;
		display: flex;
		flex-direction: column;

		.rank_top {
			width: 100%;

			.rank_bg {
				width: 100%;
				height: 458rpx;
				background: url(../../static/integral_bg.png) no-repeat;
				background-size: 100% 100%;

				.rank_title {
					font-size: 72rpx;
					font-weight: bold;
					color: #fff;
					padding: 30rpx 0 20rpx 30rpx;
				}

				.rank_btn {
					width: 100%;
					margin-left: 30rpx;

					view {
						width: 200rpx;
						height: 64rpx;
						color: #fff;
						font-size: 32rpx;
						border-radius: 32rpx;
						display: flex;
						justify-content: center;
						align-items: center;
					}

					view:first-child {
						background: linear-gradient(93deg, #ffa600 0%, #fcbc05 100%);
						margin-right: 20rpx;
					}

					view:last-child {
						background: rgba(255, 255, 255, 0.3);
					}
				}
			}

			.person {
				width: 690rpx;
				margin: -130rpx auto 0;
				height: 294rpx;
				background: #fff;
				border-radius: 24rpx;

				.person_title {
					font-size: 32rpx;
					padding: 20rpx 0 30rpx;
					font-weight: bold;
				}

				.avatar {
					width: 134rpx;
					height: 134rpx;
					margin-top: -125rpx;
					border-radius: 50%;
					border: 4rpx solid #fff;
				}

				.person_rank {
					width: 100%;

					.person_intr {
						text-align: center;

						view {
							font-size: 52rpx;
							font-weight: bold;
						}

						text {
							font-size: 28rpx;
							color: #999;
						}


					}
				}
			}

			.tabs {
				border-bottom: 2rpx solid #eee;
				margin-top: 30rpx;
			}

			.rank_intr {
				width: 100%;
				background: #fff;
				height: 92rpx;
				line-height: 92rpx;

				display: flex;
				align-items: center;

				view {
					text-align: center;
					font-size: 32rpx;
				}
			}
		}

		.rank_box {
			// flex: 1;
			height: 45vh;
			background: #f00;
			width: 100%;
			background: #fff;

			scroll-view {
				width: 100%;
				height: 100%;
			}

			.scroll_con {
				padding: 10rpx 0 100rpx 0;

				.rank_icon {
					width: 60rpx;
					height: 47rpx;
				}

				.user_avatar {
					width: 68rpx;
					height: 68rpx;
					border-radius: 50%;
					flex-shrink: 0;
				}

				>view {
					width: 100%;
					margin-top: 10rpx;

					text {
						text-align: center;
					}
				}
			}
		}
	}
</style>
