<template>
	<!-- <u-navbar title="配置包条码" placeholder border=true :autoBack="true" leftIconSize='35'></u-navbar> -->
	<BasicContainer ref="basicContainer" :option="option">
		<template #head>
			<view class="head">
				<view class="type1">
					<view>物品：<view style="color: #092C4D;">{{items.materialName}}</view>
					</view>
				</view>
				<view class="type1">
					<view>货位：<view style="color: #092C4D;">{{orderpackgedetails.warehouseArea}}</view>
					</view>
					<view>SKU：<view style="color: #092C4D;">{{items.sku}}</view>
					</view>
				</view>
				<view class="type1">
					<view>规格：<view style="color: #092C4D;">{{items.cargoNorms}}</view>
					</view>

					<view>单位：<view style="color: #092C4D;">{{items.cargoUnit}}</view>
					</view>
				</view>
				<view class="type1">
					<view>计划数量：<view style="color: #092C4D;">{{items.planNum}}</view>
					</view>
					<view>已扫数量：<view style="color: #092C4D;">{{codelist.length}}</view>
					</view>
				</view>
			</view>
			<view class="butsbx">
				<template v-if="Number(items.sourceType)===1">
					<view @click="setstockPrint(1)">采集原包条码</view>
				</template>
				<template v-else>
					<view @click="setstockPrint(2)">扫描打印包条</view>
					<view @click="showmd">新生成包码</view>
				</template>
			</view>
		</template>

		<template #body>
			<scroll-view scroll-y="true" class="scrollv">
				<view class="mabxs">
					<view class="ite" v-for="(item,index) in codelist">
						<view>
							<view>序号：{{index+1}}</view>
							<view>作业时间：{{item.createTime}}</view>
						</view>
						<view>
							<view>包条码：{{item.packageCode}}</view>
							<view></view>
						</view>
					</view>
				</view>
			</scroll-view>
		</template>
	</BasicContainer>
	<tiplist ref="tips"></tiplist>
	<saomiao2></saomiao2>
	<BluetoothList ref="bluetoothList"></BluetoothList>
</template>

