<template>
	<gui-page :custom-header="true" :is-header-sized="false"
		:header-class="['gui-theme-background-color', isLandscapeScreen?'width: 100vmax;':'']"
		:style="[isLandscapeScreen?'width: 100vmax':'']">
		<template #gHeader>
			<view :style="[isLandscapeScreen?'height:44px;width: 100vmax':'height:44px;']"
				class="gui-flex gui-nowrap gui-rows gui-align-items-center">
				<!-- 使用组件实现返回按钮及返回首页按钮 -->
				<text style="font-size:44rpx;" class="gui-header-leader-btns gui-color-white font-icons"
					@tap="goBack">&#xe6c5;</text>
				<!-- 导航文本此处也可以是其他自定义内容 -->
				<text :style="[isPad?'font-size: 18px':'font-size: 14px']"
					class="gui-h4 gui-blod gui-flex1 gui-text-center gui-color-white gui-ellipsis gui-primary-text">分切</text>
				<!-- 此处加一个右侧展位元素与左侧同宽，实现标题居中 -->
				<!-- 实际宽度请根据自己情况设置 -->
				<view style="width:40px;" />
			</view>
		</template>
		<template #gBody>
			<view class="oqc-checkout">
				<view v-if="params.slitStatus !== 2" class="scan">
					<view class="scan-card">
						<uni-easyinput ref="batchNumberInput" v-model="params.scanBatchNumber" :input-border="false"
							type="text" focus placeholder="扫描母卷" @confirm="confirmBatchNumber"
							@focus="handleInputFocus('batchNumber')" @clear="handleClear('batchNumber')" />
						<text class="font-icons" @click="handleMapass">&#xe6b7;</text>
					</view>
				</view>
				<view v-if="params.slitStatus !== 2" class="scan">
					<view class="scan-card">
						<uni-easyinput ref="employeeInput" v-model="params.scanEmployeeName" :input-border="false"
							:clearable="false" type="text" placeholder="员工码" @confirm="confirmQrCodeEmployee"
							@focus="handleInputFocus('employee')" @clear="handleClear('employee')" />
						<text class="font-icons" @click="handleMapassQrCodeEmployee">&#xe6b7;</text>
					</view>
				</view>
				<view v-if="params.slitStatus !== 2" class="scan">
					<view class="scan-card">
						<uni-easyinput ref="deviceInput" v-model="params.scanDeviceName" :input-border="false"
							:clearable="false" type="text" placeholder="设备码" @confirm="confirmQrCodeDeviceId"
							@focus="handleInputFocus('device')" @clear="handleClear('device')" />
						<text class="font-icons" @click="handleMapassQrCodeDeviceId">&#xe6b7;</text>
					</view>
				</view>
				<view class="table-title">
					<span>分切来源</span>
				</view>
				<view class="custom-table">
					<uni-table border stripe empty-text="暂无更多数据">
						<!-- 表头行 -->
						<uni-tr class="custom-table-head">
							<uni-th align="center" width="50px">序号</uni-th>
							<uni-th align="center" width="170px">批次号</uni-th>
							<uni-th align="center" width="120px">物料编码</uni-th>
							<uni-th align="center" width="140px">物料名称</uni-th>
							<uni-th align="center" width="140px">规格</uni-th>
							<uni-th align="center" width="140px">余量</uni-th>
						</uni-tr>
						<!-- 表格数据行 -->
						<uni-tr v-for="(item, key) in slittingLots" :key="key"
							:style="[params.scanBatchNumber === item.materialLots? 'background-color: rgba(109, 205, 50, 1)':'background-color: white']">
							<uni-td align="center"
								:style="[params.scanBatchNumber === item.materialLots? 'color: white':'']">{{ ++key }}</uni-td>
							<uni-td align="center"
								:style="[params.scanBatchNumber === item.materialLots? 'color: white':'']">{{ item.materialLots }}</uni-td>
							<uni-td align="center"
								:style="[params.scanBatchNumber === item.materialLots? 'color: white':'']">{{ item.materialNo }}</uni-td>
							<uni-td align="center"
								:style="[params.scanBatchNumber === item.materialLots? 'color: white':'']">{{ item.materialName }}</uni-td>
							<uni-td align="center"
								:style="[params.scanBatchNumber === item.materialLots? 'color: white':'']">{{ item.size }}</uni-td>
							<uni-td align="center"
								:style="[params.scanBatchNumber === item.materialLots? 'color: white':'']">{{ item.remainQty }}</uni-td>
						</uni-tr>
					</uni-table>
				</view>
				<view class="table-title">
					<span>分切后规格</span>
				</view>
				<view class="custom-table">
					<uni-table border stripe empty-text="暂无更多数据">
						<!-- 表头行 -->
						<uni-tr class="custom-table-head">
							<uni-th align="center" width="80px"
								@click="handleCheckedAllRow">{{ isCheckedAllRow?"取消全选": "全选" }}</uni-th>
							<uni-th align="center" width="170px">来源批次</uni-th>
							<uni-th align="center" width="120px">宽</uni-th>
							<uni-th align="center" width="140px">长</uni-th>
							<uni-th align="center" width="140px">物料名称</uni-th>
							<uni-th align="center" width="140px">数量</uni-th>
							<uni-th align="center" width="140px">生产日期</uni-th>
						</uni-tr>
						<!-- 表格数据行 -->
						<uni-tr v-for="(item, key) in slittedLots" :key="key"
							:style="[computedLight(item)? 'background-color: rgba(109, 205, 50, 1)':'background-color: white']">
							<uni-td align="center">
								<checkbox :checked="item.checked" @click="handleCkeckedRow(item)" />
							</uni-td>
							<uni-td align="center"
								:style="[computedLight(item)? 'color: white':'']">{{ item.batchNumber }}</uni-td>
							<uni-td align="center"
								:style="[computedLight(item)? 'color: white':'']">{{ item.width }}</uni-td>
							<uni-td align="center"
								:style="[computedLight(item)? 'color: white':'']">{{ item.length }}</uni-td>
							<uni-td align="center"
								:style="[computedLight(item)? 'color: white':'']">{{ item.materialName }}</uni-td>
							<uni-td align="center"
								:style="[computedLight(item)? 'color: white':'']">{{ item.qty }}</uni-td>
							<uni-td align="center"
								:style="[computedLight(item)? 'color: white':'']">{{ $parseTime(item.produceDate) }}</uni-td>
						</uni-tr>
					</uni-table>
				</view>
			</view>
			<gui-right-menus>
				<!-- 扩展按钮 -->
				<template #menus-more>
					<view hover-class="gui-tap"
						class="menu-items gui-bg-blue gui-flex gui-columns gui-justify-content-center"
						@click="handleShowPrintDialog">
						<text class="menu-text gui-block gui-text-center gui-color-white">打印</text>
					</view>
					<view v-if="(params.slitStatus === 0 || params.slitStatus === 1) && params.scanBatchNumber"
						hover-class="gui-tap"
						class="menu-items gui-bg-green gui-flex gui-columns gui-justify-content-center"
						@click="handleCutting">
						<text class="menu-text gui-block gui-text-center gui-color-white">执行分切</text>
					</view>
				</template>
				<!-- 核心按钮 -->
				<template #menus-primary>
					<view class="menu-items gui-bg-primary gui-flex gui-columns gui-justify-content-center">
						<text class="menu-icon gui-color-white gui-block gui-text-center gui-icons">&#xe614;</text>
						<text class="menu-text gui-color-white gui-block gui-text-center">操作</text>
					</view>
				</template>
			</gui-right-menus><!-- 提示窗示例 -->
			<uni-popup ref="popup" type="dialog">
				<uni-popup-dialog type="success" cancel-text="取消" confirm-text="确认" title="提示"
					@confirm="handlePrintLabel" @close="popup.close()">
					<!-- eslint-disable-next-line -->
					<template #default="custom">
						<view class="form" style="width: 100%;margin: 0;">
							<div class="form-item">
								<div class="title">打印方式</div>
								<div class="content">
									<uni-data-select v-model="printType" :localdata="printTypeList" />
								</div>
							</div>
							<div class="form-item" v-if="printType === '0'">
								<div class="title">打印机</div>
								<div class="content">
									<uni-data-select v-model="printer" :localdata="printerList" />
								</div>
							</div>
						</view>
					</template>
				</uni-popup-dialog>
			</uni-popup>
			<uni-popup ref="errorTip" type="dialog">
				<uni-popup-dialog type="error" cancel-text="关闭" confirm-text="确认" title="提示" :content="errorTipMessage"
					@confirm="handleCloseErrorTipsModal" @close="handleCloseErrorTipsModal" />
			</uni-popup>
		</template>
	</gui-page>
