<!--设备选择 -->
<template>
	<view class="devices">
		<view class="hint">
			<view class="orange-txt as-layout-horizontal as-gravity-center-start">
				<image :src="`${$imgUrl}common/icon-hint.png`" mode="aspectFill"></image>
				<view>温馨提示：</view>
			</view>
			<view>1、请在手机设置中打开NFC功能。</view>
			<view>2、请将ETC储值卡对准手机NFC天线位置（手机背部顶端），不灵敏或无响应时，请及时调整卡片位置。</view>
			<view>3、充值过程请保证卡片在手机背部，请勿移开。</view>
		</view>
		<view class="btn">
			<submit-button title="点击重新扫描NFC标签" @submit="load"></submit-button>
		</view>
	</view>
</template>
<script setup lang='ts'>
	import {
		onLoad,
		onReady
	} from "@dcloudio/uni-app";
	import {
		ref,
		reactive
	} from "vue";
	const tools = require("../../static/etcUtil/tools.js");
	const NFCAPI = require('../../static/nfc/NFCAPI.js');
	const cmd = require("../../static/etcUtil/cmdConfig.js");
	const routeType = ref(null); //来源 1激活 2圈存 3信息重写 4信息读取

	const state = reactive({
		fee: "",
		cardId: ""
	});

	onLoad((option) => {
		routeType.value = option.routeType ? option.routeType : "1";
		state.cardId = option.cardId;
		if (option.fee) {
			state.fee = option.fee;
		}
		console.log("传过来的参数", option)
	});

	//已准备
	onReady(() => {
		scanNfc();
	})

	function load() {
		scanNfc();
	}

	function scanNfc() {
		tools.showLoadingAlert("扫描NFC中");
		//先断开所有设备
		NFCAPI.remove(() => {
			//开始扫描设备
			NFCAPI.startScanDevice((res) => {
				tools.hideLoadingAlert();
				if (res.code != 0) {
					if (res.code == 1203) {
						tools.showModalAlert("扫描超时请重新扫描NFC标签", function(res) {
							if (res.confirm) {
								scanNfc();
							}
						}, null, "", true)
					} else {
						tools.showToastAlert(res.msg);
					}
					return;
				} else {
					NFCAPI.connectDevice(function() {
						if (res.code != 0) {
							tools.showToastAlert(res.msg);
							return;
						}
						//获取卡信息
						transCmd(function() {
							getCardId(res, function() {
								if (routeType.value == "2") {
									tools.toUrl(
										`/pages/recharge/recharge?connectSuccess=1&&cardId=${state.cardId}&&fee=${state.fee}`
									); //跳转到圈存界面
								}
							})
						})
					});
				}
			})
		})
	}

	function getCardId(resultCmdArr: any, callback: any) {
		var that = this;
		if (resultCmdArr.length == 4) {
			var str = resultCmdArr[2].substring(resultCmdArr[2].length - 4, resultCmdArr[2].length); //判断是否为9000
			var str3 = resultCmdArr[3].substring(resultCmdArr[3].length - 4, resultCmdArr[3].length); //判断是否为9000
			if (str == "9000" || str3 == "9000") {
				if (resultCmdArr[2].length > 86 || resultCmdArr[3] >= 12) {
					var card = {
						cardNo: resultCmdArr[2].substring(20, 40),
						startTime: resultCmdArr[2].substring(40, 48),
						endTime: resultCmdArr[2].substring(48, 56),
						version: resultCmdArr[2].substring(18, 19) >= 4 ? "4x" : "2x",
						money: that.hex2int(resultCmdArr[3].substring(0, 8)),
						v_userType: parseInt(resultCmdArr[2].substring(80, 82), 16)
					};
					console.log("写命令拿到卡号：" + JSON.stringify(card))
					var jizhangka = card.cardNo.substring(8, 10);
					if (jizhangka == '23') {
						console.log('记账卡');
						tools.alertFback("不支持记账卡圈存", function() {
							//返回首页
							uni.reLaunch({
								url: 'pages/index/index'
							})
						})
						return;
					}
					that.setData({
						cardNo: card.cardNo,
						cardMoney: card.money > 0 ? card.money : 0,
					})
					callback(card.cardNo);
				} else {
					tools.showToastAlert("CMD_READBINARY指令长度不符" + resultCmdArr[2]);
				}
				return;
			}
		} else {
			tools.showToastAlert("回包长度不符");
		}
	}

	function transCmd(callback: any) {
		tools.showLoadingAlert('透传指令中');
		var cmdArr = [cmd.HOME_DIRECTORY, cmd.APPLICATION_DIRECTORY, cmd.CMD_READBINARY, cmd.CMD_GETBALANCE];
		NFCAPI.transCmd(cmdArr, function(res) {
			tools.hideLoadingAlert();
			console.log(res);
			if (res.code == 0) {
				callback(res.data);
			} else {
				tools.showToastAlert(res.msg);
			}
		});
	}
</script>
<style lang='scss' scoped>
	image {
		width: 40rpx;
		height: 40rpx;
	}
</style>