<template>
	<u-navbar title="合托" bgColor='#D3832A' leftIconColor='#ffffff' titleStyle='color:#ffffff' placeholder
		:autoBack="true" leftIconSize='35'></u-navbar>
	<view class="topbos">
		<view class="srview">
			<view class="box">
				<view>解绑托盘码</view>
				<view>
					<input placeholder="请扫描或输入" v-model="mergeTray" @focus="setstate(1)" />
					<view class="sbm" @click="scandata">确定</view>
				</view>
			</view>
			<view class="">
				托盘名称：{{beilist.trayName||''}}
			</view>
		</view>
		<view class="srview">
			<view class="box">
				<view>合并托盘码</view>
				<view>
					<input placeholder="请扫描或输入" v-model="newmergeTray" @focus="setstate(2)" />
					<view class="sbm" @click="scandata">确定</view>
				</view>
			</view>
			<view class="">
				托盘名称：{{newbeilist.trayName||''}}
			</view>
		</view>
	</view>
	<view class="tabsetnumb">
		<view @click="settabberstate(1)" :class="tabberstate==1?'xz':''">
			<view>解绑托盘数据</view>
			<view></view>
		</view>
		<view @click="settabberstate(2)" :class="tabberstate==2?'xz':''">
			<view>合并托盘数据</view>
			<view></view>
		</view>
	</view>
	<view class="tarynumvw">
		托盘数量：{{(tabberstate==1?(beilist?.totalNum||beilist.stockNum):(newbeilist?.totalNum||beilist.stockNum))||0}}
	</view>
	<scroll-view scroll-y="true" class="scvorder">
		<template v-if="renderList">
			<view class="itembox" v-for="(item,index) in renderList">
				<!-- 计数 -->
				<!-- <view class="topon1">
					<view>{{index+1}}</view>
				</view> -->

				<!-- 定制品 -->
				<view v-if="item.goodsType === 1">
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>服务号：{{item.serviceNum}}</view>
					</view>
					<view class="tpbx" @click.stop="goorderdetail(item)">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>订单号：{{item.orderCode}}</view>
					</view>
					<!-- <view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>仓库地址：{{dtilobj.warehouseName}}</view>
					</view>
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>库位信息：{{dtilobj.areaTitle + '-' + dtilobj.allocationTitle}}</view>
					</view> -->
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>剩余数量：{{item.totalNum-item.shelfNum}}</view>
					</view>
				</view>

				<!-- 零担 -->
				<view v-if="item.goodsType === 2">
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>运单号：{{item.waybillNo}}</view>
					</view>
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>订单号：{{item.orderCode}}</view>
					</view>
					<!-- <view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>仓库地址：{{dtilobj.warehouseName}}</view>
					</view>
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>库位信息：{{dtilobj.areaTitle + '-' + dtilobj.allocationTitle}}</view>
					</view> -->
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>剩余数量：{{item.totalNum-item.shelfNum}}</view>
					</view>
				</view>

				<!-- 库存品 -->
				<view v-if="item.goodsType === 3">
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>物料编码：{{item.materialCode}}</view>
					</view>
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>物料名称：{{item.materialName}}</view>
					</view>
					<!-- <view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>仓库地址：{{dtilobj.warehouseName}}</view>
					</view>
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>库位信息：{{dtilobj.areaTitle + '-' + dtilobj.allocationTitle}}</view>
					</view> -->
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>订单号：{{item.orderCode}}</view>
					</view>
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>商场名称：{{item.marketName}}</view>
					</view>
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>批次号：{{item.incomingBatch}}</view>
					</view>
					<view class="tpbx">
						<image src="/pagesHome/static/kuweiicon.png"></image>
						<view>剩余数量：{{item.totalNum-item.shelfNum}}</view>
					</view>
				</view>

				<view class="viewnum">
					<view>总包数：{{item.totalNum}}</view>
					<view>已扫数量：{{item.shelfNum}}</view>
				</view>
			</view>
		</template>
	</scroll-view>
	<view class="butys">
		<view class="qut">取消</view>
		<view class="conf" @click="godetails">确认合并</view>
	</view>
	<tips ref="tip"></tips>
	<!-- #ifdef APP -->
	<saomiao2 :ishidestop="scanState !== 0"></saomiao2>
	<!-- #endif -->
</template>

