<template>
	<view class="activity">
		<u-navbar leftIconSize="0" :placeholder="true" bgColor="#ffffff">
			<view slot="left" style="font-size: 32rpx;">免费抽奖</view>
		</u-navbar>
		<view class="status">
			<u-tabs @change="changeTab" :scrollable="false" :list="statusArr" lineWidth="30" lineHeight="3"
				lineColor="#F9822C" :activeStyle="{
									color: '#F9822C',
									transform: 'scale(1)',
									width: '150rpx',
									
								}" :inactiveStyle="{
									color: '#999999',
									transform: 'scale(1)',
									width: '150rpx'
								}" itemStyle="padding-left: 11px; padding-right: 11px; height: 44px;text-align: center;  ">
			</u-tabs>
		</view>
		<view class="list">
			<view v-for="(item,index) in list" :key="index" class="list-view">
				<view class="list-view-slot">
					<view @click="toRecord(item)">
						<view style="position: relative;">
							<image :src="item.picUrl" mode="scaleToFill" class="list-view-slot-img"></image>
							<span v-if="item.status.value == 3" style="position: absolute; right: 30rpx; top: 300rpx; border-radius: 6rpx; font-size: 24rpx; padding: 10rpx; color: #333333; background-color: rgba(255, 255, 255, 0.5);">{{item.fakeNum}}人已参与</span>
						</view>
						<view class="list-view-slot-bor flex">
							<view class="list-view-slot-bor-title">
								<view class="time bor-title ells-one">{{item.title}}</view>
								<!-- <view v-if="item.status.value == 2" class="time">
									开始时间：{{ $parseTime(item.startTime, '{y}-{m}-{d} {h}:{i}:{s}')}}</view> -->
								<!-- <view v-else class="time">开奖时间：{{ $parseTime(item.endTime, '{y}-{m}-{d} {h}:{i}:{s}')}} -->
								<view v-if="item.status.value == 3" class="timetwo">开奖时间：{{ $parseTime(item.endTime, '{y}-{m}-{d} {h}:{i}:{s}')}}</view>
								<view v-else class="time">开奖时间：{{ $parseTime(item.endTime, '{y}-{m}-{d} {h}:{i}:{s}')}}</view>
							</view>
							<view class="details">
								<button class="details-button" v-if="item.status.value == 3">免费参与</button>
								<button class="details-buttonTwo" v-else-if="item.isHit == 0">未中奖</button>
								<button class="details-button" v-else-if="item.isHit == 1">已中奖</button>
								<!-- <button class="details-buttonTwo" v-else>即将开始</button> -->
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="flex empty" v-if="!list.length && !loading && triggerStatus == 0">
				<view class="center">
					<image class="center-img"
						src="https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/v2/nodata_3.png" mode="scaleToFill"></image>
					<view class="center-font">新的活动正在准备中...</view>
				</view>
			</view>
			<view class="flex empty" v-if="!list.length && !loading && triggerStatus != 0">
				<view class="center">
					<image class="center-img"
						src="https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/v2/nodata_3.png" mode="scaleToFill"></image>
					<view class="center-font">还没有已开奖活动</view>
				</view>
			</view>
		</view>
		<u-loadmore :line="true" v-if="list.length>5" :status="status" :loading-text="'努力加载中'" :nomore-text="'已经到底了'" />
		<custom-tab-bar :activeValue="'activity'" />
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	import appId from '@/config/appId.js'
	import CustomTabBar from '../../components/custom-tab-bar/custom-tab-bar.vue'
	export default {
		components: {
			CustomTabBar,
		},
		data() {
			return {
				status: 'nomore',//上拉刷新状态
				list: [],
				statusArr: [{
					name: '参与抽奖'
				}, {
					name: '已开奖',
				}, {
					disabled: true
				}, {
					disabled: true
				}, {
					disabled: true
				}],
				triggerStatus: 0,
				userInfo: '',
				pageNum: 1,
				total: 0,
			}
		},
		onLoad(opthios) {
			if (opthios.triggerStatus) {
				this.triggerStatus = opthios.triggerStatus
			}
			if (opthios.userId) {
				uni.setStorageSync('shareUid', opthios.userId != 'undefined'?opthios.userId:null)
				if (opthios.userId === undefined || opthios.userId === 'undefined') {
					uni.getSystemInfo({
						success(res) {
							log.error(
								`基础库：${ res.SDKVersion },设备：${ res.model }-${ res.system },页面：/pages/activity/index.vue.`
							)
						}
					})
				}
			}
			if (opthios.type) {
				uni.setStorageSync('shareType', opthios.type)
			}
		},
		onShow() {
			if(uni.getStorageSync('token')) {
				this.getBaseInfo()
			}
			this.pageList()
		},
		methods: {
			toRecord(item) {
				if (!uni.getStorageSync('token')) {
					uni.navigateTo({
						url: '/pages/login/index'
					})
					return
				}
				uni.navigateTo({
					url: item.status.value == 4 ? `/packageOperate/activity/record?id=${ item.id }` :
						`/packageOperate/activity/index?id=${ item.id }`
				})
			},
			changeTab(e) {
				if (e.index == 0) {
					this.triggerStatus = 0
					this.status = 'nomore'
				} else if (e.index == 1) {
					this.triggerStatus = 1
					this.status = 'nomore'
				}
				this.pageList()
			},
			pageList() {
				this.pageNum = 1
				this.total = 0
				this.list = []
				this.getList()
			},
			getList() {
				this.loading = true
				$http.post(`/api/v1/mp/user/marketing/list?pageNum=${this.pageNum}&pageSize=5`, {
					triggerStatus: this.triggerStatus,
				}).then(res => {
					this.loading = false
					if (res.code == 0) {
						res.rows.forEach(item => {
							let picUrlArr = item.picUrl.split(',')
							item.picUrl = env.filePublic + picUrlArr[0] + '?imageView2/2/w/750'
							item.status = JSON.parse(item.status)
						})
						this.total = res.total
						this.list = this.list.concat(res.rows)
					}
				}).catch(() => {
					this.loading = false
				})
			},
			getBaseInfo() {
				$http.post('/api/v1/mp/user/getLoginUserinfo', { appSource: appId }).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						this.userInfo = res.data
					}
				})
			},

		},
		//分享好友
		onShareAppMessage(res) {
			return {
				title: '盲票，玩的就是有趣',
				path: '/pages/index/index'
			}
		},
		//分享朋友圈
		onShareTimeline() {
			return {
				title: '盲票，玩的就是有趣',
				query: `userId=${ this.userInfo.userId?this.userInfo.userId:null }&type=1`
			}
		},
		onReachBottom() {
			if(this.total < this.pageNum * 5) return ;
			this.status = 'loading';
			++this.pageNum
			if(this.total < this.pageNum * 5) this.status = 'nomore';
				else this.status = 'loading';
			this.getList()
		},
	}
