<template>
	<view class="">
		<u-navbar
			title="活动中心"
			:placeholder="true"
			:autoBack="false"
			@leftClick="leftClick"
			 :safeAreaInsetTop="true"
		>
		</u-navbar>
		<view class="tabs-wrap">
			<u-tabs 
			:list="tabsList" 
			lineColor="#ED0000" 
			 :activeStyle="{color:'#ED0000','font-weight': '600','font-size':'30rpx'}"
			 :inactiveStyle="{color:'#999'}"
			 :itemStyle="{width:'28%','padding-bottom':'20rpx'}"
			@click="tabsClick"></u-tabs>
		</view>
		<u-empty mode="data" v-if="tabsList.length<1" >
		</u-empty>
		<mescroll-body class="" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="page-wrap" v-if="dataList.length>0" >
				<view class="coupon-list">
					<view class="item" 
						:class="{disable:item.status==2,ifReceive:item.ifReceive==1}" 
						:style="{backgroundImage:`url(${staticUrl}/img/coupon-active-bg-v2.png)`}" v-for="item in dataList" :key="item.couponId">
						<view class="content u-flex">
							<view class="left">
								<view class="quota">
									<text v-if="item.type == 1">￥</text>
									<text class="number" :class="{'small': String(item.quota).length>=3}">{{item.type==1?item.quota:item.quota/10}}</text>
									<text v-if="item.type == 2">折</text>
								</view>
							</view>
							<view class="center">
								<view class="name" :class="{'small': String(item.couponName).length>=9}">{{item.couponName}}</view>
								<view class="condition" v-if="item.useThresholdType == 2">
									消费满{{item.useThresholdAmount}}可用
								</view>
							<!-- 	<view class="tip">
									<view class="" v-if="item.useType==1">
										有效期至 {{item.useEndDate}}
									</view>
									<view class="" v-if="item.useType==2">
										领取后{{item.useDay}}天内有效
									</view>
								</view> -->
							</view>
							<view class="right">
								<text class="btn" @click="receiveCoupon(item)" v-if="item.ifReceive!==1">立即领取</text>
								<text class="btn" v-else>立即领取</text>
								<!-- <view class="time">
									<view class="">{{item.issuseStartDate}}</view>
									<view class="">{{item.issuseEndDate}}</view>
								</view> -->
							</view>
						</view>
						<view class="bottom-time">
							<view class="" v-if="item.useType==1">
								有效期 {{item.useStartDate}} - {{item.useEndDate}}
							</view>
							<view class="" v-if="item.useType==2">
								领取后{{item.useDay}}天内有效
							</view>
						</view>
						<view class="type" v-if="item.type == 1">满减券</view>
						<view class="type" v-else-if="item.type == 2">折扣卷</view>
						<!-- <image class="status-img" :src="staticUrl+'/img/coupon-status-shiyong.png'"></image> -->
					</view>
				</view>
			</view>
		</mescroll-body>
		<u-popup :show="receiveCouponSuccessShow" @close="receiveCouponSuccessClose" mode="center" bgColor="transparent">
			<view class="receive-coupon-success">
				<view class="content" :style="{backgroundImage:`url(${staticUrl}/img/receive-coupon-success.png)`}">
					<view class="title">领取成功</view>
					<view class="con">恭喜您！优惠券领取成功！</view>
				</view>
				<u-icon @click="receiveCouponSuccessClose" name="close-circle" color="#B5B5B5" size="80rpx"></u-icon>
			</view>
		</u-popup>
	</view>
</template>

