<template>
	<!-- <u-navbar title="分拣" placeholder border=true :autoBack="true" leftIconSize='35'></u-navbar> -->
	<u-navbar title="扫描" placeholder :autoBack="false" @leftClick="backPage" leftIconSize='35' bgColor='#d3832a'
		leftIconColor='#ffffff' titleStyle='color:#ffffff'></u-navbar>
	<image mode="widthFix" class="bgimg" src="/pagesHome/static/bgby.png"></image>
	<!-- <view class="headtop">
		<view>
			<input placeholder="请输入订单号" />
		</view>
	</view> -->
	<view class="mabxtop">
		<view>
			<view>
				<view>{{(detauser?.trayName)||''}}</view>
				<view>托盘名称</view>
			</view>
			<view>
				<view>{{(detauser?.trayCode)||details.trayCode}}</view>
				<view>托盘码</view>
			</view>
		</view>
		<view>
			<view>
				<view>{{(detauser?.orderlNum)||0}}</view>
				<view>订单在托数量</view>
			</view>
			<view>
				<view>{{(detauser?.orderTotalNum)||0}}</view>
				<view>订单总数</view>
			</view>
		</view>
		<view>
			<view>
				<view>{{(detauser?.stockNum)||0}}</view>
				<view>库存品在托数量</view>
			</view>
			<view>
				<view>{{(detauser?.stockTotalNum)||0}}</view>
				<view>库存品总数</view>
			</view>
		</view>
		<view>
			<view>
				<view>{{detauser?.trayTypeName}}</view>
				<view>打托方式</view>
			</view>
			<view>
				<view>{{(detauser?.name)||'暂无数据'}}</view>
				<view>名称</view>
			</view>
		</view>
		<view>
			<view @click="setshowstate(1)">
				<view>{{details.Remarktext}}</view>
				<view>备注</view>
			</view>
			<view>
				<view class="printBtn" @click="print">打印</view>
			</view>
		</view>
	</view>

	<view class="tabBar">
		<view :class="{'tabBar_item': true, 'active': details.tabBarState === 1}" @click="setTabBarState(1)">
			定制品
		</view>
		<view :class="{'tabBar_item': true, 'active': details.tabBarState === 2}" @click="setTabBarState(2)">
			零担
		</view>
		<view :class="{'tabBar_item': true, 'active': details.tabBarState === 3}" @click="setTabBarState(3)">
			库存品
		</view>
	</view>

	<scroll-view class="scvmabx" scroll-y="true">
		<template v-if="details.tabBarState === 3">
			<view class="marketName" @click="chooseMarket">
				<u-icon name="bag" color="#2979ff"></u-icon>
				<view>商场名称：</view>
				<view>
					{{details.stock.marketName||'暂无数据'}}
				</view>
			</view>
		</template>
		<view class="maxboxs">
			<block v-for="item in datelist">
				<!-- 定制品 -->
				<template v-if="details.tabBarState === 1">
					<view class="items chuanhuo" @click="godetaillist(item)">
						<view class="tophd" @click.stop.prevent>
							<image src="/pagesHome/static/topicons.png"></image>
							<view>订单号：</view>
							<view :style="item.isFleeing?'color: #ff0000;':''">{{item.dataCode}}{{item.isFleeing?'（窜）':''}}
							</view>
							<image @click="removeitem(item)" src="/pagesHome/static/removeitem.png"></image>
						</view>
						<view class="tophd" @click.stop.prevent>
							<view>服务号：</view>
							<view>{{item.serviceNumber}}</view>
						</view>
						<view class="numbxvie">
							<view>
								<view class="zhon">{{item.orderTotalNum}}</view>
								<view>总数</view>
							</view>
							<view>
								<view class="yish">{{item.scanNum}}</view>
								<view>已扫</view>
							</view>
							<view>
								<view class="weish">{{item.orderTotalNum - item.scanNum }}</view>
								<view>未扫</view>
							</view>
						</view>
						<image v-if="item.orderTotalNum!=item.scanNum" class="weiqt" src="/pagesHome/static/weiqitao.png">
						</image>
						<image v-else class="qitao" src="/pagesHome/static/qitao.png"></image>
					</view>
				</template>

				<!-- 零担 -->
				<template v-if="details.tabBarState === 2">
					<view class="items">
						<view class="tophd" @click.stop.prevent>
							<image src="/pagesHome/static/topicons.png"></image>
							<view>订单号：</view>
							<view>{{item.dataCode}}</view>
							<image @click="removeitem(item)" src="/pagesHome/static/removeitem.png"></image>
						</view>
						<view class="tophd" @click.stop.prevent>
							<view>运单号：</view>
							<view>{{item.waybillNo}}</view>
						</view>
						<view class="numbxvie">
							<view>
								<view class="zhon">{{item.orderTotalNum}}</view>
								<view>总数</view>
							</view>
							<view>
								<view class="yish">{{item.scanNum}}</view>
								<view>打托数量</view>
							</view>
						</view>
					</view>
				</template>

				<!-- 库存品 -->
				<template v-if="details.tabBarState === 3">
					<view class="items">
						<view class="tophd1">
							<view>
								<image src="/pagesHome/static/topicons.png"></image>
								<view>订单号：</view>
								<view>{{item.dataCode}}</view>
							</view>
							<image @click="removeitem(item)" src="/pagesHome/static/removeitem.png"></image>
						</view>
						<view class="tophd">
							<image src="/pagesHome/static/uname.png"></image>
							<view>物料名称：</view>
							<view>{{item.dataName}}</view>
						</view>
						<view class="tophd">
							<image src="/pagesHome/static/uname.png"></image>
							<view>批次号：</view>
							<view>{{item.incomingBatch}}</view>
						</view>
						<view class="tophd">
							<image src="/pagesHome/static/uname.png"></image>
							<view>SKU：</view>
							<view>{{item.sku}}</view>
						</view>
						<view class="tophd">
							<image src="/pagesHome/static/uname.png"></image>
							<view>物料单位：</view>
							<view>{{item.cargoUnit}}</view>
						</view>
						<view class="numbxvie">
							<view>
								<view class="zhon">{{item.orderTotalNum||0}}</view>
								<view>在库数</view>
							</view>
							<view>
								<view class="yish">{{item.scanNum||0}}</view>
								<view>已扫</view>
							</view>
						</view>
					</view>
				</template>
			</block>
		</view>
	</scroll-view>

	<!-- 零担/库存品继续录入 -->
	<template v-if="details.tabBarState !== 1">
		<view class="buts" @click="setshowstate(details.tabBarState)">继续录入</view>
	</template>

	<template v-if="details.showstate !== 0">
		<view class="tanchuangbg" @click="setshowstate(0)" v-if="showstate!==0">
			<!-- 备注 -- 修改 -->
			<view class="tanbx" @click.stop.prevent v-if="showstate==1">
				<view class="titlran">备注</view>
				<view class="textareabx">
					<textarea maxlength="-1" placeholder="请输入备注" v-model="details.Remarktext"></textarea>
				</view>
				<view class="btx">
					<view class="cancel" @click="setshowstate(0)">取消</view>
					<view class="confirm" @click="checkremaker">确认</view>
				</view>
			</view>

			<!-- 零担 -- 录入 -->
			<template v-if="showstate === 2">
				<view class="inputbox" @click.stop.prevent>
					<view class="inputscan">
						<input placeholder="输入运单号" v-model="waybillCode" />
						<view class="scanbt" @click="scanlist">搜索</view>
					</view>
					<scroll-view scroll-y="true" class="scvbw">
						<view class="scboxitem">
							<view class="items" v-for="(item,index) in codelist">
								<view class="type1s">
									<view>订单号：</view>
									<view>{{item.dataCode}}</view>
								</view>
								<view class="type1s">
									<view>
										<view>总件数：</view>
										<view>{{item.orderTotalNum}}件</view>
									</view>
								</view>
								<view class="type1s">
									<view>
										<view>剩余件数：</view>
										<view>{{item.residueNum}}件</view>
									</view>
								</view>
								<view class="rksu">
									<view>
										<view>入库数：</view>
										<input placeholder="请输入入库数" v-model="item.number" type="number" @input="inputNum(item)" />
									</view>
								</view>
								<view class="lisbite" v-if="checkinarr.indexOf(index)!=-1">
									<view class="itembox" v-for="ite in item.positionList">
										<view v-if="ite.allocationName">库位码：{{ite.allocationName}}</view>
										<view>托盘码：{{ite.trayCode}}</view>
										<view>数量：{{ite.num}}</view>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
					<view class="btx">
						<view class="cancel" @click="setshowstate(0)">取消</view>
						<view class="confirm" @click="confirmlist">确认</view>
					</view>
				</view>
			</template>


			<!-- 库存品 -- 录入 -->
			<template v-if="showstate === 3">
				<view class="inputbox" @click.stop.prevent>
					<view class="inputscan">
						<input placeholder="输入物料编码" v-model="details.stock.materialCode" />
						<view class="scanbt" @click="stockSearch">搜索</view>
					</view>
					<scroll-view scroll-y="true" class="scvbw">
						<view class="scboxitem">
							<view class="items" v-for="(item,index) in details.stock.searchStockList">
								<view class="type1s">
									<view>
										<view>物料编码：</view>
										<view>{{item.cargoNumber}}</view>
									</view>
								</view>
								<view class="type1s">
									<view>
										<view>物料名称：</view>
										<view>{{item.descriptionGoods}}</view>
									</view>
								</view>
								<view class="type1s">
									<view>
										<view>订单编号：</view>
										<view>{{item.orderCode}}</view>
									</view>
								</view>
								<view class="type1s">
									<view>
										<view>物料单位：</view>
										<view>{{item.cargoUnit}}</view>
									</view>
									<view>
										<view>包装数量：</view>
										<view>{{item.cargoNorms}}</view>
									</view>
								</view>
								<view class="type1s">
									<view>
										<view>剩余数量：</view>
										<view>{{item.quantityStock}}{{item.cargoUnit}}</view>
									</view>
									<view>
										<view>SKU：</view>
										<view>{{item.sku}}</view>
									</view>
								</view>
								<view class="rksu" @click.stop.prevent>
									<view>
										<view>数量：</view>
										<input placeholder="请输入数量" v-model="item.maknumber" @input="stockInput(item)" />
									</view>
								</view>
								<view class="lisbite" v-if="checkinarr.indexOf(index)!=-1">
									<view class="itembox" v-for="ite in item.positionList">
										<view>库位：
											<text v-if="ite.areaName">{{ite.areaName}}</text>
											<text v-if="ite.allocationName">{{'-'+ite.allocationName}}</text>
										</view>
										<view>托盘码：{{ite.trayCode}}</view>
										<view>数量：{{ite.num}}</view>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
					<view class="btx">
						<view class="cancel" @click="setshowstate(0)">取消</view>
						<view class="confirm" @click="confirmlur">确认</view>
					</view>
				</view>
			</template>
		</view>
	</template>
	<!-- #ifdef APP -->
	<saomiao2 :ishidestop="scanState !== 0"></saomiao2>
	<!-- #endif -->
	<tips ref="tip"></tips>
	<tiplist ref="tiplists"></tiplist>
	<BluetoothList ref="bluetoothList"></BluetoothList>
