<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" v-if="swiperCurrent == indexs">
							<view  
							class="check-order-list" 
							v-for="(item,index) in list" 
							:key="item.id" 
							 @click="viewRow(item)"
							>
								<view class="check-row align-center justify_between">
									<view class="orderNo">{{item.purchaseNo}}</view>
									<view class="times">{{item.createDate}}</view>
								</view>
								 <view class="check-row flex align-center justify_between">
									 <view>总款数：{{item.totalCategory}}</view>
									 <view>总金额：<text class="price">￥{{item.totalAmount}}</text></view>
								 </view>
								 <view class="check-row">
									 <view class="nums">
										 共{{item.totalQty}}件商品
									 </view>
									 <view style="text-align: right;color: #999;">
										 <span v-if="item.billStatus == 'WAIT_AUDIT'">待审核</span>
										 <span v-if="item.billStatus == 'AUDIT_REJECT'">审核不通过</span>
										 <span v-if="item.billStatus == 'WAIT_OUT_WAREHOUSE' || item.billStatus == 'WAIT_RECEIVE'">待收货</span>
										 <span v-if="item.billStatus == 'FINISH'">已完结</span>
										 <span v-if="item.billStatus == 'CANCEL'">已取消</span>
										 <u-button style="margin-left: 10px;" v-if="item.billStatus == 'WAIT_AUDIT'" size="mini" shape="circle" type="error" plain @click="cancelOrder(item)">取消订单</u-button>
										 <u-button style="margin-left: 10px;" v-if="item.billStatus == 'WAIT_RECEIVE'" size="mini" shape="circle" type="primary" @click="receiveOrder(item)">确认收货</u-button>
									 </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 { purchaseQueryPage,purchaseCancel,purchaseReceive } from '@/api/purchase.js'
	export default {
		data() {
			return {
				status: 'loading',
				noDataText: '暂无数据',
				tabList: [
					  {
						  dispName: '全部',
						  typeId: 1,
						  count: 0
					   },
					  {
					    dispName: '待收货',
					  	typeId: 2,
						count: 0
					  },
					  {
					    dispName: '已完结',
					  	typeId: 3,
						count: 0
					  },
					  {
					    dispName: '已取消',
					  	typeId: 4,
					  	count: 0
					  },
				  ],
				current: 1,
				swiperCurrent: 1,
				// 查询条件
				pageNo: 1,
				pageSize: 10,
				list: [],
				total: 0,
			}
		},
		onLoad() {
			this.$store.state.vuex_tempData = null
			let _this = this
			_this.pageInit()
			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()
			},
			// tabs通知swiper切换
			tabsChange(index) {
				if(index != this.swiperCurrent && this.status !='loading'){
					this.list = []
					this.status = "loading"
					this.swiperCurrent = index;
				}
			},
			swiperChange(event){
				this.list = []
				this.status = "loading"
				this.swiperCurrent = event.detail.current
			},
			// 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() {
				if(this.list.length < this.total){
					this.pageNo += 1
					this.getRow()
				}else{
					this.status = "nomore"
				}
			},
			// 取消订单
			cancelOrder(item){
				let _this = this
				uni.showModal({
					title: '提示',
					content: '确定取消订单吗？',
					success: function (res) {
						if (res.confirm) {
							purchaseCancel({purchaseSn:item.purchaseSn}).then(res => {
								if (res.status == 200) {
									_this.message('取消成功')
									_this.pageInit()
								} else {
									_this.message(res.message)
								}
							})
						}
					}
				});
			},
			// 确认收货
			receiveOrder(item){
				let _this = this
				uni.showModal({
					title: '提示',
					content: '确定收货吗？',
					success: function (res) {
						if (res.confirm) {
							purchaseReceive({purchaseSn:item.purchaseSn}).then(res => {
								if (res.status == 200) {
									_this.message('收货成功')
									_this.pageInit()
								} else {
									_this.message(res.message)
								}
							})
						}
					}
				});
			},
			// 查询列表
			getRow (pageNo) {
			  let _this = this
			  if (pageNo) {
			    this.pageNo = pageNo
			  }
			  let params = {
			    pageNo: this.pageNo,
			    pageSize: this.pageSize,
			  }
			  // 状态条件
			  if(this.swiperCurrent == 1){
				  params.billStatusList = ['WAIT_AUDIT','AUDIT_REJECT','WAIT_OUT_WAREHOUSE','WAIT_RECEIVE']
			  }
			  if(this.swiperCurrent == 2){
				  params.billStatusList = ['FINISH']
			  }
			  if(this.swiperCurrent == 3){
			  	  params.billStatusList = ['CANCEL']
			  }
			  this.status = "loading"
			  purchaseQueryPage(params).then(res => {
				if (res.code == 200 || res.status == 204 || res.status == 200) {
				  if(_this.pageNo>1){
					  _this.list = _this.list.concat(res.data.list || [])
				  }else{
					  _this.list = res.data.list || []
				  }
				  _this.total = res.data.count || 0
				  // 待收货
				  if(_this.swiperCurrent == 1){
					  _this.tabList[1].count = _this.total
				  }
				} else {
				  _this.list = []
				  _this.total = 0
				  _this.noDataText = res.message
				}
				_this.status = _this.total>=_this.list.length ? "nomore" : 'loadmore'
			  })
			},
			// 详细页
			viewRow(item){
				this.$store.state.vuex_tempData = item
				uni.navigateTo({
					url: "/pagesB/procureOrder?purchaseSn="+item.purchaseSn+"&state="+item.billStatus
				})
			}
		}
	}
</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;
				padding: 10upx 20upx;
				margin: 25upx;
				border-radius: 30upx;
				box-shadow: 1px 1px 3px #EEEEEE;
				.check-row{
					display: flex;
					padding: 20upx 10upx;
					font-size: 28upx;
					&:first-child{
						color: #666E75;
						font-size: 24upx;
						padding-bottom: 10upx;
						.orderNo{
							font-size: 30upx;
							color: #222222;
							margin-top: -6upx;
							flex-grow: 1;
						}
						.times{
							text-align: right;
						}
						border-bottom: 2upx solid #f5f5f5;
					}
					&:last-child{
						align-items: center;
						justify-content: space-between;
						.nums{
							font-size: 28upx;
							color: #033692;
						}
					}
					> view{
						 .price{
							 color: red;
							 font-size: 28upx;
						 }
					}
					 
					.finish{
						background-color: #2d8cf0;
					}
					.del{
						color: #666;
						border:2rpx solid #EDEDED;
						background: none;
					}
				}
			}
		}
	}
</style>
