<template>
	<view class="coupon">
		<view class="tab-box">
			<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 class="tab" :class="[query.status == 3?'active':'']" @click="handleTab(3)">
				已失效
			</view>
		</view>
		<!-- 列表 -->
		<zs-list class="store-box" mt="84rpx" @load="loadMore" :status="status">
			
			<view class="discounts-item" :class="[query.status != 1?'disabled':'']" 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>
					
					<view class="btn" v-if="query.status == 1">
						待使用
					</view>
				   <image class="icon" v-else-if="query.status == 2" src="../../static/used-icon.png" mode=""></image>
				   <image class="icon" v-else-if="query.status == 3" src="../../static/disable-icon.png" mode=""></image>
					
			</view>
		</zs-list>
		
		
	</view>
</template>

<script>
	import {couponByUser,claim} from '@/api/coupon.js'
	export default {
		data() {
			return {
				active: 0,
				open:999,
				status:'more',
				query:{
					currentPage:1,
					pageSize:10,
					status:1,
				},
				list:[],
				tabList:[]
			}
		},
		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: {
			handleTab(val){
				this.query.status = val
				this.query.currentPage = 1
				this.list = []
				this.status = 'more'
				this.getCouponList()
			},
			handleOpen(val,open){
				if(val === this[open]){
					this[open] = 999
				}else{
					this[open] = val
				}
			},
			loadMore(){
				this.getCouponList()
			},
			getCouponList(){
				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++
					}
				})
			},
			claim(couponId,type){
				if(type == 0){
					claim({couponId}).then(res=>{
						if(res.state == 'Success'){
							uni.showToast({
								title:'领取成功',
								icon:'success'
							})
							this.query.currentPage = 1
							this.list = []
							this.status = 'more'
							this.getCouponList()
						}
					})
				}
			}
			
		},
		created() {
		}
	}
</script>

<style lang="scss" scoped>
.coupon{
	background: #f5f5f5;
	height: 100vh;
	padding: 20rpx 24rpx;
	.tab-box{
		display: flex;
		align-items: center;
		background: #fff;
		position: fixed;
		width: 100%;
		top: 0%;
		left: 0%;
		.tab{
			flex: 1;
			text-align: center;
			font-size: 28rpx;
			color: #222222;
			line-height: 84rpx;
		}
		.tab.active{
			font-weight: 600;
			font-size: 32rpx;
		}
	}
	.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{
				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;
				}
			}
				.btn {
					width: 118rpx;
					height: 48rpx;
					line-height: 48rpx;
					text-align: center;
					font-size: 24rpx;
					color: #FF4D3A;
					box-sizing: border-box;
				}
				
				.icon{
					width: 100rpx;
					height: 100rpx;
				}
	}
	
	.discounts-item.disabled{
		filter: grayscale(1);
	}

}
</style>