<template>
	<view>
		<lf-nav title="积分商城" titleColor="#fff" bgColor="transparent" :showIcon="true" :spreadOut="false"></lf-nav>
		<view class="head">
			<view class="bg-view"></view>
		</view>
		<view class="menu">
			<view class="left">
				<view>
					<text class="lf-iconfont icon-jifen"></text>
					<text class="point-num">{{ point.point }}</text>
					<text class="lf-iconfont icon-xiangyou lf-font-24"></text>
				</view>
				<view class="lf-font-24">当前积分</view>
			</view>
			<view class="lf-flex">
				<view class="item" @click="$url('/pages/point/exchangeRecord/exchangeRecord')">
					<text class="lf-iconfont icon-jilu2 lf-font-50"></text>
					<text>兑换记录</text>
				</view>
				<view class="item" @click="$url('/pages/store/cart/cart')">
					<text class="lf-iconfont icon-gouwulan lf-font-50"></text>
					<text>购物车</text>
					<view class="angle-mark" v-if="car_num<99">{{car_num}}</view>
					<view class="angle-mark" v-else>99+</view>
				</view>
			</view>
		</view>
		<view class="lf-m-t-30" v-if="tab_list.length">
			<u-tabs :list="tab_list" 
				active-color="#15716E" 
				inactive-color='#777777' 
				:is-scroll="true" 
				:current="tab_current" 
				@change="tabChange">
			</u-tabs>
		</view>
		<view class="page-item" v-for="(tab_item, tab_index) in tab_list" 
			:key="tab_index" v-if="tab_current == tab_index">
			<view class="filter">
				<view :class="{'filter-item': true, 'filter-active': index == filter_current}" 
					v-for="(item, index) in filter_list" :key="index"
					@click="activeFilter(index)">{{ item.name }}
				</view>
			</view>
			<view class="goods" v-if="$isRight(tab_list)">
				<lf-waterfall :ifsale='false' :list="tab_list[tab_current].list"></lf-waterfall>
				<lf-nocontent src="/static/images/empty.png" v-if="tab_list[tab_current].list.length <= 0"></lf-nocontent>
			</view>
		</view>
		<u-back-top :scrollTop="pageScrollTop"></u-back-top>
	</view>
</template>

<script>
	import lfWaterfall from '@/components/lf-waterfall-pointgoods/lf-waterfall.vue';
	export default {
		components: {
			lfWaterfall
		},
		data(){
			return {
				tab_current: 0,
				tab_list: [],
				filter_list: [{
					name: '1～100',
					point_start: '1',
					point_end: '100'
				},{
					name: '100～1万',
					point_start: '100',
					point_end: '10000'
				},{
					name: '1万～2万',
					point_start: '10000',
					point_end: '20000'
				},{
					name: '2万～5万',
					point_start: '20000',
					point_end: '50000'
				},{
					name: '>5万',
					point_start: '50000',
					point_end: ''
				}],
				filter_current: null,
				token: '',
				point: {point: 0},
				car_num: 0
			}
		},
		onLoad(){
			this.token = this.$cookieStorage.get('user_token');
			this.getTotalPoint();
			this.getCategoryList();
		},
		onShow() {
			this.getcarNum();
		},
		methods: {
			getcarNum() {
				this.$http
				    .get({
				        api: 'api/shopping/cart/count',
						header: {
						   Authorization: this.$cookieStorage.get('user_token')
						},
				    })
				    .then(res => {
				        if (res.data.code == 200) {
				           if(res.data.data == null) {
							   this.car_num = 0;
						   }else {
							   this.car_num = res.data.data;
						   }
				        } else {
				            wx.showModal({
				                content: '请下拉页面刷新重试',
				                showCancel: false
				            });
				        }
				    })
				    .catch(() => {
				        wx.stopPullDownRefresh();
				        wx.hideLoading();
				        wx.showModal({
				            content: '请求失败',
				            showCancel: false
				        });
				    });
			},
			getTotalPoint(){
				this.$http.get({
					api: 'api/users/point',
					header: {
						Authorization: this.token
					}
				}).then(res => {
					console.log("getTotalPoint", res)
					this.point = res.data.data;
				});
			},
			getCategoryList(){
				uni.showLoading({
					title: '正在获取分类'
				})
				this.$http.get({
					api: 'api/category/list',
					header: {
						Authorization: this.token
					}
				}).then(res => {
					let list = res.data.data.map(item => {
						return {
							c_id: item.id,
							name: item.name,
							list: [],
							page: 1,
							isPage: true,
							loadingClass: true,
							loadingText: '正在加载中'
						}
					})
					this.tab_list = list;
					uni.hideLoading();
					this.getPointList();
				}).catch(err => uni.hideLoading())
			},
			getPointList(){
				uni.showLoading({
					title: '正在获取商品'
				})
				let tabItem = this.tab_list[this.tab_current];
				let par = {
					is_largess: 1,
					page: tabItem.page,
					c_id: tabItem.c_id
				};
				if(this.filter_current != null){
					let f_item = this.filter_list[this.filter_current];
					par.point_start = f_item.point_start;
					par.point_end = f_item.point_end;
				}
				this.$http.get({
					api: 'api/store/list',
					data: par,
					header: {
						Authorization: this.token
					}
				}).then(res => {
					let list = res.data.data.map(item => {
						return {
							id: item.id,
							sale: item.sale_count,
							picture: item.img,
							title: item.name,
							store_nums: item.redeem_point
						}
					})
					let pagination = res.data.meta.pagination;
					tabItem.isPage = pagination.current_page < pagination.total_pages;
					if(tabItem.page == 1){
						tabItem.list = list;
					}else{
						tabItem.list.push(...list);
					}
					uni.hideLoading();
				}).catch(err => uni.hideLoading())
			},
			activeFilter(index){
				if(this.filter_current == index){
					this.filter_current = null;
				}else{
					this.filter_current = index;
				}
				let tabItem = this.tab_list[this.tab_current];
				tabItem.page = 1;
				tabItem.isPage = true;
				tabItem.loadingClass = true;
				tabItem.loadingText = '正在加载中';
				this.getPointList();
			},
			tabChange(event){
				this.tab_current = event;
				if(this.tab_list[event].list.length <= 0){
					this.getPointList();
				}
			}
		},
		onReachBottom(){
			let tabItem = this.tab_list[this.tab_current];
			if(tabItem.isPage){
				tabItem.page = tabItem.page+1;
				this.getPointList();
			}else{
				this.$msg('没有更多啦~')
			}
		}
	}
