<template>
	<view>
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="我的盲票"></u-navbar>
		<!-- 状态 -->
		<view class="state">
			<u-tabs @change="changeTab" :scrollable="false" :list="stateArr" lineWidth="60" lineHeight="3"
				lineColor="#F9822C" :activeStyle="{
									color: '#F9822C',
									transform: 'scale(1.1)',
									width: '100px'
								}" :inactiveStyle="{
									color: '#999',
									transform: 'scale(1)',
									width: '100px'
								}" itemStyle="padding-left: 25px; padding-right: 25px; height: 44px; text-align: center;">
			</u-tabs>
		</view>
		<!-- 列表 -->
		<view class="list">
			<view class="list-item" v-for="(item, index) in list" :key="index">
				<image :src="item.picUrl" mode="aspectFit"></image>
				<view class="list-item-content flex">
					<view class="top">
						<view class="top-title">{{ item.title }}</view>
						<view class="top-num">序列号：{{ item.serialNo }}</view>
						<view class="top-price" v-if="item.status != '2'">面值：￥{{ $numberFormat(item.facePrice) }}</view>
					</view>
					<view class="bottom flex" v-if="item.status == '2'">
						<view class="bottom-price">面值：<text>￥{{ $numberFormat(item.facePrice) }}</text></view>
						<view class="bottom-btn" @click="toChoice(item)">立即兑奖</view>
					</view>
					<view class="bottom" v-else>
						<view class="bottom-price ells-one">奖品：<text>{{ item.prizeInfo || '-' }}</text></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();
					if (res.code == 0) {
						res.rows.forEach(item => {
							item.picUrl = env.filePublic + item.picUrl + '?imageView2/2/w/170'
						})
						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: `/packagePrize/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>
	// 状态
	.state {
		display: flex;
		position: fixed;
		background-color: #FFFFFF;
		width: 100%;
		z-index: 10;
		box-shadow: 0 5rpx 5rpx #ececec;
	}
	
	// 列表
	.list {
		padding: 120rpx 0 100rpx;
		margin: 0 34rpx 0;
		
		&-item {
			display: flex;
			background: #FFFFFF;
			border-radius: 11px;
			padding: 34rpx;
			margin-bottom: 34rpx;
			
			image {
				width: 200rpx;
				height: 270rpx;
				border-radius: 22rpx;
				margin-right: 15rpx;
			}
			
			&-content {
				flex: 1;
				flex-direction: column;
				justify-content: space-between;
				padding: 16rpx 0;
				
				.top {
					width: 100%;
					display: flex;
					flex-direction: column;
					
					&-title {
						font-size: 36rpx;
						line-height: 36rpx;
						font-weight: bold;
						margin-bottom: 46rpx;
					}
					
					&-num {
						font-size: 26rpx;
						line-height: 26rpx;
					}
					
					&-price {
						margin-top: 30rpx;
						font-size: 26rpx;
						line-height: 26rpx;
					}
				}
				
				.bottom {
					width: 100%;
					justify-content: space-between;
					
					&-price {
						font-size: 26rpx;
						line-height: 26rpx;
						
						text {
							color: #F9822C ;
						}
					}
					
					&-btn {
						width: 220rpx;
						height: 72rpx;
						line-height: 72rpx;
						text-align: center;
						background: #F9822C;
						border-radius: 36rpx;
						color: #FFFFFF;
						font-size: 30rpx;
					}
				}
			}
		}
	}

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