<template>
	<view class="app-container">
		<view class="list-container">
			<block v-for="(item, index) in couponList" :key='index'>
				<view class="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">
										<view>领取时间：</view>
										<view>
											{{item.obtainStartTime | dateToYYmmdd2}}-{{item.obtainEndTime | dateToYYmmdd2}}
										</view>
									</view> -->
									<view class="date">
										<view>使用时间：</view>
										<view>
											{{item.activeStartTime | dateToYYmmdd2}}-{{item.activeEndTime | dateToYYmmdd2}}
										</view>
									</view>
									<view class="button red" @tap="handleCoupon(item)">立即领取</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</block>
		</view>
		<view  v-if="couponList.length" style="position: fixed;bottom: 0;left: 0;right: 0;box-sizing: border-box;padding: 30rpx;background: #ffffff;">
			<button type="primary" @tap="handleCouponAll">
				一键领取
			</button>
		</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: '/couponNew/list/all',
					method: 'get',
					params: {
						userId: this.userId
					},
					isLoading: 1
				}).then(res => {
					this.couponList = res.data;
				})
			},

			// 处理优惠券
			handleCoupon(item) {
				this.$axios({
					url: '/couponNew/obtain',
					method: 'get',
					params: {
						userId: this.userId,
						couponIds: item.couponId
					}
				}).then(res => {
					this.$successToast('领取成功');
					this.getCouponList();
				})
			},

			handleCouponAll() {
				this.$axios({
					url: '/couponNew/obtain',
					method: 'get',
					params: {
						userId: this.userId,
						couponIds: this.couponList.map(item => item.couponId).join(",")
					}
				}).then(res => {
					this.$successToast('领取成功');
					this.getCouponList();
				})
			},
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.list-container {
		box-sizing: border-box;
		padding-bottom: 160rpx;
		.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>