</template>
<script>
	import {
		computed,
		defineComponent,
		ref,
		onMounted,
		onBeforeMount
	} from 'vue'
	import tsc from '@/unit/CHITEN_SDK_APP/tsc.js'
	export default defineComponent({
		// onLoad() {
		// 	// // #ifdef APP-PLUS
		// 	// plus.screen.lockOrientation('default');
		// 	// // #endif
		// },
		// onUnload() {
		// 	// // #ifdef APP-PLUS
		// 	// plus.screen.lockOrientation('portrait-primary');
		// 	// // #endif
		// },
		// onResize() {
		// 	if ([90, -90].includes(plus.navigator.getOrientation())) {
		// 		this.isLandscapeScreen = true
		// 	} else {
		// 		this.isLandscapeScreen = false
		// 	}
		// },
		setup(options) {
			const popup = ref()
			const printType = ref('0')
			const printer = ref('')
			const printerList = ref([]);
			const printData = ref([]);
			const isLandscapeScreen = ref(false)
			const parentRow = options?.params ?? {}
			const bluetooth = ref()
			const blueConfig = ref({
				loopBuffer: 0,
				currentTime: 1,
				lastData: 0,
				onBufferSize: 20,
				printerNum: 1,
				currentPrint: 1,
				isLabelSend: false
			})
			// 绑定的检查人信息
			const params = ref({
				eBillNo: '',
				slitStatus: 0,
				materialNo: '',
				scanBatchNumber: '',
				scanEmployee: '',
				scanEmployeeName: '',
				scanDeviceId: '',
				scanDeviceName: '',
				scanChildrenList: []
			})
			const slittingLots = ref([])
			const slittedLots = ref([])
			// 是否全选
			const isCheckedAllRow = ref(false)
			const batchNumberInput = ref()
			const employeeInput = ref()
			const deviceInput = ref()
			const emptyDocument = ref()
			const errorState = ref(0)
			const errorTip = ref('')
			const errorTipMessage = ref('')

			// 是否存在分切来源标签码
			const computedLight = computed(() => {
				return item => {
					if (params.value.scanChildrenList.includes(item.batchNumber)) {
						return true
					}
					return false
				}
			})

			// 多选框点击事件
			const handleCkeckedRow = function(ret) {
				const fdIndex = slittedLots.value.findIndex(item => item?.id === ret?.id)
				slittedLots.value[fdIndex].checked = !slittedLots.value[fdIndex].checked
			}

			onBeforeMount(() => {
				// #ifdef APP-PLUS
				const bluetoothConfig = uni.getStorageSync('bluetoothConfig')
				if ([null, '', undefined].includes(bluetoothConfig)) {
					bluetooth.value = {
						'isOpenBle': false,
						'deviceId': '',
						'serviceId': '',
						'writeId': '',
						'notifyId': ''
					}
					setTimeout(() => {
						// #ifdef APP-PLUS
						plus.device.beep(2)
						// #endif
						errorTipMessage.value = '蓝牙打印机未连接'
						errorState.value = -1
						errorTip.value.open()
					}, 800)
				} else {
					bluetooth.value = JSON.parse(bluetoothConfig)
				}
				// #endif

				if (Object.keys(JSON.parse(parentRow)).length > 0) {
					params.value.slitStatus = JSON.parse(parentRow)?.slitStatus
					params.value.eBillNo = JSON.parse(parentRow)?.ebillNo
					params.value.materialNo = JSON.parse(parentRow)?.materialNo

					uni.$reqGet('getSlittingLots', {
							eBillNo: params.value.eBillNo
						})
						.then(({
							data
						}) => {
							slittingLots.value = data
						})

					uni.$reqGet('getSlittedLots', {
							eBillNo: params.value.eBillNo
						})
						.then(({
							data
						}) => {
							data.map(ret => {
								ret.checked = false
							})
							slittedLots.value = data
						})
				} else {
					uni.showToast({
						title: '没有物料编码',
						icon: 'none',
						duration: 1000
					})
					setTimeout(() => {
						uni.$goBack('/pages/workbranch/production/cutting/cuttingList')
					}, 1500)
				}
			})

			onMounted(() => {
				const paramsToJSON = JSON.parse(parentRow)
				if (Object.keys(paramsToJSON).length > 0) {
					if (Object.prototype.toString.call(paramsToJSON) === '[object Object]') {
						if (![NaN, null, undefined, ''].includes(paramsToJSON?.qrCode)) {
							uni.$reqPost('scanSlitting', {
									ebillNo: paramsToJSON.ebillNo,
									qrCode: paramsToJSON.qrCode
								})
								.then(({
									code,
									data,
									msg
								}) => {
									if (code === 0) {
										params.value.scanBatchNumber = data?.batchNumber
										params.value.scanChildrenList = data?.list
										employeeInput.value.onBlur()
										employeeInput.value.onFocus()
									} else {
										// #ifdef APP-PLUS
										plus.device.beep(2)
										// #endif
										errorTipMessage.value = msg
										errorTip.value.open()
										errorState.value = 0
									}
								})
						} else {
							setTimeout(() => {
								if (params.value.slitStatus !== 2) {
									handleMapass()
								}
							}, 500)
						}
					}
				}

				// GetPrinters();
			})

			const GetPrinters = function() {
				var cfgData = new Object;
				cfgData.XAction = "Printers";
				cfgData.XOpenId = "10000861";
				cfgData.XTokens = "+fdBeHkvLvLW2tIeEVMWr2DikZtd9jOb";
				cfgData.XUrl = "http://192.168.1.58:51320";
				// uniapp 1 其它 0
				cfgData.FaceType = "1";
				uni.CCPrintingAPI.Excute(cfgData,
					function(XTimeout) {},
					function(XFail) {
						// #ifdef APP-PLUS
						plus.device.beep(2)
						// #endif
						errorTipMessage.value = "API可能未安装或未启动"
						errorTip.value.open()
						errorState.value = -1
					},
					function(XSuccess) {
						var apiResult = JSON.parse(XSuccess.trim());
						if (apiResult.Status == "1") {
							/*获取到打印机列表*/
							if (apiResult.Message.length > 1) {
								for (var i = 1; i < apiResult.Message.length; i++) {
									printerList.value.push({
										text: apiResult.Message[i],
										value: apiResult.Message[i]
									})
								}
								/*设置上次使用的打印机*/
								printer.value = 'selected'
							}
						}
					}
				);
			}

			const goBack = function() {
				uni.reLaunch({
					url: '/pages/workbranch/production/cutting/cuttingList'
				})
			}

			const handleMapass = function() {
				// #ifdef APP-PLUS
				const mpaasScanModule = uni.requireNativePlugin('Mpaas-Scan-Module')
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_code === 1000) {
							uni.$reqPost('scanSlitting', {
									ebillNo: params.value.eBillNo,
									qrCode: ret.resp_result
								})
								.then(({
									code,
									data,
									msg
								}) => {
									if (code === 0) {
										params.value.scanBatchNumber = data?.batchNumber
										params.value.scanChildrenList = data?.list
										employeeInput.value.onBlur()
										employeeInput.value.onFocus()
									} else {
										// #ifdef APP-PLUS
										plus.device.beep(2)
										// #endif
										errorTipMessage.value = msg
										errorTip.value.open()
										errorState.value = 0
									}
								})
						}
					})
				// #endif
			}

			const confirmBatchNumber = function(e) {
				uni.$reqPost('scanSlitting', {
						ebillNo: params.value.eBillNo,
						qrCode: e
					})
					.then(({
						code,
						data,
						msg
					}) => {
						if (code === 0) {
							params.value.scanBatchNumber = data?.batchNumber
							params.value.scanChildrenList = data?.list
							employeeInput.value.onBlur()
							employeeInput.value.onFocus()
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = msg
							errorTip.value.open()
							errorState.value = 0
						}
					})
			}

			const handleMapassQrCodeEmployee = function() {
				// #ifdef APP-PLUS
				const mpaasScanModule = uni.requireNativePlugin('Mpaas-Scan-Module')
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_code === 1000) {
							uni.$reqGet("getEmployeeDetail", {
									id: ret.resp_result
								})
								.then(({
									data
								}) => {
									params.value.scanEmployee = ret.resp_result
									params.value.scanEmployeeName = data?.employeeName;
									deviceInput.value.onBlur()
									deviceInput.value.onFocus()
								})
						}
					})
				// #endif
			}

			const handleMapassQrCodeDeviceId = function() {
				// #ifdef APP-PLUS
				const mpaasScanModule = uni.requireNativePlugin('Mpaas-Scan-Module')
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_code === 1000) {
							params.value.scanDeviceId = ret.resp_result
						}
					})
				// #endif
			}

			const setInputFocus = function() {
				params.value.scanBatchNumber = ''
				batchNumberInput.value.onBlur()
				batchNumberInput.value.onFocus()
			}

			// 关闭错误信息弹窗
			const handleCloseErrorTipsModal = async function() {
				errorTip.value.close()
				if (errorState.value === 0) {
					await setInputFocus()
				}
			}

			const handleInputFocus = function(text) {
				// #ifdef APP-PLUS
				setTimeout(() => {
					uni.hideKeyboard()
				}, 100)
				// #endif
				emptyDocument.value ? emptyDocument.value.$el.className = 'uni-easyinput' : false
				if (text === 'batchNumber') {
					batchNumberInput.value.$el.className = 'uni-easyinput light-focus'
					emptyDocument.value = batchNumberInput.value
				}
				if (text === 'employee') {
					employeeInput.value.$el.className = 'uni-easyinput light-focus'
					emptyDocument.value = employeeInput.value
				}
				if (text === 'device') {
					deviceInput.value.$el.className = 'uni-easyinput light-focus'
					emptyDocument.value = deviceInput.value
				}
			}

			const handleClear = function(text) {
				if (text === 'batchNumber') {
					batchNumberInput.value.onBlur()
					batchNumberInput.value.onFocus()
				}
				if (text === 'employee') {
					employeeInput.value.onBlur()
					employeeInput.value.onFocus()
				}
				if (text === 'device') {
					deviceInput.value.onBlur()
					deviceInput.value.onFocus()
				}
			}

			const confirmQrCodeEmployee = function(e) {
				uni.$reqGet("getEmployeeDetail", {
						id: e
					})
					.then(({
						code, data, msg
					}) => {
						if (code === 0) {
							params.value.scanEmployee = e
							params.value.scanEmployeeName = data?.employeeName;
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = "用户信息不存在请重新扫码"
							errorTip.value.open()
							errorState.value = -1
						}
						deviceInput.value.onBlur()
						deviceInput.value.onFocus()
					})
			}

			const confirmQrCodeDeviceId = function(e) {
				uni.$reqGet("getEquipmentBaseDetail", {
						id: e
					})
					.then(({
						code, data, msg
					}) => {
						if (code === 0) {
							params.value.scanDeviceId = e
							params.value.scanDeviceName = data?.equipmentName;
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = "设备信息不存在请重新扫码"
							errorTip.value.open()
							errorState.value = -1
						}
						deviceInput.value.onBlur()
						deviceInput.value.onFocus()
						deviceInput.value.$el.className = 'uni-easyinput'
					})
			}

			const Send = function(buff) {
				var currentTime = blueConfig.value.currentTime
				var loopBuffer = blueConfig.value.loopBuffer
				var lastData = blueConfig.value.lastData
				var onBufferSize = blueConfig.value.onBufferSize
				var printNum = blueConfig.value.printerNum
				var currentPrint = blueConfig.value.currentPrint
				var buf
				var dataView
				if (currentTime < loopBuffer) {
					buf = new ArrayBuffer(onBufferSize)
					dataView = new DataView(buf)
					for (let i = 0; i < onBufferSize; ++i) {
						dataView.setUint8(i, buff[(currentTime - 1) * onBufferSize + i])
					}
				} else {
					buf = new ArrayBuffer(lastData)
					dataView = new DataView(buf)
					for (let i = 0; i < lastData; ++i) {
						dataView.setUint8(i, buff[(currentTime - 1) * onBufferSize + i])
					}
				}
				plus.bluetooth.writeBLECharacteristicValue({
					deviceId: bluetooth.value.deviceId,
					serviceId: bluetooth.value.serviceId,
					characteristicId: bluetooth.value.writeId,
					value: buf,
					success: function(res) {
						if (currentPrint <= printNum) {
							wx.showToast({
								title: '数据打印中',
								icon: 'loading'
							})
						}
						// 将complete中处理数据的代码挪过来，增加30ms延迟, 解决蓝牙打印数据传输10007问题
						setTimeout(() => {
							currentTime += 1
							if (currentTime <= loopBuffer) {
								blueConfig.value.isLabelSend = true
								blueConfig.value.currentTime = currentTime
								Send(buff)
							}
							// end
							if (currentTime === loopBuffer) {
								blueConfig.value.currentPrint += 1
								blueConfig.value.isLabelSend = false
							}
						}, 10)
					},
					fail: function(e) {
						wx.showToast({
							title: '打印第' + currentPrint + '张失败' + 'error: code ' + e
								?.code + ', message ' + e?.message,
							icon: 'none',
							duration: 4000
						})
					},
					complete: function() {}
				})
			}

			const prepareSend = function(buff) {
				var time = blueConfig.value.onBufferSize
				var loopBuffer = parseInt(buff.length / time)
				var lastData = parseInt(buff.length % time)
				blueConfig.value.loopBuffer = loopBuffer + 1
				blueConfig.value.lastData = lastData
				blueConfig.value.currentTime = 1
				Send(buff)
			}

			// 打印测试
			const labelTest = async function(ret) {
				var command = tsc.dlabelPrinter.createNew()
				command.setBackFeed(6)
				// (640 * 320)dots
				command.setSize(80, 40)
				command.setCls()
				command.setGap(8)
				command.setBox(0, 0, 635, 315, 1)
				command.setBar(0, 40, 640, 2)
				command.setBar(0, 80, 640, 2)
				command.setBar(0, 120, 640, 2)
				command.setBar(0, 160, 640, 2)
				command.setBar(0, 200, 366, 2)
				command.setBar(0, 240, 366, 2)
				command.setBar(0, 280, 640, 2)
				// 1
				command.setBar(115, 0, 2, 40)
				command.setBar(115, 40, 2, 40)
				command.setBar(115, 80, 2, 40)
				command.setBar(115, 120, 2, 40)
				command.setBar(115, 160, 2, 40)
				command.setBar(115, 200, 2, 40)
				command.setBar(115, 240, 2, 40)
				command.setBar(115, 280, 2, 40)
				// 2
				command.setBar(360, 40, 2, 40)
				command.setBar(360, 80, 2, 40)
				command.setBar(360, 120, 2, 40)
				command.setBar(360, 160, 2, 40)
				command.setBar(360, 200, 2, 40)
				command.setBar(360, 240, 2, 40)
				command.setBar(360, 280, 2, 40)
				// 3
				command.setBar(475, 40, 2, 40)
				command.setBar(475, 80, 2, 40)
				command.setBar(475, 120, 2, 40)
				command.setBar(475, 280, 2, 40)

				command.setText(10, 6, 'TSS24.BF2', 0, 1, 1, '物料名称')
				command.setText(126, 6, 'TSS24.BF2', 0, 1, 1, ret.materialName)
				// row2
				command.setText(10, 46, 'TSS24.BF2', 0, 1, 1, '物料编码')
				command.setText(126, 46, 'TSS24.BF2', 0, 1, 1, ret.materialNo)
				command.setText(366, 46, 'TSS24.BF2', 0, 1, 1, '供应商')
				command.setText(481, 46, 'TSS24.BF2', 0, 1, 1, ret.supplierName)
				// row3
				command.setText(10, 86, 'TSS24.BF2', 0, 1, 1, '物料规格')
				command.setText(126, 86, 'TSS24.BF2', 0, 1, 1, ret.size)
				command.setText(366, 86, 'TSS24.BF2', 0, 1, 1, '分切日期')
				command.setText(481, 86, 'TSS24.BF2', 0, 1, 1, ret.createTime)
				// row4
				command.setText(10, 126, 'TSS24.BF2', 0, 1, 1, 'Lot no.')
				command.setText(126, 126, '1', 0, 1, 1, ret.materialLots)
				command.setText(366, 126, 'TSS24.BF2', 0, 1, 1, '储位')
				command.setText(481, 126, 'TSS24.BF2', 0, 1, 1, ret.wmsStockLocationName)
				// row5
				command.setText(10, 166, 'TSS24.BF2', 0, 1, 1, '有效期')
				command.setText(126, 166, 'TSS24.BF2', 0, 1, 1, ret.validityDate)
				// row6
				command.setText(10, 206, 'TSS24.BF2', 0, 1, 1, '检测日期')
				command.setText(126, 206, 'TSS24.BF2', 0, 1, 1, ret.inspectTime)
				// row7
				command.setText(10, 246, 'TSS24.BF2', 0, 1, 1, '存储条件')
				const strList = ret.storageEnvironment?.split(' ')
				const strText = strList[0] + ' ' + strList[1]
				command.setText(126, 246, 'TSS24.BF2', 0, 1, 1, strText)
				// row8
				command.setText(10, 286, 'TSS24.BF2', 0, 1, 1, '分切单号')
				command.setText(126, 286, 'TSS24.BF2', 0, 1, 1, ret.slittingOrderNo)
				command.setText(366, 286, 'TSS24.BF2', 0, 1, 1, '生产日期')
				command.setText(481, 286, 'TSS24.BF2', 0, 1, 1, ret.produceDate)

				command.setText(480, 216, '2', 0, 1, 1, ret.qty)
				command.setQrcode(366, 166, 'L', 4, 'A', ret.batchNumber)
				command.setPagePrint()
				blueConfig.value.isLabelSend = true
				const forMatData = await command.getData()
				await prepareSend(forMatData)
			}

			const handleCutting = async function() {
				if ([undefined, null, ''].includes(params.value.scanEmployee)) {
					uni.showToast({
						title: '请绑定员工',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if ([undefined, null, ''].includes(params.value.scanDeviceId)) {
					uni.showToast({
						title: '请绑定设备',
						icon: 'none',
						duration: 1000
					})
					return
				}

				uni.$reqPost('executeCutting', {
						slittingOrderNo: params.value.eBillNo,
						employee: params.value.scanEmployee,
						deviceId: params.value.scanDeviceId
					})
					.then(({
						code,
						data,
						msg
					}) => {
						if (code === 0) {
							uni.showToast({
								title: '分切成功',
								icon: 'none',
								duration: 2000
							})
						} else {
							uni.showToast({
								title: msg,
								icon: 'none',
								duration: 2000
							})
						}
					})
				// uni.$reqPost("executeCutting", {
				// 		slittingOrderNo: params.value.eBillNo,
				// 		employee: params.value.scanEmployee,
				// 		deviceId: params.value.scanDeviceId,
				// 	})
				// 	.then(({
				// 		code,
				// 		data,
				// 		msg
				// 	}) => {
				// 		if (code === 0) {
				// 			// #ifdef APP-PLUS
				// 			const Activity = plus.android.runtimeMainActivity();
				// 			const Intent = plus.android.importClass("android.content.Intent");
				// 			const Uri = plus.android.importClass("android.net.Uri");
				// 			const browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
				// 				`${uni.$baseUrl}/jmreport/view/821903765761437696?id=${params.value.eBillNo}`
				// 			))
				// 			Activity.startActivity(browserIntent);
				// 			// #endif

				// 			// #ifdef H5
				// 			const dom = document.createElement('a');
				// 			dom.setAttribute("href",
				// 				`${uni.$baseUrl}/jmreport/view/821903765761437696?id=${params.value.eBillNo}`
				// 			);
				// 			dom.setAttribute("target", "_blank");
				// 			dom.click();
				// 			// #endif
				// 			uni.navigateTo({
				// 				url: "/pages/workbranch/production/cutting/cuttingList"
				// 			})
				// 		} else {
				// 			uni.showToast({
				// 				title: msg,
				// 				icon: "none",
				// 				duration: 1000,
				// 			})
				// 		}
				// 	})
			}

			// 获取打印数据
			const handleGetPrintData = function(id) {
				uni.request({
					url: uni.$baseUrl + '/report/print/slittingPrintData',
					data: {
						id
					}
				}).then(async ({
					data
				}) => {
					printData.value = [];
					if (data.data?.length > 0) {
						printData.value = data.data
					} else {
						// #ifdef APP-PLUS
						plus.device.beep(2)
						// #endif
						errorTipMessage.value = '无打印数据'
						errorTip.value.open()
						errorState.value = -1
					}
				})
			}

			// 蓝牙打印
			const bluetoothPrint = function() {
				if (printData.value.length > 0) {
					blueConfig.value.currentPrint = 1
					blueConfig.value.printNum = printData.value.length

					const timer = setInterval(async () => {
						const currentPrint = blueConfig.value.currentPrint -
							1
						if (blueConfig.value.isLabelSend === false &&
							currentPrint <
							blueConfig.value.printNum) {
							const paramsObj = {
								materialName: printData.value[currentPrint]
									.materialName ?? '',
								materialNo: printData.value[currentPrint]
									.materialNo ??
									'',
								supplierName: printData.value[currentPrint]
									.supplierName ??
									'',
								size: printData.value[currentPrint]
									.size ??
									'',
								createTime: printData.value[currentPrint]
									.createTime ??
									'',
								materialLots: printData.value[currentPrint]
									.materialLots ??
									'',
								wmsStockLocationName: printData.value[
										currentPrint]
									.wmsStockLocationName ??
									'',
								validityDate: printData.value[currentPrint]
									.validityDate ??
									'',
								inspectTime: printData.value[currentPrint]
									.inspectTime ??
									'',
								storageEnvironment: printData.value[currentPrint]
									.storageEnvironment ??
									'',
								slittingOrderNo: printData.value[currentPrint]
									.slittingOrderNo ??
									'',
								produceDate: printData.value[currentPrint]
									.produceDate ??
									'',
								batchNumber: printData.value[currentPrint]
									.batchNumber ??
									'',
								qty: printData.value[currentPrint]
									.qty ??
									''
							}
							await labelTest(paramsObj)
						}
						if (currentPrint === blueConfig.value.printNum) {
							await clearInterval(timer)
						}
					}, 3000)
				}
			}

			// 智高打印
			const hicorePrint = function() {
				var cfgData = new Object;
				cfgData.XAction = "Print"
				cfgData.XOpenId = "10000004";
				cfgData.XTokens = "+fdBeHkvLvJgWBKF/6iODpmABblarwVz";
				cfgData.XUrl = "http://192.168.1.58:51320";
				// uniapp 1 其它 0
				cfgData.FaceType = "1";
				/*指定打印机*/
				cfgData.PrinterName = printer.value;
				/*模板方式，0-客户端模板，1-远程模板，
							2-如果本地模板存在，使用本地，不存在使用远程模板，3-不使用模板*/
				cfgData.TemplateMode = "2";
				/*远程模板URL或本地模板的文件夹*/
				// cfgData.TemplateURL = "http://img.51321.cn/web/chicore/VPrinting/API";
				cfgData.TemplateURL = "http://113.105.183.190:9163/bucket163";
				// cfgData.TemplateURL =  "D:\\\\";
				/*模板文件名*/
				// cfgData.TemplateName = "183601-70X40.Lblx";
				cfgData.TemplateName = "2024-01-26-10-04-38-1706234678133_Label3.Lblx";
				// cfgData.TemplateName = "Label2.Lblx";
				/*打印数据的类型，0-数组，1-Json数组，元素为JSON对象，字段：值*/
				cfgData.PrintDataType = 1;
				/*传递打印数据 Json数组*/
				/*内容不允许 半角的逗号及半角的分隔符*/
				var arr = printData.value;
				cfgData.PrintData = JSON.stringify(arr);
				/*打印 发送给打印机进行打印*/
				uni.CCPrintingAPI.Excute(cfgData,
					function(XTimeout) {},
					function(XFail) {
						// #ifdef APP-PLUS
						plus.device.beep(2)
						// #endif
						errorTipMessage.value = "API可能未安装或未启动"
						errorTip.value.open()
						errorState.value = -1
					},
					function(XSuccess) {
						var apiResult = JSON.parse(XSuccess.trim());
						if (apiResult.Status == "1") {
							uni.showToast({
								title: '标签打印成功',
								icon: "none",
								duration: 1500
							})
						}
					}
				);
			}

			const handleShowPrintDialog = function() {
				const ids = []
				slittedLots.value.forEach(ret => {
					if (ret.checked) {
						ids.push(ret?.id)
					}
				})
				if (ids.length === 0) {
					// #ifdef APP-PLUS
					plus.device.beep(2)
					// #endif
					errorTipMessage.value = '请选中至少一行打印数据'
					errorTip.value.open()
					errorState.value = -1
				} else {
					popup.value.open();

					handleGetPrintData(ids.toString());
				}
			}

			const handlePrintLabel = function() {
				if (printType.value === '0') {
					hicorePrint();
				}
				if (printType.value === '1') {
					bluetoothPrint();
				}
			}

			// 全选
			const handleCheckedAllRow = function() {
				slittedLots.value.map(ret => {
					ret.checked = !isCheckedAllRow.value
				})
				isCheckedAllRow.value = !isCheckedAllRow.value
			}

			return {
				printTypeList: [{
					text: '智高标签打印',
					value: '0',
				}, {
					text: '蓝牙打印',
					value: '1',
				}, ],
				popup,
				goBack,
				printType,
				printer,
				printerList,
				slittedLots,
				slittingLots,
				isLandscapeScreen,
				batchNumberInput,
				employeeInput,
				handleInputFocus,
				deviceInput,
				errorState,
				errorTip,
				errorTipMessage,
				params,
				computedLight,
				handleClear,
				handleMapass,
				handleCutting,
				isCheckedAllRow,
				handlePrintLabel,
				confirmQrCodeEmployee,
				confirmQrCodeDeviceId,
				confirmBatchNumber,
				handleCkeckedRow,
				handleShowPrintDialog,
				handleCheckedAllRow,
				handleMapassQrCodeEmployee,
				handleMapassQrCodeDeviceId,
				handleCloseErrorTipsModal
			}
		}
	})
