<template>
	<view class="app-container">
		<view class="list-container">
			<block v-for="(item, index) in couponList" :key='index'>
				<view class="item" @tap="chooseCoupon(index, item.useableFlag)">
					<view class="bg">
						<image src="@/static/mine/coupon/bg_0.png" v-if="!item.useableFlag"></image>
						<image src="@/static/mine/coupon/bg_1.png" v-if="item.useableFlag"></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">
										<view>使用时间：</view>
										<view>{{item.activeStartTime | dateToYYmmdd2}}-{{item.activeEndTime | dateToYYmmdd2}}</view>
									</view>
									<view class="button2" v-if="!item.useableFlag">不可用</view>
									<view class="button" v-if="item.useableFlag">使用</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</block>
		</view>
		<no-data v-if="!couponList.length" :showText="'暂无可用优惠券'"></no-data>
		
		<view class="bottom-container">
			<view class="button" @tap="noUseCoupon">不使用优惠券</view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	import EventBus from '@/utils/eventbus.js';
	
	export default {
		data() {
			return {
				couponList: [],
				orderAmount: 0,
				goodsIds: [],
			}
		},
		
		computed:{
			...mapState(['userInfo', 'isLogin', 'userId'])
		},
		
		onLoad({orderAmount, goodsIds}) {
			this.orderAmount = orderAmount;
			this.goodsIds = JSON.parse(goodsIds);
			this.getCouponList();
		},
		
		methods: {
			getCouponList() {
				this.$axios({
					url: '/coupon/list/useable',
					method: 'get',
					params: {
						orderAmount: this.orderAmount,
						goodsSpecIds: this.goodsIds.join(','),
						userId: this.userId
					},
					isLoading: 1
				}).then(res => {
					this.couponList = res.data;
				})
			},
			
			// 选择优惠券
			chooseCoupon(index, canUse) {
				if(!canUse) {
					return this.$toast('该优惠券不可用');
				}
				EventBus.$emit('chooseCoupon', this.couponList[index]);
				uni.navigateBack({
					delta: 1
				})
			},
			
			// 不使用优惠券
			noUseCoupon() {
				uni.$emit('chooseCoupon', '');
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		padding: 20rpx 20rpx 120rpx;
		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: 100rpx;
								height: 40rpx;
								text-align: center;
								line-height: 40rpx;
								border-radius: 40rpx;
								border: 1px solid #FF3F42;
								font-size: 24rpx;
								color: #FF3F42;
								flex-shrink: 0;
							}
							.button2 {
								width: 100rpx;
								height: 40rpx;
								text-align: center;
								line-height: 40rpx;
								border-radius: 40rpx;
								border: 1px solid #b0b0b0;
								font-size: 24rpx;
								color: #999999;
								flex-shrink: 0;
							}
						}
					}
					.tag {
						position: absolute;
						right: 20rpx;
						top: 10rpx;
						image {
							width: 80rpx;
							height: 80rpx;
						}
					}
				}
			}
		}
	}
	.bottom-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 0 20rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		height: 100rpx;
		.button {
			width: 100%;
			height: 68rpx;
			line-height: 68rpx;
			border-radius: 68rpx;
			text-align: center;
			font-size: 28rpx;
			color: #666666;
			border: 1px solid #B0B0B0;
		}
	}
</style>
