<template>
	<view class="ranking">
		<custom-nav :title="pageTitle"></custom-nav>
		<view class="content">
			<view class="day-title">
				<text @tap="switchDay(0)" :class="day === 0 ? 'active' : ''">总榜单</text>
				<text @tap="switchDay(1)" :class="day === 1 ? 'active' : ''">昨日榜单</text>
			</view>
			<view class="title-bar">
				<view class="title-drog" :style="{ left: day ? '50%' : '0%' }"></view>
			</view>
			<image :src="userServerInfo.banner_1" mode="scaleToFill"></image>
			<view class="type-title">
				<text v-for="(item, index) in typeList" :key="index" @tap="switchType(index)" :class="type === index ? 'active' : ''">{{ item }}</text>
			</view>
			<view class="list">
				<scroll-view v-if="scrollview" show-scrollbar="true" :scroll-top="scrollTop" scroll-with-animation="true" scroll-y :style="{ height: scrollViewHeight + 'px' }" @scrolltolower="scrolltolower">
					<ranking-item v-for="(item, index) in list" :key="item.id" :index="index" :item="item" :rank="user.rank" :type="type" :bg="(user.id === item.id && userServerInfo.type !== 3 && day === 0 && type + 1 === userServerInfo.type)"></ranking-item>
					<custom-reach-bottom v-if="nomore || list.length" :nomore="nomore"></custom-reach-bottom>
				</scroll-view>
			</view>
			<view v-if="showFiexed" class="fiexed">
				<view class="left">
					<text class="rank">{{ user.rank }}</text>
					<text>我的排名</text>
				</view>
				<view class="center">
					<image :src="userWeixinInfo.avatarUrl" mode="scaleToFill"></image>
					<image v-if="userServerInfo.score >= 600" class="hat" src="../../static/icon/hat.png" mode="scaleToFill"></image>
					<text>{{ userServerInfo.name }}</text>
				</view>
				<view class="right" v-if="userServerInfo.type === 1">
					<view class="score">
						<image src="../../static/icon/level.png" mode="scaleToFill"></image>
						<text>{{ user.score }}</text>
					</view>
					<view class="float" v-if="user.float > 0">
						<image src="../../static/icon/userup.png" mode="scaleToFill"></image>
						<text>{{ user.float }}</text>
					</view>
				</view>
				<view class="right" v-else>
					<button open-type="share" >分享给好友</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import rankingItem from '../../components/ranking-item.vue'
	import { 
		api_todayPerson, 
		api_todayCrown, 
		api_todayTeam, 
		api_todayWar, 
		api_yesterdayPerson, 
		api_yesterdayCrown, 
		api_yesterdayTeam, 
		api_yesterdayWar 
	} from '../../api.js'
	export default {
		onShareAppMessage: function (res) {
			uni.showLoading({ title: '加载中', mask: true })  //显示loading
			if (res.from === 'button') {
				return {
					title: '我的排名',
					path: `/pages/crownshare/crownshare?top=${JSON.stringify(this.list.slice(0, 3))}&list=${JSON.stringify(this.shareList)}`
				}
			}
			return {
				title: '大卫博士商学院卖货争霸',
				path: '/pages/index/index'
			}
		},
		components: {
			rankingItem,
		},
		data() {
			return {
				pageTitle: '排行榜',
				typeList: [],  //个人/代理公司/团队/战区 导航栏显示内容
				day: 0,  //今日/昨日
				type: 0,  //个人/代理公司/团队/战区
				page: 1,  //
				list: [],
				user: {}, //用户当前排名和浮动情况
				scrollViewHeight: 0,  //scrollview 高
				requesting: false,  //是否正在请求
				nomore: false,  //没得更多了
				apis: [  //用二维数组来表示 今日昨日一共八个 api
					[api_todayPerson, api_todayCrown, api_todayTeam, api_todayWar],
					[api_yesterdayPerson, api_yesterdayCrown, api_yesterdayTeam, api_yesterdayWar]
				],
				scrollview: true, // 显示隐藏 scrollvew 解决切换后 scrolltop 不回到顶部的BUG
				shareList: [],  //代理公司分享列表
				scrollTop: 0 //设置竖向滚动条位置
			};
		},
		computed: {
			userWeixinInfo () {  //用户微信信息
				return this.$store.state.userWeixinInfo
			},
			userServerInfo () {  //用户信息
				return this.$store.state.userServerInfo
			},
			showFiexed () {  //是否在屏幕下方显示显示个人信息
			return this.userServerInfo.type === (this.type + 1) && this.userServerInfo.type !== 3 && this.day === 0
			}
		},
		methods: {
			switchDay (day) {  //切换天
				this.day = day
				this.switched()
			},
			switchType (index) {  //切换类型
				this.type = index
				this.switched()
			},
			switched () {  //切换数据
				uni.showLoading({ title: '加载中', mask: true })  //显示loading
				this.scrollViewHeight = 0  //将 scrollview 变为高变为零
				this.scrollview = false  // 隐藏 scrollvew 解决切换后 scrolltop 不回到顶部的BUG
				this.page = 1  //恢复页码
				this.nomore = false  //恢复没得更多了
				this.$ajax.get(`${this.apis[this.day][this.type]}?page=${this.page}&season=${this.userServerInfo.season}`).then(([ , { data: res }]) => {
					this.scrollview = true  // 显示 scrollvew 解决切换后 scrolltop 不回到顶部的BUG
					res.data.list.length < 20 ? this.nomore = true : ''  //如果返回的数据长度小于20，说明没得更多了
					res.data.per_data ? this.user = res.data.per_data : ''  //是否有用户排名信息传过来，有就赋给 user
					this.list = this.list = res.data.list
					this.page ++  //请求成功，页数增加 1
					this.$scrollViewHeight('.list')  //设置页面内 scroll view 的高度
					if (this.userServerInfo.type === 2 && this.type === 1 && this.day === 0) {  //如果是代理公司点击了代理公司榜
						this.makeCrownShareList()
					} else {
						this.$hideLoading()  //异步操作结束，停止 loading
					}
				})
			},
			scrolltolower () {  //下拉 scroll view 触底函数
				if (this.userServerInfo.type === 1 && this.type === 1) {  //普通代理查看代理公司榜时不会触发
					return
				}
				if (!this.requesting && !this.nomore) {  //防抖
					this.requesting = true
					this.$ajax.get(`${this.apis[this.day][this.type]}?page=${this.page}&season=${this.userServerInfo.season}`).then(([ , { data: res }]) => {
						res.data.list.length < 20 ? this.nomore = true : ''  //如果返回的数据长度小于20，说明没得更多了
						this.requesting = false
						this.page ++ 
						this.list = [...this.list, ...res.data.list]
					}, () => {
						this.requesting = false
					})
				}
			},
			async makeCrownShareList () {  //生成代理公司分享页面的列表数据
				const user = Object.assign({  //生成用户信息
					name: this.userServerInfo.name,
					avatar: this.userWeixinInfo.avatarUrl,
					self: true
				}, this.user)
				const rank = start => {
					this.shareList.forEach((e, i) => {
						e.rank = i + start
					})
				}
				if (this.user.rank < 5) {  //前四名的处理方式
					this.shareList = this.list.slice(0, 4)
					this.shareList.splice(this.user.rank - 1, 1, user)
					rank(1)  //为分享数组里面的对象添加排名
					this.$hideLoading()  //异步操作结束，停止 loading
				} else if (this.user.rank > 4 && this.user.rank < 20) {  //5 - 19 名的处理方式
					this.shareList = this.list.slice(this.user.rank - 3, this.user.rank + 1)
					this.shareList.splice(2, 1, user)
					rank(this.user.rank - 2)  //为分享数组里面的对象添加排名
					this.$hideLoading()  //异步操作结束，停止 loading
				} else if (this.user.rank === 20) {  //第 20 名的处理方式
					this.shareList = this.list.slice(17, 19)
					this.$ajax.get(`${api_todayCrown}?page=2&season=${this.userServerInfo.season}`).then(([ , { data: res }]) => {
						this.shareList.push(user)
						this.shareList.push(res.data.list[0])
						rank(this.user.rank - 2)  //为分享数组里面的对象添加排名
						this.$hideLoading()  //异步操作结束，停止 loading
					})
				} else {
					if (this.user.rank) {
						const page = Math.floor(this.user.rank / 20) + 1  //自己排名所处的页数（第几页）
						const index = this.user.rank % 20  //自己在自己那页的下标（第几个）
						const [ , { data: res1 }] = await this.$ajax.get(`${api_todayCrown}?page=${page - 1}&season=${this.userServerInfo.season}`)
						const [ , { data: res2 }] = await this.$ajax.get(`${api_todayCrown}?page=${page}&season=${this.userServerInfo.season}`)
						const [ , { data: res3 }] = await this.$ajax.get(`${api_todayCrown}?page=${page + 1}&season=${this.userServerInfo.season}`)
						const totalList = [...res1.data.list, ...res2.data.list, ...res3.data.list, ]
						this.shareList = totalList.slice(20 + index - 3, 20 + index + 1)
						this.shareList.splice(2, 1, user)
						rank(this.user.rank - 2)  //为分享数组里面的对象添加排名
						this.$hideLoading()  //异步操作结束，停止 loading
					}
				}
			}
		},
		created() {
			uni.showLoading({ title: '加载中', mask: true })  //显示loading 
			if (this.userServerInfo.type == 1) {  //根据用户类型生成要展示的数据列表
				this.typeList = ['个人榜', '战区榜']
				this.apis = [  //用二维数组来表示 今日昨日一共八个 api
					[api_todayPerson, api_todayWar],
					[api_yesterdayPerson, api_yesterdayWar]
				]
			} else if (this.userServerInfo.type == 2) {
				this.typeList = ['个人榜', '代理公司榜', '团队榜']
			} else if (this.userServerInfo.type == 3) {
				this.typeList = ['个人榜', '代理公司榜', '团队榜', '战区榜']
			}
			this.$scrollViewHeight('.list')  //设置页面内 scroll view 的高度
			this.$ajax.get(`${this.apis[this.day][this.type]}?page=${this.page}&season=${this.userServerInfo.season}`).then(([ , { data: res }]) => {
				res.data.list.length < 20 ? this.nomore = true : ''  //如果返回的数据长度小于20，说明没得更多了
				res.data.per_data ? this.user = res.data.per_data : ''  //是否有用户排名信息传过来，有就赋给 user
				this.list = this.list = res.data.list
				this.$hideLoading()  //异步操作结束，停止 loading
				this.page ++
			})
		},
		watch: {
			list (n) {
				if (n.length < 20) {
					this.nomore = true
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.ranking {
		@include page();
		.content {
			border-top: $custom-nav-borderbot-height solid $custom-nav-borderbot-color;
			display: flex;
			flex-direction: column;
			.day-title {
				width: 100%;
				height: 70rpx;
				display: flex;
				font-size: 36rpx;
				color: #999999;
				text {
					height: 100%;
					flex: 1;
					display: flex;
					justify-content: center;
					align-items: center;
					&.active {
						color: #FA6342;
					}
				}
			}
			.title-bar {
				height: 4rpx;
				width: 100%;
				background: #DEDEDE;
				position: relative;
				.title-drog {
					width: 50%;
					height: 100%;
					background: #FA6342;
					position: absolute;
					top: 0;
					left: 0;
					&::after {
						content: '';
						display: block;
						width: 0;
						height: 0;
						border-style: solid;
						border-width: 8rpx 12rpx;
						border-color: transparent transparent red transparent;
						transform: translate(175.5rpx, -100%);
					}
				}
			}
			image {
				display: block;
				width: 100%;
				height: 240rpx;
			}
			.type-title {
				height: 60rpx;
				border-bottom: 4rpx solid #DEDEDE;
				display: flex;
				justify-content: space-around;
				text {
					height: 60rpx;
					flex: 1;
					color: #999999;
					font-size: 32rpx;
					line-height: 60rpx;
					text-align: center;
					box-sizing: border-box;
					position: relative;
					&.active::after {
						content: '';
						position: absolute;
						left: 50%;
						transform: translateX(-50%);
						bottom: 0;
						height: 4rpx;
						width: 96rpx;
						background: #FA6342;
					}
				}
			}
			.list {
				flex: 1;
			}
			.fiexed {
				height: 100rpx;
				bottom: 0;
				background: #FA6342;
				display: flex;
				.left {
					height: 100%;
					width: 136rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					text {
						color: #FFFFFF;
						font-size: 26rpx;
						&.rank {
							font-size: 32rpx;
						}
					}
				}
				.center {
					width: 380rpx;
					margin-left: 10rpx;
					height: 100%;
					display: flex;
					align-items: center;
					color: #FFFFFF;
					position: relative;
					image {
						width: 64rpx;
						height: 64rpx;
						border-radius: 50%;
						&.hat {
							position: absolute;
							top: 6rpx;
							left: -5rpx;
							width: 72rpx;
							height: 88rpx;
						}
					}
					text {
						margin-left: 30rpx;
						font-size: 24rpx;
					}
				}
				.right {
					flex: 1;
					display: flex;
					align-items: center;
					color: #FFFFFF;
					button {
						width: 146rpx;
						height: 48rpx;
						border: 2rpx solid #FFFFFF;
						border-radius: 24rpx;
						padding: 0;
						background: transparent;
						line-height: 48rpx;
						text-align: center;
						font-size: 24rpx;
						color: #FFFFFF;
					}
					.score {
						width: 130rpx;
						height: 100%;
						display: flex;
						align-items: center;
						image {
							width: 30rpx;
							height: 30rpx;
						}
						text {
							font-size: 24rpx;
							margin-left: 10rpx;
						}
					}
					.float {
						flex: 1;
						display: flex;
						align-items: center;
						image {
							width: 24rpx;
							height: 30rpx;
						}
						text {
							margin-left: 10rpx;
							font-size: 24rpx;
						}
					}
				}
			}
		}
	}
</style>
