<template>
	<view>
		<lf-nav :showIcon="true" :spreadOut="false" bgColor="transparent"></lf-nav>
		<view class="head">
			<image class="img" mode="aspectFill" src="https://picsum.photos/seed/picsum/200/300"></image>
			<view class="suspension" hover-class="lf-opacity" @click="$url('/pages/coupon/index/index')">我的优惠券</view>
		</view>
		<view class="coupon-wrap">
			<view class="coupon-box" v-for="(item,index) of coupon_list" :key="index">
				<view class="coupon-card lf-m-b-30">
					<view class="coupon-circle-top">
						<view class="coupon-circle1"></view>
					</view>
					<view class="coupon-circle-bottom">
						<view class="coupon-circle1"></view>
					</view>
					<view class="coupon-radius">
						<view class="coupon-left">
							<view class="lf-color-white" v-if="item.action_type.type == 'cash'">
								<text class="lf-font-24">￥</text>
								<text class="lf-font-70 lf-color-white lf-font-bold">{{item.action_type.value}}</text>
							</view>
						</view>
						<view class="coupon-right">
							<view class="lf-font-32 lf-font-bold lf-color-white">{{item.title}}</view>
							<view class="lf-font-24 lf-color-white">有效期{{item.starts_at}}~{{item.ends_at}}</view>
						</view>
					</view>
				</view>
				<view class="coupon-receive" @click="receive(item.code)">立即领取</view>
				<block v-if="item.ifpast">
					<view class="coupon-opacity"></view>
					<view class="coupon-end">
						<view>抢光了</view>
					</view>
				</block>
			</view>
		</view>
		<u-back-top :scrollTop="pageScrollTop"></u-back-top>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				coupon_list: []
			}
		},
		onLoad(){
			this.getCouponsList();
		},
		onPullDownRefresh() {
			this.getCouponsList();
		},
		methods: {
			receive(code){
				this.$http.post({
					api: 'api/coupon/convert',
					data: {
						coupon_code: code
					},
					header: {
					   Authorization: this.$cookieStorage.get('user_token')
					}
				}).then(res => {
					if(res.data.code == 200) {
						this.$msg('领取成功');
						this.getCouponsList();
					}else {
						this.$msg(JSON.stringify(res.data.message));
					}
				}).catch(err => {
					console.log("====", err);
				})
			},
			compareDate(val) {
				var nowTime = new Date(new Date().toLocaleDateString()).getTime();
				let oldTime = new Date(new Date(val).toLocaleDateString()).getTime();
				if(nowTime>oldTime) {
					return true;
				}else {
					return false;
				}
			},
			//获取优惠券列表
			getCouponsList() {
				this.$http.get({
					api: 'api/coupons/list',
					header: {
					   Authorization: this.$cookieStorage.get('user_token')
					}
				}).then(res => {
					this.coupon_list = res.data.data;
					this.coupon_list.forEach((item,index) => {
						if(this.compareDate(item.ends_at)) {
							this.$set(item,'ifpast',true);
						}else {
							this.$set(item,'ifpast',false);
						}
					})
					uni.stopPullDownRefresh();
				}).catch(err => {
					console.log("====", err);
				})
			}
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.head{
		height: 376rpx;
		width: 750rpx;
		position: relative;
		.img{
			width: 100%;
			height: 100%;
		}
		.suspension{
			position: absolute;
			bottom: 30rpx;
			right: 0;
			width: 165rpx;
			height: 54rpx;
			background: #15716E;
			border-radius: 100rpx 0rpx 0rpx 100rpx;
			border: 2rpx solid #FFFFFF;
			font-size: 24rpx;
			color: #FFFFFF;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
	
	.coupon-wrap {
		display: flex;
		justify-content: center;
		margin-top: 30rpx;
		flex-direction: column;
		align-content: center;
		align-items: center;
	}
	.coupon-box{
		display: flex;
		align-items: center;
		position: relative;
	}
	.coupon-card {
		overflow: hidden;
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 637rpx;
		// display: flex;
		height: 171rpx;
		// opacity: 0.59;
		// border: 1rpx solid #FFFFFF;
		background: #15716E;
		border-radius: 20rpx;
		z-index: 3;
	}
	.coupon-radius {
		width: 627rpx;
		display: flex;
		height: 161rpx;
		border: 1rpx dashed #ccc;
		// background: #15716E;
		border-radius: 20rpx;
	}
	.coupon-circle1 {
		width: 40rpx;
		height: 40rpx;
		background-color: white;
		border-radius: 50%;
	}
	.coupon-circle-top {
		width: 50rpx;
		height: 50rpx;
		border-radius: 50%;
		// background-color: red;
		position: absolute;
		border: 1px dashed #ccc;
		left: 190rpx;
		top: -20rpx;
		display: flex;
		align-items: center;
		text-align: center;
		justify-content: center;
	}
	.coupon-circle-bottom {
		width: 50rpx;
		height: 50rpx;
		border-radius: 50%;
		// background-color: red;
		position: absolute;
		border: 1px dashed #ccc;
		left: 190rpx;
		bottom: -20rpx;
		display: flex;
		align-items: center;
		text-align: center;
		justify-content: center;
	}
	.coupon-right {
		text-align: left;
		justify-content: center;
		align-items: flex-start;
		display: flex;
		flex-direction: column;
		margin-left: 84rpx;
	}
	.coupon-left {
		margin-left: 40rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	.coupon-receive{
		width: 45rpx;
		height: 126rpx;
		font-size: 24rpx;
		background: #22A19F;
		border-radius: 6rpx;
		line-height: 1.2;
		color: #FFFFFF;
		text-align: center;
		display: flex;
		justify-content: center;
		align-items: center;
		transform: translate(-16rpx, -9rpx) rotate(10deg);
		transform-origin: bottom;
		position: relative;
		z-index: 1;
	}
	.coupon-opacity{
		width: 102%;
		height: 100%;
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		background-color: rgba(255,255,255,0.5);
		z-index: 5;
	}
	.coupon-end{
		position: absolute;
		z-index: 7;
		width: 136rpx;
		height: 136rpx;
		background-color: #15716E;
		border-radius: 50%;
		top: calc(50% - 81rpx);
		left: calc(50% - 68rpx);
		display: flex;
		justify-content: center;
		align-items: center;
		&>view{
			width: 126rpx;
			height: 126rpx;
			border: 2rpx dashed #FFFFFF;
			border-radius: 50%;
			font-size: 32rpx;
			color: #FFFFFF;
			display: flex;
			justify-content: center;
			align-items: center;
			transform: rotate(-38deg);
		}
	}
</style>
