<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.userNameStr }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">身份证号</view>
				<view class="cell-right">{{ card.idNumStr }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车辆类型</view>
				<view class="cell-right">{{ card.typeStr }}</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.colorStr }}</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.favourableStr }}</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.typeStr }}</view>
			</view>
			<view class="cell">
				<view class="cell-left">车辆用户类型</view>
				<view class="cell-right">{{ obu.userTypeStr }}</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.vehiclePlateColorStr }}</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.isJHStr }}</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,requestNew
	} from "@/utils/network/request";
	import {cardObuQuery,obuFileDataDecrypt,cardObuQueryinLog} 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: "",
		userNameStr:"",
		idNum: "",
		idNumStr:"",
		vehiclePlate: "",
		vehiclePlateColor: "",
		color: "",
		colorStr:"",
		version: "",
		type: "",
		typeStr:"",
		favourable: "",
		favourableStr:"",
		money: "",
		v_userType: ""
	})
	const obu = reactive({
		num: "",
		startTime: "",
		endTime: "",
		version: "",
		approvedCount: "",
		axleCount: "",
		axleDistance: "",
		engineNum: "",
		type: "",
		typeStr:"",
		userType: "",
		userTypeStr:"",
		vehiclePlate: "",
		vehiclePlateColor: "",
		vehiclePlateColorStr:"",
		vin: "",
		wheelCount: "",
		isJH: "",
		isJHStr: "",
		outsideDimensions: "",
	})
	const state = reactive({
		vehicleId: "",
		obuStatus: "",
		cardStatus: "",
		cardTypeName: "",
		backIndex:""
	})
	onLoad((option) => {
		state.backIndex=option.backIndex
		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
		console.log("5201",res)
			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 = parseInt(res[2].substring(84, 86), 16);
						card.typeStr = getVehicleType(parseInt(res[2].substring(84, 86), 16));
						card.color = parseInt(res[2].substring(82, 84), 16)
						card.colorStr = getVehiclePlateColor(parseInt(res[2].substring(82, 84), 16))
					} else {
						//2x卡--获取车牌颜色
						card.type = "--";
						card.color =parseInt(res[2].substring(82, 84), 16)
						card.colorStr = getVehiclePlateColor(parseInt(res[2].substring(82, 84), 16))
					}
					state.vehicleId = 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) {
								console.log("res====",res)
								card.userName = HexToStrig(res[1].substring(4, 44)); //姓名
								card.userNameStr = card.userName[0] + '*'.repeat(card.userName.length - 1)
								card.idNum = HexToStrig(res[1].substring(44, 108)); //身份证号码		
								card.idNumStr = 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);
												card.favourable=_str
											switch (_str) {
												case 0:
													card.favourableStr = "否";
													break;
												case 1:
													card.favourableStr = "是";
													break;
												default:
													card.favourableStr = '未知' + "_" + _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)
					obu.isJHStr = res[1].substring(53, 54) == "1" ? "是" : res[1].substring(53, 54) == "0" ?
							"否" : "其他：" + res[1].substring(53, 54);

					console.log("开始执行车辆信息识读");
					let rand = res[1].substring(18, 20) >= 50?'40':'00';
					var arr = [cmd.OBU_DF01, '00B400000A000000000000000045'+ rand];
					bluetoothUtil.transCmd(arr, '20', function(res) {
						console.log(res);
						console.log("开始执行车辆信息识读成功");
						let str2 = res[1].substring(res[1].length - 4, res[1].length);
						let obuVersion = res[1].substring(18, 20);
						let keyIndex = obuVersion > 50 ? '40' : '00';
						let reqType = '00';
						let proviceCode = '5201';
						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), 
							obuVersion, rand, reqType, proviceCode,
								function(res) {
								
									obu.approvedCount = res.approvedCount;
									obu.axleCount = res.axleCount;
									obu.axleDistance = res.axleDistance;
									obu.engineNum = res.engineNumber;
									obu.type = res.collectionType;
									obu.typeStr = getVehicleType(res.collectionType);
									obu.userType = res.userType;
									if(res.userType == 0){
										obu.userTypeStr = "普通车" ;
									}else if(res.userType == 6){
										obu.userTypeStr = "公务车" ;
									}
									obu.vehiclePlate = res.vehiclePlate;
									//获取车牌颜色
									obu.vehiclePlateColor = res.vehiclePlateColor;
									obu.vehiclePlateColorStr = getVehiclePlateColor(res.vehiclePlateColor);
									obu.vin = res.vin;
									obu.wheelCount = res.wheelsCount;
									obu.outsideDimensions = res.carLong + "x" + res.carWidth + "x" + res.carHeight
									getObuList();
							

									tools.hideLoadingAlert();
								});
							//断开蓝牙
							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,obuVersion, keyIndex, reqType, proviceCode, func) => {
		const options = {
			type: 2,
			data: {
				contractSN: num,
				cipherData: data,
				reqType: reqType,
				keyIndex: keyIndex,
				proviceCode: proviceCode
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(obuFileDataDecrypt, options).then((res) => {
			console.log("obuFileDataDecrypt",res)
			const data =res
			func(data)
		});
	};

	//返回按钮
	const back = () => {
		uni.$emit("refreshOrder");
		if(state.backIndex=="1"){
			uni.switchTab({
				url: "/pages/index/index"
			})
		}else{
			uni.redirectTo({
				url: "/pages/order/order"
			})
		}
		
	}

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

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

		//提示对话框
		tools.showModalAlert(msg);
	}
	const getObuList = () => {
		var data = {
			obuId: obu.num,
			cardId:card.cardNo,
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		requestNew(cardObuQuery, options).then((res) => {
			tools.hideLoadingAlert();
			let result = res;
			state.obuStatus = result.obuStatus?result.obuStatus:""
			state.cardStatus = result.cardStatus
			if (result.cardType == 1 && result.debitType == 1) {
				state.cardTypeName = "预存卡"
				card['cardTypeName']=3
			} else if (result.cardType == 2) {
				state.cardTypeName = "储值卡"
				card['cardTypeName']=2
			} else {
				state.cardTypeName = "记账卡"
				card['cardTypeName']=1
			}
			// 卡签提交得信息
			card['cardStatus']=result.cardStatus
			obu['obuStatus']=result.obuStatus?result.obuStatus:""
			cardObuMsgUp()
		})
	}
	// 签信息提交日志记录
	const cardObuMsgUp = () => {
		var data = {
			cardLogInfo: card,
			obuLogInfo:obu,
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		requestNew(cardObuQueryinLog, options).then((res) => {
			tools.hideLoadingAlert();
			console.log("提交成功")
		})
	}
</script>

<style scoped>
	.wrapper {
		padding-bottom: 50rpx;
		background: #E9EDF0;
		overflow: hidden;
	}

	.info-card {
		margin: 30rpx;
		border-radius: 16rpx;
		padding-bottom: 40rpx;
		background-color: white;
		padding: 0 30rpx;
		box-sizing: border-box;
	}

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

	.info-card .cell {
		display: flex;
		flex-direction: row;
		padding: 18rpx 0rpx;
		font-size: 30rpx;
		border-bottom: 1rpx solid #E6E9EB;
	}

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

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

	.btn {
		background: linear-gradient(to right,#01243A,#004576);
		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>