<script lang="ts" setup>
	import {
		onLoad,
		onShow,
		onHide,
		onUnload
	} from '@dcloudio/uni-app'
	import {
		distributionStockupstockupStockListPackageCode,
		distributionStockupstockupScan,
		distributionStockuporderInfo
	} from '@/api/user.js'
	import { detailType } from '@/interfaces/pagesHome/setbarcode'
	import { reactive, ref, toRefs, inject } from "vue";
	import useBluetoothStore from '@/store/useBluetoothStore.js';
	import { storeToRefs } from 'pinia';
	const bluetoothList = ref(null)
	const bluetoothStore = useBluetoothStore()
	const { bluetoothInfo } = storeToRefs(bluetoothStore)
	const utils = inject('utils') as any
	let details = reactive<detailType>({
		items: {},
		reservationId: '',
		codelist: [],
		printlist: [],
		packageNum: 0,
		goodsAreaName: '',
		allocationId: '',
		stockupId: '',
		stockPrint: 1,
		customer: '',
		deliveryAddress: '',
		completeStact: false,
		orderpackgedetails: {}
	})
	const tips = ref(null)
	const basicContainer = ref(null)
	// 基础组件配置项
	const option = {
		title: '配置包条码',
		haveData: true,
		async pullDownRefreshInitPage() {
			await initpage()
		}
	}
	// const tiplists=ref(null)
	onLoad((op) => {
		details.stockupId = op.stockupId
		details.items = JSON.parse(op.item)
		details.reservationId = op.reservationId
		details.goodsAreaName = op.goodsAreaName
		details.allocationId = op.allocationId
		if (op.completeStact == 'true') {
			details.completeStact = true
		}
		// scandata('1231')
	})
	onShow(() => {
		uni.$on('scancodedate', function (code) {
			if (code) {
				console.log(code);
				scandata(code)
			}
		})
		// 页面显示延时调用组件刷新函数
		const timer = setTimeout(() => {
			basicContainer.value.startPullDownRefresh()
			clearTimeout(timer)
		}, 200)
	})
	onHide(() => {
		uni.$off('scancodedate')
	})
	function showsdqs() {
		tips.value.setdetails({
			isshow: true,
			tipstate: 1,
			title: '请输入扫描的码',
			placeholder: '请输入扫描的码',
			inputtext: '',
			success: (detail) => {
				// details.scancode=
				scandata(detail.inputtext)
				tips.value.setdetails({ isshow: false })
			},
			cancel: () => {
				tips.value.setdetails({ isshow: false })
			},
			close: () => {
				tips.value.setdetails({ isshow: false })
			}
		})
	}
	function setstockPrint(values) {
		details.stockPrint = values
		if (values == 1) {
			utils.ttsspke('请扫描原有包条码')
		} else {
			utils.ttsspke('请扫描打印的包条码')
		}

	}
	async function initpage() {
		let data = {
			orderStatus: 2,
			reservationId: details.reservationId,
			stockListId: details.items.stockListId,
			stockupId: details.stockupId,
			allocationId: details.allocationId,
			status: details.completeStact ? 1 : 0
		}
		let response = await distributionStockuporderInfo(data)
		if (response.code !== 200) return
		details.codelist = response.data.stockList
		details.customer = response.data.customer
		details.deliveryAddress = response.data.deliveryAddress
		details.orderpackgedetails = response.data
	}
	function showmd() {
		tips.value.setdetails({
			list: details.printlist,
			isshow: true,
			tipstate: 1,
			placeholder: '请输入需要生成的包条码数量',
			success: (deta) => {
				console.log(deta);
				details.packageNum = deta.inputtext
				if (Number(details.packageNum) > Number(details.items.planNum)) {
					uni.showToast({
						title: '不能大于计划数量，请重新输入',
						icon: 'none'
					})
					tips.value.setdetails({ inputtext: '' })
					return
				}
				ctokListpackage()

			},
			cancel: (details) => {
				tips.value.setdetails({ isshow: false })
			},
			close: (details) => {
				tips.value.setdetails({ isshow: false })
			}
		})
	}
	async function ctokListpackage() {
		let data = {
			reservationId: details.reservationId,
			stockListId: details.items.stockListId,
			packageNum: details.packageNum,
			allocationId: details.allocationId,
			status: details.completeStact ? 1 : 0
		}
		let response = await distributionStockupstockupStockListPackageCode(data)
		if (response.code !== 200) return
		console.log(response);
		details.printlist = response.data
		let checklist = []
		response.data.map((item, index) => {
			checklist.push(index)
		})
		tips.value.setdetails({
			title: '请选择要打印的包条',
			tipstate: 2,
			list: response.data,
			checklist: checklist,
			inputtext: '',
			confirmTxt: '打印',
			success: (deta) => {
				if (deta.checklist.length == 0) {
					uni.showToast({
						title: '请先选择需要打印的包条码',
						icon: 'none'
					})
					return
				}
				// #ifdef APP
				if (!bluetoothInfo.value.name) return bluetoothList.value.setdetails({ isshow: true })
				let _isReturn = false

				const printList = checklist.map(val => deta.list[val])

				printList.map(item => {
					// const util=(inject('utils') as any)
					let text = '! 0 200 200 330 1\r\n'
					text += 'SETBOLD 2\r\n'
					text += `T 56 0 0 0 ${item}\r\n`
					text += 'LINE 0 50 420 50 3\r\n'
					text += `T 55 0 0 60 客户\r\n`
					text += 'LINE 40 50 40 110 3\r\n'
					text += `T 55 0 50 60 ${details.customer}\r\n`
					text += 'LINE 40 80 420 80 3\r\n'
					text += `T 55 0 50 90 ${details.deliveryAddress}\r\n`
					text += 'LINE 0 110 420 110 3\r\n'
					text += `T 55 0 0 120 物料\r\n`
					text += 'LINE 40 110 40 140 3\r\n'
					text += `T 55 0 60 120 ${details.items.materialName}\r\n`
					text += 'LINE 0 140 420 140 3\r\n'
					text += 'B QR 150 150 M 2 U 4\r\n'
					text += `MA,${item}\r\n`
					text += 'ENDQR\r\n'
					text += 'PRINT\r\n'

					utils.getbl(bluetoothInfo.value, text).catch(() => {
						bluetoothList.value.setdetails({ isshow: true })
						_isReturn = true
					})
				})
				if (_isReturn) return
				// #endif
				// scandata(response.data[0])
				tips.value.setdetails({ isshow: false })
			},
			cancel: (details) => {
				tips.value.setdetails({ isshow: false })
			},
			close: (details) => {
				tips.value.setdetails({ isshow: false })
			}
		})
	}
	async function scandata(code) {
		let data = {
			scanType: 2,
			stockListId: details.items.stockListId,
			packetBarCode: code,
			stockupArea: details.goodsAreaName,
			reservationId: details.reservationId,
			stockupId: details.stockupId,
			stockPrint: details.items.sourceType,
			allocationId: details.allocationId,
			status: details.completeStact ? 1 : 0,
			id: details.orderpackgedetails.stockListId
		}
		// utils.ttsspke('stockPrint是'+details.stockPrint)
		let res = await distributionStockupstockupScan(data)
		console.log(res);
		if (res.code == 200) {
			basicContainer.value.startPullDownRefresh()
		}
	}
	const { orderpackgedetails, items, codelist, printlist } = toRefs(details)