</script>

<style>
	page{
		overflow-x: hidden;
	}
</style>
<style lang="scss" scoped="scoped">
	.head{
		width: 750rpx;
		height: 283rpx;
		background: linear-gradient(90deg, #22A2A0 0%, #187B7A 100%);
		position: relative;
		overflow: hidden;
		.bg-view{
			position: absolute;
			right: -100rpx;
			top: -200rpx;
			width: 400rpx;
			height: 400rpx;
			border-radius: 50%;
			background-color: rgba(255,255,255,0.04);
		}
	}
	.menu{
		width: 686rpx;
		height: 170rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 2rpx 8rpx 1rpx rgba(0, 0, 0, 0.1);
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: #15716E;
		margin: -80rpx auto 0;
		position: relative;
		z-index: 2;
		.left{
			width: 340rpx;
			.point-num{
				font-size: 48rpx;
				font-weight: bold;
				margin: 0 10rpx;
			}
		}
		.item{
			width: 100rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			font-size: 24rpx;
			position: relative;
			&:nth-child(2n){
				margin-left: 40rpx;
			}
			.angle-mark{
				width: 30rpx;
				height: 30rpx;
				background-color: #FF9D9D;
				color: #FFFFFF;
				border-radius: 50%;
				position: absolute;
				right: 10rpx;
				top: 10rpx;
				font-size: 18rpx;
				text-align: center;
				line-height: 30rpx;
			}
		}
	}
	
	.page-item{
		width: 750rpx;
		height: max-content;
		.filter{
			width: 100%;
			height: 83rpx;
			border: 1rpx solid #e5e5e5;
			box-sizing: border-box;
			padding: 0 32rpx;
			display: flex;
			align-items: center;
			.filter-item{
				width: 123rpx;
				height: 43rpx;
				font-size: 24rpx;
				color: #777777;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 26rpx;
				&:nth-child(n+2){
					margin-left: 28rpx;
				}
				&.filter-active{
					background-color: #15716E;
					color: #FFFFFF;
				}
			}
		}
		.goods{
			padding: 30rpx 32rpx;
		}
	}
	
	// tabs 样式修改
	/deep/.u-scroll-box {
		display: flex;
		justify-content: center;
		align-items: center;
		// border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
		width: max-content;
	}
	/deep/.u-scroll-box .u-tab-bar {
		background-color: #15716E!important;
		width: 80rpx!important;
		position: absolute;
		left: 0;
		bottom: -12rpx;
	}
	
	/deep/.special_tab .u-tabs .u-scroll-box .u-tab-bar {
		background-color: #15716E!important;
		width: 56rpx!important;
		position: absolute;
		height: 5rpx!important;
		left: 8rpx;
		bottom: -4rpx;
	}
	
	/deep/ .u-tab-item {
		font-size: 28rpx!important;
	}
</style>
