<template>
	<view class="pagesCons">
		<view class="utabs">
			<u-tabs-swiper ref="uTabs" bar-width="90" :list="tabList" 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" v-for="(tabs, index) in tabList" :key="index">
				<scroll-view scroll-y class="scroll-view" @scrolltolower="onreachBottom">
					<view  
					class="check-order-list" 
					v-if="index==current"
					v-for="(item,sindex) in list" 
					:key="item.id+'-'+sindex"
					@click="viewRow(item)" >
						<view class="check-row">
							<view>
							{{item.shelfInfo&&item.shelfInfo.shelfName}}
							<u-tag size='mini' type="info" shape="circle" v-if="item.shelfInfo&&item.shelfInfo.state=='WRITE_OFF'" text="已注销"></u-tag>
							<u-tag size='mini' type="info" shape="circle" v-if="item.shelfInfo&&item.shelfInfo.state=='SUSPEND'" text="已暂停"></u-tag>
							<u-tag size='mini' type="info" shape="circle" v-if="item.shelfInfo&&item.shelfInfo.state=='DISABLED'" text="已停用"></u-tag>
							</view>
							<view>
								<text :style="{color: item.billState=='FINISH'?$config('successColor'):item.billState=='CANCEL'?$config('infoColor'):$config('errorColor')}">{{item.billStateDictValue}}</text>
							</view>
						</view>
						<view class="check-col">
							<view>补货单号：{{item.replenishBillNo}}</view>
							<view class="flex align_center justify_between">
								<view v-if="item.billState=='WAIT_OUT_STOCK' || item.billState=='WAIT_CHECK' || item.billState=='CANCEL'">补货数量：{{item.totalQty}}</view>
								<view v-if="item.billState=='FINISH'">签收数量：{{item.totalPutQty}}</view>
							</view>
						</view>
						<view class="ptools flex align_center justify_between">
							<view>
								<text v-if="item.billState == 'WAIT_CHECK'">出库时间：{{item.outStockTime}}</text>
								<text v-else-if="item.billState == 'FINISH'">签收时间：{{item.putStockTime}}</text>
								<text v-else>创建时间：{{item.createDate}}</text>
							</view>
							<view class="flex align_center justify_between">
								<view v-if="item.billState=='WAIT_CONFIRM'||item.billState=='WAIT_OUT_STOCK'">
									<u-button shape="circle" size="mini" @click.stop="handleCancelOrder(item)" type="error" plain>取消</u-button>
								</view>
								<view v-if="item.billState=='CANCEL'">
									<u-button shape="circle" size="mini" @click.stop="handleDelOrder(item)" type="error" plain>删除</u-button>
								</view>
								<view class="print" v-if="item.billState!='CANCEL'&&item.billState!='WAIT_CONFIRM'" @click.stop="print(item)">
									<u-image :src="`/static/${$config('themePath')}/icon_printer@3x.png`" width="48" height="48"></u-image>
								</view>
							</view>
						</view>
					 </view>
					 <u-empty :src="`/static/${$config('themePath')}/def_no_data@3x.png`" icon-size="240" :text="noDataText" img-width="120" v-if="list.length==0 && status!='loading'" mode="list"></u-empty>
					 <view v-if="index==current" style="padding: 20upx;">
						 <u-loadmore :load-text="$config('loadText')" v-if="total>pageSize || status=='loading'" :status="status" />
					 </view>
				</scroll-view>
			</swiper-item>
		</swiper>
		<!-- 打印贴签 -->
		<print-sticker-modal v-if="printModal" :openModal="printModal" @confirm="modalPrint" @close="printModal=false" />
	</view>
</template>