</script>

<style lang="scss">
	.scrollv {
		width: 686upx;
		height: 60vh;
		// padding: 10upx;
		box-sizing: border-box;
		background-color: #ffffff;
		margin: auto;
		margin-top: 20upx;
		padding: 20upx;
		box-sizing: border-box;
		border-radius: 10upx;

		.mabxs {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-top: 5upx;

			.ite {
				width: 96%;
				// width: 100%;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20upx 15upx;
				box-sizing: border-box;
				border-radius: 8upx;
				box-shadow: 0upx 2upx 10upx 0upx #e2e2e3;
				margin-bottom: 20upx;
				padding-bottom: 10upx;
				font-size: 26upx;

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

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

					&:nth-last-child(1) {
						margin-bottom: none !important;
					}

					>view {
						// width: 50%;
						// flex: 1;
						font-size: 26upx;
						font-weight: 400;
						color: #092C4D;
					}
				}
			}
		}
	}

	.butsbx {
		display: flex;
		align-items: center;
		// justify-content: space-around;
		margin-top: 25upx;
		padding: 0 4%;

		>view {
			width: 204upx;
			height: 64upx;
			background: #FFFFFF;
			border-radius: 8upx;
			opacity: 1;
			font-size: 28upx;
			display: flex;
			align-items: center;
			justify-content: center;
			border: 2upx solid #D3832A;
			color: #D3832A;

			&:nth-of-type(2) {
				margin-left: 20upx;
			}
		}
	}

	.head {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-around;
		padding: 20upx;
		// height: 176upx;
		box-sizing: border-box;
		// border-bottom: 1upx solid #ececec;
		background-color: #ffffff;

		>.type1 {
			width: 100%;
			// height: 60upx;
			display: flex;
			align-items: flex-start;
			justify-content: space-between;
			font-size: 28upx;
			font-weight: 400;
			color: #90A0AF;
			margin-bottom: 10upx;

			>view {
				flex: 1;
				display: flex;
				align-items: flex-start;

				>view:nth-of-type(1) {
					flex: 1;
				}
			}
		}
	}
</style>