<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.shelfOrderNo}}</view>
									<view class="times">{{item.createDate}}</view>
								</view>
								 <view class="check-row flex align-center">
									 <view class="carImage">
										 <u-image v-if="!item.billSource||item.billSource=='vin'" :src="item.vehicleLogo" border-radius="30" width="100" height="100" bg-color="#EBEBEB" ></u-image> 
										 <u-image v-if="item.billSource=='qr_code'" src="/static/qrcode.png" border-radius="30" width="100" height="100" bg-color="#EBEBEB" ></u-image> 
										 <u-image v-if="item.billSource=='bar_code'" src="/static/barcode.png" border-radius="30" width="100" height="100" bg-color="#EBEBEB" ></u-image>
										 <u-image v-if="item.billSource=='product_code'" src="/static/tag.png" border-radius="30" width="100" height="100" bg-color="#333" ></u-image> 
									 </view>
									 <view v-if="!item.billSource">
										 <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 v-else>
										  <view class="pcode flex">
											 <text>{{item.shelfOrderDetailList[0].productCode}}</text>
										  </view>
										  <view class="carModel flex">
											  <text>{{item.shelfOrderDetailList[0].productName}}</text>
										  </view>
									 </view>
								 </view>
								 <view class="check-row">
									 <view class="nums">
										 {{item.totalQty}}件商品
									 </view>
									 <view style="text-align: right;">
										 <button :hover-stop-propagation="true" @click.stop="deleteRow(item,index)" v-if="item.billState != 'CLOSE'&&item.settleState=='WAIT'" class="action del" size="mini">取消订单</button>
										 <!-- <button :hover-stop-propagation="true" @click.stop="sanCodeRow(item)" v-if="item.billState == 'WAIT'" class="action finish" size="mini">扫码取货</button> -->
										 <button :hover-stop-propagation="true" @click.stop="onceQh(item,index)" :loading="loading" v-if="item.billState == 'WAIT'" class="action finish" size="mini">一键取货</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 carNo from '@/components/carNo.vue'
	import { getShelfOrderList, cancelShelfOrder, shelfGetTotalWaitQty, takeGoods } from '@/api/shelf.js'
	import moment from 'moment'
	export default {
		components: {carNo},
		data() {
			return {
				loading: false,
				theme:'',
				status: 'loading',
				noDataText: '暂无数据',
				stateList:['WAIT','FINISH','CLOSE'],
				tabList: [
					  {
						  dispName: '待取货',
						  typeId: 1,
						  count: 0
					   },
					  {
					    dispName: '已取货',
					  	typeId: 2,
						count: 0
					  },
					  {
					    dispName: '已取消',
					  	typeId: 3,
						count: 0
					  },
				  ],
				current: 1,
				swiperCurrent: 1,
				// 查询条件
				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()
			},
			// 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,
				billState: this.stateList[this.swiperCurrent]
			  }
			  this.status = "loading"
			  getShelfOrderList(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.current == 0){
					  _this.tabList[0].count = _this.total
				  }
				} 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/orderDetail?shelfOrderSn="+item.shelfOrderSn+"&state="+this.stateList[this.swiperCurrent]
				})
			},
			// 删除
			delOrder(sn,index){
				cancelShelfOrder({
					'sn': sn
				}).then(res => {
					console.log(res)
					if(res.status == 200){
						this.list.splice(index,1)
						if(this.current == 0){
							this.tabList[0].count = this.tabList[0].count - 1
						}
					}else{
						uni.showModal({
							title: '提示',
							content: res.message,
							confirmText: '知道了',
							showCancel: false
						})
					}
					this.message(res.data.message)
				})
			},
			// 确认取消订单
			deleteRow (params,index) {
			  let _this = this
			  uni.showModal({
					title: '提示',
					content: '确认取消订单吗？',
					success: (ret) => {
					  if(ret.confirm){
						  _this.delOrder(params.shelfOrderSn,index)
					  }
					}
			  })
			},
			// 打开扫描取货
		   sanCodeRow (item) {
			     uni.navigateTo({
			     	url:"/pagesA/digitalShelf/scanBarcode/scanBarcode?shelfOrderSn="+item.shelfOrderSn
			     })
		    },
			// 一键取货
			onceQh(item,index){
				const _this = this
				uni.showModal({
					title: '提示',
					content: '确认一键取货吗？',
					success: (ret) => {
					  if(ret.confirm){
						  _this.loading = true
						  takeGoods({sn:item.shelfOrderSn}).then(res => {
							 if(res.status == 200){
								 _this.list.splice(index,1)
								 if(_this.current == 0){
									_this.tabList[0].count = _this.tabList[0].count - 1
								 }
							 }
							 uni.showToast({
								title: res.message,
								icon:"none"
							 })
							 _this.loading = false
						  })
					  }
					}
				})
			},
		}
	}
</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{
						.pcode{
							margin-bottom: 10rpx;
						}
						.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: #066cff;
					}
					.del{
						color: #666;
						border:2rpx solid #EDEDED;
						background: none;
					}
				}
			}
		}
	}
    
	.search-popup{
		.search-title{
			text-align: center;
			padding: 20upx;
			color: #333;
			border-bottom: 1px solid #eee;
		}
		.uni-list{
			padding:10upx 20upx;
			margin: 0;
			border-bottom: 1px solid #EEEEEE;
			.uni-list-cell{
				border: 0;
				.uni-list-cell-db{
					flex: 1;
					width: 100%;
				}
				.uni-input{
					height: 2.5em;
					line-height: 2.5em;
					font-size: 28upx;
					display: flex;
					align-items: center;
					> view{
						&:first-child{
							flex-grow: 1;
							color: #666;
						}
						&:last-child{
							color: #999;
						}
					}
				}
				.item-icon{
					margin-left: 10upx;
				}
			}
		}
		.uni-list-btns{
			display: flex;
			padding: 50upx 20upx;
			uni-button{
				font-size: 28upx;
				margin: 0 30upx;
				flex:1;
				border-radius: 100upx;
				&:after{
					border: 0;
				}
			}
		}
		.checkbox-items{
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			.checkbox{
				width: 24%;
				background: #F8F8F8;
				text-align: center;
				margin: 20upx 0;
				padding: 10upx 0;
				border-radius: 50upx;
				font-size: 24upx;
				border:1upx solid #eee;
			}
			.checked{
				background: rgba(4, 133, 246, 0.15);
				border-color: rgba(4, 133, 246, 0.5);
				color: rgba(4, 133, 246, 1);
			}
		}
	}
</style>
