<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="card-content" >
					<view class="price" :class="[item.condition?'fs20':'']">
						{{item | filterCoupon(that)}}
					</view>
					<view class="info">
						<view class="title">
							{{item.goodsCouponName}}
						</view>
						<view class="limit">
							{{item.useType | filterType(item)}}
						</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.couponGoodsLogId)" :src="item.couponGoodsLogId == choose?require('@/static/choosed.png'):require('@/static/unChoose.png')" mode=""></image>
					</view>
					
				</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 {getCouponType,getCouponList} from '@/api/goods.js'
	export default {
		data() {
			return {
				that:this,
				type:'',//判断是线下支付还是线上支付
				choose:'',
				open:999,
				status:'more',
				couponList:{},
				query:{
					currentPage:1,
					pageSize:10,
					shopId:'',
					userId: JSON.parse(uni.getStorageSync('userInfo')).userId
				},
				list:[],
				info:{},//订单金额信息，用于返回订单页面传回
			}
		},
		filters: {
			filterUseTime: function(val) {
				if(val.useType == 0){
					return '无限制'
				}else if(val.useType == 1){
					return val.useStartTime + '-' + val.useEndTime +'可使用'
				}else if(val.useType == 2){
					return '每周'+val.day
				}else if(val.useType == 3){
					return '每月'+val.day +'日'
				}
			},
			filterTime: function(val) {
				if(val.validType == 1){
					return '永久有效'
				}else {
					return val.validStartTime + '-' + val.validEndTime
				}
			},
			filterType: function(val,item) {
				if(val == 1){
					return '无使用限制'
				}else if(val == 2){
					return '使用期至' + item.useEndTime
				}else if(val == 3){
					return '每周'+ item.day +'可用'
				}else if(val == 4){
					return '每月'+ item.day +'可用'
				}
			},
			filterType1: function(val,item) {
				if(val == 1){
					return '有效期：无限制'
				}else if(val == 2){
					return '有效期：' + item.validStartTime + '-' + item.validEndTime
				}
			},
			filterCoupon:function(val,that){
				if(that.couponList[val.goodsCode] == '立减券'){
					return `¥${val.discount}`
				}else if(that.couponList[val.goodsCode] == '折扣券'){
					return `${val.discount/10}折`
				}else if(that.couponList[val.goodsCode] == '满减券'){
					return `满${val.condition}减${val.discount}`
				}
				
			}
		},
		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.getCouponList()
			},
			getCouponList(){
				this.status = 'loading'
				getCouponList(this.query).then(res=>{
					if(res.state == 'Success'){
						this.list = this.list.concat(res.content)
						this.list.length>= res.content.total?this.status = 'noMore':this.status = 'more'
						this.query.currentPage++
					}
				})
			},
			getCouponType(){
				getCouponType().then(res=>{
					if(res.state == 'Success'){
						this.couponList = res.content
					}
				})
			},
			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
			this.getCouponType()
		},
		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-bottom: 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: 0 32rpx;
		.card-content.card-bg{
			background-image: url('@/static/used-card-bg.png') ;
		}
		.card-content{
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: rgb(239, 239, 239);
			background-image: url('https://oss.dev.zonelife.cn/static/guida/img/life/card1.png') ;
			background-repeat: no-repeat;
			background-size: 100% 144rpx;
			border-radius: 16rpx;
			height: 144rpx;
			padding: 0 50rpx 0 160rpx; 
			margin-top: 20rpx;
			position: relative;
			.price.fs20{
				font-size: 20rpx;
			}
			.price{
				font-weight: bold;
				color: #FF4A39;
				font-size: 36rpx;
				position: absolute;
				top:50%;
				left: 30rpx;
				transform: translateY(-50%);
				width: 120rpx;
				text-align: center;
			}
			.info {
				.title {
					color: #0F0F0F;
					font-size: 30rpx;
				}
				.desc{
					display: flex;
					align-items: center;
					font-size: 20rpx;	
					color: #AAAAAA;
					.jiantou{
						width: 20rpx;
						height: 20rpx;
						transform: rotateZ(90deg);
						display: block;
						margin-left: 10rpx;
						transition: transform .3s;
					}
				}
				.open{
					.jiantou{
						transform: rotateZ(270deg)!important;
					}
				}
				.red{
					color: $uni-text-primary!important;
				}
				.limit{
					font-size: 20rpx;
					color: #AAAAAA;
					margin:10rpx 0;
				}
				.time {
					color: #9a9a9a;
					font-size: 24rpx;
					margin-top: 15rpx;
				}
			}
			.radio-box{
				.radio{
					width: 40rpx;
					height: 40rpx;
					vertical-align: bottom;
				}
				
			}
			.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>