</script>

<style lang="scss" scoped>
	.activity {
		width: 100%;
		height: 100%;
		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}

	.status {
		width: 100%;
		position: fixed;
		z-index: 100;
		background-color: #FFFFFF;
		box-shadow: 0 5rpx 5rpx #ececec;
	}

	.list {
		padding: 120rpx 20rpx 0;

		&-view {
			text-align: center;
			height: 560rpx;

			&-slot {
				background-color: #FFFFFF;
				// margin-bottom: 20rpx;
				border-radius: 22rpx;

				&-img {
					width: 100%;
					height: 370rpx;
					border-radius: 22rpx 22rpx 0 0;

				}

				&-bor {
					justify-content: space-between;
					line-height: 50rpx;
					width: 700rpx;
					height: 140rpx;
					margin: 0 auto;
					padding: 10rpx 20rpx 0 20rpx;
					border-radius: 0 0 22rpx 22rpx;

					&-title {
						text-align: left;
						float: left;

						.time {
							font-size: 30rpx;
							color: #333333;
						}

						.bor-title {
							font-size: 32rpx;
							// width: 70%;
						}

						.timetwo {
							width: 420rpx;
							background: rgba(249, 130, 44, 0.08);
							margin-top: 10rpx;
							border: 2rpx solid #F9822C;
							font-size: 24rpx;
							border-radius: 6rpx;
							color: #F9822C;
							padding: 0 15rpx;
							line-height: 40rpx;
						}
					}

					.details {
						float: right;

						.details-button {
							width: 200rpx;
							height: 72rpx;
							line-height: 70rpx;
							background-color: #F9822C;
							color: #fff;
							margin-top: 30rpx;
							font-size: 30rpx;
							border-radius: 20rpx;
						}

						.details-buttonTwo {
							width: 200rpx;
							height: 72rpx;
							line-height: 70rpx;
							background-color: #BBBBBB;
							color: #fff;
							margin-top: 30rpx;
							font-size: 30rpx;
							border-radius: 20rpx;
						}
					}

				}
			}
		}

	}

	.empty {
		height: 60vh;

		.center {
			text-align: center;

			&-img {
				width: 228rpx;
				height: 320rpx;
			}

			&-font {
				font-size: 30rpx;
				font-weight: 400;
				color: #999999;
				margin-bottom: 250rpx;
			}
		}
	}
</style>