<script>
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				staticUrl:this.$commonConfig.staticUrl,
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				tabsList:[],
				params:{
					actId:''
				},
				activeIndex:0,
				dataList: [],
				receiveCouponSuccessShow:false,
			}
		},
		onShow() {
			
		},
		onLoad() {
			this.goodsClassify();
			// this.params.type = this.tabsList[this.activeIndex].actId;
			// console.log('1111', this.tabsList[this.activeIndex]);
		},
		methods: {
			leftClick(e){
				let pages = getCurrentPages();
				if(pages.length==1){
					uni.$u.route('/pages/index/index')
				}else{
					uni.navigateBack()
				};
			},
			goodsClassify(){
				this.$u.api.validActList().then(res=>{
					this.tabsList = res.data.list.map(item=>{
						return {name:item.activityName,actId:item.id}
					});
					// this.tabsList.unshift({label:'全部',value:''});
					console.log('this.tabsList',this.tabsList);
					this.reloadList();
				}).catch(err=>{
					console.log('goodsClassify',err);
				})
			},
			/*下拉刷新的回调, 重置列表为第一页 (此处可删,mixins已默认)
			downCallback(){
				this.mescroll.resetUpScroll();
			},
			/*上拉加载的回调*/
			upCallback(page) {
				// 此处可以继续请求其他接口
				// if(page.num == 1){
				// 	// 请求其他接口...
				// }
			
				// 如果希望先请求其他接口,再触发upCallback,可参考以下写法
				this.params.actId = this.tabsList[this.activeIndex]?.actId;
				if(!this.params.actId){
					this.mescroll.endErr();
					return // 此处return,先获取xx
				}
			
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条isAsc:0//时间排序 0:降序 1:升序 (默认星级降序排序)

				this.params = Object.assign(this.params,{pageNum:pageNum,pageSize:pageSize});
				this.$u.api.couponList(this.params).then(data => {
					console.log('data',JSON.parse(JSON.stringify(data)));
					// 接口返回的当前页数据列表 (数组)
					let curPageData = data.data.list;
					console.log('curPageData',JSON.parse(JSON.stringify(curPageData)));
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length; 
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					// let totalPage =  data.data.data.totalPage; 
					// 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
					let totalSize = data.data.total; 
					// 接口返回的是否有下一页 (true/false)
					// let hasNext = data.xxx; 
					// console.log('totalPage',totalPage,'curPageLen',curPageLen);
					//设置列表数据
					if(page.num == 1) this.dataList = []; //如果是第一页需手动置空列表
					this.dataList = this.dataList.concat(curPageData); //追加新数据
					// 请求成功,隐藏加载状态
					//方法一(推荐): 后台接口有返回列表的总页数 totalPage
					// this.mescroll.endByPage(curPageLen, totalPage); 
					//方法二(推荐): 后台接口有返回列表的总数据量 totalSize
					this.mescroll.endBySize(curPageLen, totalSize); 
				}).catch(err => {
					this.mescroll.endErr()
					console.log(err)
				});	
			
			},
			/*若希望重新加载列表,只需调用此方法即可(内部会自动page.num=1,再主动触发up.callback)*/
			reloadList() {
				this.mescroll.resetUpScroll();
			},
			tabsClick(item){
				this.activeIndex = item.index;
				this.reloadList()
				console.log('item',item);
			},
			receiveCoupon(item){
				// console.log('receiveCoupon',item);
				let param ={
					actId:this.tabsList[this.activeIndex].actId,
					couponId:item.couponId
				}
				this.$u.api.receiveCoupon(param).then(res=>{
					this.receiveCouponSuccessShow = true;
					setTimeout(()=>{
						uni.$u.route('/center/mycoupon')
					},2000);
				}).catch(err=>{
					console.log('receiveCoupon',err);
				})
			},
			receiveCouponSuccessClose(){
				this.receiveCouponSuccessShow = false;
				uni.$u.route('/center/mycoupon')
				// this.reloadList();
			}
		}
	}
</script>
<style>
page{
	background-color: #f5f5f5;
}
</style>
<style lang="scss" scoped>
.credit{
	padding: 20rpx;
	background-color: #fff;
	border-radius: 8rpx;
	margin: 0 20rpx 20rpx;
	font-size: 36rpx;
	font-weight: 600;
	color: #00A447;
}
.tabs-wrap{
	background-color: #fff;
	margin-bottom: 10rpx;
}
.list{
	border-radius: 8rpx;
	padding: 0 20rpx 20rpx;
	background-color: #fff;
	.item{
		padding: 20rpx 0;
		border-bottom: 0.5px solid #ddd;
		.name{
			font-size: 30rpx;
			font-weight: 400;
			color: #333333;
			line-height: 42rpx;
			margin-bottom: 8rpx;
		}
		.time{
			font-size: 24rpx;
			font-weight: 400;
			color: #999999;
			line-height: 33rpx;
		}
		.num{
			font-size: 30rpx;
			font-weight: 600;
			color: #FFB100;
			&.plus{
				color: #00A447;
			}
		}
	}
}
.receive-coupon-success{
	position: relative;
	.content{
		width: 477rpx;
		height: 499rpx;
		background-size: 100% 100%;
		text-align: center;
		padding: 0 24rpx;
		font-weight: bold;
		overflow: hidden;
	}
	.title{
		font-size: 40rpx;
		color: #FF1212;
		line-height: 60rpx;
		margin-top: 290rpx;
		margin-bottom: 32rpx;
	}
	.con{
		font-size: 28rpx;
		color: #4E4E4E;
	}
	/deep/ .u-icon{
		justify-content: center;
	}
}
</style>