<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">使用期限：{{ $parseTime(item.validStart, '{y}.{m}.{d}') }}-{{ $parseTime(item.validEnd, '{y}.{m}.{d}') }}</view>
							<view class="txt">使用范围：{{ item.useAreaDesc }}</view>
						</view>
						<view class="flex btn">
							<view class="amt"><text>￥</text>{{ item.discount / 100 }}</view>
						</view>
						<view class="state" v-if="state == 2">已使用</view>
						<view class="state" v-else>已过期</view>
					</view>
				</view>
			</view>
		</view>
		<view class="flex empty" v-if="!list.length">
			<u-empty text="数据为空" mode="order" />
		</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()
		},
		methods: {
			getList() {
				let _this = this
				let url = '/api/v1/mp/user/mine/coupon/list'
				uni.showLoading({
					title: '加载中'
				});
				this.loading = true
				$http.post(`${ url }?pageNum=${_this.pageNum}&pageSize=20`, {
					status: this.state
				}).then(res => {
					uni.hideLoading();
					this.loading = false
					if (res.code == 0) {
						res.rows.forEach(item => {
							let picUrlArr = item.picUrl.split(',')
							item.picUrl = env.filePublic + picUrlArr[0]
							item.status = JSON.parse(item.status)
						})
						_this.total = res.total
						_this.list = _this.list.concat(res.rows)
					}
				}).catch(() => {
					uni.hideLoading();
					this.loading = false
				})
			},
			
			pageList() {
				this.pageNum = 1
				this.list = []
				this.getList()
			},
			
			changeTab(e) {
				if (e.index == 0) {
					this.state = 2
				} else if (e.index == 1) {
					this.state = 3
				}
				this.pageList()
			},

			changeChecked() {

			},
		},
		
		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;
	}

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

		&-list {

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

				image {
					width: 94rpx;
					height: 132rpx;
				}

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

				.desc {
					height: 132rpx;
					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;
				}
				
				.state{
					position: absolute;
					right: 50rpx;
					transform: rotate(20deg);
					-webkit-transform:rotate(20deg);
					width: 160rpx;
					height:160rpx;
					line-height: 160rpx;
					border: #E1E1E1 2px solid;
					border-radius: 50%;
					background-color: #f9f2ef;
					text-align: center;
					color: #000000;
					opacity: .5;
				}
			}

			&-item:last-child {
				margin-bottom: 0;
			}
		}
	}
	
	.empty{
		height: 60vh;
	}
</style>
