<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>
					<view class="btn" :class="[item.exist > 0?'used':'']" @click="claim(item.id,item.exist)">
						{{item.exist > 0?'已领取':'领取'}}
					</view>
			</view>
		</zs-list>
		
		
	</view>
</template>

<script>
	import {getCouponList,claim} from '@/api/coupon.js'
	export default {
		data() {
			return {
				active: '',
				open:999,
				status:'more',
				query:{
					currentPage:1,
					pageSize:10,
					showType: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: {
			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.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;
	.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;
					background: #EE4320;
					border-radius: 24rpx;
					color: #FFF;
					box-sizing: border-box;
				}
				.btn.used{
					border: 2rpx solid #EE421F;
					color: #EE421F;
					background: none;
				}
	}

}
</style>