<template>
	<view class="selectCar-box">
		<view class="details">
			<view class="title"> 基础信息 </view>
			<view class="details-item">
				<view> 订单编号： </view>
				<text>{{ state.params.orderNo }}</text>
			</view>
			<view class="details-item">
				<view> 用户名称： </view>
				<text>{{ state.params.customerName }}</text>
			</view>
			<view class="details-item">
				<view> 用户证件类型： </view>
				<text>{{ getCodeName('CERTIFICATE_TYPE',state.params.customerIdTypeCode) }}</text>
			</view>
			<view class="details-item">
				<view> 用户证件号： </view>
				<text>{{ state.params.customerIdNum }}</text>
			</view>
			<view class="details-item">
				<view> 订单车牌号： </view>
				<text style="color: #00b38b">{{ state.params.vehiclePlate }}</text>
			</view>
			<view class="details-item">
				<view> 车牌颜色： </view>
				<text style="color: #00b38b">{{getVehiclePlateColor(state.params.vehiclePlateColor)}}</text>
			</view>
			<view class="details-item">
				<view> 收费车型： </view>
				<text>{{ state.params.vehicleType }}</text>
			</view>
		</view>
		<view class="title"> 卡信息 </view>
		<view class="card">
			<view class="card-left">
				<image :src="`${$imgUrl}card2.png`" mode=""></image>
				<view class="card-center">
					<view class="card-center-head"> {{state.params.cardId}} </view>
					<view class="tips">
						<text>储蓄卡</text>
						<text class="tips-card">{{getCodeName('CARD_STATE_TYPE',state.params.cardStatusCode)}}</text>
					</view>
					<view class="choose-item"> 有效期：{{state.params.cardExpireTime }}</view>
				</view>
			</view>

		</view>
		<view class="title"> OBU设备信息 </view>
		<view class="card">
			<view class="card-left">
				<image :src="`${$imgUrl}card1.png`" mode=""></image>
				<view class="card-center">
					<view class="card-center-head"> {{state.params.obuId}} </view>
					<view class="tips">
						<text class="tips-card">{{getCodeName('OBU_STATE_TYPE',state.params.obuStatusCode)}}</text>
					</view>
					<view class="choose-item"> 有效期：{{state.params.obuExpireTime }} </view>
				</view>
			</view>
		</view>
		<button class="submit" @click="toPage" v-if="!state.flag" :disabled="state.disabled">设备升级</button>
		<button class="submit" @click="toBack" v-else>返回服务中心</button>

		<view class="mask" v-show="state.showPopup">
			<view class="main">
				<view class="top">
					<image class="icon-close" :src="`${$imgUrl}common/icon-close.png`" @click="cancel"></image>
				</view>
				<image class="icon-success" :src="`${$imgUrl}bluetooth/card-success.png`"></image>
				<view class="title">设备升级成功！</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive
	} from "vue";
	import {
		navTo,
		getFormatDate,
		desensitization
	} from "@/utils/utils";
	import {
		onLoad,
		onUnload,
		onShow
	} from "@dcloudio/uni-app";
	import {
		request,requestNew
	} from "@/utils/network/request.js";
	import {deviceUpgradeApply,deviceUpgradeOrderConfirm,commGetDetail,deviceUpgradeConfirm} from "@/utils/network/api.js";
	import {
		getVehiclePlateColor
	} from "@/datas/vehiclePlateColor";
	import {
		searchVehicleInfoQuery
	} from "@/subpackage/after-sale/js/publicRequest";
	import {
		getCodeName
	} from "@/datas/queryKey.js";
	import {
		IntegerToHexString,
	} from "@/utils/util/fileData.js";
	import {
		stringToJson
	} from "@/utils/network/encryption";
	import {
		getItem
	} from "@/utils/storage.ts"
	const tools = require("../../../static/etcUtil/tools.js");
	const bluetoothUtil = require("../../../static/etcUtil/index.js");
	const cmd = require("../../../static/etcUtil/cmdConfig.js");
	const state = reactive({
		showPopup: false,
		flag: false,
		disabled: false,
		orderId: "",
		vehicleId:"",
		params:{} //上一个页面传递过来得参数
	})
	let orderInfo = {};
	let cmdRandNum = '';
	onLoad((option) => {
		state.params = JSON.parse(decodeURIComponent(option.params))
		console.log("state.params", state.params)
	});

	onShow((option) => {
		uni.$on('bluetoothLink', res => {
			console.log(res);
			if (res.status) {
				tools.showLoadingAlert("正在执行指令");
				state.disabled = true;
				deviceUpgradeApplyRequest()
			}
		})
	})
	

	//指令申请
	function deviceUpgradeApplyRequest(){
		tools.showLoadingAlert("升级中");
		const options = {
			type: 2, //type: 2,JSON格式提交数据(默认表单形式提交)
			showLoading: true,
			data: {
				orderId: state.params.id,
				cardId: state.params.cardId,
				obuId: state.params.obuId,
			}, //请求参数
			method: "POST", //提交方式(默认POST)
		};
		console.log("options2222", options)
		requestNew(deviceUpgradeApply, options)
			.then((res) => {
				let result = res;
				console.log("指令申请", res);
				tools.hideLoadingAlert();
				let cmdArray = result.cardCmd ? result.cardCmd.split(",") : "";
				
				if (cmdArray.length > 0) {
					tools.showLoadingAlert("正在执行指令");
					bluetoothUtil.transCmd(cmdArray, result.cmdType.includes('CARD')?"10":'20', function(res) {
						tools.hideLoadingAlert();
						let status = res[cmdArray.length - 1].substring(res[cmdArray.length - 1].length -
							4, res[cmdArray.length - 1].length);
						if (status == "9000") {
							deviceUpgradeOrderConfirmRequest(result.cardCmd, result.cosRecordId, res.toString(),result.cmdType);
						}
					})
				}
			})
			.catch((err) => {
				console.log(err);
				tools.hideLoadingAlert();
			});
	}
	//设备升级指令回传
	function deviceUpgradeOrderConfirmRequest(cmd, cosRecordId, response,cmdType){
		tools.showLoadingAlert("加载中");
		const options = {
			type: 2, //type: 2,JSON格式提交数据(默认表单形式提交)
			showLoading: true,
			data: {
				cardId: state.params.cardId,
				obuId: state.params.obuId,
				cmd: cmd,
				cmdResult: response,
				cosRecordId: cosRecordId,
				cmdType: cmdType,
				vehicleId: state.params.vehicleId,
			}, //请求参数
			method: "POST", //提交方式(默认POST)
		};
		console.log("options2222", options)
		requestNew(deviceUpgradeOrderConfirm, options)
			.then((res) => {
				console.log("设备升级指令回传", res);
				tools.hideLoadingAlert();
				let result = res;
				if (result.hasNext) {
					tools.showLoadingAlert("执行指令中");
					bluetoothUtil.transCmd(result.cmd.split(","), result.cmdType.includes('CARD')?"10":'20', function(res) {
						tools.hideLoadingAlert();
						let response = res.toString();
						deviceUpgradeOrderConfirmRequest(result.cmd, cosRecordId, response,result.cmdType);
					});
				} else {
					bluetoothUtil.transCmd(result.cmd.split(","), result.cmdType.includes('CARD')?"10":'20', function(res) {
						tools.hideLoadingAlert();
						let response = res.toString();
						deviceUpgradeConfirmRequest(result.cmd,cosRecordId,response,result.cmdType);
					});
					
				}
			})
			.catch((err) => {
				console.log(err);
				tools.hideLoadingAlert();
			});
	}
	//设备升级确认
	function deviceUpgradeConfirmRequest(cmd,cosRecordId,cmdResult,cmdType){
		const options = {
			type: 2, //type: 2,JSON格式提交数据(默认表单形式提交)
			showLoading: true,
			data: {
				cmd,
				cosRecordId,
				cmdResult,
				cmdType,
				orderId:state.params.id
			}, //请求参数
			method: "POST", //提交方式(默认POST)
		};
		console.log("options2222", options)
		requestNew(deviceUpgradeConfirm, options)
			.then((res) => {
				console.log("设备升级确认", res);
				tools.hideLoadingAlert();
				state.showPopup = true;
			})
			.catch((err) => {
				console.log(err);
				tools.hideLoadingAlert();
			});
	}
	onUnload(() => {
		//移除监听
		uni.$off('bluetoothLink')
	})
	
	

	

	//去连接蓝牙
	const toPage = () => {
		// getDeviceUpgrade()
		navTo("/pages/bluetooth/bluetooth?routeType=5"); //去连接蓝牙
	};

	//返回列表
	const toBack = () => {
		uni.navigateBack({
			delta: 2
		})
	}

	//关闭弹窗
	const cancel = () => {
		state.flag = true;
		state.showPopup = false;
		uni.navigateTo({
			url: `/subpackage/after-sale/deviceInfo/deviceInfo`
		})
	}

