<template>
	<!-- <u-navbar title="配送客户扫描" bgColor='#D3832A' leftIconColor='#ffffff' titleStyle='color:#ffffff' placeholder 
	:autoBack="true" leftIconSize='35'></u-navbar>-->

	<BasicContainer ref="basicContainer" :option="option">
		<template #head>
			<view class="hedtopt">
				<view class="typto">
					<view>配车类型：{{items.type_name}}</view>
					<view>配车状态：{{items.vehicleStatus_name}}</view>
				</view>
				<view class="typto">
					<view>车牌号：{{items.vehicleName}}</view>
					<view>司机：{{items.driverName}}</view>
				</view>
				<view class="typto">
					<view>装卸班组：{{items.loadingTeamName}}</view>
					<view>备货是否完成：{{items.isstock}}</view>
				</view>
				<view class="kehupeison">
					<view>
						<view>配送客户数</view>
						<view>{{items.customersNumber}}</view>
					</view>
					<view>
						<view>订单总数</view>
						<view>{{ordernumber}}</view>
					</view>
					<view>
						<view>计划件数</view>
						<view>{{deliveryNumber}}</view>
					</view>
					<view>
						<view>装车件数</view>
						<view>{{scannedNumber}}</view>
					</view>
				</view>
			</view>
		</template>
		<template #body>
			<scroll-view class="scoolv" scroll-y="true">
				<view class="minbx">
					<view class="its" v-for="item in datalist">
						<view class="titl">
							客户：{{item.consignee}}
						</view>
						<view class="contsx">
							<view class="tps">
								<view><text style="color: #90A0AF;">备货是否完成：</text>{{item.isstock}}</view>
								<view><text style="color: #90A0AF;">装车是否完成：</text>{{item.isload}}</view>
							</view>
							<view class="tps">
								<view><text style="color: #90A0AF;">备货区：</text>{{item.stockupArea||'无备货区'}}</view>

							</view>
							<view class="numbx">
								<view @click="goCustomerOrdersDetails(item)">
									<view>订单总数</view>
									<view class="golink">{{item.ordNub}}</view>
								</view>
								<view>
									<view>计划件数</view>
									<view>{{item.reservationNum}}</view>
								</view>
								<view>
									<view>装车件数</view>
									<view>{{item.loadingNub}}</view>
								</view>
								<view @click="goCustomerTrayDetails(item)">
									<view>托盘个数</view>
									<view class="golink">{{item.trayNum}}</view>
								</view>
							</view>
						</view>
						<view class="bts" v-if="authbuts.indexOf('一键装车')!=-1&&item.isload!='是'">
							<view @click="ckoneloading(item)">一键装车</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</template>
	</BasicContainer>

	<tiplist ref="tiplists"></tiplist>
	<!-- #ifdef APP -->
	<saomiao2 :ishidestop="scanState !== 0"></saomiao2>
	<!-- #endif -->
	<tips ref="tip"></tips>
</template>

