<template>
	<view class="digitShelf-pagesCons">
		<view class="tab-body">
			    <view>
					<u-tabs-swiper ref="uTabs" :list="tabList" :offset="[5,40]" bar-width='100' active-color="#1283d4" name="dispName" :current="current" @change="tabsChange" :is-scroll="false"
					 swiperWidth="750"></u-tabs-swiper>
				</view>
				<swiper class="check-list" :current="swiperCurrent" @transition="transition" @change="swiperChange" @animationfinish="animationfinish">
					<swiper-item class="swiper-item" style="height: 100%;width: 100%;overflow: hidden;" v-for="(tabs, indexs) in tabList" :key="indexs">
						<scroll-view scroll-y style="height: 100%;width: 100%;overflow: auto;" @scrolltolower="onreachBottom">
							<view  
							class="check-order-list" 
							v-for="(item,index) in list" 
							:key="item.id" 
							>
								<view>
									<view class="dio dio-left"></view>
									<view class="dio dio-right"></view>
									<view class="check-order-left" :class="'imgbg-'+current">
										￥<text>{{item.ticketValue}}</text>
									</view>
									<view class="check-order-right">
										<view class="check-order-title">{{item.activeName}}</view>
										<view class="check-order-subtitle">{{item.activeTitle}}</view>
										<view class="check-order-date">
											<view>{{item.validStartDate}} 至</view>
											<view>{{item.validEndDate}} 到期 </view>
											<!-- <view class="use-date" v-if="current==1">使用时间：2021-12-31</view> -->
										</view>
									</view>
								</view>
								<view v-if="item.ticketExplain">
									<view class="check-order-desc" :class="'border-'+current" @click="showDesc(item)">
										<text>使用说明：</text>
										<u-icon :name="item.showDesc?'arrow-up':'arrow-down'"></u-icon>
									</view>
									<view class="check-order-desc-content" v-if="item.showDesc">
										 {{item.ticketExplain}}
									</view>
								</view>
							 </view>
							 <view style="padding: 20upx;">
								 <u-empty :src="`/static/nodata.png`" icon-size="180" :text="noDataText" img-width="120" v-if="list.length==0 && status!='loading'" mode="list"></u-empty>
								 <u-loadmore v-if="(total>=list.length&&list.length)||status=='loading'" :status="status" />
							 </view>
						</scroll-view>
					</swiper-item>
				</swiper>
		</view>
	</view>
</template>

