<template>
	<view class="coupon">
		<!-- 列表 -->
		<zs-list class="store-box" mt="0" @load="loadMore" :status="status">
			
			<view class="discounts-item" v-for="(item,index) in list" :key="index" >
					<view class="type-box">
						<view class="price">
							{{item | filterPrice}}
						</view>
						<view class="type">
							{{item | filterType}}
						</view>
					</view>
					<view class="info">
						<view class="title">
							{{item.couponName}}
						</view>
						<view class="limit">
							{{item.useStartTime}}后可用
						</view>
						<view class="desc">
							{{item.usrDetail || '-'}}
						</view>
						<!-- <view class="desc" :class="[open == index?'open':'']" @click.stop="handleOpen(index,'open')">
							展开详情 <image class="jiantou" src="@/static/jiantou-icon.png" mode=""></image>
						</view> -->
					</view>
					<view class="radio-box">
						<image class="radio"  @click="handleRadio(item.couponId)" :src="item.couponId == choose?require('@/static/choosed.png'):require('@/static/unChoose.png')" mode=""></image>
					</view>
					
				<!-- <view class="card-desc" v-show="open == index">
					<view class="time">
						{{item.validType | filterType1(item)}}
					</view>
					<view class="desc">
						{{item.goodsDescribe}}
					</view>
				</view> -->
			</view>
		</zs-list>
		
		<view class="btn-box">
			<button class="save-btn" type="default" @click="save">确定</button>
		</view>
		
	</view>
</template>

<script>
	import {getMyCouponList} from '@/api/coupon.js'
	import {couponByUser,claim} from '@/api/coupon.js'
	export default {
		data() {
			return {
				that:this,
				type:'',//判断是线下支付还是线上支付
				choose:'',
				open:999,
				status:'more',
				query:{
					currentPage:1,
					pageSize:10,
					status:1,
					// userId: JSON.parse(uni.getStorageSync('userInfo')).userId
				},
				list:[],
				info:{},//订单金额信息，用于返回订单页面传回
			}
		},
		filters: {
			filterPrice: function(val) {
				if(val.couponType == 1){
					return `￥${val.couponDiscount}`
				}else if(val.couponType == 2){
					return `${val.couponDiscount}折`
				}else if(val.couponType == 3){
					return `￥${val.couponDiscount}`
				}
			},
			filterType: function(val) {
				if(val.couponType == 1){
					if(val.couponCondition){
						return `满${val.couponCondition}立减`
					}else{
						return '无门槛立减'
					}
				}else if(val.couponType == 2){
					if(val.couponCondition){
						return `满${val.couponCondition}`
					}else{
						return '无门槛立减'
					}
				}else if(val.couponType == 3){
					if(val.couponCondition){
						return `满${val.couponCondition}立减`
					}else{
						return '无门槛立减'
					}
				}
			},
	
		},
		methods: {
			handleRadio(id){
				if(id == this.choose){
					this.choose = -1
				}else{
					this.choose = id
				}
			},
			
			handleOpen(val,open){
				if(val === this[open]){
					this[open] = 999
				}else{
					this[open] = val
				}
			},
			
			loadMore(){
				this.getMyCouponList()
			},
			getMyCouponList(){
				this.status = 'loading'
				couponByUser(this.query).then(res=>{
					if(res.state == 'Success'){
						this.list = this.list.concat(res.content.records)
						this.list.length>= res.content.total?this.status = 'noMore':this.status = 'more'
						this.query.currentPage++
					}
				})
			},
			save(id){
				this.info.couponId = this.choose
				let that = this
				if(this.type == 'offlinePayment'){//扫码支付
					uni.navigateTo({
						url:'/pay/offlinePayment?scene='+this.query.shopId,
						success: function(res) {
						  // 通过eventChannel向被打开页面传送数据
						  res.eventChannel.emit('pay', that.info)
						}
						
					})
				}else{//线上支付
					uni.navigateTo({
						url:'./pay',
						success: function(res) {
						  // 通过eventChannel向被打开页面传送数据
						  res.eventChannel.emit('pay', that.info)
						}
					})
				}
			},
		},
		created() {
			let info = JSON.parse(uni.getStorageSync('userInfo'))
			this.query.userId = info.userId
		},
		onLoad(options) {
			this.choose = options.couponId
			this.type = options.type
			let that = this
			const eventChannel = this.getOpenerEventChannel();
				eventChannel.on('pay', function(data) {
					that.info = data
					that.query.shopId = data.shopId
				})
		}
	}
</script>

<style lang="scss" scoped>
.coupon{
	background: #f5f5f5;
	padding: 20rpx 24rpx 130rpx;
	min-height: 100vh;
	.tab-group{
		display: flex;
		align-items: center;
		background: #fff;
		position: fixed;
		height: 80rpx;
		top: 0%;
		left: 0%;
		width: 100%;
		z-index: 9;
		.tab{
			width: 150rpx;
			padding: 15rpx 30rpx;
			text-align: center;
			color: #999999;
			font-size: 28rpx;
		}
		.active{
			font-weight: bold;
			font-size: 32rpx;
			position: relative;
			color: #222222;
			&::before{
				display: block;
				content: '';
				position: absolute;
				left: 50%;
				bottom: 0%;
				transform: translateX(-50%);
				width: 40rpx;
				height: 8rpx;
				background: linear-gradient(265deg, #FF4A39 0%, #FC8B45 100%);
				box-shadow: inset 0rpx 6rpx 12rpx 2rpx rgba(255,255,255,0.16);
				border-radius: 4rpx 4rpx 4rpx 4rpx;
			}
			
		}
	}
	
	.discounts-item {
		margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			background-color: rgb(239, 239, 239);
			background: url('@/static/new-card.png') no-repeat;
			background-repeat: no-repeat;
			background-size: 100% 144rpx;
			border-radius: 16rpx;
			height: 144rpx;
			padding: 0 24rpx;
			box-sizing: border-box;
			.type-box{
				width: 135rpx;
				text-align: center;
				.price{
					ont-size: 40rpx;
					color: #FF4D3A;
				}
				.type{
					font-size: 20rpx;
					color: #AAAAAA;
					margin-top: 12rpx;
				}
			}
			.info {
				flex: 1;
				margin-left: 40rpx;
				.title {
					font-size: 28rpx;
					color: #222222;
				}
				.desc{
					font-size: 20rpx;	
					color: #AAAAAA;
				}
				.limit{
					font-size: 20rpx;
					color: #AAAAAA;
					margin:10rpx 0;
				}
			}
			.radio-box{
				padding-right: 30rpx;
				.radio{
					width: 40rpx;
					height: 40rpx;
					vertical-align: bottom;
				}
				
				.unChoose{
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					box-sizing: border-box;
					border: 2rpx solid #FF4D3A;
				}
			}
			.num{
				position: absolute;
				white-space: nowrap;
				bottom: 10rpx;
				right: 10rpx;
				font-size: 18rpx;
				color: #FFFFFF;
			}
	
		.card-desc{
			font-size: 20rpx;
			color: #AAAAAA;
			padding: 20rpx 10rpx;
			background: #FFFFFF;
			border-radius: 0rpx 0rpx 16rpx 16rpx;
			.time{
				
			}
			.desc{
				margin-top: 22rpx;
			}
		}
	}
	
	.btn-box{
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		box-sizing: border-box;
		background: #fff;
		padding: 20rpx 0 40rpx;
		.save-btn{
			margin: 0 auto;
			width: 690rpx;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 40rpx;
			background: $uni-color-primary;
			color: #fff;
		}
	}
}
</style>