<template>
	<view class="prize">
		<!-- 分段器 -->
		<view class="segmented">
			<view class="tab_nav">
				<view class="navTitle" v-for="(item,index) in items" :key="index">
					<view :class="{'active':isActive == index}" @click="checked(index)">
						{{item}}
					</view>
				</view>
			</view>
		</view>
		<!-- 占位 -->
		<view style="width: 100%;height: 112rpx;"></view>

		<view class="msg-null" v-if="isActive == 0 && PhysicalPrizes.length == 0 ">
			<image src="https://t9.9026.com/imgs/dataNull.png" style="width: 394rpx;height: 396rpx;" mode=""></image>
			<view class="msg-null-text">
				<text>暂无数据</text>
			</view>
		</view>

		<view class="msg-null" v-if="isActive == 1 && VirtualPrizes.length == 0 ">
			<image src="https://t9.9026.com/imgs/dataNull.png" style="width: 394rpx;height: 396rpx;" mode=""></image>
			<view class="msg-null-text">
				<text>暂无数据</text>
			</view>
		</view>
		<view class="msg-null" v-if="isActive == 2 && prizeRecord.length == 0 ">
			<image src="https://t9.9026.com/imgs/dataNull.png" style="width: 394rpx;height: 396rpx;" mode=""></image>
			<view class="msg-null-text">
				<text>暂无数据</text>
			</view>
		</view>


		<view style="margin-top: 24rpx; background-color: #f9f9f9; ">
			<!-- 实物奖品 -->
			<view class="prizeList" :style="{'--height':ListHeight+'rpx'}"
				v-if="isActive == 0  && PhysicalPrizes.length > 0  ">
				<view class="ListItem" v-for="(item,index) in PhysicalPrizes" :key="index">
					<view class="title"><text>{{item.name}}</text></view>
					<view class='data'><text>{{item.exchange_start_time}}--{{item.exchange_end_time}}</text></view>
					<view class="alreadyBtn" v-if="item.status == 2">
						<text>已兑换</text>
					</view>
					<view class="instantBtn" v-if="item.status == 1" @click="goExchangePhy(item.id,item.product_id)">
						<text>立即兑换</text>
					</view>
					<view class="alreadyBtn" v-if="item.status == 3">
						<text>已过期</text>
					</view>
				</view>
			</view>

			<!-- 线下奖品 -->
			<view class="prizeList" :style="{'--height':ListHeight+'rpx'}"
				v-if="isActive == 1  && VirtualPrizes.length > 0 ">
				<view class="ListItem" v-for="(item,index) in VirtualPrizes" :key="index">
					<view class="title"><text>{{item.name}}</text></view>
					<view class='data'><text>{{item.exchange_start_time}}--{{item.exchange_end_time}}</text></view>
					<view class="instantBtn" v-if="item.status == 1" @click="goExchangeVir(item.id,item.product_id)">
						<text>立即兑换</text>
					</view>
					<view class="alreadyBtn" v-if="item.status == 2">
						<text>已兑换</text>
					</view>
					<view class="alreadyBtn" v-if="item.status == 3">
						<text>已过期</text>
					</view>
					<view class="alreadyBtn" v-if="item.status == 4">
						<text>未开始</text>
					</view>

				</view>
			</view>

			<!-- 中奖记录 -->
			<view class="prizeList" :style="{'--height':ListHeight+'rpx'}" style="padding-bottom: 40rpx;"
				v-if="isActive == 2 && prizeRecord.length > 0 ">
				<view class="ListItem" v-for="(item,index) in prizeRecord" :key="index">
					<view style="display: flex; justify-content: space-between;">
						<view class="title"><text>{{item.name || '积分奖品'}}</text> </view>
					</view>
					<view class="content"><text>{{item.remark||'无'}}</text></view>
					<view class='data'>
						<text>{{item.created_at}}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 已经到底啦 -->
		<view class="home-bottom" style="margin-top: 88rpx;padding-bottom: 60rpx;"
			v-if="isActive == 0  && PhysicalPrizes.length > 0  ">
			<uni-load-more :status="status" color="#CCCCCC" :content-text="contentText" />
		</view>

		<view class="home-bottom" style="margin-top: 88rpx;padding-bottom: 60rpx;"
			v-if="isActive == 1  && VirtualPrizes.length > 0  ">
			<uni-load-more :status="status" color="#CCCCCC" :content-text="contentText" />
		</view>
		<view class="home-bottom" style="margin-top: 88rpx;padding-bottom: 60rpx;"
			v-if="isActive == 2  && prizeRecord.length > 0  ">
			<uni-load-more :status="status" color="#CCCCCC" :content-text="contentText" />
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 分段器标题
				items: ['实物奖品', '虚拟奖品', '中奖记录'],
				isActive: 0,
				// 列表高度
				ListHeight: 800,
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '—— 已经到底啦 ——'
				},
				// 实物奖品列表
				PhysicalPrizes: [],
				// 虚拟奖品列表
				VirtualPrizes: [],
				// 中奖记录
				prizeRecord: [],
				// 分页
				page: 1,
				pagesize: 15,
				totalElements: '',
				allListItem: '',
				// 组件uni-load-more
				status: 'noMore',
				totalElementsPhy: '',
				totalElementsVir: '',
			}
		},
		onLoad(o) {
			if (o.product_type == 2) {
				//分段器标题切换
				this.checked(o.product_type - 1)
			} else {
				//实物奖品
				this.getPrizeList(0)
			}
			uni.$on('refreshPrizeData', () => {
				// 中奖记录
				this.getPrizeRecord()
				//实物奖品
				this.getPrizeList(0)
				//虚拟奖品
				this.getPrizeList(1)
			})
		},

		onUnload() {
			uni.$emit('refreshOrderNum')
		},

		onReachBottom() {
			// 触底的时候请求数据，即为上拉加载更多
			var allTotal = this.page * this.pagesize
			//this.page为加载次数，this.pagesize为每一次加载的数据条数
			if (this.isActive == 1) {
				console.log('虚拟')
				if (allTotal < this.totalElementsVir) {
					//this.totalElements为请求数据的总条数。只要现有条数小于总条数就就执行一下代码
					this.allListItem = false;
					this.page++;
					//加载次数递加
					this.status = "loading"
					this.$api.lottery.getDrawRecord({ //请求更多数据列表
						page: this.page,
						is_virtual: this.isActive,
					}).then(res => {
						let ret = [...this.VirtualPrizes, ...res.data.data]
						this.VirtualPrizes = ret
						console.log(ret)
					})
				} else {
					this.allListItem = true;
					console.log('已加载全部数据')
					this.status = "noMore"
				}
			} else if (this.isActive == 2) {
				if (allTotal < this.totalElements) {
					//this.totalElements为请求数据的总条数。只要现有条数小于总条数就就执行一下代码
					this.allListItem = false;
					this.page++;
					//加载次数递加
					this.status = "loading"
					this.$api.lottery.getDrawRecord({ //请求更多数据列表
						page: this.page,
					}).then(res => {
						let ret = [...this.prizeRecord, ...res.data.data]
						this.prizeRecord = ret
						console.log(ret)
					})
				} else {
					this.allListItem = true;
					console.log('已加载全部数据')
					this.status = "noMore"
				}
			} else if (this.isActive == 0) {
				console.log(allTotal, '实物');
				if (allTotal < this.totalElementsPhy) {
					//this.totalElements为请求数据的总条数。只要现有条数小于总条数就就执行一下代码
					this.allListItem = false;
					this.page++;
					//加载次数递加
					this.status = "loading"
					this.$api.lottery.getDrawRecord({ //请求更多数据列表
						page: this.page,
						is_virtual: this.isActive,
					}).then(res => {
						let ret = [...this.PhysicalPrizes, ...res.data.data]
						this.PhysicalPrizes = ret
						console.log(ret)
					})
				} else {
					this.allListItem = true;
					console.log('已加载全部数据')
					this.status = "noMore"
				}
			}
		},

		methods: {
			// 获取奖品列表
			getPrizeList(type) {
				this.$api.lottery.getDrawRecord({
					is_virtual: type,
					page: 0
				}).then(res => {
					//实物奖品
					if (type == 0) {
						this.PhysicalPrizes = res.data.data
						this.totalElementsPhy = res.data.total
						this.pagesize = res.data.per_page
					} else {
						this.VirtualPrizes = res.data.data
						this.totalElementsVir = res.data.total
						this.pagesize = res.data.per_page
						console.log(this.totalElementsVir, '虚拟');
					}
				})
			},
			// 获取中奖记录
			getPrizeRecord() {
				this.$api.lottery.getDrawRecord({
					page: 0
				}).then(res => {
					console.log(res, "中奖记录")
					if (res.code == 0) {
						this.prizeRecord = res.data.data
						this.totalElements = res.data.total
						this.pagesize = res.data.per_page
					}
				})
			},
			//分段器标题切换
			checked(index) {
				if (index <= 1) {
					this.page = 1
					this.isActive = index
					console.log(this.isActive, '------->this.isActive');
					this.getPrizeList(index)
				} else {
					this.page = 1
					this.isActive = index
					this.getPrizeRecord()
				}
			},
			// 实物立即兑换
			goExchangePhy(id, product_id) {
				uni.navigateTo({
					url: `/pages/my/prize/exchangePrize?id=${id}&isPhy=1&product_id=${product_id}`
				})
			},
			// 虚拟立即兑换
			goExchangeVir(id, product_id) {
				uni.navigateTo({
					url: `/pages/my/prize/exchangePrize?id=${id}&isPhy=2&product_id=${product_id}`
				})
			},
		},

	}
