<template>
	<view>
		<view class="top-cont">
			<!-- 背景图片 -->
			<image class="bg" src="../../static/images/jf_top_Bg.png" mode=""></image>
			<view class="top-inner">			
				<!-- 这里是状态栏 -->
				<view class="fixed-top-box" :style="{ background: bg }">
					<view class="status_bar" :style="{height: statusBarHeight}"></view>
					<view class="back-box" @click="back">
						<image src="../../static/images/back_white.png" mode=""></image>
						<text class="title">我的积分</text>
						<text></text>
					</view>
				</view>
				<!-- 顶部固定后站位元素 -->
				<view style="padding-bottom: 88upx;">
					<view :style="{height: statusBarHeight}"></view>
				</view>
				<!-- 可用积分 -->
				<view class="available-points">
					<text class="label">可用积分</text>
					<text class="num">{{integral}}</text>
				</view>
				<!-- 签到 -->
				<view class="singn-content">
					<view class="sign-in-box">
						<view class="inner">
							<view class="title-box">已连续签到<text class="num">{{signNum}}</text>天</view>
							<!-- 签到天数 -->
							<view class="sign-list">
								<view v-for="(item,index) in sign" :key="index"  :class="signNum >= index+ 1?'item active':'item'">
									<view class="line"></view>
									<view class="right">
										<!-- 已签到图标 -->
										<image v-if="signNum >= index+ 1" src="../../static/images/right_org.png" mode=""></image>
										<!-- 未签到图标 -->
										<image v-else src="../../static/images/right_org_ling.png" mode=""></image>
										<text class="text">{{ item.day }}</text>
									</view>
								</view>
							</view>
						</view>
						<!-- 签到按钮 -->
						<view class="sign-btn-box"  >
							<view class="btn" v-if="isDaySign==false" @click="doSign()">
								<image src="../../static/images/sign.png" mode=""></image>
								<text class="text">签到</text>
							</view>
							<view class="btn" v-else>
								<image src="../../static/images/sign.png" mode=""></image>
								<text class="text">已签到</text>
							</view>
						</view>
					</view>
				</view>
				<view class="content">
					<!-- 积分列表 -->
					<view class="points-cont">
						<!-- tab切换 -->
						<view class="pub-tab-box">
							<view class="tab-inner">
								<view 
									v-for="(item,index) in tags" 
									:key="index"
									:class="tabIndex == item.value?'item active':'item'"
									@click="tabChange(item)"
								>
									<view class="text">
										{{ item.lable }}
										<image v-show="tabIndex == item.value" class="tab-bg" src="../../static/images/tab_bg2.png" mode=""></image>
									</view>
								</view>
							</view>
						</view>
						<!-- 列表 -->
						<view class="point-list">
							<view v-for="(item,index) in list" :key="index" class="item">
								<view class="left">
									<view class="title">{{item.title}}</view>
									<view class="time">{{item.createTime}}</view>
								</view>
								<view class="right">
									<text v-if="item.number<0" class="less">{{item.number}}</text>
									<text v-else class="add">+{{item.number}}</text>
								</view>
							</view>
						</view>
						<Loading :loaded="loaded" :loading="loading"></Loading>
					</view>
				</view>
				
			</view>
		</view>
		
	</view>
</template>

