<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="60" 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="prize-coupon-list-item" v-for="(item, index) in list" :key="index">
					<image src="../../static/icon/coupon_bg2.png" mode=""></image>
					<view class="info">
						<view class="info-item">
							<text>使用期限：</text>
							<text>{{ $parseTime(item.validStart, '{y}.{m}.{d}') }}-{{ $parseTime(item.validEnd, '{y}.{m}.{d}') }}</text>
						</view>
						<view class="info-item">
							<text>使用范围：</text>
							<text>{{ item.useAreaDesc || '-' }}</text>
						</view>
					</view>
					<view class="flex price-title">
						<view class="price"><text>￥</text>{{ item.discount / 100 }}</view>
						<view class="title">{{ item.title }}</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] + '?imageView2/2/w/170'
							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 {
				position: relative;
				background-color: #FFFFFF;
				margin-bottom: 20rpx;
				border-radius: 10rpx;

				image {
					width: 100%;
					height: 156rpx;
					margin-bottom: 24rpx;
				}

				.info {
					padding-left: 40rpx;
					padding-bottom: 8rpx;

					.info-item {
						line-height: 40rpx;
						color: #333333;
						margin-bottom: 12rpx;
					}
				}

				.price-title {
					justify-content: flex-start;
					position: absolute;
					top: 0;
					width: 100%;
					padding: 36rpx 0 0 40rpx;

					.price {
						color: #FFFFFF;
						font-size: 60rpx;
						margin-right: 20rpx;

						text {
							font-size: 40rpx;
						}
					}

					.title {
						color: #FFFFFF;
						font-size: 48rpx;
					}
				}
			}

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

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