<script lang="ts" setup>
	import {
		warehouseTrayTypescanSourceTrayCode,
		warehouseTrayTypescanTargetTrayCode,
		warehouseTrayTypemergeTray
	} from '@/api/user.js'
	import {
		onLoad,
		onShow,
		onHide,
		onUnload,
		onPullDownRefresh,
	} from '@dcloudio/uni-app'
	import { reactive, ref, toRefs } from "vue";
	import utils from '@/utils/utils.js';
	import useSystemSettingsStore from '@/store/useSystemSettingsStore';
	import { storeToRefs } from 'pinia';
	const { scanState } = storeToRefs(useSystemSettingsStore())
	const tip = ref(null)
	const details = reactive({
		scantype: 1,
		mergeTray: '',//来源托盘
		newmergeTray: '',//目标托盘
		scancode: '',
		tabberstate: 1,
		beilist: {} as any,
		newbeilist: {} as any,
		renderList: []
	})
	onLoad((op) => {
	})

	onShow(() => {
		// #ifdef APP
		uni.$off('scancodedate')
		uni.$on('scancodedate', function (code) {
			if (code) {
				details.scancode = code
				scandata()
			}
		})
		// #endif

	})

	/* 	onHide(() => {
			uni.$off('scancodedate')
		}) */
	onPullDownRefresh(() => {
		const timer = setTimeout(() => {
			// 重置数据
			details.scantype = 1
			details.beilist = {}
			details.newbeilist = {}
			details.scancode = ''
			details.renderList = []
			details.mergeTray = ''
			details.newmergeTray = ''

			// 关闭下拉动画
			uni.stopPullDownRefresh()
			clearTimeout(timer)
		}, 500)
	})
	function setstate(state : number) {
		details.scantype = state
		if (state === 1) utils.ttsspke('请扫描待解绑托盘')
		if (state === 2) utils.ttsspke('请扫描合托托盘')
	}
	function goorderdetail(item) {
		uni.navigateTo({
			url: '/pagesHome/pages/orderDetails/orderDetails?orderCode=' + item.orderCode
		})
	}
	async function scandata() {
		// let res=await 
		let res = null
		if (details.scantype == 1) {
			if (details.mergeTray) {
				details.scancode = details.mergeTray
			}
			let data = {
				trayCode: details.scancode
			}
			res = await warehouseTrayTypescanSourceTrayCode(data)
		} else if (details.scantype == 2) {
			if (details.newmergeTray) {
				details.scancode = details.newmergeTray
			}
			let data = {
				trayCode: details.scancode
			}
			res = await warehouseTrayTypescanTargetTrayCode(data)
		}

		if (res.code == 200) {
			if (details.scantype == 1) {
				details.beilist = res.data
				details.mergeTray = details.scancode
				details.scantype = 2
			} else if (details.scantype == 2) {
				details.newbeilist = res.data
				details.newmergeTray = details.scancode
				details.scantype = 1
			}
			// 给渲染;列表赋值
			details.renderList = details.tabberstate === 1 ? details.beilist.list : details.newbeilist.list
		} else {

		}
	}
	function settabberstate(state : number) {
		details.tabberstate = state
		if (state === 1 && details.beilist.list) details.renderList = details.beilist.list
		else if (state === 2 && details.newbeilist.list) details.renderList = details.newbeilist.list
		else details.renderList = []
	}
	function godetails() {
		// uni.navigateTo({
		// 	url:'/pagesHome/pages/MergeTrayDetails/MergeTrayDetails'
		// })
		if (!details.newmergeTray) {
			utils.ttsspke('请扫描解绑托盘')
			uni.showToast({
				title: '请扫描解绑托盘',
				icon: 'none'
			})
			return
		}
		if (!details.mergeTray) {
			utils.ttsspke('请扫描合并托盘')
			uni.showToast({
				title: '请扫描合并托盘',
				icon: 'none'
			})
			return
		}
		tip.value.setdetails({
			isshow: true,
			content: '是否确认合并？',
			confirmTxt: '合并',
			success: async () => {
				uni.showLoading({
					mask: true,
					title: '合并中'
				})
				let data = {
					sourceTrayCode: details.mergeTray,
					targetTrayCode: details.newmergeTray
				}
				let res = await warehouseTrayTypemergeTray(data)
				// 关闭loading动画
				uni.hideLoading()
				if (res.code == 200) {
					utils.ttsspke('合托成功')
					uni.navigateTo({
						url: '/pagesHome/pages/MergeTrayDetails/MergeTrayDetails?tray=' + details.newmergeTray
					})
				}
				tip.value.setdetails({ isshow: false })
			},
			cancel: () => {
				tip.value.setdetails({ isshow: false })
			},
			close: () => {
				tip.value.setdetails({ isshow: false })
			},
		})
	}

	const {
		beilist,
		newbeilist,
		newmergeTray,
		mergeTray,
		scantype,
		scancode,
		tabberstate,
		renderList
	} = toRefs(details)
</script>