</script>

<style lang="scss" scoped>
	$pageColor:#F9F9F9;
	$bgColor:#FFFFFF;

	@mixin flexlayout {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.prize {
		height: 100%;
		background: $pageColor;
	}

	.msg-null {
		padding-top: 60rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.msg-null-text {
			margin-top: 40rpx;
			font-size: 28rpx;
			color: #333;
		}
	}

	.segmented {
		position: fixed;
		top: 0;
		z-index: 999;
		width: 750rpx;
		height: 112rpx;
		background: $bgColor;
		box-shadow: 0px 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.04);
		border-radius: 0rpx 0rpx 16rpx 16rpx;
		margin-bottom: 24rpx;
		padding: 0rpx 30rpx;
		box-sizing: border-box;

		//菜单切换
		.tab_nav {
			width: 690rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-family: PingFang-SC-Heavy, PingFang-SC;
		}

		.tab_nav .navTitle {
			margin-top: 20rpx;
			width: 128rpx;
			flex: none;
			height: 28rpx;
			font-size: 32rpx;
			color: #666;
			position: relative;
		}

		.active {
			color: #D9A94D;
			font-weight: bold;

			&::after {
				display: inline-block;
				content: '';
				width: 48rpx;
				height: 12rpx;
				background: linear-gradient(90deg, #F3D69F 0%, #D9A94D 100%);
				border-radius: 6px;
				position: absolute;
				bottom: -30rpx;
				left: 40rpx;
			}
		}
	}

	::v-deep .segmented-control__text {
		font-size: 32rpx !important;
		font-family: PingFang-SC-Heavy, PingFang-SC;
		font-weight: 500 !important;
	}

	::v-deep .segmented-control__item--text {
		font-size: 32rpx;
		font-family: PingFang-SC-Heavy, PingFang-SC;
		font-weight: 800 !important;
		padding: 10rpx 0 !important;
		border-radius: 4rpx !important;
	}

	.prizeList {
		width: 750rpx;
		// height: var(--height);
		background: $bgColor;
		box-shadow: 0px 4rpx 24rpx -10rpx rgba(101, 95, 90, 0.3);
		border-radius: 12rpx;
		padding: 0rpx 30rpx 0rpx;
		box-sizing: border-box;

		.ListItem {
			width: 690rpx;
			border-bottom: #F0F0F0 solid 0.5rpx;
			position: relative;
			padding-top: 35rpx;
			padding-bottom: 35rpx;
			box-sizing: border-box;

			.title {
				margin-bottom: 5rpx;

				text {
					font-size: 32rpx;
					font-family: PingFang-SC-Bold, PingFang-SC;
					font-weight: bold;
					color: #333333;
				}
			}

			.data {
				text {
					font-size: 26rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #999999;
				}
			}

			.alreadyBtn {
				position: absolute;
				top: 50rpx;
				right: 0rpx;
				width: 148rpx;
				height: 60rpx;
				background: $bgColor;
				border-radius: 30rpx;
				border: 2rpx solid #D0D0D0;
				@include flexlayout();

				text {
					font-size: 26rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #D0D0D0;
				}
			}

			.instantBtn {
				position: absolute;
				top: 50rpx;
				right: 0rpx;
				width: 148rpx;
				height: 60rpx;
				background: $bgColor;
				border-radius: 30rpx;
				border: 2rpx solid #FF6200;
				@include flexlayout();

				text {
					font-size: 26rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					color: #FF6200;
				}
			}

			.content {
				text {
					font-size: 28rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #333333;
				}
			}
		}

		.ListItem:last-child {
			width: 690rpx;
			height: 160rpx;
			border-bottom: #F0F0F0 solid 0rpx;
			position: relative;
			padding-top: 35rpx;
			box-sizing: border-box;

			.title {
				margin-bottom: 10rpx;

				text {
					font-size: 32rpx;
					font-family: PingFang-SC-Bold, PingFang-SC;
					font-weight: bold;
					color: #333333;
				}
			}

			.data {
				text {
					font-size: 26rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					color: #999999;
				}
			}

			.alreadyBtn {
				position: absolute;
				top: 50rpx;
				right: 0rpx;
				width: 148rpx;
				height: 60rpx;
				background: $bgColor;
				border-radius: 30rpx;
				border: 2rpx solid #D0D0D0;
				@include flexlayout();

				text {
					font-size: 26rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #D0D0D0;
				}
			}

			.instantBtn {
				position: absolute;
				top: 50rpx;
				right: 0rpx;
				width: 148rpx;
				height: 60rpx;
				background: $bgColor;
				border-radius: 30rpx;
				border: 2rpx solid #FF6200;
				@include flexlayout();

				text {
					font-size: 26rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #FF6200;
				}
			}

			.content {
				text {
					font-size: 28rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #333333;
				}
			}
		}
	}
</style>