</script>

<style>
	page {
		width: 100%;
		height: 100%;
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.mask {
		background: rgba(0, 0, 0, .35);
		position: fixed;
		left: 0;
		top: 0;
		bottom: 0;
		right: 0;
	}

	.main {
		width: 560rpx;
		padding: 25rpx 20rpx 55rpx;
		text-align: center;
		background: #fff;
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		border-radius: 20rpx;

		.top {
			text-align: right;

			.icon-close {
				width: 48rpx;
				height: 48rpx;
			}
		}

		.icon-success {
			width: 500rpx;
			height: 320rpx;
			margin-top: 22rpx;
		}

		.title {
			color: #333333;
			font-size: 40rpx;
			font-weight: 600;
			text-align: center;
			margin-top: 55rpx;
		}
	}

	.selectCar-box {
		// width: 100%;
		// height: 100%;
		padding: 30rpx;

		.title {
			font-size: 30rpx;
			font-family: Microsoft YaHei UI;
			font-weight: 400;
			color: #333333;
			margin-bottom: 30rpx;
		}

		.details {
			.title {
				font-size: 30rpx;
				font-family: Microsoft YaHei UI;
				font-weight: 400;
				color: #333333;
				margin-bottom: 30rpx;
			}

			.details-item {
				display: flex;
				font-size: 26rpx;
				font-family: Noto Sans S Chinese;
				font-weight: 400;
				color: #999999;
				margin-bottom: 30rpx;

				text {
					font-size: 26rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #333333;
				}
			}
		}

		.card {
			height: 150rpx;
			background: #ffffff;
			box-shadow: 0rpx 4rpx 13rpx 3rpx rgba(223, 223, 223, 0.8);
			border-radius: 20rpx;
			padding: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 60rpx;

			.card-left {
				display: flex;
				align-items: center;

				image {
					width: 100rpx;
					height: 90rpx;
				}

				.card-center {
					margin-left: 30rpx;

					.card-center-head {
						font-size: 32rpx;
						font-family: Noto Sans S Chinese;
						font-weight: 400;
						color: #333333;
					}

					.tips {
						font-size: 26rpx;
						font-family: Noto Sans S Chinese;
						font-weight: 400;
						color: #666666;

						.tips-card {
							width: 70rpx;
							height: 40rpx;
							background: #d3f2ef;
							border-radius: 6rpx;
							font-size: 20rpx;
							font-family: Noto Sans S Chinese;
							font-weight: 400;
							color: #0a8f8a;
							padding: 5rpx 10rpx;
							margin-left: 20rpx;
						}
					}
				}
			}

			.choose-item {
				margin-right: 20rpx;
				height: 50rpx;
				border-radius: 50%;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				font-size: 25rpx;
				align-self: end;

				.active {
					width: 38rpx;
					height: 38rpx;
					background: #00b38b;
					border-radius: 50%;
				}
			}
		}

		.remark {
			font-size: 26rpx;
			font-family: Microsoft YaHei UI;
			font-weight: 400;
			color: #666666;
			text-indent: 30rpx;
			margin-bottom: 30rpx;
		}

		.submit {
			margin-top: 100rpx;
			width: 670rpx;
			height: 80rpx;
			background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
			border-radius: 40rpx;
			font-size: 32rpx;
			font-family: Noto Sans S Chinese;
			font-weight: 400;
			color: #ffffff;
			line-height: 80rpx;
		}

		.submit::after {
			border: 0;
		}

		.submit[disabled] {
			background: #f0f0f0;
			color: #333;
		}
	}
</style>