<template>
	<u-navbar title="按库位移库" bgColor='#D3832A' leftIconColor='#ffffff' titleStyle='color:#ffffff' placeholder
		:autoBack="true" leftIconSize='35'></u-navbar>
	<image mode="widthFix" class="bgimg" src="/pagesHome/static/bgby.png"></image>
	<view class="scanqcview">
		<image src="/pagesHome/static/scanqc.png"></image>
		<view>请直接扫描库位码</view>
	</view>
	<view class="topviewbox">
		<view class="oebox">
			<image src="/pagesHome/static/changku.png"></image>
			<view>
				{{(dtilobj?.warehouseName||'')+'-'+(dtilobj?.areaTitle||'')+'-'+(dtilobj?.shelfTitle||"")+'-'+(dtilobj?.allocationTitle||'')}}
			</view>
		</view>
		<view class="contview">
			<view>
				<view>
					<view>仓库</view>
				</view>
				<view>{{dtilobj?.warehouseName||'暂无数据'}}</view>
			</view>
			<view>
				<view>
					<view>被移库位</view>
				</view>
				<view class="btm">
					<input v-model="scancode" placeholder="请扫描或输入" />
					<view @click="scandata(true,1)">确认</view>
				</view>
			</view>
		</view>
	</view>
	<view class="topviewbox">
		<view class="oebox">
			<image src="/pagesHome/static/changku.png"></image>
			<view>
				{{(obldate?.warehouseName||'')+'-'+(obldate?.areaTitle||'')+'-'+(obldate?.shelfTitle||"")+'-'+(obldate?.allocationTitle||'')}}
			</view>
		</view>
		<view class="contview">
			<view>
				<view>
					<view>仓库</view>
				</view>
				<view>{{obldate?.warehouseName||'暂无数据'}}</view>
			</view>
			<view>
				<view>
					<view>目标库位</view>
				</view>
				<view class="btm">
					<input v-model="scancode1" placeholder="请扫描或输入" />
					<view @click="scandata(true,2)">确认</view>
				</view>
			</view>
		</view>
	</view>
	<view class="scview">
		<scroll-view class="sclcs" scroll-y="true">
			<view class="visc">
				<view class="item2" v-for="(item,index) in scanlist">
					<!-- 定制品 -->
					<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>
					<image class="qtimgzt"
						:src="item.totalNum==item.shelfNum?'/pagesHome/static/qitao.png':'/pagesHome/static/weiqitao.png'">
					</image>
				</view>
			</view>
		</scroll-view>
	</view>
	<view class="butbox">
		<view>取消</view>
		<view @click="setstate(true)">确认移库</view>
	</view>
	<view class="bgtps" v-if="isshowtot">
		<view class="trary">
			<view class="titqr">移库确认</view>
			<view class="contenbox">
				<view class="topbs1">
					<view>请确认从</view>
					<view>
						<view>
							<image src="/pagesHome/static/kuwei.png"></image>
							<view>库位</view>
						</view>
						<view>{{(dtilobj?.areaTitle||'')+'-'+(dtilobj?.shelfTitle||"")+'-'+(dtilobj?.allocationTitle||'')}}
						</view>
					</view>
				</view>
				<view class="topbs1">
					<view>移动到</view>
					<view>
						<view>
							<image src="/pagesHome/static/kuwei.png"></image>
							<view>库位</view>
						</view>
						<view>{{(obldate?.areaTitle||'')+'-'+(obldate?.shelfTitle||"")+'-'+(obldate?.allocationTitle||'')}}
						</view>
					</view>
				</view>
			</view>
			<view class="btbox">
				<view @click="setstate(false)">取消</view>
				<view @click="uplist">确认</view>
			</view>
		</view>
	</view>
	<saomiao2></saomiao2>
	<tips ref="tip"></tips>
</template>

