<template>
	<view class="coupon">
		<view class="tab-group">
			<view class="tab" :class="[query.status == 1?'active':'']" @click="handleTab(1)">
				未使用
			</view>
			<view class="tab" :class="[query.status == 2?'active':'']" @click="handleTab(2)">
				已使用
			</view>
		</view>
		
		<!-- 列表 -->
		<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" :class="[query.status == 2?'card-bg':'']">
					<view class="info">
						<view class="title">
							{{item.goodsName}}
						</view>
						<view class="limit">
							{{item.useType | filterType(item)}}
						</view>
						
						<view class="desc" :class="[open == index?'open':'']" @click="handleOpen(index,'open')">
							展开详情 <image class="jiantou" src="../../static/jiantou-icon.png" mode=""></image>
						</view>
					</view>
					<view class="btn-box">
						<view class="btn" >
							{{query.status == 2?'已使用':'未使用'}}
						</view>
					</view>
					<view class="num" v-if="item.buySet == 2">
						剩余领取次数：{{item.useCount}}次
					</view>
				</view>
				<view class="card-desc" v-show="open == index">
					<view class="time">
						{{item.validType | filterType1(item)}}
					</view>
					<view class="desc" v-html="item.goodsDescribe">
						{{item.goodsDescribe}}
					</view>
				</view>
			</view>
		</zs-list>
		
		
	</view>
</template>

<script>
	import {getWhetherCoupon} from '@/api/goods.js'
	export default {
		data() {
			return {
				active: 1,
				open:999,
				status:'more',
				query:{
					currentPage:1,
					pageSize:10,
					status:1,//
					setMealId:JSON.parse(uni.getStorageSync('userInfo')).setMealId,
					userId: JSON.parse(uni.getStorageSync('userInfo')).userId
				},
				list:[]
			}
		},
		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
				}
			}
		},
		methods: {
			handleOpen(val,open){
				if(val === this[open]){
					this[open] = 999
				}else{
					this[open] = val
				}
			},
			handleTab(val) {
				this.query.status = val
				this.list = []
				this.status = 'more'
				this.query.currentPage = 1
				this.getWhetherCoupon()
			},
			loadMore(){
				this.getWhetherCoupon()
			},
			getWhetherCoupon(){
				this.status = 'loading'
				getWhetherCoupon(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++
					}
				})
			}
		},
		created() {
			let info = JSON.parse(uni.getStorageSync('userInfo'))
			this.query.userId = info.userId
			this.query.setMealId = info.setMealId
		}
	}
</script>

<style lang="scss" scoped>
.coupon{
	background: #f5f5f5;
	height: 100vh;
	padding-top: 80rpx;
	.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: url('../../static/used-card-bg.png') no-repeat;
			background-size: 100% 144rpx;
		}
		.card-content{
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: rgb(239, 239, 239);
			background: url('../../static/card.png') no-repeat;
			background-repeat: no-repeat;
			background-size: 100% 144rpx;
			border-radius: 16rpx;
			height: 144rpx;
			padding: 0 30rpx 0 48rpx;
			margin-top: 20rpx;
			position: relative;
			.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;
				}
			}
			.btn-box{
				.btn {
					width: 118rpx;
					height: 48rpx;
					line-height: 48rpx;
					text-align: center;
					font-size: 24rpx;
					background: linear-gradient(90deg, #FEFBF2 0%, #F7D188 100%);
					border-radius: 35rpx;
					color: #E5452C;
					margin-top: 15rpx;
				}
				
			}
			.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;
			}
		}
	}
	
	.item{
		display: flex;
		align-items: center;
		margin: 30rpx;
		background: url('../../static/card.png') no-repeat;
		background-size: 100% 100%;
		width: 690rpx;
		height: 160rpx;
		box-sizing: border-box;
		padding: 30rpx;
		.info{
				display: flex;
			.left{
				width: 160rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: space-between;
				.price{
					font-weight: bold;
					color: #222222;
					font-size: 36rpx;
				}
				.limit{
					color: #999999;
					font-size: 24rpx;
					margin-top: 10rpx;
				}
			}
			.right{
				padding-left: 30rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.title{
					font-size: 28rpx;		
					color: #222222;
				}
				.time{
			
					font-size: 24rpx;		
					color: #999999;
					margin-top: 10rpx;
				}
			}
		}
	}
}
</style>