</template>

<script lang="ts" setup>
	import {
		warehouseTrayTypedeleteOrderCode,
		warehouseTrayTypeorderScanOrderPackageCode,
		warehouseTrayTypeorderScanTrayCode,
		warehouseTrayTypedeleteStockByTrayGoodsId,
		warehouseTrayTypezeroOrderUpdateRemark,
		warehouseTrayenterZeroOrderByTrayCode,
		warehouseTrayTypegetZeroOrderByWaybillCode,
		warehouseTrayTypeselectStockByMaterialCodeAndMarketId,
		warehouseTrayTypeenterStockNoDataMaterialCode,
		warehouseTrayTypefindAllMarket,
		warehouseTrayTypedeleteZeroOrderByTrayCode
	} from '@/api/user.js'
	import {
		onLoad,
		onShow,
		onHide,
		onPullDownRefresh
	} from '@dcloudio/uni-app'
	import utils from '@/utils/utils.js'
	import { reactive, ref, toRefs } from "vue";
	import dayjs from 'dayjs';
	// 引入Pinia仓库
	import useBluetoothStore from '@/store/useBluetoothStore.js';
	import useSystemSettingsStore from '@/store/useSystemSettingsStore';
	import { storeToRefs } from 'pinia';
	const { scanState } = storeToRefs(useSystemSettingsStore())
	let details = reactive({
		trayCode: '',
		trayType: '',
		scancode: '',
		datelist: [],
		codelist: [],
		checkinarr: [],
		waybillCode: '',
		detauser: null as any,
		trayCodevalue: '',
		iscan: false,
		tabBarState: 1,
		// 备注
		Remarktext: '',
		// 零担弹窗开启状态
		showstate: 0,
		// 库存品
		stock: {
			// 商城Id
			marketId: '',
			marketName: '',
			// 物料编码
			materialCode: '',
			// 库存品列表
			searchStockList: []
		}
	})
	let tip = ref(null)
	const tiplists = ref(null)
	const bluetoothList = ref(null)

	const bluetoothStore = useBluetoothStore()
	const { bluetoothInfo } = storeToRefs(bluetoothStore)
	onLoad((op) => {
		details.trayCode = op.trayCode
		details.trayType = op.trayType
		if (op.pageName) utils.ttsspke('当前打托方式为' + op.pageName)
		console.log('details.trayType :>> ', details.trayType);
		if (!op.trayType) {
			uni.showToast({
				title: '获取打托方式出错',
				icon: 'none'
			})
		}
	})
	onShow(async () => {
		// #ifdef APP
		uni.$off('scancodedate')
		uni.$on('scancodedate', function (code) {
			if (code) {
				console.log(code);
				details.scancode = code
				scandata()
				details.iscan = true
			}
		})
		// #endif

		initlist()
	})
	/* 	onHide(() => {
			uni.$off('scancodedate')
		}) */
	// 下拉刷新
	onPullDownRefresh(() => {
		const timer = setTimeout(async () => {
			await initlist()

			uni.stopPullDownRefresh()
			clearTimeout(timer)
		}, 500)
	})
	async function godetaillist(item) {
		uni.navigateTo({
			url: '/pagesHome/pages/OrderSortingDetailList/OrderSortingDetailList?orderCode='
				+ item.dataCode + '&trayCode='
				+ details.trayCode
		})
	}
	/**
	 * 删除
	 */
	function removeitem(item : any) {
		// 定制品
		if (details.tabBarState === 1) removeOrder(item)
		// 零担
		else if (details.tabBarState === 2) removeZero(item)
		// 库存品
		else if (details.tabBarState === 3) removeStock(item)
	}

	/**
	 * 删除定制品
	 */
	function removeOrder(item : any) {
		tip.value.setdetails({
			title: '提示',
			content: '确认删除该订单与托盘码的绑定？',
			confirmTxt: '确认',
			isshow: true,
			isshowcancel: true,
			success: async () => {
				let data = {
					trayCode: details.trayCode,
					orderCode: item.dataCode,
				}
				let res = await warehouseTrayTypedeleteOrderCode(data)
				if (res.code !== 200) return
				console.log('res :>> ', res);
				initlist()
				tip.value.setisshow(false)
			},
			cancel: () => {
				tip.value.setisshow(false)
			},
			close: () => {
				tip.value.setisshow(false)
			}
		})
	}
	/**
	 * 删除零担
	 */
	function removeZero(item : any) {
		tip.value.setdetails({
			title: '提示',
			content: '确认删除该订单和托盘的绑定？',
			confirmTxt: '确认',
			isshow: true,
			isshowcancel: true,
			success: async () => {
				let data = {
					trayCode: details.trayCode,
					orderCode: item.dataCode
				}
				let response = await warehouseTrayTypedeleteZeroOrderByTrayCode(data)
				if (response.code == 200) {
					initlist()
				}
				tip.value.setisshow(false)
			},
			cancel: () => {
				tip.value.setisshow(false)
			},
			close: () => {
				tip.value.setisshow(false)
			}
		})
	}
	/**
	 * 删除库存品
	 */
	function removeStock(item : any) {
		tip.value.setdetails({
			isshow: true,
			content: '确认删除该数据？',
			confirmTxt: '删除',
			success: async () => {
				let data = {
					trayGoodsId: item.trayGoodsId
				}
				let res = await warehouseTrayTypedeleteStockByTrayGoodsId(data)
				console.log('res :>> ', res);
				if (res.code == 200) {
					initlist()
				}
				tip.value.setdetails({ isshow: false })
			},
			cancel: () => {
				tip.value.setdetails({ isshow: false })
			},
			close: () => {
				tip.value.setdetails({ isshow: false })
			},
		})
	}

	async function initlist() {
		let data = {
			trayCode: details.trayCode
		}
		let res = await warehouseTrayTypeorderScanTrayCode(data)
		console.log(res.data, 'resresresres');
		if (res.code == 200) {
			// 有数据
			if (res.data.isHasData == 1) {
				// details.datelist = res.data.data.list
				details.detauser = res.data.data
				details.stock.marketId = res.data.data.marketId
				details.stock.marketName = res.data.data.marketName
				details.Remarktext = res.data.data.reamrk
				console.log(' details.detauser :>> ', details.detauser);
				if (details.tabBarState === 1) details.datelist = details.detauser.packageList
				else if (details.tabBarState === 2) details.datelist = details.detauser.zeroList
				else if (details.tabBarState === 3) details.datelist = details.detauser.stockList
			}
			// 无数据
			else {
				res.data.data.map(item => {
					if (item.dictKey == details.trayType) {
						details.trayCodevalue = item.dictValue
					}
				})
			}
		}
		return res
	}
	function scandata() {
		// 当扫描的码值为托盘码时
		if (details.scancode[0] === 'T') scanTray()
		// 定制品
		else if (details.tabBarState === 1) return scanOrder()
		// 零担
		else if (details.tabBarState === 2) return
		// 库存品
		else if (details.tabBarState === 3) return scanStock()
	}

	/**
	 * 扫描托盘码
	 */
	async function scanTray() {
		let data = {
			trayCode: details.scancode
		}
		let res = await warehouseTrayTypeorderScanTrayCode(data)
		if (res.code !== 200) return

		console.log('res', res);
		if (Number(res.data.isHasData) === 0) {
			uni.setStorageSync('trayCodeType', res.data.data)
			uni.navigateTo({
				url: '/pagesHome/pages/ScanSortingType/ScanSortingType?trayCode=' + details.scancode
			})
		} else {
			uni.navigateTo({
				url: '/pagesHome/pages/OrderSortingDetail/OrderSortingDetail?trayCode='
					+ res.data.data.trayCode + '&trayType='
					+ res.data.data.trayType
			})
		}
	}

	/**
	 * 扫描包件执行的回调
	 */
	async function scanOrder() {
		let submitData = {
			trayType: details.trayType,
			trayCode: details.trayCode,
			orderPackageCode: details.scancode,
		}
		let response = await warehouseTrayTypeorderScanOrderPackageCode(submitData)
		console.log('response', response);
		const res = await initlist()
		const { code } = res

		console.log('res :>> ', res);

		console.log('details.scancode :>> ', details.scancode);

		// 判定扫描包件状态是否正常或是否已打托
		if (code !== 200) return

		if (details?.detauser?.trayNum && details?.detauser?.trayNum > 0) {
			if (response.code === 200) utils.ttsspke(details.detauser.trayNum + '件')

			const { data: { orderCode } } = response
			// 查询被扫描的订单, 检测订单内的数量是否齐套
			const scanOrder = details.datelist.find(val => val.dataCode === orderCode)
			console.log('scanOrder :>> ', scanOrder);
			if (scanOrder && scanOrder.orderTotalNum === scanOrder.scanNum) {
				// #ifdef APP
				utils.ttsspke(`订单${orderCode}已齐套`)
				// #endif
			}
		}
	}

	/**
	 * 扫描库存品执行的回调
	 */
	async function scanStock() {
		let data = {
			orderPackageCode: details.scancode
		}

		const res = warehouseTrayTypeenterStockNoDataMaterialCode(data)
		console.log('res :>> ', res);
	}

	/**
	 * 修改tabBar选中状态
	 */
	async function setTabBarState(state : number) {
		details.tabBarState = state
		// 定制品
		if (details.tabBarState === 1) details.datelist = details.detauser ? details.detauser.packageList : []
		// 零担
		else if (details.tabBarState === 2) details.datelist = details.detauser ? details.detauser.zeroList : []
		// 库存品
		else if (details.tabBarState === 3) {
			details.datelist = details.detauser ? details.detauser.stockList : []
			if (!details.stock.marketId) chooseMarket()
		}
	}

	/**
	 * 库存品 -- 选择商场
	 */
	async function chooseMarket() {
		console.log('111 :>> ', 111);
		// 如果没有商场ID, 弹出弹窗选择商场
		console.log('details.datelist :>> ', details.datelist.length);
		// 没有打托库存品时, 可以选择商场
		if (details.datelist.length === 0) {
			const res = await warehouseTrayTypefindAllMarket({})
			console.log('res :>> ', res);
			if (res.code !== 200) return
			const _marketArr = res.data

			const list = _marketArr.map(val => val.marketName)

			tiplists.value.setdetails({
				isshow: true,
				tipstate: 2,
				title: '请选择商场',
				list,
				isonecheck: true,
				success: (detail) => {
					if (detail.checklist.length === 0) {
						return uni.showToast({
							title: '请选择商场',
							icon: 'none'
						})
					}
					console.log('detail :>> ', detail);
					details.stock.marketId = _marketArr[detail.checklist[0]].marketId
					console.log('_marketName :>> ', _marketArr);
					details.stock.marketName = _marketArr[detail.checklist[0]].marketName
					tiplists.value.setdetails({ isshow: false })
				},
				cancel: () => {
					tiplists.value.setdetails({ isshow: false })
				},
				close: () => {
					tiplists.value.setdetails({ isshow: false })
				}
			})
		}
	}

	/**
	 * 托盘 -- 修改备注
	 */
	async function checkremaker() {
		let data = {
			trayCode: details.trayCode,
			remark: details.Remarktext
		}
		let response = await warehouseTrayTypezeroOrderUpdateRemark(data)
		if (response.code == 200) {
			initlist()
			setshowstate(0)
		}
	}

	/**
	 * 零担弹框开启状态
	 */
	function setshowstate(state : number) {
		details.showstate = state
	}

	// 输入防抖并限制输入数字的最大和最小值
	let isClick = null
	function inputNum(item) {
		if (isClick) clearTimeout(isClick)
		isClick = setTimeout(() => {
			item.number = parseInt(item.number)
			// 当值不是数字为NaN时, item.number为0
			if (item.number !== item.number) item.number = 0
			// 最大限制
			if (item.number >= item.residueNum) item.number = item.residueNum
			// 最小限制
			if (item.number <= 0) item.number = 0
			clearTimeout(isClick)
		}, 500)
	}

	// 输入防抖并限制输入数字的最大和最小值
	let isClickS = null
	function stockInput(item) {
		if (isClick) clearTimeout(isClick)
		isClickS = setTimeout(() => {
			item.maknumber = parseInt(item.maknumber)
			// 当值不是数字为NaN时, item.maknumber为0
			if (item.maknumber !== item.maknumber) item.maknumber = 0
			// 最大限制
			if (item.maknumber >= item.quantityStock) item.maknumber = item.quantityStock
			// 最小限制
			if (item.maknumber <= 0) item.maknumber = 0
			clearTimeout(isClickS)
		}, 500)
	}

	async function scanlist() {
		let data = {
			waybillCode: details.waybillCode,

		}
		let response = await warehouseTrayTypegetZeroOrderByWaybillCode(data)
		if (response.code !== 200) return
		console.log(response);
		details.codelist = response.data
		details.codelist.map(item => {
			item['number'] = 0
			return item
		})

	}

	/**
	 * 零担 -- 提交
	 */
	async function confirmlist() {
		let zeroList = []
		// 是否退出提交
		let isfl = false

		console.log('details.codelist :>> ', details.codelist);

		// return
		isfl = !details.codelist.some(item => {
			if (item.number === 0) {
				return false
			}
			if (item.number > item.orderTotalNum) {
				isfl = true
				uni.showToast({
					title: "不能大于订单总数",
					icon: 'none'
				})
			} else {
				let obj = {
					orderId: item.dataId,
					orderCode: item.dataCode,
					num: item.number,
				}
				zeroList.push(obj)
			}

			return true
		})
		// 当数据为空或物料数据未修改时, 不进行提交
		if (isfl || details.codelist.length === 0) {
			return uni.showToast({
				title: '最少修改一项物料',
				icon: 'none'
			})
		}

		// 不符合提交要求, 退出函数
		if (isfl) {
			return
		}

		let data = {
			trayType: details.trayType,
			trayCode: details.trayCode,
			zeroList,
		}
		let res = await warehouseTrayenterZeroOrderByTrayCode(data)
		if (res.code == 200) {
			setshowstate(0)
			initlist()
			details.codelist = []
			details.waybillCode = ''
		}
	}

	/**
	 * 库存品 -- 搜索
	 */
	async function stockSearch() {
		let data = {
			marketId: details.stock.marketId,
			materialCode: details.stock.materialCode
		}
		let res = await warehouseTrayTypeselectStockByMaterialCodeAndMarketId(data)
		if (res.code !== 200) return
		// 筛选剩余数大于0的物料
		details.stock.searchStockList = res.data.filter(val => val.quantityStock !== 0)
	}

	/**
	 * 库存品 -- 提交
	 */
	async function confirmlur() {
		try {
			if (details.stock.searchStockList.length === 0) {
				return uni.showToast({
					title: '最少修改一项物料',
					icon: 'none'
				})
			}
			uni.showLoading({
				mask: true,
				title: '提交中'
			})
			const PromiseArr = details.stock.searchStockList.map(async (item, index) => {
				if (item.maknumber > 0) {
					let data = {
						trayType: 100,
						trayCode: details.trayCode,
						num: item.maknumber,
						marketId: details.stock.marketId,
						materialCode: item.cargoNumber,
						incomingBatch: item.incomingBatch
					}
					return warehouseTrayTypeenterStockNoDataMaterialCode(data)
				}
			})
			// 等待所有请求结束
			Promise.all(PromiseArr).then((res) => {
				console.log('res :>> ', res);
				setshowstate(0)
				initlist()
				details.stock.searchStockList = []
				details.stock.materialCode = ''
			})
		} catch (err) {
			console.log('err :>> ', err);
			//TODO handle the exception
		} finally {
			// 关闭loading动画
			uni.hideLoading()
		}
	}



	/**
	 * 打印
	 */
	const print = () => {
		console.log('bluetoothList :>> ', bluetoothList);
		console.log('details.detauser :>> ', details.detauser);
		tiplists.value.setdetails({
			title: '是否打印',
			isshow: true,
			tipstate: 2,
			list: [],
			checklist: [],
			inputtext: '',
			confirmTxt: '确认打印',
			isonecheck: true,
			success: () => {
				// 没有蓝牙信息时进行蓝牙信息设置
				// #ifdef APP
				if (!bluetoothInfo.value.name) return bluetoothList.value.setdetails({ isshow: true })
				// 打印时间
				const time = dayjs().format('YYYY/MM/DD HH:mm:ss')

				const data = details.detauser
				const _arr = [...data.packageList, ...data.zeroList, ...data.stockList]
				let orderListText = ''
				// 订单列表初始位置
				let _position = 300
				// 总件数
				let totalOrder = 0
				// 总在托数
				let totalScanNum = 0
				_arr.forEach((value, index) => {
					_position += 30
					// 订单自编号
					if (value.isFleeing) orderListText += `T 55 0 10 ${_position} ${'(窜)' + value.dataCode}\r\n`
					else orderListText += `T 55 0 10 ${_position} ${value.dataCode}\r\n`
					// orderListText += `T 55 0 10 ${_position} ${value.dataCode}\r\n`
					// 总件数
					orderListText += `T 55 0 300 ${_position} ${value.orderTotalNum}\r\n`
					// 在托件数
					orderListText += `T 55 0 380 ${_position} ${value.scanNum}\r\n`

					totalOrder += value.orderTotalNum
					totalScanNum += value.scanNum
					if (index === _arr.length - 1) {
						_position += 30
						// 订单自编号
						orderListText += `T 55 0 10 ${_position} 总件数\r\n`
						// 总件数
						orderListText += `T 55 0 300 ${_position} ${totalOrder}\r\n`
						// 在托件数
						orderListText += `T 55 0 380 ${_position} ${totalScanNum}\r\n`
					}
				})
				// let text = `! 0 400 200 ${_position + 60} 1\r\n`
				let text = `! 0 400 200 600 1\r\n`
				text += 'SETBOLD 2\r\n'
				text += 'T 56 0 10 0 打托清单\r\n'
				text += 'B QR 300 0 M 2 U 4\r\n'
				text += `MA,${data.trayCode}\r\n`
				text += 'ENDQR\r\n'
				text += `T 55 0 10 120 打印时间: ${time}\r\n`
				text += `T 55 0 300 120 汇通龙泉\r\n`
				// 商城
				text += `T 55 0 10 150 按${data.trayTypeName}\r\n`
				text += `T 55 0 300 150 ${data.name}\r\n`
				// 车次号
				text += `T 55 0 10 180 车次号\r\n`
				text += `T 55 0 300 180 ${''}\r\n`
				// 分拣人
				text += `T 55 0 10 210 分拣人\r\n`
				text += `T 55 0 300 210 ${data.userName || ''}\r\n`
				// 备注
				text += `T 55 0 10 240 备注:\r\n`
				text += `T 55 0 10 270 ${data.reamrk || ''}:\r\n`

				// 订单清单
				text += `T 55 0 10 300 订单自编号\r\n`
				text += `T 55 0 300 300 总件数\r\n`
				text += `T 55 0 380 300 在托件数\r\n`
				text += orderListText
				text += 'FORM\r\n'
				text += 'PRINT\r\n'
				// 将数据传入打印函数中
				utils.getbl(bluetoothInfo.value, text).then(() => {
					return tiplists.value.setdetails({ isshow: false })
				}).catch(() => {
					return bluetoothList.value.setdetails({ isshow: true })
				})
				/* const _arr = []
				let list = ''
				_arr.forEach((value, index) => {
					const _position = 280 + (index * 30)
					// 是否窜货
					if (value.isFleeing) list += `^FO0,${_position}^FD(窜)订单自编号^FS`
					else list += `^FO0,${_position}^FD订单自编号^FS`
					list += `
													^FO300,${_position}^FD总件数^FS
													^FO380,${_position}^FD在托件数^FS
												`
				})

				let text = `
										^XA
										^CF0,20
										^FO100,550^BC^FD12345678^FS
										^FO0,100^FD打印时间:${time}^FS
										^FO350,100^FD汇通龙泉03850^FS
										^FO0,130^FD按商场: 江油欧派衣橱^FS
										^FO0,160^FD车次号: QYZX231021008^FS
										^FO0,190^FD分拣人: 陈勇^FS
										^FO0,220^FD分拣完成时间: 2023/10/2318:45:00^FS
										^FO0,250^FD备注:^FS
										${list}
									^XZ               
								` */
				// #endif
			},
			cancel: (details) => {
				tiplists.value.setdetails({ isshow: false })
			},
			close: (details) => {
				tiplists.value.setdetails({ isshow: false })
			}
		})
	}

	/**
	 * 返回上一页
	 */
	function backPage() {
		uni.redirectTo({
			url: '/pagesHome/pages/scansorting/scansorting'
		})
	}

	const { datelist, detauser, showstate, waybillCode, checkinarr, Remarktext, codelist } = toRefs(details)