<script>
	import {getUserSign,getIntegral,doSign} from '@/api/userSign'
	import Loading from "@/components/Loading";
	export default {
		components: {
		  Loading,
		},
		data() {
			return {
				isDaySign:false,
				top:0,
				signNum:0,
				integral:0,
				sign:[],
				// 状态栏的高度
				statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,
				// tab切换
				tags: [{lable:'全部',value:-1},{lable:'获得',value:1},{lable:'消耗',value:0}],
				// 选中的tab
				tabIndex: -1,
	
				current: 0,
				page: {
				  billType:-1,
				  page: 1,
				  pageSize: 10
				},
				list: [],
				loaded: false,
				loading: false
			};
		},
		onLoad(option) {
			this.getUserSign()
			this.getIntegral()
		},
		onReachBottom() {
		  !this.loading && this.getIntegral();
		},
		onPageScroll(e) {
			if(e.scrollTop > 30) {
				this.topFixed = true
			} else {
				this.topFixed = false
			}
		},
		onPageScroll(e) {
			this.top=e.scrollTop;
		},
		computed: {
			// 计算属性的 getter
			bg: function() {
				return 'rgba(255,142,60, ' + this.top / 30 + ')';
			},
		},
		methods: {
			doSign(){
				var data={};
				uni.showLoading({
					title:"正在加载中..."
				})
				doSign(data).then(
					res => {
						uni.hideLoading()
						if(res.code==200){
							uni.showToast({
								icon:'success',
								title: res.msg,
							});
							this.list=[];
							this.page.page=1;
							this.list=[];
							this.loaded=false;
							this.loading=false;
							this.getIntegral();
							this.getUserSign();
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			getUserSign(){
				getUserSign().then(
					res => {
						if(res.code==200){
							this.data=res.member;
							this.signNum=res.signNum;
							this.isDaySign=res.isDaySign;
							this.integral=res.integral;
							this.sign=JSON.parse(res.sign);
							
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			getIntegral() {
			  let that = this;
			  if (that.loaded == true || that.loading == true) return;
			  that.loading = true;
			  uni.showLoading({
			  	title:"加载中..."
			  })
			  getIntegral(that.page).then(
			    res => {
			      that.loading = false;
			      that.loaded = res.data.list.length < that.page.pageSize;
			      that.page.page = that.page.page + 1;
			      that.list.push.apply(that.list, res.data.list);
				  uni.hideLoading()
			    },
			    err => {
					uni.hideLoading()
					uni.showToast({
							title: err.msg ,
							icon: 'none',
							duration: 2000
						});
			    }
			  );
			},
			// 返回上一页
			back() {
				uni.navigateBack()
			},
			// tab选择
			tabChange(item) {
				console.log(item)
				this.tabIndex = item.value
				this.page.billType=this.tabIndex;
				this.page.page=1;
				this.list=[];
				this.loaded=false;
				this.loading=false;
				this.getIntegral();
			}
		}
	}
</script>

<style lang="scss">
	.fixed-top-box{
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 1000;
		transition: all 0.5s;
		 
	}
	.top-cont{
		width: 100%;
		height: 654upx;
		position: relative;
		.bg{
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			z-index: 1;
		}
		.top-inner{
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			z-index: 2;
			.back-box{
				height: 88upx;
				padding-left: 22upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 20upx;
				image{
					width: 40upx;
					height: 40upx;
				}
				.title{
					font-size: 36upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
			.available-points{
				margin-top: 40upx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				.label{
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
					line-height: 1;
				}
				.num{
					font-size: 80upx;
					font-family: Gilroy;
					font-weight: 500;
					color: #FFFFFF;
					line-height: 1;
					margin-top: 28upx;
				}
			}
			.singn-content{
				padding: 0 20upx;
				margin-top: 50upx;
			}
			.sign-in-box{
				height: 380upx;
				background: #FFFFFF;
				border-radius: 16upx;
				.inner{
					padding: 40upx 30upx;
					.title-box{
						font-size: 26upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #666666;
						line-height: 1;
						.num{
							font-size: 32upx;
							font-family: PingFang SC;
							font-weight: Bold;
							color: #FF7511;
							margin: 0 10upx;
							line-height: 1;
						}
					}
					.sign-list{
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-top: 40upx;
						.item{
							display: flex;
							justify-content: center;
							.right{
								display: flex;
								flex-direction: column;
								align-items: center;
								justify-content: center;
								image{
									width: 44upx;
									height: 44upx;
									margin-bottom: 20upx;
								}
								.text{
									font-size: 24upx;
									font-family: PingFang SC;
									font-weight: 500;
									color: #FF7511;
									line-height: 1;
									white-space: nowrap;
								}
							}
							.line{
								width: 34upx;
								height: 4upx;
								background: #F6CDA7;
								border-radius: 2upx;
								margin-top: 22upx;
							}
							&:first-child{
								.line{
									display: none;
								}
							}
							&.active{
								.line{
									background: #FF8E3C;
								}
							}
						}
					}
				}
				.sign-btn-box{
					padding: 0 14upx;
					.btn{
						width: 100%;
						height: 120upx;
						background-image: url(../../static/images/org_btn.png);
						background-repeat: no-repeat;
						background-size: 100% 100%;
						display: flex;
						justify-content: center;
						image{
							width: 32upx;
							height: 32upx;
							margin-top: 32upx;
							margin-right: 16upx;
						}
						.text{
							font-size: 30upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FFFFFF;
							line-height: 1;
							margin-top: 36upx;
						}
					}
				}
			}
			
		}
	}
	.content{
		margin-top: 20upx;
		padding: 0 20upx 40upx;
		.points-cont{
			
			background-color: #FFFFFF;
			border-radius: 16upx;
			.pub-tab-box{
				padding: 0 80upx;
				.tab-inner{
					height: 88upx;
					line-height: 88upx;
					display: flex;
					align-items: center;
					justify-content: space-between;
				}
				.item{
					font-size: 28upx;
					white-space: nowrap;
					line-height: 1;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
					display: flex;
					align-items: center;
					justify-content: center;
					&.active{
						font-weight: bold;
						color: #333333;
					}
					.text{
						position: relative;
						z-index: 1;
					}
					.tab-bg{
						width: 72upx;
						height: 28upx;
						position: absolute;
						top: 17upx;
						left: 50%;
						transform: translateX(-36upx);
						z-index: -1;
					}
				}
			}
			.point-list{
				padding: 0 30upx;
				.item{
					padding: 30upx 0;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 1px solid #F0F0F0;
					&:last-child{
						border-bottom: none;
					}
					.left{
						.title{
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
							line-height: 1;
						}
						.time{
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #999999;
							line-height: 1;
							margin-top: 22upx;
						}
					}
					.right{
						.add{
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
						}
						.less{
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #F56C6C;
						}
					}
				}
			}
		}
	}
	
</style>
