<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" 
							 @click="viewRow(item)"
							>
								<view class="check-row align-center justify_between">
									<view class="orderNo">{{item.tempNo}}</view>
									<view class="times">{{item.createDate}}</view>
								</view>
								 <view class="check-row flex align-center">
									 <view class="carImage flex align-center">
										 <u-image :src="item.vehicleLogo" border-radius="30" width="100" height="100" bg-color="#EBEBEB" ></u-image> 
									 </view>
									 <view>
										 <view class="carModel flex align-center" v-if="item.vehicleNumber" >
										 	<view><carNo color="#0055ff" :val="item.vehicleNumber"></carNo></view>
											<text style="margin-left: 20rpx;" v-if="item.vehicleModel">{{item.vehicleModel.split(' ')[0]}}</text>
										 </view>
										  <view class="carModel flex" v-if="!item.vehicleNumber" >
											 <text>{{item.vehicleModel}}</text>
										  </view>
										  <view class="carVin flex align-center">
											  VIN码：{{item.vin||'暂无'}}
											  <text v-if="item.vin" @click.stop="copyVin(item.vin)">复制</text>
										  </view>
									 </view>
								 </view>
								 <view class="check-row">
									 <view class="nums">
										 {{item.totalQty}}件商品
									 </view>
									 <view style="text-align: right;">
										 <span v-if="item.billState == 'WAIT_AUDIT'">待审核</span>
										 <span v-if="item.billState == 'AUDIT_REJECT'">审核不通过</span>
										 <span v-if="item.billState == 'WAIT_OUT_WAREHOUSE'">待收货</span>
										 <span v-if="item.billState == 'FINISH'">已完结</span>
									 </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 carNo from '@/components/carNo.vue'
	import { getShelfTempBillList, shelfTempTotalWaitQty } from '@/api/shelf.js'
	import moment from 'moment'
	export default {
		components: {carNo},
		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:{
			// 复制
			copyVin(vin){
				uni.setClipboardData({
					data: vin||'',
				})
			},
			message(title){
				uni.showToast({
					icon:'none',
					title: title
				})
			},
			pageInit(){
				this.total = 0
				this.pageNo = 1
				this.getRow()
				// 获取总条数
				this.getWaitTotal()
			},
			// tabs通知swiper切换
			tabsChange(index) {
				this.swiperCurrent = index;
			},
			swiperChange(event){
				console.log(event.detail)
				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,
			  }
			  // 状态条件
			  if(this.swiperCurrent == 1){
				  params.billStateList = ['WAIT_AUDIT','AUDIT_REJECT','WAIT_OUT_WAREHOUSE']
			  }
			  if(this.swiperCurrent == 2){
				  params.billStateList = ['FINISH']
			  }
			  this.status = "loading"
			  getShelfTempBillList(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
				} else {
				  _this.list = []
				  _this.total = 0
				  _this.noDataText = res.message
				}
				_this.status = _this.total>=_this.list.length ? "nomore" : 'loadmore'
			  })
			},
			// 详细页
			viewRow(item){
				uni.navigateTo({
					url: "/pagesA/digitalShelf/tempOrderList/orderDetail?orderSn="+item.tempSn+"&state="+(item.billState == 'FINISH'?1:0)
				})
			},
			// 获取待拿货数量
			async getWaitTotal(){
				 const ret = await shelfTempTotalWaitQty({billStateList:['WAIT_AUDIT','AUDIT_REJECT','WAIT_OUT_WAREHOUSE']}).then(res => res.data)
				 this.tabList[1].count = ret || 0
			}
		}
	}
</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{
						.carModel{
							color: #222222;
							font-size: 32upx;
							font-weight: bold;
							line-height: normal;
							margin-bottom: 10rpx;
						}
						.carVin{
							color: #666E75;
							font-size: 24upx;
							text{
								background-color: #EBEBEB;
								border-radius: 100rpx;
								padding: 0 20rpx;
								color: #033692;
								font-size: 20rpx;
								display: flex;
								align-items: center;
								justify-content: center;
								margin-left: 20rpx;
							}
						}
					}
					.carImage{
						padding-right: 20rpx;
					}
					.action{
						padding: 0 30rpx;
						color: #fff;
						border-radius: 100rpx;
						margin-left: 15upx;
						font-size: 24upx;
						&:after{
							border: 0;
						}
						&:active{
							opacity: 0.5;
						}
					}
					 
					.finish{
						background-color: #2d8cf0;
					}
					.del{
						color: #666;
						border:2rpx solid #EDEDED;
						background: none;
					}
				}
			}
		}
	}
</style>
