<!-- 订单详情-确认收货 -->
<template>
	<view class="card-box">
		<view class="as-layout-horizontal as-gravity-center-start title">
			<image :src="`${$imgUrl}order/icon-star-green.png`" class="icon"></image>
			<text class="txt-title">{{ state.orderInfo.productName }}</text>
		</view>
		<view style="border-bottom: 1px solid #dcdcdc" />

		<!-- 订单信息 -->
		<view class="order-box">
			<view class="order-item" v-for="(item, index) in state.list" :key="index">
				<order-info-item :label="item.label" :value="item.value"></order-info-item>
			</view>
		</view>
	</view>

	<view class="hint">请核对实收货物与上述货物编号是否一致！</view>
	<view class="btn">
		<submit-button title="确认收货" @submit="$util.confirm('是否确认收到货？', confirmReceiptClick, '收货确认')"></submit-button>
	</view>
</template>

<script setup lang="ts">
	import orderInfoItem from "./components/order-info-item";
	import { reactive } from "vue";
	import { onLoad, onUnload, onShow } from "@dcloudio/uni-app";
	import { request } from "@/utils/network/request.js";
	import { msg, confirm, getOrderStatusName, navTo } from "@/utils/utils";
	import { orderDetail, receiveOrder } from "@/utils/network/api";
	import { getItem, StorageKeys } from "@/utils/storage";
	import { stringToJson } from "@/utils/network/encryption";
	const bluetoothUtil = require("../../static/etcUtil/index.js");
	const cmd = require("../../static/etcUtil/cmdConfig.js");
	const tools = require("../../static/etcUtil/tools.js");
	const state = reactive({
		//订单信息
		orderInfo: {} as any,
		//显示订单信息
		list: [
			{
				label: "订单编号：",
				value: "",
			},
			{
				label: "订单车牌：",
				value: "",
			},
			{
				label: "订单状态：",
				value: "",
			},
			{
				label: "发货卡号：",
				value: "",
			},
			{
				label: "发货签号：",
				value: "",
			},
		],
		readCardId: "",//蓝牙读取的
		readObuId: "",//蓝牙读取的
	});
	onShow((option) => {
		uni.$on('bluetoothLink', res => {
			console.log(res);
			if (res.status) {
				getCardId()
			}
		})

	})
	/**
	 * 获取卡号 读卡
	 */
	const getCardId = () => {
		console.log('======获取卡信息======')
		let cmdArr = [
			cmd.HOME_DIRECTORY,
			//选择主目 
			cmd.APPLICATION_DIRECTORY,
			//选择文件1001--DF01联网收费应用目录 
			cmd.CMD_READBINARY,
			//15文件--卡片发行基本数据文件
			cmd.CMD_GETBALANCE
			//钱包
		];
		tools.showLoadingAlert("正在执行指令");
		bluetoothUtil.transCmd(cmdArr, "10", function (res) {
			tools.hideLoadingAlert();
			//10：写卡   20：写OBU
			let str = res[2].substring(res[2].length - 4, res[2].length);
			let str3 = res[3].substring(res[3].length - 4, res[3].length);
			if (str == "9000" || str3 == "9000") {
				if (res[2].length > 86 || res[3] >= 12) {
					state.readCardId = res[2].substring(20, 40); //卡号
					if (state.orderInfo.cardId != state.readCardId) {
						uni.showModal({
							title: '提示',
							content: '卡号不一致,前往订单管理页面申请退货',
							showCancel: false,
							success: function (res) {
								if (res.confirm) {
									console.log('用户点击确定');
									uni.switchTab({
										url: "/pages/order/order"
									})
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					} else {
						getObuId()
					}

				} else {
					console.error("CMD_READBINARY指令长度不符" + res[2])
					tools.hideLoadingAlert();
				}
			}
			tools.hideLoadingAlert();
		});
	};
	/**
	 * 获取OBU号 读OBU
	 */
	const getObuId = () => {
		console.log('======获取OBU号======')
		let cmdArray = [cmd.HOME_DIRECTORY, cmd.OBU_SYSTEM_FILE, cmd.RANDOM_NUMBER];
		tools.showLoadingAlert("正在执行指令");
		bluetoothUtil.transCmd(cmdArray, "20", function (res) {
			tools.hideLoadingAlert();
			var status = res[1].substring(res[1].length - 4, res[1].length);

			console.log('获取OBU号执行结果' + status, res)
			//第一次获取随机数
			if (status == "9000") {
				state.readObuId = res[1].substring(20, 36);
				if (state.orderInfo.obuId != state.readObuId) {
					uni.showModal({
						title: '提示',
						content: '签号不一致,前往订单管理页面申请退货',
						showCancel: false,
						success: function (res) {
							if (res.confirm) {
								console.log('用户点击确定');
								uni.switchTab({
									url: "/pages/order/order"
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					// 确认收货
					confirmReceipt()
				}
			}
		});
	};
	const confirmReceipt = () => {
		const options = {
			type: 2,
			data: {
				id: state.orderInfo.id,
				source: "WECHAT",
				opId: getItem(StorageKeys.OpenId),
			},
			method: "POST",
			showLoading: true,
		};
		request(receiveOrder, options).then((res) => {
			confirm(
				"您已完成订单收货",
				() => {
					uni.$emit("refreshOrder");
					uni.navigateBack();
				},
				"收货完成",
				false
			);
		});
	}
	onUnload(() => {
		//移除监听
		uni.$off('bluetoothLink')
	})
	//确认收货
	const confirmReceiptClick = (e) => {
		navTo("/pages/bluetooth/bluetooth?routeType=5"); //去连接蓝牙

	};

	//获取订单详情
	const getOrderDetails = (id) => {
		const options = {
			type: 2,
			data: { id: id },
			method: "POST",
			showLoading: true,
		};
		request(orderDetail, options).then((res) => {
			state.orderInfo = stringToJson(res.bizContent);

			state.list[0].value = state.orderInfo.orderId;
			state.list[1].value = state.orderInfo.vehiclePlate;
			state.list[2].value = getOrderStatusName(state.orderInfo.orderStep);
			state.list[3].value = state.orderInfo.cardId ? state.orderInfo.cardId : "";
			state.list[4].value = state.orderInfo.obuId ? state.orderInfo.obuId : "";
		});
	};

	onLoad((options) => {
		getOrderDetails(options.id);
	});
</script>

<style>
	page {
		background-color: #eef7f7;
	}
</style>
<style lang="scss" scoped>
	.card-box {
		background-color: white;
		border-radius: 20rpx;
		box-shadow: 0px 0px 6rpx 2rpx rgba(223, 223, 223, 0.3);
		margin: 30rpx;
		padding: 20rpx 0;

		.title {
			padding: 0 20rpx 20rpx;
		}

		.icon {
			width: 48rpx;
			height: 48rpx;
			margin-right: 10rpx;
		}

		.txt-title {
			font-size: 30rpx;
			color: #333333;
		}

		.order-box {
			padding: 0px 30rpx 10rpx;

			.order-item {
				padding: 28rpx 0px 0px;
			}
		}
	}

	.hint {
		font-size: 28rpx;
		color: #ff8000;
		margin-top: 60rpx;
		text-align: center;
	}

	.btn {
		margin: 500rpx 40rpx 50rpx;
	}
</style>