<template>
	<view class="page">
		<ul class="tab-wrap">
			<scroll-view scroll-x :scroll-left="scrollLeft" class="tab tab-scroll">
				<li v-for="(tab, i) in tabs" :key="i" :class="{active: curTab === tab.id}" @click="tabChange(tab)">{{ tab.name }}</li>
			</scroll-view>
		</ul>
		
		<view class="list">
			<view v-for="(item, i) in list" :key="i" class="item">
				<view class="texts">
					<view class="name">{{ item.competitionName }}</view>
					<view class="meta">{{ item.status === 1 ? '未生效 |' : '' }} 有效期 {{ item.playEndTime.substr(0, 16) }}</view>
				</view>
				<view :class="['use', {ing: item.status}]" @click.stop="use(item)">{{ item.status === 1 ? '电子入场券' : '使用' }}</view>
			</view>
		</view>
		
		<uni-popup ref="popup" type="dialog">
			<view class="sign-popup">
				<uni-icons class="close" type="closeempty" size="16" color="#4876f9" @click="closePopup"></uni-icons>
				<view class="activity-name">{{ curRow.competitionName }}</view>
				<view class="title">签到二维码</view>
				<image class="qrcode" src="http://124.71.79.122/images/miniProgram/qrcode.png"></image>
				<view v-if="curRow.playStartTime" class="date">有效期限{{ curRow.playStartTime.substr(0, 16) + ' ~ ' + curRow.playEndTime.substr(0, 16) }}</view>
				<view class="userName">{{ info.hrUserInfo.userName }}</view>
				<view class="phone">{{ info.hrUserInfo.phone }}</view>
				<view v-if="info.organizationInfoList && info.organizationInfoList.length" class="org-name">{{ info.organizationInfoList[0].organizationName }}</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { postLoginActivity } from '@/apis/modules/activity.js'
	import { viewUserDetails } from '@/apis/modules/user.js'
	import Util from '@/libs/util'
	export default {
		data() {
			return {
				teamId: uni.getStorageSync('teamId') || '',
				openId: uni.getStorageSync('openId'),
				reachBottom: 0, // 是否是上拉加载。0->否,1->是,-1->加载完所有数据
				status: 'more', // 上拉加载状态 more|loading|noMore
				keyword: '',
				list: [],
				page: 1,
				pageSize: 5,
				total: 0,
				curTab: 1,
				curItem: null,
				submiting: false,
				tabs: [
					{
						id: 1,
						name: '活动券票'
					},
					{
						id: 2,
						name: '品牌福利'
					},
					{
						id: 3,
						name: '会员福利'
					},
				],
				scrollLeft: 0,
				timer: null,
				now: '',
				curRow: {},
				info: {},
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.initList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1500)
		},
		// 上拉加载
		onReachBottom() {
			if (this.reachBottom >= 0) {
				this.reachBottom = 1
				this.status = 'loading'
				this.getList()
			}
		},
		onShow() {
			this.keyword = ''
			this.initList()
			this.getInfo()
		},
		methods: {
			getList() {
				postLoginActivity({
					pageNum: this.page,
					pageSize: this.pageSize,
					listType: 1,
					keyWord: this.keyword,
					platformSource: ''
				}).then(async ({ data }) => {
					this.list = this.reachBottom > 0 ? [...this.list, ...data.records] : data.records
					this.page++ // 每次获取了数据后page+1
					const noMore = this.list.length === data.total // 是否加载完所有数据
					this.status = noMore ? 'noMore' : 'more' // 加载完了则设置为noMore
					this.reachBottom = noMore ? -1 : 0 // 加载完了则设置为-1
					
					this.now = await Util.getNow()
					this.statusInterval()
					uni.hideLoading()
					this.timer = setInterval(() => {
						this.now = new Date(this.now.setSeconds(this.now.getSeconds() + 1))
						this.statusInterval()
					}, 1000)
				}).catch(e => {})
			},
			async statusInterval () {
				this.list.map(item => {
					if (item.signUpStartTime && item.signUpEndTime) {
						this.handleStatus(item)
					}
				})
			},
			// 定时处理时间及状态
			handleStatus (item) {
				let status = 0
				const playStartTime = new Date(item.playStartTime) // 报名开始时间
				const playEndTime = new Date(item.playEndTime) // 报名结束时间
				const { now } = this
				
				if (now < playStartTime) { // 未生效
					status = 1
				} else if (now < playEndTime) { // 生效期中
					status = 2
				}
				this.$set(item, 'status', status)
			},
			initList() {
				this.page = 1
				this.reachBottom = 0
				this.getList()
			},
			
			
			// 获取组织名字
			async getInfo() {
				const { result } = await viewUserDetails({
					openId: this.openId
				})
				this.info = result
			},
			// tab切换
			tabChange(tab) {
				this.curTab = tab.id
				this.initList()
			},
			// 展示电子券
			use(row) {
				if (row.status) {
					this.curRow = row
					this.$refs.popup.open()
				}
			},
			// 关闭弹框
			closePopup() {
				this.$refs.popup.close()
			},
		}
	}
</script>

<style scoped lang="scss">
.page {
	padding: 20rpx;
}
.tab-wrap {
	display: flex;
	.tab-scroll {
		width: calc(100% - 100rpx);
		white-space: nowrap;
		li {
			display: inline-block;
		}
	}
}
.list {
	.item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx;
		margin-bottom: 28rpx;
		background-color: #fff;
		overflow: hidden;
		border-radius: 8px;
	}
	.name {
		font-size: 30rpx;
		font-weight: 600;
		color: #333;
	}
	.meta {
		margin-top: 20rpx;
		font-size: 24rpx;
		color: #999;
	}
	.use {
		padding: 8rpx 28rpx;
		margin-left: 20rpx;
		font-size: 28rpx;
		color: #fff;
		white-space: nowrap;
		background-color: #afafaf;
		border-radius: 4px;
	}
	.ing {
		font-size: 28rpx;
		background-color: $uni-primary;
	}
}
.sign-popup {
	position: relative;
	width: 80vw;
	padding: 40rpx 0;
	text-align: center;
	box-sizing: border-box;
	background-color: #fff;
	border-radius: 8px;
	.close {
		position: absolute;
		top: 20rpx;
		right: 20rpx;
	}
	.activity-name {
		padding: 0 10rpx;
		font-size: 36rpx;
		color: $uni-primary;
	}
	.title {
		padding: 20rpx 0;
		margin: 40rpx 0 20rpx;
		font-size: 32rpx;
		color: #fff;
		background-color: $uni-primary;
	}
	.qrcode {
		width: 200rpx;
		height: 200rpx;
	}
	.date {
		margin: 20rpx;
		font-size: 24rpx;
		color: #8c8c8c;
	}
	.userName {
		font-size: 30rpx;
		color: #333;
	}
	.phone {
		margin: 20rpx;
		font-size: 24rpx;
		color: #6f6f6f;
	}
	.org-name {
		font-size: 24rpx;
		color: #333;
	}
}
</style>