</script>

<style lang="scss" scoped>
	.gui-header-leader-btns {
		color: black;
		font-size: 24px !important;
		margin-left: 24rpx;
	}

	.gui-sbody {
		background-color: rgba(234, 239, 242, 1);
	}

	.gui-relative {
		overflow: visible;
		height: 100vh;

		.oqc-checkout {
			width: 100vw;
			height: calc(100vh - 75px);
			position: absolute;
			top: 95px;
			overflow-y: scroll;
		}
	}

	.popup-content {
		width: 100vw;
		height: 55vh;
		overflow-y: scroll;
	}

	.card-list-flexbox-popup {
		width: 100vw;
		display: flex;
		flex-direction: row;
		align-items: center;
		flex-wrap: wrap;

		.card-list-item,
		.card-list-item-input {
			width: 100vw;
			height: 100%;
			min-height: 40px;
			display: flex;
			flex-direction: row;
			align-items: center;
			background-color: #fff;

			uni-text {
				font-size: 14px;
				height: 50rpx;
				text-align: left;
				padding: 0 12px;
				display: flex;
				flex-direction: row;
				align-items: center;
			}

			uni-image {
				width: calc(100vw / 2 - 4px);
				height: calc(100vw / 3.2 - 2px);
				pointer-events: none;
			}

			uni-image::after {
				content: "×";
				width: 22px;
				height: 22px;
				position: absolute;
				top: 5px;
				right: 5px;
				display: flex;
				justify-content: center;
				align-items: center;
				color: rgba(255, 255, 255, 0.9);
				background-color: rgba(0, 0, 0, 0.5);
				border-radius: 50%;
				font-size: 24px;
				/*给伪元素开启鼠标事件，将事件冒泡到父元素的点击事件中*/
				pointer-events: auto;
			}

			.text-1 {
				flex: 1;
				height: 40px;
				justify-content: flex-start;
			}

			.text-2 {
				flex: 3;
				height: 40px;
				justify-content: flex-end;
				margin-right: 4px;
				padding: 2px 6px;
			}

			.text-3 {
				width: calc(100% - 15%);
				min-height: 35px;
				justify-content: flex-end;
				padding: 2px 6px;
			}

			.text-3::before {}

			.btn-mg {
				margin: 0 5px;
			}
		}

		.card-list-item {
			justify-content: center;
		}

		.card-list-item-input {
			margin: 10px 0 10px 2%;
			display: flex;
			flex-direction: column;

			uni-view {
				width: 100%;
				height: 100%;
				display: flex;

				uni-textarea {
					flex: 8;
				}

				uni-view {
					flex: 1;
				}

				.operation-icon,
				.operation-icon-2 {
					height: auto;

					uni-text {
						width: 22px;
						height: 100%;
						padding: 0 !important;
						text-align: center;
						display: flex;
						justify-content: center;
						align-items: center;
						border-top-right-radius: 4px;
						border-bottom-right-radius: 4px;
						color: white;
						background-color: #80b7ff;
					}
				}

				.operation-icon-2 {
					uni-text {
						background-color: #ff5962;
					}
				}

			}
		}

		.grid-image {
			/* 设置容器布局为grid布局 */
			display: grid;
			/* 指定每一行的宽度 每个宽度中间用空格隔开 */
			grid-template-rows: repeat(auto-fill, 1fr);
			/* 指定每一列的宽度 每个宽度中间用空格隔开 */
			grid-template-columns: repeat(2, 1fr);
			padding: 0 4px;
		}

		.card-list-item:nth-of-type(1) {

			.text-1 {
				flex: 9;
				font-weight: bold;
				color: black !important;
			}

			.text-1::before {
				content: "";
				width: 4px;
				height: 20px;
				border-radius: 4px;
				margin-right: 4px;
				background-color: skyblue;
			}

			.text-2 {
				flex: 4;
				display: flex;
				flex-direction: row;
				justify-content: flex-end;
				align-items: center;
				color: limegreen !important;
			}
		}

	}

	.table-title {
		height: 40px;
		line-height: 40px;
		margin: 0 0 -3px 0;
		padding: 0 12px;
		font-size: 16px;
		font-weight: bold;
		background-color: white;
	}

	.uni-list-cell {
		width: 100vw;
		height: 35px;
		margin: 4px 0;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		border-bottom: 2px solid #eaeff2;
		background-color: #fff;

		view {
			font-size: 14px;
			height: 50rpx;
			text-align: left;
			padding: 0 8px;
			display: flex;
			flex-direction: row;
			align-items: center;
		}

		.uni-list-cell-left {
			flex: 1;
			height: 35px;
			justify-content: flex-start;
		}

		.uni-list-cell-db {
			flex: 3;
			height: 35px;
			justify-content: flex-end;
			margin-right: 4px;
			padding: 2px 6px;

			uni-picker {
				width: 100%;
				display: flex;
				flex-direction: row;
				justify-content: center;
			}
		}
	}

	.modal-btns {
		height: 100rpx;
		line-height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.line {
		margin-top: 10rpx;
		height: 80rpx;
		width: 1rpx;
		background-color: #dcdcdc;
	}

	.font-icons {
		width: 40px;
		font-size: 20px;
	}

	.scan {
		height: 45px;
		width: calc(100% - 48px);
		margin: 0 12px 12px 12px;
		padding: 0 12px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-radius: 6px;
		background-color: white;

		.scan-card {
			width: 100%;
			display: grid;
			grid-template-rows: 1fr;
			grid-template-columns: 7fr 2fr;
			align-items: center;

			input {
				height: 35px;
				line-height: 35px;
			}

			text {
				width: 100%;
				text-align: right;
			}
		}
	}

	.light-focus {
		border: 1px solid #008aff;
		border-radius: 6px !important;

		::v-deep .uni-easyinput__content {
			border-radius: 6px !important;
		}
	}

	.light-focus::before {
		position: absolute;
		left: -10px;
		top: -10px;
		content: "✏️";
		width: 20px;
		height: 20px;
		color: red;
		font-weight: bold;
		font-size: 18px;
	}

	.form {
		font-size: 14px;
		margin: 0 12px 12px 12px;
		padding: 4px 8px;
		border-radius: 4px;
		background-color: white;

		.form-title {
			height: 32px;
			line-height: 32px;
			text-align: center;
			font-size: 16px;
			font-weight: bold;
			border-bottom: 2px dashed #edeeee;
		}

		.form-item {
			.title {
				height: 32px;
				line-height: 32px;
				text-align: left;
				font-weight: bold;
				color: #333;
			}

			.content {
				height: 42px;
				line-height: 42px;
				margin: 0 0 6px 0;
				position: relative;

				.button-add {
					position: absolute;
					width: 37px;
					height: 37px;
					right: 0;
					top: 0;
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 4px;
					color: white;
					cursor: pointer;
					background-color: #00a0e9;
				}
			}
		}
	}
</style>