<script lang="ts" setup>
	import {
		deliverycustom,
		deliveryloadingscan,
		deliveryoneloading,
		postAbnormalTruckLoading,
		deliveryloadinginventory
	} from '@/api/user.js'
	import {
		onLoad,
		onShow,
		onHide,
		onUnload,
		onPullDownRefresh
	} from '@dcloudio/uni-app'
	import { computed, reactive, ref, toRefs } from "vue";
	import useSystemSettingsStore from '@/store/useSystemSettingsStore';
	import { storeToRefs } from 'pinia';
	import utils from '@/utils/utils.js';
	const { scanState } = storeToRefs(useSystemSettingsStore())

	const option = reactive({
		title: '配送客户扫描',
		haveData: true,
		async pullDownRefreshInitPage() {
			return await initpage()
		}
	})

	let details = reactive({
		id: '',
		datalist: [],
		items: {} as any,
		scancode: '',
		authbuts: uni.getStorageSync('authbuts')
	})
	const tip = ref(null)
	const tiplists = ref(null)
	const basicContainer = ref(null)
	onLoad((op) => {
		details.id = op.id
		details.items = JSON.parse(op.item)
	})
	// onHide(() => {
	// 	uni.$off('scancodedate')
	// 	console.log("uni", uni)
	// })
	onShow(() => {
		const timer = setTimeout(() => {
			// 调用组件内刷新函数的方法
			basicContainer.value.startPullDownRefresh()

			// 消除定时器
			clearTimeout(timer)
		}, 200)

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

	})

	const ordernumber = computed(() => {
		let num = 0
		details.datalist.map(item => {
			num += Number(item.ordNub)
		})
		return num
	})
	const deliveryNumber = computed(() => {
		let num = 0
		details.datalist.map(item => {
			num += Number(item.reservationNum)
		})
		return num
	})
	const scannedNumber = computed(() => {
		let num = 0
		details.datalist.map(item => {
			num += Number(item.loadingNub)
		})
		return num
	})
	function showsdqs() {
		tiplists.value.setdetails({
			isshow: true,
			tipstate: 1,
			title: '请输入扫描的码',
			placeholder: '请输入扫描的码',
			inputtext: '',
			success: (detail) => {
				details.scancode = detail.inputtext
				scandata()
				tiplists.value.setdetails({ isshow: false })
			},
			cancel: () => {
				tiplists.value.setdetails({ isshow: false })
			},
			close: () => {
				tiplists.value.setdetails({ isshow: false })
			}
		})
	}
	/** 一键装车 */
	async function ckoneloading(item) {
		tip.value.setdetails({
			title: '提示',
			content: '确认一键装车？',
			confirmTxt: '确认',
			isshow: true,
			isshowcancel: true,
			success: async () => {
				let loadingId = uni.getStorageSync('checkvehicle').id
				let data = {
					reservationId: item.id,
					loadingId,
					deliveryId: details.id
				}
				let response = await deliveryoneloading(data)
				if (response.code == 200) {

				}
				tip.value.setisshow(false)
			},
			cancel: () => {
				tip.value.setisshow(false)
			},
			close: () => {
				tip.value.setisshow(false)
			}
		})

	}

	/* 
	deliveryId: details.id,
	loadingId,
	type: 1,
	barcode: details.scancode 
	 */

	/** 扫描订制品 */
	const scanOrder = async (loadingId) => {
		let data = {
			deliveryId: details.id,
			loadingId,
			type: 1,
			barcode: details.scancode
		}
		let res = await deliveryloadingscan(data)
		const { code, audio } = res
		// 异常装车
		if (code === 5000) {
			utils.ttsspke('不在本次计划, 请确认客户再异常装车')
		}
		// 窜货
		if (code === 3001) utils.ttsspke(audio)
	}

	/** 扫描库存品 */
	const scanStock = async (loadingId) => {
		let data = {
			reservationId: details.items.id,
			deliveryId: details.id,
			loadingId,
			type: 1,
			barcode: details.scancode
		}
		let res = await deliveryloadinginventory(data)
		const { audio } = res
		console.log('res   :>> ', res);
		if (!audio) return
		// #ifdef APP
		utils.ttsspke(res.audio)
		// #endif
	}

	// 扫描验证规则, 开头带HT的为库存品
	// const reg = /^HT./

	/** 扫描执行回调 */
	function scandata() {
		let loadingId = uni.getStorageSync('checkvehicle').id
		scanOrder(loadingId)
	}

	/**
	 * 请求页面数据
	 */
	async function initpage() {
		try {
			let data = {
				id: details.id
			}
			let response = await deliverycustom(data)
			if (response.code !== 200) return
			console.log('response', response)
			details.datalist = response.data.records
		} catch (e) {
			//TODO handle the exception
		} finally {
			return null
		}
	}
	function goCustomerOrdersDetails(item) {
		uni.navigateTo({
			url: '/pagesHome/pages/CustomerOrdersDetails/CustomerOrdersDetails?item=' + JSON.stringify(item) + '&id=' + details.id
		})
	}
	function goCustomerTrayDetails(item) {
		uni.navigateTo({
			url: '/pagesHome/pages/CustomerTrayDetails/CustomerTrayDetails?item=' + JSON.stringify(item) + '&id=' + details.id
		})
	}
	function goLoadingDetails(item) {
		uni.navigateTo({
			url: '/pagesHome/pages/LoadingDetails/LoadingDetails?id=' + item.id
		})
	}
	const { authbuts, items, datalist } = toRefs(details)
</script>

<style lang="scss">
	.scoolv {
		width: 100%;
		height: 60vh;
		margin-top: 20upx;

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

			>.its {
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 686upx;
				background: #FFFFFF;
				border-radius: 8upx;
				// padding:0upx 0;
				box-sizing: border-box;
				margin-bottom: 20upx;

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

				.titl {
					width: 100%;
					font-size: 32upx;
					font-weight: 400;
					color: #092C4D;
					border-bottom: 3upx solid #EEEEEE;
					padding: 26upx 24upx;
					box-sizing: border-box;
				}

				.bts {
					display: flex;
					align-items: center;
					justify-content: center;
					padding: 20upx 0;

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

				.contsx {
					width: 100%;
					display: flex;
					flex-direction: column;
					padding: 34upx 24upx;
					box-sizing: border-box;
					border-bottom: 3upx solid #EEEEEE;

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

						>view {
							flex: 1;
						}
					}

					>.numbx {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-around;
						margin-top: 44upx;

						>view {
							display: flex;
							flex-direction: column;
							align-items: center;
							font-size: 28upx;
							color: #092C4D;

							&:nth-of-type(1) {
								>view:nth-of-type(2) {
									color: #0086F1;
								}
							}

							&:nth-of-type(2) {
								>view:nth-of-type(2) {
									color: #3AD8BC;
								}
							}

							&:nth-of-type(3) {
								>view:nth-of-type(2) {
									color: #FA8C16;
								}
							}

							>view:nth-of-type(2) {
								font-size: 36upx;
								color: #0086F1;
								margin-top: 20upx;
							}
						}
					}
				}
			}
		}
	}

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

		.kehupeison {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-around;
			margin-top: 20upx;

			>view {
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 28upx;
				font-weight: 400;
				color: #092C4D;

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

				&:nth-of-type(2) {
					>view:nth-of-type(2) {
						color: #0086F1;
					}
				}

				&:nth-of-type(3) {
					>view:nth-of-type(2) {
						color: #3AD8BC;
					}
				}

				&:nth-of-type(4) {
					>view:nth-of-type(2) {
						color: #FA8C16;
					}
				}

				>view:nth-of-type(2) {
					font-size: 36upx;
					font-weight: 400;
					color: #092C4D;
					margin-top: 20upx;
				}
			}
		}
	}
</style>