<template>
	<view class="order">
		<!-- header -->
		<cu-custom bgColor="bg-gradual-blue" isBack><view slot="content">我的卡券</view></cu-custom>
		<!-- section -->
		<cu-nav :active="active" @onChange="onChange" :value="value" />
		<view  v-for="(item,index) in value" :key="index"  class="box">
		  <view v-show="index==active">
			  <view v-for="(itemB,indexB) of item.value" :key="indexB" class="bg-white padding-sm radius margin-lr margin-top">
			  	<view class="text-bold text-black">{{itemB.coupon_name}}</view>
			  	<view class="text-sm text-black padding-top-sm">{{itemB.receive_date | changeDate}} - {{itemB.end_date | changeDate}}</view>
			      <view v-if="itemB.write_off_time" class="text-sm text-black padding-top-sm">使用时间：{{itemB.write_off_time}}</view>
			      <view class="text-gray text-sm margin-top">{{itemB.activity_name}}</view>
			  </view>
			  <van-empty v-if="item.value.length==0  && item.no_more" description="暂无卡券" />
			  <view v-else :class="!item.no_more && item.pageCount<=item.totalPage ? 'cu-load bg-grey loading margin-top' : 'cu-load bg-grey over margin-top'"></view>
		  </view>
		</view>
		<!-- footer  -->
	</view>
</template>

<script>
export default {
	data() {
		return {
			active:0,
			value: [
				{
					id: 1,
					name:"待使用",
					no_more:false,//没有更多
					pageCount: 1,
					totalPage: 1,
					value: []
				},
				{
					id: 2,
					name:"已使用",
					no_more:false,//没有更多
					pageCount: 1,
					totalPage: 1,
					value: []
				},
				{
					id: 3,
					name:"已过期",
					no_more:false,//没有更多
					pageCount: 1,
					totalPage: 1,
					value: []
				}
			]
		};
	},
	onLoad() {
		this.request();
	},
	filters:{
		changeDate(value){
			return value.replaceAll("-",".")
		}
	},
	methods: {
		// 不同状态
		onChange(index){
			this.active=index;
			this.request();
		},
		// 请求
		async request() {
			var $value=JSON.parse(JSON.stringify(this.value));
			var {name,pageCount,no_more,totalPage,value,id}=$value[this.active];
			if(pageCount>totalPage) return false;
			var res=await this.$global.request({url:"/customer/coupon/",
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				Authorization: uni.getStorageSync('token')
			},method:'get',data:{
				page:pageCount,limit:20,
				status:id,appid:this.$store.state.userInfo.appid,activity_id:this.$store.state.userInfo.activity_id}});
			pageCount++;
			totalPage=res.totalPage;
			if(pageCount>totalPage) no_more=true;
			value=value.concat(res.data);
			$value[this.active]={name,pageCount,no_more,totalPage,value,id};
			this.value=$value;
		}
	},
	watch:{
		
	}
};
</script>

<style></style>
