<template>
	<view class="app-container">
		<view class="list-container">
			<block v-for="(item, index) in couponList" :key='index'>
				<view class="item" @tap="handleCoupon(item)">
					<view class="bg">
						<image src="@/static/mine/coupon/bg_1.png"></image>
					</view>
					<view class="content">
						<view class="left">
							<view class="price">{{item.couponValue}}<text>元</text></view>
							<view class="text" v-if="item.couponType == 'SATISFY'">满{{item.orderAmount}}可用</view>
						</view>
						<view class="right">
							<view class="main">
								<view class="row1 ellipsis-2">{{item.couponName}}</view>
								<view class="row2">
									<view class="date" v-if="item.obtainType == 0 || item.obtainType == 1">
										<view>领取时间：</view>
										<view>{{item.obtainStartTime | dateToYYmmdd2}}-{{item.obtainEndTime | dateToYYmmdd2}}</view>
									</view>
									<view class="date" v-if="item.obtainType == 2">
										<view>使用时间：</view>
										<view>{{item.activeStartTime | dateToYYmmdd2}}-{{item.activeEndTime | dateToYYmmdd2}}</view>
									</view>
									<view class="button gary" v-if="item.obtainType == 0">待开始</view>
									<view class="button red" v-if="item.obtainType == 1">立即领取</view>
									<view class="button white" v-if="item.obtainType == 2">去使用</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</block>
		</view>
		<no-data v-if="!couponList.length" :showText="'暂无可领取优惠券'"></no-data>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	
	export default {
		data() {
			return {
				couponList: [],
			}
		},
		
		computed:{
			...mapState(['userInfo', 'isLogin', 'userId'])
		},
		
		onLoad() {
			this.getCouponList();
		},
		
		methods: {
			getCouponList() {
				this.$axios({
					url: '/coupon/list/all',
					method: 'get',
					params: {
						userId: this.userId
					},
					isLoading: 1
				}).then(res => {
					this.couponList = res.data;
				})
			},
			
			// 处理优惠券
			handleCoupon(item) {
				if(item.obtainType == 0) {
					return this.$toast('该优惠券不可领取');
				}else if(item.obtainType == 1) {
					this.$axios({
						url: '/coupon/obtain',
						method: 'get',
						params: {
							userId: this.userId,
							couponId: item.couponId
						}
					}).then(res => {
						this.$successToast('领取成功');
						this.getCouponList();
					})
				}else if(item.obtainType == 2) {
					uni.switchTab({
					    url: '/pages/goods/classify'
					});
				}
			},
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		padding: 20rpx;
		box-sizing: border-box;
	}
	.list-container {
		.item {
			position: relative;
			margin-bottom: 20rpx;
			.bg {
				image {
					width: 710rpx;
					height: 160rpx;
					display: block;
				}
			}
			.content {
				position: absolute;
				left: 0;
				top: 0;
				width: 710rpx;
				height: 160rpx;
				display: flex;
				align-items: center;
				.left {
					width: 240rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;
					.price {
						font-size: 60rpx;
						color: #FFFFFF;
						text {
							font-size: 28rpx;
							margin-top: 20rpx;
						}
					}
					.text {
						color: #FFFFFF;
						font-size: 28rpx;
					}
				}
				.right {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 0 20rpx;
					width: 470rpx;
					height: 160rpx;
					box-sizing: border-box;
					.main {
						width: 430rpx;
						height: 160rpx;
						padding: 16rpx 0;
						box-sizing: border-box;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						.row1 {
							font-size: 28rpx;
							line-height: 32rpx;
						}
						.row2 {
							display: flex;
							justify-content: space-between;
							align-items: center;
							.date {
								font-size: 24rpx;
								color: #999999;
								line-height: 28rpx;
							}
							.button {
								width: 120rpx;
								height: 40rpx;
								border-radius: 40rpx;
								font-size: 24rpx;
								flex-shrink: 0;
								box-sizing: border-box;
								display: flex;
								justify-content: center;
								align-items: center;
								&.gary {
									background: #AAAAAA;
									color: #FFFFFF;
									border: 1px solid #AAAAAA;
								}
								&.red {
									background: #C434FF;
									color: #FFFFFF;
									border: 1px solid #C434FF;
								}
								&.white {
									color: #FF3F42;
									border: 1px solid #FF3F42;
								}
							}
						}
					}
				}
			}
		}
	}
</style>