</script>

<style lang="scss">
	// 主题色
	$subjectColor: #d3832a;

	.mabxtop {
		position: relative;
		width: 684upx;
		// height: 348upx;
		background: #FFFFFF;
		border-radius: 20upx;
		margin: auto;
		margin-top: 20upx;
		padding: 60upx;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		>view {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 10upx;

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

			>view {
				&:nth-of-type(1) {
					width: 60%;
				}

				&:nth-of-type(2) {
					width: 40%;
				}

				display: flex;
				flex-direction: column;
				align-items: flex-start;

				>view:nth-of-type(1) {
					font-size: 32upx;
					font-weight: 400;
					color: #020B18;
					word-wrap: break-word;
					word-break: break-all;
				}

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

		.printBtn {
			background: $subjectColor;
			color: #fff !important;
			width: 100upx;
			padding: 10upx;
			text-align: center;
			border-radius: 5upx;
		}
	}

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

	}

	.scvmabx {
		width: 684rpx;
		height: 45vh;
		margin: auto;
		margin-top: 20upx;

		// 商场名称
		.marketName {
			display: flex;
			margin-bottom: 20upx;
			align-items: center;
		}

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

			.chuanhuo {
				border: #ff0000;
			}

			.weiqt {
				position: absolute;
				right: 0;
				top: 0;
				width: 70upx;
				height: 70upx;
			}

			.qitao {
				position: absolute;
				right: 0;
				top: 0;
				width: 70upx;
				height: 70upx;
			}

			>.items {
				width: 690upx;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20upx 30upx;
				box-sizing: border-box;
				background-color: #ffffff;
				border-radius: 10upx;
				margin-bottom: 30upx;
				position: relative;

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

				.tophd {
					width: 100%;
					display: flex;
					align-items: flex-start;
					justify-content: flex-start;
					padding: 15upx 20upx;
					box-sizing: border-box;
					padding-top: 30upx;
					border-bottom: 4upx solid #EEEEEE;

					>image {
						width: 48upx;
						height: 48upx;
						margin-right: 10upx;
						box-sizing: border-box;
					}

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

						&:nth-of-type(1) {
							width: 180upx;
						}
					}

					>view:nth-of-type(2) {
						flex: 1;
					}
				}

				.tophd1 {
					width: 100%;
					display: flex;
					box-sizing: border-box;
					padding: 15upx 20upx;
					border-bottom: 4upx solid #EEEEEE;

					&>view {
						display: flex;
						flex: 1;
						align-items: center;
					}

					image {
						width: 64upx;
						height: 64upx;
					}
				}

				.numbxvie {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-around;
					padding: 15upx 25upx;
					box-sizing: border-box;

					.zhon {
						color: $subjectColor;
					}

					.yish {
						color: #1197EB;
					}

					.weish {
						color: #020B18;
					}

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

						>view:nth-of-type(1) {
							font-size: 36upx;
							margin-bottom: 12upx;
						}

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

	.headtop {
		width: 684upx;
		padding: 20upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		// margin-top: 20upx;
		background-color: #FFFFFF;
		padding: 20upx 30upx;
		box-sizing: border-box;
		position: relative;
		margin: auto;
		border-radius: 20upx;
		margin-top: 30upx;

		>view {
			flex: 1;

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

	.hedtopt {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		background-color: #ffffff;
		padding: 36upx 32upx;
		box-sizing: border-box;

		.typto {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;
			font-size: 28upx;
			font-weight: 400;
			color: #092C4D;
			margin-bottom: 20upx;

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

			>view {
				flex: 1;
			}
		}

	}
</style>

<style lang="scss" scoped>
	$subjectColor: #d3832a;

	// tabBar
	.tabBar {
		margin-top: 10upx;
		padding: 0 30upx;
		display: flex;

		&_item {
			flex: 1;
			flex-basis: 0;
			text-align: center;
			line-height: 80upx;
			background: #fff;
			position: relative;

			&::after {
				content: '';
				display: block;
				position: absolute;
				height: 4upx;
				background: $subjectColor;
				width: 0;
				left: 50%;
				bottom: 0;
				transition: all 0.5s;
			}

			&.active::after {
				left: 0;
				width: 100%;
			}
		}
	}

	.buts {
		position: fixed;
		bottom: 20upx;
		left: 50%;
		transform: translateX(-50%);
		width: 320upx;
		height: 88upx;
		border-radius: 8upx 8upx 8upx 8upx;
		opacity: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32upx;
		font-weight: bold;
		color: $subjectColor;
		margin: auto;
		margin-top: 20upx;
		border: 3upx solid $subjectColor;
	}

	// 零担弹窗
	.tanchuangbg {
		width: 100%;
		height: 100%;
		position: fixed;
		z-index: 99;
		left: 0;
		top: 0;
		background-color: #00000050;
		display: flex;
		align-items: center;
		justify-content: center;
		transition: all 1s;

		.tanbx {
			width: 670upx;
			// height: 600upx;
			background-color: #ffffff;
			box-sizing: border-box;
			border-radius: 10upx;
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 30upx;

			.titlran {
				font-size: 32upx;
				color: #092C4D;
				margin-bottom: 40upx;
			}

			.textareabx {
				width: 560upx;
				height: 240upx;
				background: #F5F5F6;
				border-radius: 8upx;
				padding: 20upx 40upx;
				box-sizing: border-box;

				>textarea {
					width: 100%;
					height: 100%;
					box-sizing: border-box;
					font-size: 28upx;
					font-weight: 400;
				}
			}

			.btx {
				display: flex;
				align-items: center;
				justify-content: space-around;
				margin-top: 50upx;

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

				>.cancel {
					background-color: #F5F5F6;
					color: #5A6875;
					margin-right: 20upx;
				}

				>.confirm {
					background-color: #D3832A;
					color: #FFFFFF;
				}
			}
		}

		.inputbox {
			width: 670upx;
			// height: 600upx;
			background-color: #ffffff;
			box-sizing: border-box;
			border-radius: 10upx;
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 30upx;

			.inputscan {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-around;

				>input {
					width: 440upx;
					height: 64upx;
					border: 1upx solid #092C4D30;
					border-radius: 8upx;
					box-sizing: border-box;
					font-size: 28upx;
					padding: 0upx 20upx;
					box-sizing: border-box;
				}

				.scanbt {
					width: 128upx;
					height: 64upx;
					background: #D3832A;
					border-radius: 8upx;
					opacity: 1;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 30upx;
					font-weight: 400;
					color: #FFFFFF;
				}
			}

			.scvbw {
				width: 100%;
				height: 450upx;
				margin-top: 30upx;

				.scboxitem {
					display: flex;
					flex-direction: column;
					align-items: center;

					.items {
						width: 590upx;
						display: flex;
						flex-direction: column;
						box-shadow: 0upx 2upx 10upx 0upx #e2e2e3;
						margin-top: 10upx;
						padding: 20upx;
						box-sizing: border-box;
						position: relative;

						>.lisbite {
							width: 100%;
							display: flex;
							align-items: center;
							justify-content: center;

							// position: absolute;
							>.itembox {
								width: 100%;
								display: flex;
								flex-direction: column;
								align-items: flex-start;
								justify-content: space-between;
								font-size: 28upx;
								color: #020B18;
								border: 1upx solid #D3832A;
								padding: 10upx 20upx;
								box-sizing: border-box;
								border-radius: 20upx;
								color: #D3832A;

								>view {
									word-wrap: break-word;
									word-break: break-all;
								}
							}
						}

						>.type1s {
							display: flex;
							align-items: center;
							justify-content: space-between;
							margin-bottom: 20upx;

							>view {
								min-width: 50%;
								display: flex;
								align-items: center;
								justify-content: flex-start;
								font-size: 28upx;

								>view:nth-of-type(1) {
									color: #90A0AF;
								}

								>view:nth-of-type(2) {
									color: #092C4D;
								}
							}
						}

						.rksu {
							display: flex;
							align-items: center;
							justify-content: space-between;
							margin-bottom: 20upx;

							>view {
								// width: 50%;
								display: flex;
								align-items: center;
								justify-content: flex-start;
								font-size: 28upx;

								>view:nth-of-type(1) {
									color: #90A0AF;
								}

								>input {
									width: 200upx;
									padding: 0 15upx;
									border: 1upx solid #00000020;
								}
							}
						}
					}
				}
			}

			.btx {
				display: flex;
				align-items: center;
				justify-content: space-around;
				margin-top: 50upx;

				>view {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 260upx;
					height: 76upx;
					border-radius: 7upx;
					font-size: 28upx;
				}

				>.cancel {
					background-color: #F5F5F6;
					color: #5A6875;
					margin-right: 20upx;
				}

				>.confirm {
					background-color: #D3832A;
					color: #FFFFFF;
				}
			}
		}
	}
</style>