<template>
	<view class="wrapper">
		<view class="account">
			<image :src="`${$imgUrl}common/bg-recharge.png`" class="head-bg"></image>
			<view class="account-text">
				<view class="balance">
					<text class="balance-tit">账户余额</text>
					<text class="balance-val">20.00元</text>
				</view>
				<view class="right-box">
					<view class="no">账户编号：000100</view>
					<view class="btn btn-record">账户充值记录</view>
				</view>
			</view>
		</view>
		<view class="input-box">
			<input placeholder="请输入金额" class="input" />
			<view class="tips">最低充值金额不小于10元</view>
		</view>
		<view class="tabs">
			<view class="tab-tit">在线充值</view>
			<view class="tab">
				<view class="item">50元</view>
				<view class="item active">100元</view>
				<view class="item">200元</view>
				<view class="item">300元</view>
				<view class="item">500元</view>
				<view class="item">1000元</view>
			</view>
		</view>
		<view class="btn btn-primary" @click="recharge">立即充值</view>
	</view>
</template>

<script setup lang="ts">
	import {
		stringToJson
	} from "@/utils/network/encryption";
	import navBar from "../components/nav-bar/nav-bar2.vue";
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import {
		quanCheck,
		quanApply,
		quanXf,
		cardCzPayResultCheck,
		cardCzApply
	} from "@/utils/network/api.js";
	import {
		request
	} from "@/utils/network/request.js";
	import {
		reactive
	} from "vue";
	import {
		msg
	} from "@/utils/utils";
	import {
		getItem,
		StorageKeys
	} from "@/utils/storage";
	const datas = require("../../static/etcUtil/datas.js");
	const bluetoothUtil = require("../../static/etcUtil/index.js");
	const recharge = () => {
		uni.navigateTo({
			url: "/pages/recharge/result"
		})
	}
	onLoad((option) => {
		/*1.圈层检测*/
		quanCheckAction().then(val => {
			//圈层检测正常
			if (val.chargeStatus === 1) {
				czCheckAction()

			} else {
				if (option.connectSuccess !== '1') {

					uni.navigateTo({
						url: `/pages/bluetooth/device-active-step3?routeType=2`,
					});


				} else {
					let cmdArr = val.command.split(",");
					uni.showLoading({
						title: "写入中"
					})
					bluetoothUtil.transCmd(cmdArr, "10", function(res) {
						let response = res.toString();
						var dic = {
							command: val.command,
							cosResponse: response,
							rechargeId: val.rechargeId
						}
						quanFixAction(dic).then(value => {
							var fixStatus = value.fixStatus;
							//圈存修复COS指令Response信息不足,重新进行修复初始化
							if (fixStatus === 1) {

							} else if (fixStatus === 2) {
								quanApplyAction()
								//圈存确认
							} else if (fixStatus === 3) {
								//圈存确认
							}
							console.log('修复成功')
						})
						uni.hideLoading()

					});
				}
			}
		})

	});
	/*圈层检测*/
	const quanCheckAction = () => {
		var data = {
			cardId: '52011328220201979745',
			fee: 199,
			preBalance: 19588,
			tradeType: 14,
			agentId: "52010106004",
			channelId: "5201010200601130001",
			channelType: "1",
			staffId: "opId",
			terminalId: "999999999999"
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await request(quanCheck, options);
			const data = stringToJson(res.bizContent);
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
	/*圈存修复*/
	const quanFixAction = (val) => {
		var data = {
			command: val.command, //修复初始化的指令
			cosResponse: val.cosResponse, //修复初始化结果
			rechargeId: val.rechargeId, //充值流水号
			agentId: "52010106004",
			channelId: "5201010200601130001",
			channelType: "1",
			staffId: "opId",
			terminalId: "999999999999"
		};
		console.log('111111111')
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await request(quanXf, options);
			const data = stringToJson(res.bizContent);
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
	/*圈层申请*/
	const quanApplyAction = (data) => {

		var form = {
			cardId: '52011328220201979745',
			fee: 50,
			preBalance: 100,
			tradeType: 14,
			command: data.command,
			cosResponse: '',

			agentId: "52010106004",
			channelId: "5201010200601130001",
			channelType: "1",
			staffId: "opId",
			terminalId: "999999999999"
		};
		const options = {
			type: 2,
			data: form,
			method: "POST",
			showLoading: true,
		};
		request(quanApply, options).then((res) => {
			const data = stringToJson(res.bizContent);
			quanApplyAction(data)

		});
	}

	/*充值检测*/
	const czCheckAction = (data) => {
		var form = {
			cardId: '52011328220201979745',
			openId: getItem(StorageKeys.OpenId),
			agentId: "52010106004",
			channelId: "5201010200601130001",
			channelType: "1",
			staffId: "opId",
			terminalId: "999999999999"
		};
		const options = {
			type: 2,
			data: form,
			method: "POST",
			showLoading: true,
		};
		request(cardCzPayResultCheck, options).then((res) => {
			const data = stringToJson(res.bizContent);
			cardCzApplyAction().then(val => {
				console.log(val)
			})

		});
	}
	const cardCzApplyAction = () => {
		var data = {
			cardId: '52011328220201979745',
			openId: getItem(StorageKeys.OpenId),
			rechargeMoney: 100,
			agentId: "52010106004",
			channelId: "5201010200601130001",
			channelType: "1",
			staffId: "opId",
			terminalId: "999999999999"
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await request(cardCzApply, options);
			const data = stringToJson(res.bizContent);
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
</script>

<style>
	.account {
		height: 224rpx;
		width: 690rpx;
		margin: 30rpx;
		position: relative;
	}

	.account .account-text {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 50rpx;
		height: 100%;
	}

	.account .balance {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-start;
		color: #ffffff;
	}

	.account .balance-tit {
		font-size: 26rpx;
		margin-bottom: 10rpx;
	}

	.account .balance-val {
		font-size: 56rpx;
	}

	.account .right-box {
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: flex-end;
	}

	.account .right-box .no {
		font-size: 26rpx;
		color: #ffffff;
	}

	.account .right-box .btn {
		font-size: 26rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
		height: 42rpx;
		line-height: 42rpx;
		color: #28d20f;
		background: #ffffff;
		border-radius: 21rpx;
		display: inline-block;
		margin-top: 18rpx;
	}

	.account .head-bg {
		width: 690rpx;
		height: 224rpx;
		position: absolute;
		left: 0;
		top: 0;
		z-index: -99;
	}

	.input-box {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding: 20rpx 0;
	}

	.input-box .input {
		width: 430rpx;
		height: 98rpx;
		line-height: 98rpx;
		background: #f8f8f8;
		border: 1px solid #999999;
		border-radius: 6rpx;
		font-size: 30rpx;
		text-align: center;
	}

	.input-box .tips {
		color: #666666;
		font-size: 26rpx;
		margin-top: 18rpx;
	}

	.tabs {}

	.tabs .tab-tit {
		font-size: 30rpx;
		color: #333;
		padding: 45rpx 0 22rpx 30rpx;
	}

	.tabs .tab {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-evenly;
	}

	.tabs .tab .item {
		width: 210rpx;
		height: 100rpx;
		background: #F6FFF7;
		border: 1px solid #DCDDE1;
		box-sizing: border-box;
		line-height: 100rpx;
		border-radius: 6rpx;
		text-align: center;
		margin-bottom: 24rpx;
		color: #333333;
		font-size: 32rpx;
	}

	.tabs .tab .item.active {
		border: 1px solid #24cc49;
		color: #24cc49;
	}

	.btn-primary {
		width: 670rpx;
		height: 80rpx;
		line-height: 80rpx;
		background: linear-gradient(-90deg, #43A1E0 0%, #13E7C1 100%);
		border-radius: 40rpx;
		font-size: 32rpx;
		color: #ffffff;
		margin: 200rpx 40rpx 0;
		text-align: center;
	}
</style>
