<template>
	<view>
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="我的盲票"></u-navbar>
		<!-- 优惠券筛选 -->
		<view class="prize-state">
			<u-tabs @change="changeTab" :scrollable="false" :list="stateArr" lineWidth="40" lineHeight="1"
				lineColor="#D70909" :activeStyle="{
									color: '#D70909',
									transform: 'scale(1)'
								}" :inactiveStyle="{
									color: '#333',
									transform: 'scale(1)'
								}" itemStyle="padding-left: 25px; padding-right: 25px; height: 44px;">
			</u-tabs>
		</view>
		<!-- 兑奖列表 -->
		<view class="prize-coupon">
			<view class="prize-coupon-list">
				<view class="flex prize-coupon-list-item" v-for="(item, index) in list" :key="index">
					<image :src="item.picUrl" mode="aspectFill"></image>
					<view class="flex info">
						<view class="flex desc">
							<view class="title">{{ item.title }}</view>
							<view class="txt">面值：{{ item.facePrice / 100 }}元</view>
							<view class="txt">序列号：{{ item.serialNo }}</view>
						</view>
						<view class="flex btn">
							<view class="action" @click="toChoice(item)" v-if="item.status == '2'">立即兑奖</view>
						</view>
					</view>
				</view>
			</view>
			<view class="flex empty" v-if="!list.length">
				<u-empty text="数据为空" mode="order" />
			</view>
		</view>
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				stateArr: [{
					name: ' 待兑奖',
				}, {
					name: ' 已兑奖',
				}],
				state: 2,
				pageNum: 1,
				total: 0,
				list: [],


			};
		},
		onShow() {
			this.pageList()
		},
		onLoad(){
			this.rewardNum()
		},
		methods: {
			getList() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post(`/api/v1/mp/user/mine/ticket/list?pageNum=${this.pageNum}&pageSize=20`, {
					status: this.state
				}).then(res => {
					uni.hideLoading();
					console.log(res)
					if (res.code == 0) {
						res.rows.forEach(item => {
							item.picUrl = env.filePublic + item.picUrl
						})
						this.total = res.total
						this.list = this.list.concat(res.rows)
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			pageList() {
				this.pageNum = 1
				this.list = []
				this.getList()
			},

			toChoice(item) {
				uni.navigateTo({
					url: `/pages/choice/index?id=${ item.ticketId }&type=onLine`
				})
			},

			changeTab(e) {
				if (e.index == 0) {
					this.state = 2
				} else if (e.index == 1) {
					this.state = 3
				}
				this.pageList()
			},


			rewardNum() {
				$http.post(`/api/v1/mp/user/mine/ticket/list?pageNum=${this.pageNum}&pageSize=20`, {
					status: 2
				}).then(res => {
					$http.post(`/api/v1/mp/user/mine/ticket/list?pageNum=${this.pageNum}&pageSize=20`, {
						status: 3
					}).then(data => {
						if (res.code == 0) {
							this.stateArr = [{
									name: ' 待兑奖 (' + res.total + ') '
								},
								{
									name: ' 已兑奖 (' + data.total + ') '
								},
							]
						} else {
							this.stateArr = [{
								name: ' 待兑奖 (0)',
							}, {
								name: ' 已兑奖 (0)',
							}, ]
						}
					})
				})
			}
		},

		onReachBottom() {
			// 判断是否有数据
			if (this.total > this.pageNum * 20) {
				setTimeout(() => {
					++this.pageNum
					this.getList()
				}, 500)
			} else {
				uni.$u.toast('没有更多数据了')
			}
		},
	}
</script>

<style lang="scss" scoped>
	.prize-state {
		position: fixed;
		background-color: #FFFFFF;
		width: 100%;
		padding-bottom: 16rpx;
		z-index: 10;
		box-shadow: 0 5rpx 5rpx #ececec;
	}

	.prize-coupon {
		margin-top: 104rpx;
		padding: 40rpx 30rpx 140rpx;

		&-list {

			&-item {
				justify-content: space-between;
				background-color: #FFFFFF;
				padding: 40rpx 20rpx;
				border-radius: 10rpx;
				margin-bottom: 24rpx;

				image {
					width: 124rpx;
					height: 174rpx;
				}

				.info {
					position: relative;
					justify-content: space-between;
					flex: 1;
				}

				.desc {
					height: 174rpx;
					flex-direction: column;
					justify-content: space-between;
					align-items: flex-start;
					padding-left: 20rpx;
				}

				.txt {
					font-size: 24rpx;
				}

				.btn {
					flex-direction: column;
				}

				.amt {
					font-size: 48rpx;
					font-weight: bold;
					line-height: 72rpx;
				}

				text {
					font-size: 24rpx;
				}

				.action {
					width: 124rpx;
					height: 40rpx;
					line-height: 40rpx;
					border-radius: 20rpx;
					background-color: rgba(215, 9, 9, 100);
					color: rgba(255, 255, 255, 100);
					font-size: 24rpx;
					text-align: center
				}
			}

			&-item:last-child {
				margin-bottom: 0;
			}
		}
	}

	.empty {
		height: 60vh;
	}
</style>