<script>
	import printStickerModal from './printStickerModal.vue'
	import { clzConfirm } from '@/libs/tools';
	import { 
		shelfReplenishList, 
		shelfReplenishStateCount, 
		shelfReplenishCancel, 
		shelfReplenishDelete } from '@/api/shelfReplenish'
	export default {
		components: {printStickerModal},
		data() {
			return {
				status: 'loadmore',
				noDataText: '暂无数据',
				tabList: [
					{
						dispName: '全部',
						status: 'ALL'
					},
					// {
					// 	dispName: '待确认',
					// 	status: 'WAIT_CONFIRM',
					// 	count: 0
					// },
					{
						dispName: '待出库',
						status: 'WAIT_OUT_STOCK',
						count: 0
					},
					{
						dispName: '待签收',
						status: 'WAIT_CHECK',
						count: 0
					},
					{
						dispName: '已完成',
						status: 'FINISH'
					},
					{
						dispName: '已取消',
						status: 'CANCEL'
					},
				],
				current: 0,
				swiperCurrent: 0,
				pageNo: 1,
				pageSize: 15,
				list: [],
				total: 0,
				action:'swiperChange', // 操作类型，上划分页，或左右滑动
				printModal: false,
				tempData: null
			}
		},
		onLoad(options) {
			const _this = this
			if(options&&options.billState){  // 指定单据状态
				const index = this.tabList.findIndex(item => item.status == options.billState)
				_this.swiperCurrent = index
				_this.current = index
			}else{
				_this.swiperCurrent = 0
				_this.current = 0
			}
			// 监听整改完成后刷新事件
			uni.$on('refreshBL', this.queryByTypeSum)
			this.queryByTypeSum()
			// 跳到指定tab
			uni.$on('refreshBhList',this.refreshBhList)
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.$config('primaryColor')
			})
		},
		onUnload() {
			uni.$off('refreshBL')
		},
		onNavigationBarButtonTap(e) {
			if(e.index == 0){
				uni.navigateTo({
					url: "/pages/soldOut/shelfList"
				})
			}
		},
		methods:{
			// tabs通知swiper切换
			tabsChange(index) {
				this.swiperCurrent = index
			},
			swiperChange(event){
				this.action = 'swiperChange'
				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
				let isCurtab = this.current == current
				if(this.status!="nomore"){
					let loadData = this.action == 'swiperChange' ? !isCurtab : isCurtab
					if(loadData){
						this.$refs.uTabs.setFinishCurrent(current)
						this.swiperCurrent = current
						this.current = current
						this.resetPage()
						this.queryByTypeSum(true)
					}
				}
			},
			// scroll-view到底部加载更多
			onreachBottom() {
				this.action = 'onreachBottom'
				if(this.list.length < this.total){
					this.resetPage()
				}else{
					this.status = "nomore"
				}
			},
			// 查询列表
			getRow (pageNo) {
				if (pageNo) {
					this.pageNo = pageNo
				}
				let params = {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					billState: this.tabList[this.current].status=='ALL'?'':this.tabList[this.current].status
				}
				this.status = "loading"
				shelfReplenishList(params).then(res => {
					if (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 = "loadmore"
				})
			},
			resetPage(){
				this.status = 'loading';
				// 上划分页
				if(this.action == 'onreachBottom'){
					this.pageNo += 1
					this.getRow()
				}
				// 左右切换tab
				if(this.action == 'swiperChange'){
					this.list = []
					this.getRow(1)
				}
			},
			// 详细页
			viewRow(item){
				if(item.billState=='WAIT_CONFIRM'){ // 待确认
					uni.navigateTo({ url: "/pages/replenishmentManage/confirm?sn="+item.replenishBillSn })
				}else if(item.billState=='WAIT_OUT_STOCK'){ // 待出库
					uni.navigateTo({ url: "/pages/replenishmentManage/outWarehousing?sn="+item.replenishBillSn })
				}else if(item.billState=='WAIT_CHECK'){ // 待签收
					uni.navigateTo({ url: "/pages/replenishmentManage/signWarehousing?sn="+item.replenishBillSn })
				}else if(item.billState=='FINISH'){ // 已完成
					uni.navigateTo({ url: "/pages/replenishmentManage/detail?sn="+item.replenishBillSn+"&type=success" })
				}else if(item.billState=='CANCEL'){ // 已取消
					uni.navigateTo({ url: "/pages/replenishmentManage/detail?sn="+item.replenishBillSn+"&type=cancel" })
				}
			},
			// 取消补货单
			handleCancelOrder (row) {
				const _this = this;
				clzConfirm({
					title: '提示',
					content: '确认取消补货单吗？',
					success: function(res) {
						if (res.confirm || res.index == 0) {
							_this.showLoading('正在取消...')
							shelfReplenishCancel({ replenishBillSn: row.replenishBillSn }).then(res => {
							  if (res.status == 200) {
							    _this.toashMsg(res.message)
								_this.queryByTypeSum()
							  }
							  uni.hideLoading()
							})
						}
					}
				});
			},
			// 删除补货单
			handleDelOrder(row){
				const _this = this;
				clzConfirm({
					title: '提示',
					content: '确认删除补货单吗？',
					success: function(res) {
						if (res.confirm || res.index == 0) {
							_this.showLoading('正在删除...')
							shelfReplenishDelete({ replenishBillSn: row.replenishBillSn }).then(res => {
							  if (res.status == 200) {
							    _this.toashMsg(res.message)
								_this.queryByTypeSum()
							  }
							  uni.hideLoading()
							})
						}
					}
				});
			},
			// 查询每个状态的数据条数
			queryByTypeSum (flag) {
				shelfReplenishStateCount({}).then(res => {
					if (res.data && res.status == 200) {
						// this.tabList[1].count = res.data.WAIT_CONFIRM || 0
						this.tabList[1].count = res.data.WAIT_OUT_STOCK || 0
						this.tabList[2].count = res.data.WAIT_CHECK || 0
						if(!flag){
							this.getRow(1)
						}
					}
				})
			},
			refreshBhList(tab){
				const _this = this
				const index = this.tabList.findIndex(item => item.status == tab)
				_this.tabsChange(index)
			},
			print(item){
				this.tempData = item
				this.printModal=true
			},
			// 打印贴签 confirm
			modalPrint(type){
				this.printModal = false
				if(type == 'manual'){  // 手动打印
					uni.navigateTo({ url: "/pages/replenishmentManage/manualPrint?sn="+this.tempData.replenishBillSn })
				}else if(type == 'scan'){  // 扫码打印
					uni.navigateTo({ url: "/pages/replenishmentManage/scanCodePrint?sn="+this.tempData.replenishBillSn })
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}
	.pagesCons{
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		.text-right {
			text-align: right;
		}
		.utabs{
			border-bottom: 1px solid #eee;
		}
		.check-list{
			flex: 1;
			.swiper-item{
				width: 100%;
				height: 100%;
				overflow: hidden;
				.scroll-view {
					width: 100%;
					height: 100%;
					overflow: auto;
				}
			}
		}
		 
		// 列表样式
		.check-order-list{
			background: #ffffff;
			padding: 20upx 30upx;
			margin: 20upx;
			border-radius: 20upx;
			box-shadow: 1px 1px 3px #EEEEEE;
			.check-row{
				 display: flex;
				 justify-content: space-between;
				 >view{
					 &:first-child{
						 color: #333;
						 font-size: 30rpx;
						 font-weight: bold;
						 width: 80%;
					 }
					 &:last-child{
						 font-size: 28rpx;
						 margin-top: 5rpx;
					 }
				 }
			}
			.check-col{
				> view{
					padding: 2rpx 0;
					font-size: 28rpx;
					color: #666666;
				}
			}
			.ptools{
				color: #707070;
				.print{
					padding: 0 10rpx 0 30rpx;
				}
			}
		}
	}
</style>