<style lang="scss">
	.butys {
		display: flex;
		align-items: center;
		margin-top: 20upx;
		justify-content: space-around;

		>.conf {
			width: 320upx;
			height: 100upx;
			border-radius: 20upx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32upx;
			font-weight: 400;
			color: #FFFFFF;
			background-color: #D3832A;
		}

		>.qut {
			width: 320upx;
			height: 100upx;
			border-radius: 20upx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32upx;
			font-weight: 400;
			color: #5A6875;
			background-color: #FFFFFF;
		}
	}

	.scvorder {
		width: 100%;
		margin-top: 20upx;
		height: 47vh;

		.itembox {
			width: 686upx;
			border-radius: 20upx;
			padding: 0 20upx;
			box-sizing: border-box;
			background-color: #ffffff;
			margin: auto;
			margin-bottom: 20upx;

			>.topon1 {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20upx 10upx;
				box-sizing: border-box;
				border-bottom: 4upx solid #EEEEEE;

				>view:nth-of-type(1) {
					width: 50rpx;
					height: 50rpx;
					background: #178AF220;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 28upx;
					font-weight: 500;
					color: #178AF2;
				}

				>view:nth-of-type(2) {
					display: flex;
					align-items: center;

					>view {
						width: 136upx;
						height: 64upx;
						border-radius: 8upx;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 28rpx;
						font-weight: 400;
						color: #90A0AF;
						background-color: #F5F5F6;
						margin-right: 20upx;

						&:nth-last-child(1) {
							margin-right: 0upx;
						}
					}

					.xzc {
						background-color: #178AF2 !important;
						color: #ffffff !important;
					}

					.xze {
						background-color: #F8544B !important;
						color: #ffffff !important;
					}

					>image {
						width: 32upx;
						height: 32upx;
					}
				}
			}

			>.topon2 {
				display: flex;
				align-items: center;
				justify-content: flex-start;
				padding: 20upx 10upx;
				box-sizing: border-box;
				border-bottom: 4upx solid #EEEEEE;

				>image {
					width: 32upx;
					height: 32upx;
					margin-right: 20upx;
				}

				>view {
					font-size: 28upx;
					font-weight: 400;
					color: #020B18;
					flex: 1;
				}
			}
		}
	}

	.tpbx {
		display: flex;
		align-items: center;
		padding: 18upx 24upx;
		box-sizing: border-box;
		border-bottom: 4upx solid #EEEEEE;

		>image {
			flex: none;
			width: 32upx;
			height: 32upx;
			margin-right: 15upx;
		}

		>view {
			font-size: 28upx;
			font-weight: 400;
			color: #020B18;

			.numb {
				font-size: 28upx;
				color: #178AF2;
			}
		}

	}

	.viewnum {
		height: 100upx;
		display: flex;
		align-items: center;
		justify-content: space-around;

		>view {
			font-size: 28upx;
			font-weight: 400;
			color: #020B18;
		}
	}

	.tarynumvw {
		width: 100%;
		padding: 24upx 70upx;
		box-sizing: border-box;
		background-color: #ffffff;
		margin-top: 4upx;
		box-sizing: border-box;
	}

	.tabsetnumb {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #ffffff;
		margin-top: 20upx;

		.xz {
			>view:nth-of-type(1) {
				color: #D3832A !important;
			}

			>view:nth-of-type(2) {
				background-color: #D3832A !important;
			}
		}

		>view {
			width: 50%;
			display: flex;
			flex-direction: column;
			align-items: center;

			>view:nth-of-type(1) {
				font-size: 32upx;
				font-weight: 400;
				color: #020B18;
				padding: 30upx 0;
			}

			>view:nth-of-type(2) {
				width: 96upx;
				height: 8upx;
				background-color: #D3832A00;
				border-radius: 4upx;
			}
		}
	}

	.topbos {
		display: flex;
		flex-direction: column;

		>.srview {

			padding: 26upx 32upx;
			box-sizing: border-box;
			background-color: #ffffff;
			margin-bottom: 4upx;
			font-size: 28upx;

			.box {
				display: flex;
				align-items: center;

				&:nth-last-child(1) {
					margin-bottom: 0;
				}

				>view:nth-of-type(1) {
					width: 190upx;
					font-size: 28upx
				}

				>view:nth-of-type(2) {
					font-size: 28upx;
					flex: 1;
					display: flex;
					align-items: center;

					>input {
						flex: 1;
						font-size: 28upx;
					}
				}
			}

			.sbm {
				width: 150upx;
				height: 64upx;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: #D3832A;
				color: #ffffff;
				border-radius: 10upx;
			}


		}
	}
</style>