<script lang="ts" setup>
	import {
		warehouseUpdownTypemoveAllocationByAllocation,
		warehouseUpdownTypemoveAllocationScanSourceAllocation,
		warehouseUpdownTypemoveAllocationScanAllocation,
	} from '@/api/user.js'
	import {
		onLoad,
		onShow,
		onHide,
		onUnload
	} from '@dcloudio/uni-app'
	import { computed, reactive, ref, toRefs, watchEffect } from "vue";
	import utils from '../../../utils/utils';
	const tip = ref(null)
	let details = reactive({
		scancode1: '',
		scancode: '',
		pagetype: '',
		allocationId: '',
		targetAllocationId: '',
		dtilobj: {} as any,
		trayCode: '',
		scanlist: [],
		obldate: {} as any,
		codenumer: 0,
		isshowtot: false,

	})
	onLoad((op) => {
		details.pagetype = op.type
		if (op.pageName) utils.ttsspke(`当前移库方式为${op.pageName}, 请先扫描要移动的库位，再扫描目标库位`)
	})
	// 页面隐藏时, 关闭监听扫描事件
	onHide(() => {
		uni.$off('scancodedate')
	})
	// 开启扫描监听
	onShow(() => {
		uni.$on('scancodedate', function (code) {
			if (code) {
				// console.log(code);
				if (!details.allocationId) {
					details.scancode = code
				} else if (!details.targetAllocationId) {
					details.scancode1 = code
				}

				scandata()
			}
		})
	})
	function goorderdetail(item) {
		uni.navigateTo({
			url: '/pagesHome/pages/orderDetails/orderDetails?orderCode=' + item.orderCode
		})
	}
	const traynumber = computed(() => {
		let numbers = 0
		details.scanlist.map(item => {
			if (item.shelfNum) {
				numbers += item.shelfNum
			} else if (item.num) {
				numbers += item.num
			}
		})
		return numbers
	})
	watchEffect(() => {
		details.scanlist.map(item => {
			details.codenumer += item.shelfNum
		})
		console.log(details.codenumer);
	})
	async function scandata(flg ?: boolean, type ?: number) {
		let res = null
		if (details.scancode[0] === 'T') {
			uni.showToast({
				title: '请扫描库位码',
				icon: 'none'
			})
			// #ifdef APP
			utils.ttsspke('请扫描库位码')
			// #endif
			return
		}
		if (flg) {
			if (type == 1) {
				let data = {
					allocationId: details.scancode
				}
				res = await warehouseUpdownTypemoveAllocationScanSourceAllocation(data)
				if (res.code == 200) {
					details.dtilobj = res.data
					details.allocationId = res.data.allocationId
					details.scanlist = res.data.list
					// details.scancode = ''
					utils.ttsspke('扫描成功, 请再扫描被移动的库位')
				}
				return
			} else {
				let data = {
					allocationId: details.scancode1,
				}
				res = await warehouseUpdownTypemoveAllocationScanAllocation(data)
				if (res.code == 200) {
					details.targetAllocationId = res.data.allocationId
					details.obldate = res.data
				}
			}
			return
		}

		if (!details.allocationId) {
			let data = {
				allocationId: details.scancode
			}
			res = await warehouseUpdownTypemoveAllocationScanSourceAllocation(data)
			if (res.code == 200) {
				details.dtilobj = res.data
				details.allocationId = res.data.allocationId
				details.scanlist = res.data.list
				console.log('res1 :>> ', res);
				// details.scancode = ''
			}
			return
		} else {
			let data = {
				allocationId: details.scancode1,
			}
			res = await warehouseUpdownTypemoveAllocationScanAllocation(data)
			if (res.code == 200) {
				details.targetAllocationId = res.data.allocationId
				details.obldate = res.data
				console.log('res2 :>> ', res);
			}
		}
		// if (details.pagetype == '1') {
		// 	let data = {
		// 		trayCode: details.scancode,
		// 	}
		// 	res = await warehouseUpdownTypemoveAllocationScanTrayCode(data)
		// 	if (res.code == 200) {
		// 		details.trayCode = details.scancode
		// 		details.scanlist = res.data.list
		// 		details.obldate = res.data
		// 	}

		// } else if (details.pagetype == '2') {
		// 	let data = {
		// 		allocationId: details.allocationId,
		// 		orderPackageCode: details.scancode,
		// 	}
		// 	res = await warehouseUpdownTypemoveAllocationScanPackage(data)
		// 	// details.trayCode=details.scancode
		// 	if (res.code == 200) {
		// 		details.scanlist = details.scanlist.concat(res.data)
		// 	}
		// }
	}
	async function uplist() {
		let res = null
		uni.showLoading({
			title: '移库中',
			mask: true
		})
		let data = {
			allocationId: details.allocationId,
			targetAllocationId: details.targetAllocationId,
		}
		res = await warehouseUpdownTypemoveAllocationByAllocation(data)
		// 关闭loading
		uni.hideLoading()
		if (res.code == 200) {
			uni.showToast({
				title: '移库成功',
				icon: 'success'
			})
			details.scancode = ''
			details.scancode1 = ''
			details.allocationId = ''
			details.targetAllocationId = ''
			details.dtilobj = {}
			details.obldate = {}
			details.trayCode = ''
			details.scanlist = []
			details.isshowtot = false
		} else {
			uni.showToast({
				title: '移库失败',
				icon: 'error'
			})
		}
	}
	function setstate(flg : boolean) {
		if (flg && !details.trayCode && details.pagetype == '1') {
			uni.showToast({
				title: '请扫描正确的托盘码',
				icon: 'none'
			})
			return
		} else if (flg && details.scanlist.length == 0 && details.pagetype == '2') {
			uni.showToast({
				title: '请扫描正确的包件码',
				icon: 'none'
			})
			return
		}
		details.isshowtot = flg
	}

	const {
		scancode1,
		obldate,
		isshowtot,
		scancode,
		pagetype,
		dtilobj,
		scanlist,
		trayCode,
	} = toRefs(details)
