<template>
	<view class="activitylist" :style="{ height: contentHeight }">
		<u-tabs :list="list" :is-scroll="false" :current="current" @change="change" :active-color="mainColor"></u-tabs>
		<view class="activitylist-top">
			<text style="font-weight: bold;">
				推荐优惠活动
			</text>
			<view class="a-top-r" style="display: flex;flex-direction: row;align-items: center;">
				<text style="margin-right: 5px;">我的奖励</text>
				<u-icon name="arrow" :size="20" color="#A5A4AC"></u-icon>
			</view>
		</view>
		<u-empty v-if="g.length == 0" text="暂无活动列表" mode="list"></u-empty>
		<view v-if="g.length > 0" style="height: calc(100vh - 100px);width: calc(100vw);overflow: hidden; ">
			<scroll-view :scroll-y="true" :enable-back-to-top="true" :enable-flex="true" @scrolltolower="lower" :style="{ height: '100%' }">
				<view class="interDList" >
					<view class="inter-item" v-for="(x, i) in g" :key="i">
						<view style="width: 100%;height: 190px;border-top-left-radius: 6px;border-top-right-radius: 6px;position: relative;">
							<image :src="x.CoverImage" mode='aspectFill' style="width: 100%;height: 190px;;border-top-left-radius: 6px;border-top-right-radius: 6px;"></image>
							<view class="imgview" v-if="x.IsFinish==1 && x.SurplusDay!=0" style="background: #CCCCCC;color: #777777;">活动已完成</view>
							<view class="imgview" v-if="x.IsFinish==2 && x.SurplusDay!=0" :style="{background:mainColor,color:'#FFF'}">距离活动结束还有{{x.SurplusDay}}天</view>
							<view class="imgview" v-if=" x.SurplusDay==0" style="background: #CCCCCC;color: #777777;">活动已结束{{x.SurplusDay}}天</view>
						</view>
						<view class="inter-item-title">
							{{x.ActivityName}}
						</view>
						<view style="padding: 0 15px 15px;">
							<u-line-progress :active-color="mainColor" :percent="calculation(x)" height='6' :show-percent="false"></u-line-progress>
							<text style="margin-top: 5px;font-size: 10px;color: #111111;">当前进度{{calculation(x)}}%</text>
						</view>
						
					</view>
				</view>
				<u-loadmore :status="status" :load-text="loadText" :font-size="24" :margin-top="20" :margin-bottom="20" bg-color="#FAF8F9" />
			</scroll-view>
		</view>
		<!-- 加载中 -->
		<view class="loading" v-if="loading">
			<u-loading mode="flower" size="48">></u-loading>
			<Text style="color: #fff; margin-top: 10rpx;">加载中...</Text>
		</view>
		<!-- 取消提示 -->
		<u-toast ref="uToast" />
		<auth v-if="showAuth" @changeuserinfo="reloadUserinfo" @gbAuth='gbAuth'></auth>
	</view>
</template>

<script>
	import auth from "@/components/auth/index.vue";
	export default {
		components: {
			auth
		},
		data() {
			return {
				pageTitle: "积分明细",
				current: 0,
				list: [{
						name: "进行中"
					},
					{
						name: "已结束"
					},
				],
				showAuth: false,
				msg: {
					pageIndex: 1,
					pageSize: 10,
					QActivityState: 0
				},
				mainColor: "",
				secondary: '',
				contentHeight: 0,
				page: 1,
				page_count: 1,
				g: [],
				loading: false,
				isover: false,
				status: "loadmore",
				loadText: {
					loadmore: "轻轻上拉，加载更多",
					loading: "努力加载中",
					nomore: "没有更多了",
				},
			};
		},
		created() {
			this.contentHeight = this.$utils.calcContentHeight(-40) + "px";
			this.mainColor = this.$uiConfig.mainColor;
			this.secondary = this.$uiConfig.secondary;
		},
		mounted() {
			let currentPages = getCurrentPages();
			let u = "/" + currentPages[currentPages.length - 1].route;
			let pages = wx.getStorageSync("basedata") ?
				wx.getStorageSync("basedata").bar_title :
				[];
			pages.forEach((x) => {
				if (x.value == u) {
					this.pageTitle = x.new_name ? x.new_name : x.name;
				}
			});
			uni.setNavigationBarTitle({
				title: this.pageTitle,
			});
		},
		onLoad() {
			this.u = uni.getStorageSync("mall_UserInfo");
			if (!this.u) {
				this.u = {
					nickName: "未登录",
					avatarUrl: ""
				};
				this.showAuth = true;
			} else {
				this.init();
			}
		},
		methods: {
			change(index) {
    			this.current = index;
				this.msg.pageIndex = 1;
				this.g = [];
				this.loading = true;
				this.init();
			},
			init() {
				this.loading = true;
				this.msg.QActivityState = this.current + 1
				this.request2({
						url: '/api/AppletOrder/GetGoodsActivityPageList',
						data: this.msg
					},
					res => {
						if (res.resultCode == 1) {
							this.loading = false;
							this.g = this.g.concat(res.data.pageData);
							this.page_count = res.data.pageCount;
							if (this.page_count == 1) {
								this.status = "nomore";
							}
						}
					}
				);
			},
			lower(e) {
				if (this.msg.pageIndex < this.page_count) {
					this.msg.pageIndex++;
					this.status = "loading";
					this.init();
				} else {
					this.status = "nomore";
				}
			},
			reloadUserinfo() {
				this.u = uni.getStorageSync("mall_UserInfo");
				this.init();
			},
			//关闭登录窗口
			gbAuth() {
				uni.navigateBack()
			},
			calculation(x){
				let percent = 0
				if(x.Type==1){//计算完成率
					percent = (x.MyOrderNum/x.OrderNum)*100
				}else if(x.Type==2){
					percent = (x.MyOrderMoney/x.OrderMoney)*100
				}
				else if(x.Type==3){
					percent = (x.MyGoodsNum/x.GoodsNum)*100
				}
				if(x.IsFinish ==1){ //如果完成了直接是百分百
					percent = 100
				}else{
					percent.toFixed(2)
				}
				return percent
			}
		},
	};
</script>
<style>
	.activitylist {
		width: 100%;
		height: 100%;
		background: #FAF8F9;
	}

	.activitylist .loading {
		width: 200rpx;
		height: 200rpx;
		background: #000000;
		opacity: 0.7;
		border-radius: 10rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: fixed;
		left: 50%;
		top: 30%;
		margin-left: -100rpx;
		z-index: 999;
	}

	.activitylist .interDList {
		width: 100%;
		padding: 0 15px;
		
	}
	.inter-item{
		width: 100%;
		border-radius: 6px;
		margin-top: 15px;
		background: #FFF;
		box-shadow:0 6px 10px 1px #F2F2F2;
	}
	.activitylist .activitylist-top{
		width: 100%;
		height: 50px;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		padding: 0 15px;
		font-size: 15px;
		color: #111111;
	}
	.activitylist .a-top-r{
		display: flex;
		flex-direction: row;
		align-items: center;
		font-size: 12px;
	}
	.activitylist .imgview{
		height: 22px;
		border-radius: 2px;
		font-size: 11px;
		position: absolute;
		right: 20px;
		bottom: 8px;
		text-align: center;
		line-height: 20px;
		padding: 0 8px;
	}
	.activitylist .inter-item-title{
		width: 100%;
		padding:12px 15px 0px;
		font-size: 13px;
		color: #111111;
		font-weight: bold;
		overflow: hidden;white-space: nowrap;text-overflow: ellipsis
	}
</style>