<script>
	import { ticketQueryList } from '@/api/user.js'
	import moment from 'moment'
	export default {
		data() {
			return {
				theme:'',
				status: 'loading',
				noDataText: '暂无优惠券',
				tabList: [
					  {
						  dispName: '未使用',
						  typeId: 1,
						  count: 0
					   },
					  {
					    dispName: '已使用',
					  	typeId: 2,
						count: 0
					  },
					  {
					    dispName: '已过期',
					  	typeId: 3,
						count: 0
					  },
				  ],
				current: 0,
				swiperCurrent: 0,
				// 查询条件
				pageNo: 1,
				pageSize: 10,
				list: [],
				total: 0,
			}
		},
		onLoad() {
			let _this = this
			_this.pageInit()
			_this.theme = getApp().globalData.theme
			uni.$on('refreshOrder',function(data){
				 _this.list = []
				 _this.status = "loading"
			     _this.pageInit()
			})
		},
		methods:{
			message(title){
				uni.showToast({
					icon:'none',
					title: title
				})
			},
			pageInit(){
				this.total = 0
				this.pageNo = 1
				this.getRow()
			},
			showDesc(item){
				item.showDesc = !item.showDesc
				this.list.splice()
			},
			// tabs通知swiper切换
			tabsChange(index) {
				this.swiperCurrent = index;
			},
			swiperChange(event){
				this.list = []
				this.status = "loading"
			},
			// swiper-item左右移动，通知tabs的滑块跟随移动
			transition(e) {
				let dx = e.detail.dx;
				this.$refs.uTabs.setDx(dx);
			},
			// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
			// swiper滑动结束，分别设置tabs和swiper的状态
			animationfinish(e) {
				let current = e.detail.current;
				if(current != this.current){
					this.$refs.uTabs.setFinishCurrent(current);
					this.swiperCurrent = current;
					this.current = current;
					this.pageInit()
				}
			},
			// scroll-view到底部加载更多
			onreachBottom() {
				console.log(this.list.length, this.total)
				if(this.list.length < this.total){
					this.pageNo += 1
					this.getRow()
				}else{
					this.status = "nomore"
				}
			},
			checkNums (states, i){
			  if(states){
				  let arr = states.split(',')
				  return arr[i]
			  }
				return 0
			},
			// 查询列表
			getRow (pageNo) {
			  let _this = this
			  if (pageNo) {
			    this.pageNo = pageNo
			  }
			  let params = {
			    pageNo: this.pageNo,
			    pageSize: this.pageSize,
				ticketState: 'UNUSED'
			  }
			  // 状态条件
			  if(this.swiperCurrent == 1){
				  params.ticketState = 'USED'
			  }
			  if(this.swiperCurrent == 2){
				  params.ticketState = 'DATED'
			  }
			  this.status = "loading"
			  ticketQueryList(params).then(res => {
				if (res.code == 200 || res.status == 204 || res.status == 200) {
				  const list = res.data.list || []
				  list.map(k => k.showDesc = false)
				  _this.list = _this.list.concat(list)
				  _this.total = res.data.count || 0
				} else {
				  _this.list = []
				  _this.total = 0
				  _this.noDataText = res.message
				}
				_this.status = _this.total>=_this.list.length ? "nomore" : 'loadmore'
			  })
			},
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
	}
	.digitShelf-pagesCons{
		height: 100%;
		display: flex;
		flex-direction: column;
		.tab-body{
			.check-list{
				height: calc(100vh - 44px);
			}
			.check-order-list{
				background: #ffffff;
				margin: 25upx;
				border-radius: 30upx;
				overflow: hidden;
				box-shadow: 1px 1px 3px #EEEEEE;
				> view{
					&:first-child{
						position: relative;
						display: flex;
						align-items: center;
						justify-content: space-around;
					}
				}
				.dio{
					width: 30rpx;
					height: 30rpx;
					background: #f8f8f8;
					border-radius: 100rpx;
					position: absolute;
					z-index: 1000;
				}
				.dio-left{
					left: -15rpx;
					top: 85rpx;
				}
				.dio-right{
					right: -15rpx;
					top: 85rpx;
				}
				.check-order-left{
					width: 30%;
					height: 200rpx;
					display: flex;
					align-items: center;
					justify-content:center;
					font-size: 30rpx;
					text{
						font-size: 65rpx;
					}
					color: #fff;
				}
				.imgbg-0{
					background: #2196F3;
				}
				.imgbg-1{
					background: #4CAF50;
					color: #ffffff;
				}
				.imgbg-2{
					background: #d2d1d1;
				}
				.check-order-right{
					width: 70%;
					padding: 0 25rpx;
					.check-order-title{
						font-size: 32rpx;
					}
					.check-order-subtitle{
						font-size: 28rpx;
						color: #666;
						margin: 10rpx 0;
					}
					.check-order-date{
						font-size: 24rpx;
						color: #666;
						.use-date{
							color: #ff5500;
						}
					}
				}
				.check-order-desc{
					font-size: 24rpx;
					color: #666;
					padding: 10rpx 20rpx;
					border-top: 1rpx solid #2196F3;
					display: flex;
					justify-content: space-between;
				}
				.border-0{
					border-top: 1rpx solid #eee;
				}
				.border-1{
					border-top: 1rpx solid #CDDC39;
				}
				.border-2{
					border-top: 1rpx solid #d2d1d1;
				}
				.check-order-desc-content{
					padding: 5rpx 25rpx 25rpx 25rpx;
					font-size: 24rpx;
				}
			}
		}
	}
</style>