</script>

<style lang="scss">
	.bgtps {
		width: 100vw;
		height: 100vh;
		position: fixed;
		left: 0;
		top: 0;
		background-color: #00000050;
		z-index: 999;
		display: flex;
		align-items: center;
		justify-content: center;

		>.anjian {
			width: 640upx;
			// height: 800upx;
			background: #FFFFFF;
			border-radius: 8upx;
			display: flex;
			flex-direction: column;
			align-items: center;
			box-sizing: border-box;
			padding: 25upx 20upx;

			>.titqr {
				font-size: 32upx;
				font-weight: 400;
				color: #092C4D;
				margin-bottom: 30upx;
			}

			>.contenbx {
				width: 580upx;
				display: flex;
				align-items: center;
				flex-direction: column;
				margin-bottom: 20upx;

				.titypo {
					font-size: 30upx;
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: flex-start;
					margin-bottom: 40upx;
				}

				>.topbs1 {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: flex-start;
					margin-bottom: 30upx;

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

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

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

						>view:nth-of-type(1) {
							display: flex;
							flex-direction: column;
							align-items: center;
							margin-right: 20upx;

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

							>view {
								font-size: 24upx;
								font-weight: 400;
								color: #90A0AF;
							}
						}

						>view:nth-of-type(2) {
							flex: 1;
							font-size: 28upx;
							font-weight: 400;
							color: #020B18;
						}
					}
				}
			}

			.btbox {
				display: flex;
				align-items: center;
				justify-content: center;
				margin-top: 20upx;

				>view {
					width: 260upx;
					height: 88upx;
					border-radius: 8upx;
					display: flex;
					align-items: center;
					justify-content: center;
					box-sizing: border-box;
					font-size: 32upx;

					&:nth-of-type(1) {
						background-color: #F5F5F6;
						color: #5A6875;
					}

					&:nth-of-type(2) {
						background-color: #D3832A;
						color: #FFFFFF;
						margin-left: 20upx;
					}
				}
			}
		}

		>.trary {
			width: 640upx;
			// height: 800upx;
			background: #FFFFFF;
			border-radius: 8upx;
			padding: 25upx 20upx;
			display: flex;
			flex-direction: column;
			align-items: center;
			box-sizing: border-box;

			>.titqr {
				font-size: 32upx;
				font-weight: 400;
				color: #092C4D;
				margin-bottom: 30upx;
			}

			.contenbox {
				width: 580upx;
				display: flex;
				align-items: center;
				flex-direction: column;
				margin-bottom: 20upx;

				>.topbs1 {
					width: 100%;
					display: flex;
					align-items: center;
					margin-bottom: 30upx;

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

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

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

						>view:nth-of-type(1) {
							display: flex;
							flex-direction: column;
							align-items: center;
							margin-right: 20upx;

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

							>view {
								font-size: 24upx;
								font-weight: 400;
								color: #90A0AF;
							}
						}

						>view:nth-of-type(2) {
							flex: 1;
							font-size: 28upx;
							font-weight: 400;
							color: #020B18;
						}
					}
				}
			}

			.btbox {
				display: flex;
				align-items: center;
				justify-content: center;
				margin-top: 20upx;

				>view {
					width: 260upx;
					height: 88upx;
					border-radius: 8upx;
					display: flex;
					align-items: center;
					justify-content: center;
					box-sizing: border-box;
					font-size: 32upx;

					&:nth-of-type(1) {
						background-color: #F5F5F6;
						color: #5A6875;
					}

					&:nth-of-type(2) {
						background-color: #D3832A;
						color: #FFFFFF;
						margin-left: 20upx;
					}
				}
			}
		}
	}

	.butbox {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 15upx;
		font-size: 32upx;

		>view:nth-of-type(1) {
			width: 320upx;
			height: 100upx;
			background: #FFFFFF;
			border-radius: 20upx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #5A6875;
			font-size: 32upx;
			font-weight: 400;
		}

		>view:nth-of-type(2) {
			width: 320upx;
			height: 100upx;
			background: #D3832A;
			border-radius: 20upx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #FFFFFF;
			font-size: 32upx;
			font-weight: 400;
			color: #FFFFFF;
			margin-left: 20upx;
		}
	}

	.sclcs {
		width: 686upx;
		height: 40vh;
		margin: auto;
		margin-top: 20upx;

		.visc {
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;

			.item1 {
				width: 686upx;
				padding: 0 24upx;
				box-sizing: border-box;
				background-color: #FFFFFF;
				border-radius: 30upx;
				position: relative;
				margin-bottom: 30upx;

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

				.removeicons {
					position: absolute;
					right: 0;
				}

				.qtimgzt {
					width: 96upx;
					height: 96upx;
					position: absolute;
					right: 0;
					top: 0;
				}

				// >view:nth-last-child(1){
				// 	border-bottom: none !important;
				// }
				>view {
					width: 100%;
					display: flex;
					align-items: center;
					padding: 18upx 24upx;
					box-sizing: border-box;
					border-bottom: 4upx solid #EEEEEE;

					&:nth-last-child(1) {
						border-bottom: 0upx solid #EEEEEE00 !important;
					}

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

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

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

			.item2 {
				width: 686upx;
				margin: auto;
				padding: 0 24upx;
				box-sizing: border-box;
				background-color: #FFFFFF;
				border-radius: 30upx;
				position: relative;
				margin-bottom: 30upx;

				.qtimgzt {
					width: 96upx;
					height: 96upx;
					position: absolute;
					right: 0;
					top: 0;
				}

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

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

			.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;
					}
				}
			}
		}
	}

	.tabchecklist {
		width: 684upx;
		height: 100upx;
		margin: auto;
		margin-top: 30upx;
		margin-bottom: 20upx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-around;

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

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

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

			>view:nth-of-type(1) {
				font-size: 32upx;
				font-weight: 400;
				margin-bottom: 20upx;
				color: #020B18;
				transition: all 0.2s;
			}

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

	.topviewbox {
		width: 686upx;
		// height: 456upx;
		background: #FFFFFF;
		border-radius: 20upx 20upx 20upx 20upx;
		opacity: 1;
		box-sizing: border-box;
		position: relative;
		margin: auto;
		margin-top: 20upx;

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

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

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

		.numbox {
			width: 100%;
			padding: 20upx;
			box-sizing: border-box;

			>view {
				width: 640upx;
				height: 160upx;
				background: #F5F7FB;
				border-radius: 8upx 8upx 8upx 8upx;
				opacity: 1;
				display: flex;
				align-items: center;
				justify-content: space-around;

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

					&:nth-of-type(1) {
						>view:nth-of-type(2) {
							color: #FF9545 !important;
						}
					}

					&:nth-of-type(2) {
						>view:nth-of-type(2) {
							color: #178AF2 !important;
						}
					}

					>view:nth-of-type(1) {
						font-size: 28upx;
						font-weight: 400;
						color: #020B18;
						margin-bottom: 18upx;
					}

					>view:nth-of-type(2) {
						font-size: 36upx;
						font-weight: 400;

					}
				}
			}
		}

		>.contview {
			display: flex;
			flex-direction: column;
			width: 100%;
			padding: 20upx;
			box-sizing: border-box;

			>view {
				display: flex;
				align-items: center;
				justify-content: flex-start;
				margin-bottom: 20upx;

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

				>view:nth-of-type(1) {
					display: flex;
					align-items: center;
					justify-content: flex-start;
					margin-right: 30upx;
					margin-left: 50upx;
					width: 120upx;
					color: #020B18;
					font-size: 28upx;
					font-weight: 400;

					// >image{
					// 	width: 48upx;
					// 	height: 48upx;
					// 	margin-right: 12upx;
					// }
					>view {
						font-size: 28upx;
						font-weight: 400;
						color: #020B18;
					}
				}

				>view:nth-of-type(2) {
					font-size: 28upx;
					font-weight: 400;
					color: #020B18;

					>input {
						flex: 1;
					}
				}

				.btm {
					display: flex;
					align-items: center;
					justify-content: space-between;

					>view {
						width: 120upx;
						height: 60upx;
						background: #D3832A;
						border-radius: 8upx;
						display: flex;
						align-items: center;
						justify-content: center;
						color: #FFFFFF;
						font-size: 32upx;
						font-weight: 400;
						color: #FFFFFF;
						margin-left: 20upx;
					}
				}
			}
		}
	}

	.bgimg {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;

	}

	.scanqcview {
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		margin-top: 10upx;

		>image {
			width: 72upx;
			height: 72upx;
			margin-right: 16upx;
		}

		>view {
			font-size: 32upx;
			font-weight: 400;
			color: #FFFFFF;
		}
	}
</style>