<template>
	<view class="wrapper">
		<view class="info-card">
			<view class="title">卡信息</view>
			<view class="cell">
				<view class="cell-left">卡号</view>
				<view class="cell-right">{{ card.cardNo }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">用户姓名</view>
				<view class="cell-right">{{ card.userName }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">身份证号</view>
				<view class="cell-right">{{ card.idNum }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车辆类型</view>
				<view class="cell-right">{{ card.type }}</view>
			</view>
			<!-- <view class="cell">
				<view class="cell-left">车辆用户类型</view>
				<view class="cell-right">{{ card.v_userType }}</view>
			</view> -->
			<view class="cell">
				<view class="cell-left">车牌号</view>
				<view class="cell-right">{{ card.vehiclePlate }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车牌颜色</view>
				<view class="cell-right">{{ card.color }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">启用时间</view>
				<view class="cell-right">{{ card.startTime }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">结束时间</view>
				<view class="cell-right">{{ card.endTime }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">卡签绑定</view>
				<view class="cell-right">{{ card.favourable }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">校验值</view>
				<view class="cell-right">{{ card.money }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">版本号</view>
				<view class="cell-right">{{ card.version }}</view>
			</view>
			<view class="cell" v-if="state.cardStatus">
				<view class="cell-left">卡状态</view>
				<view class="cell-right">{{getCodeName('CARD_STATE_TYPE',state.cardStatus)}}</view>
			</view>
			<view class="cell" v-if="state.cardTypeName">
				<view class="cell-left">卡类型</view>
				<view class="cell-right">{{state.cardTypeName}}</view>
			</view>
		</view>
		<view class="info-card">
			<view class="title">OBU信息</view>
			<view class="cell">
				<view class="cell-left">obu号</view>
				<view class="cell-right">{{ obu.num }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车辆类型</view>
				<view class="cell-right">{{ obu.type }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车辆用户类型</view>
				<view class="cell-right">{{ obu.userType }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车牌号</view>
				<view class="cell-right">{{ obu.vehiclePlate }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车牌颜色</view>
				<view class="cell-right">{{ obu.vehiclePlateColor }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">核定载人数</view>
				<view class="cell-right">{{ obu.approvedCount }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车辆识别代号</view>
				<view class="cell-right">{{ obu.vin }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车牌发动机号</view>
				<view class="cell-right">{{ obu.engineNum }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车轴数</view>
				<view class="cell-right">{{ obu.axleCount }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车轮数</view>
				<view class="cell-right">{{ obu.wheelCount }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">轴距</view>
				<view class="cell-right">{{ obu.axleDistance }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">外廓尺寸</view>
				<view class="cell-right">{{ obu.outsideDimensions }}mm</view>
			</view>
			<view class="cell">
				<view class="cell-left">启用时间</view>
				<view class="cell-right">{{ obu.startTime }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">结束时间</view>
				<view class="cell-right">{{ obu.endTime }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">是否激活</view>
				<view class="cell-right">{{ obu.isJH }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">版本号</view>
				<view class="cell-right">{{ obu.version }}</view>
			</view>
			<view class="cell" v-if="state.obuStatus">
				<view class="cell-left">签状态</view>
				<view class="cell-right">{{getCodeName('OBU_STATE_TYPE',state.obuStatus)}}</view>
			</view>
		</view>
		<view class="btn" @click="back">返回订单列表</view>
	</view>
</template>

<script setup>
	import {
		getCodeName
	} from "@/datas/queryKey.js";
	import {
		reactive
	} from "vue";
	import {
		onLoad,
		onUnload
	} from "@dcloudio/uni-app";
	import {
		request
	} from "@/utils/network/request";
	import {
		obuDecrypt,
		cardList,
		searchObuInfo
	} from "@/utils/network/api";
	import {
		stringToJson
	} from "@/utils/network/encryption";
	import {
		HexToStrig,
		hexStringToByteArray,
		HexToInt
	} from "@/utils/util/fileData.js";
	import {
		getVehicleType
	} from "@/subpackage/after-sale/js/vehicleType.js"
	import {
		getVehiclePlateColor
	} from "@/datas/vehiclePlateColor.js"

	const cmd = require("../../../static/etcUtil/cmdConfig.js");
	const tools = require("../../../static/etcUtil/tools.js");
	const datas = require("../../../static/etcUtil/datas.js");
	const bluetoothUtil = require("../../../static/etcUtil/index.js");
	const card = reactive({
		cardNo: "",
		startTime: "",
		endTime: "",
		userName: "",
		idNum: "",
		vehiclePlate: "",
		vehiclePlateColor: "",
		color: "",
		version: "",
		type: "",
		favourable: "",
		money: "",
		v_userType: "",
	})
	const obu = reactive({
		num: "",
		startTime: "",
		endTime: "",
		version: "",
		approvedCount: "",
		axleCount: "",
		axleDistance: "",
		engineNum: "",
		type: "",
		userType: "",
		vehiclePlate: "",
		vehiclePlateColor: "",
		vin: "",
		wheelCount: "",
		isJH: "",
		outsideDimensions: "",
	})
	const state = reactive({
		vehicleId: "",
		obuStatus: "",
		cardStatus: "",
		cardTypeName: ""
	})
	onLoad((option) => {
		getCardId()
	})

	/**
	 * 获取卡号
	 */
	const getCardId = () => {
		//执行0015文件
		tools.showLoadingAlert("执行指令");

		let cmdArr = [cmd.HOME_DIRECTORY, cmd.APPLICATION_DIRECTORY, cmd.CMD_READBINARY, cmd.CMD_GETBALANCE];
		console.log(cmdArr);
		console.log(bluetoothUtil);
		bluetoothUtil.transCmd(cmdArr, '10', function(res) { //10：写卡   20：写OBU
			let str = res[2].substring(res[2].length - 4, res[2].length); //判断是否为9000
			let str3 = res[3].substring(res[3].length - 4, res[3].length); //判断是否为9000
			if (str == "9000" || str3 == "9000") {
				if (res[2].length > 86 || res[3] >= 12) {
					console.log("res[2]", res[2].substring(84, 86), getVehiclePlateColor(parseInt(res[2]
						.substring(82, 84), 16)), res[2].substring(18, 19))
					card.cardNo = res[2].substring(20, 40);
					card.startTime = res[2].substring(40, 48);
					card.endTime = res[2].substring(48, 56);
					card.version = res[2].substring(18, 19) >= 4 ? "4x" : "2x";
					card.money = res[3].substring(0, 8);
					card.v_userType = parseInt(res[2].substring(80, 82), 16);
					console.log("card.v_userType", card.v_userType)
					card.vehiclePlate = HexToStrig(res[2].substring(56, 80));
					if (res[2].substring(18, 19) >= 4) {
						//4x卡
						card.type = getVehicleType(parseInt(res[2].substring(84, 86), 16));
						// card.vehiclePlateColor = parseInt(res[2].substring(82, 84), 16);
						card.color = getVehiclePlateColor(parseInt(res[2].substring(82, 84), 16))
					} else {
						//2x卡--获取车牌颜色
						card.type = "--";
						// card.vehiclePlateColor = parseInt(res[2].substring(82, 84), 16);
						card.color = getVehiclePlateColor(parseInt(res[2].substring(82, 86), 16))
					}
					state.vehicleId = card.vehiclePlate + "_" + parseInt(res[2].substring(82, 84), 16)
					console.log("card.color===========", card.vehiclePlate, parseInt(res[2].substring(82, 84),
						16))
					// 读卡信息
					getCardList()
					//执行0016文件
					let arr = [cmd.HOME_DIRECTORY, cmd.PERSONAL_INFORMATION]
					bluetoothUtil.transCmd(arr, '10', function(res) {
						let str2 = res[1].substring(res[1].length - 4, res[1].length);
						if (str2 == "9000") {
							if (res[1].length > 108) {
								card.userName = HexToStrig(res[1].substring(4, 44)); //姓名
								card.userName = card.userName[0] + '*'.repeat(card.userName.length - 1)
								card.idNum = HexToStrig(res[1].substring(44, 108)).replace(/^\d{14}/,
									'**************'); //身份证号码					


								//获取是否有优惠
								let cmdArr3 = [cmd.HOME_DIRECTORY, cmd.APPLICATION_DIRECTORY, cmd
									.CMD_00B08E0000
								];
								bluetoothUtil.transCmd(cmdArr3, '10', function(res) {
									let str3 = res[2].substring(res[2].length - 4, res[2]
										.length);
									if (str3 == "9000") {
										if (res[2].length > 131) {
											let _str = parseInt(res[2].substring(129, 130),
												16);
											switch (_str) {
												case 0:
													card.favourable = "否";
													break;
												case 1:
													card.favourable = "是";
													break;
												default:
													card.favourable = '未知' + "_" + _str;
													break;
											}
											//回调读取OBU指令
											getObuId()
											tools.hideLoadingAlert();

										} else {
											alertF("CMD_00B08E0000指令长度不符" + res[2]);
										}
										return;
									}
									alertF(cmd.PERSONAL_INFORMATION + "_" + str);
								})
							} else {
								alertF("PERSONAL_INFORMATION指令长度不符" + res[1]);
							}
							return;
						}
						alertF("PERSONAL_INFORMATION指令长度不符" + res[1]);
					})
				} else {
					alertF("CMD_READBINARY指令长度不符" + res[2]);
				}
				return;
			}
			alertF("CMD_READBINARY指令长度不符" + res[2]);

		})
	};
	/**
	 * 获取OBU号
	 */
	const getObuId = () => {
		tools.showLoadingAlert("执行指令");
		let cmdArr = [cmd.HOME_DIRECTORY, cmd.OBU_SYSTEM_FILE];
		bluetoothUtil.transCmd(cmdArr, '20', function(res) {
			let str = res[1].substring(res[1].length - 4, res[1].length);
			if (str == "9000") {
				if (res[1].length > 52) {

					obu.num = res[1].substring(20, 36);
					obu.startTime = res[1].substring(36, 44);
					obu.endTime = res[1].substring(44, 52);
					obu.version = res[1].substring(18, 19) >= 4 ? "4x" : "2x";
					obu.isJH = res[1].substring(53, 54) == "1" ? "是" : res[1].substring(53, 54) == "0" ?
						"否" : "其他：" + res[1].substring(53, 54);


					let rand = datas.generateMixed(16);
					let arr = [cmd.OBU_DF01, cmd.OBU_00B400000A + rand + cmd.OBU_4F00];
					bluetoothUtil.transCmd(arr, '20', function(res) {
						console.log(res);
						let str2 = res[1].substring(res[1].length - 4, res[1].length);
						if (str2 == "9000") {
							console.log("===========", obu.num, res[1].substring(0, res[1].length - 4))
							decryptObuVehicleInfo(obu.num, res[1].substring(0, res[1].length - 4),
								function(res) {
									console.log("obu信息", '车辆类型', res, res.substring(28, 30),
										HexToInt(res.substring(28, 30)));
									obu.approvedCount = HexToInt(res.substring(48, 54));
									obu.axleCount = HexToInt(res.substring(42, 44));
									obu.axleDistance = HexToInt(res.substring(44, 48));
									obu.engineNum = HexToStrig(res.substring(86, 118));
									obu.type = getVehicleType(HexToInt(res.substring(28, 30)));
									if(HexToInt(res.substring(30, 32)) == 0){
										obu.userType = "普通车" ;
									}else if(HexToInt(res.substring(30, 32)) == 6){
										obu.userType = "公务车" ;
									}
									obu.vehiclePlate = HexToStrig(res.substring(0, 24), 2);
									//获取车牌颜色
									obu.vehiclePlateColor = getVehiclePlateColor(HexToInt(res
										.substring(24, 28)));
									obu.vin = HexToStrig(res.substring(54, 86));
									obu.wheelCount = HexToInt(res.substring(40, 42));
									obu.outsideDimensions = HexToInt(res.substring(32, 36))*100 + "x" +
										HexToInt(res.substring(36, 38))*100 + "x" + HexToInt(res
											.substring(38, 40))*100
									getObuList();

									tools.hideLoadingAlert();
									//提交设备信息
									// that.submitDeviceInfo();
								});
							//断开蓝牙
							bluetoothUtil.disconnectDevice();
							tools.showToastAlert("蓝牙已断开");
							console.log('蓝牙已断开=========')
							return;
						}
						alertF("OBU_00B400000A+rand+OBU_4F00指令长度不符" + res[1]);
					})

				} else {
					alertF("OBU_SYSTEM_FILE指令长度不符" + res[1]);
				}
				return;
			}
			alertF("OBU_SYSTEM_FILE指令长度不符" + res[1]);
		})
	};


	/**
	 * 解密OBU车辆信息
	 */
	const decryptObuVehicleInfo = (num, data, func) => {
		const options = {
			type: 2,
			data: {
				obuSerailNo: num,
				fileData: data
			},
			method: "POST",
			showLoading: true,
		};
		request(obuDecrypt, options).then((res) => {
			const data = stringToJson(res.bizContent)
			const FileData = data.data.FileData
			func(FileData)
		});
	};

	//返回按钮
	const back = () => {
		uni.$emit("refreshOrder");
		uni.switchTab({
			url: "/pages/order/order"
		})
	}

	//提示对话框
	const alertF = (msg) => {

		//隐藏加载框
		tools.hideLoadingAlert();

		//提示对话框
		tools.showModalAlert(msg);
	}
	const getCardList = () => {
		var data = {
			vehicleId: state.vehicleId
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		request(cardList, options).then((res) => {
			let result = stringToJson(res.bizContent);
			state.cardStatus = result.data[0].cardStatus
			if (result.data[0].cardType == 1 && result.data[0].debitType == 1) {
				state.cardTypeName = "预存卡"
			} else if (result.data[0].cardType == 2) {
				state.cardTypeName = "储值卡"
			} else {
				state.cardTypeName = "记账卡"
			}
			console.log("152", result)
		})
	}
	const getObuList = () => {
		var data = {
			obuId: obu.num
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		request(searchObuInfo, options).then((res) => {
			tools.hideLoadingAlert();
			let result = stringToJson(res.bizContent);
			console.log("153", result)
			state.obuStatus = result.data[0]?result.data[0].obuStatus:""
		})
	}
</script>

<style scoped>
	.wrapper {
		padding-bottom: 50rpx;
	}

	.info-card {
		margin: 30rpx;
		border-radius: 16rpx;
		padding-bottom: 40rpx;
		box-shadow: 0rpx 6rpx 25rpx 0rpx rgba(0, 0, 0, 0.3);
	}

	.info-card .title {
		padding: 30rpx 0;
		font-size: 36rpx;
		text-align: center;
		color: #13E7C1;
	}

	.info-card .cell {
		display: flex;
		flex-direction: row;
		padding: 15rpx 40rpx;
		font-size: 30rpx;
	}

	.cell-left {
		flex: 1;
		text-align: left;
		color: #666;
	}

	.cell-right {
		margin-left: 30rpx;
		text-align: right;
		color: #333;
		overflow: hidden;
	}

	.btn {
		background: linear-gradient(to right, #13E7C1, #43A1E0);
		opacity: 1;
		border-radius: 100rpx;
		color: #fff;
		font-size: 30rpx;
		height: 80rpx;
		line-height: 80rpx;
		box-shadow: 0rpx 4rpx 11rpx 1rpx rgba(223, 223, 223, 0.5);
		width: 670rpx;
		margin: 40rpx;
		text-align: center;
	}
</style>