<template>
	<view class="picture-wrapper" @click="cardFileImageUpdate('1')">
		<view class="bg">
			<view class="">
				<view class="name"> 行驶证主页 </view>
				<view class="value"> 上传行驶证的主页 </view>
				<view class="tip">
					<view class="tip-value"> 拍摄规范 </view>
				</view>
			</view>

			<image v-if="!form.licenseFrontUrl" class="icon" :src="`${$imgUrl}applyCard/car-zhu.png`">
			</image>
			<image v-else class="icon" :src="strReplace(form.licenseFrontUrl)"></image>
		</view>
	</view>
	<view class="picture-wrapper" @click="cardFileImageUpdate('2')">
		<view class="bg">
			<view class="">
				<view class="name"> 行驶证副页 </view>
				<view class="value"> 上传行驶证的副页 </view>
				<view class="tip">
					<view class="tip-value"> 拍摄规范 </view>
				</view>
			</view>
			<image v-if="!form.licenseReverseUrl" class="icon" :src="`${$imgUrl}applyCard/car-fu.png`">
			</image>
			<image v-else class="icon" :src="strReplace(form.licenseReverseUrl)"></image>
		</view>
	</view>
	<view class="upload-box">
		<u-form :model="form" ref="myForm" :error-type="errorType">
			<view class="from">
				<view class="title"> 请填写注销原因 </view>
				<u-form-item prop="remark">
					<view class="from_item">
						<text><text style="color: red"></text>注销原因:</text>
						<view style="display: flex">
							<u-input v-model="form.ReasonCancellation" @click="showPicker()" class="input" disabled
								placeholder="请选择" />
							<u-icon name="arrow-right" style="margin-left: 10px;display: flex;"></u-icon>
						</view>
					</view>
				</u-form-item>
			</view>
		</u-form>
		<view v-if="form.cancelList">
			<view class="message">退款信息</view>
			<view class="list-item">
				<view><text>姓名：</text><text>{{form.cancelList.cusName}}</text></view>
				<view><text>开户行：</text><text>{{form.array[form.cancelList.bankType]}}</text></view>
				<view><text>开户行支行：</text><text>{{form.cancelList.branchName}}</text></view>
				<view><text>银行卡号：</text><text>{{form.cancelList.bankCardId}}</text></view>
				<view class="change">
					<view @click="next()">去修改</view>
				</view>

			</view>
		</view>

		<button class="submit" @click="next()" v-if="form.isShow">下一步</button>
		<button class="submit" @click="toPage()" v-else>注销</button>
		<view>
			<!-- 原因 -->
			<u-select v-model="show" mode="single-column" :list="list" @confirm="confirm"></u-select>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import {
		reactive,
		ref
	} from "vue";
	import {
		onLoad,
		onShow,
		onUnload,
		onReady,
	} from "@dcloudio/uni-app";
	import {
		navTo,
		checkStr,
		msg, strReplace, uploadFile
	} from "@/utils/utils";
	import {
		request
	} from "@/utils/network/request.js";
	import {
		writeCardBack,
		CardSignCancellation,
		confirmSignCancellation, etcCarOcrCard
	} from "@/utils/network/api.js";
	import {
		stringToJson
	} from "@/utils/network/encryption.js";
	const bluetoothUtil = require("../../../static/etcUtil/index.js");
	import { getItem } from "@/utils/storage";
	// 表单数据
	const form = reactive({
		userType: "",
		obuId: '',
		cardId: '',
		orderId: '',
		ReasonCancellation: "",
		equipmentState: '',
		licenseFrontUrl: "", //行驶证正面
		licenseReverseUrl: "",//行驶证反面
		customerIdnum: "",
		mobile: "",
		isShow: true,//储值卡要去下一步填写信息，记账卡直接注销
		cancelList: "",
		array: ['贵阳银行股份有限公司', '中国工商银行股份有限公司', '中国建设银行股份有限公司', '中国银行股份有限公司', '中国农业银行股份有限公司', '中国交通银行股份有限公司',
			'兴业银行股份有限公司', '平安银行股份有限公司', '中国邮政储蓄银行股份有限公司', '中国光大银行股份有限公司', '贵州银行股份有限公司', '中国民生银行股份有限公司',
			'招商银行股份有限公司', '.农村信用社联合社', '农村商业银行'],
	});
	const list = reactive([]);

	// 验证提示类型(toast要版本为1.3.5才支持)
	const errorType = ["border-bottom", "toast"];

	// 设置验证规则
	const myForm = ref(null);

	onReady(() => {

	});
	onShow(() => {
		uni.$once('list', (query) => {
			console.log("111", query)
			form.cancelList = query.item
			form.isShow = false
		})
	})
	onLoad((option) => {
		let options = JSON.parse(decodeURIComponent(option.params))
		form.obuId = options.obuId;
		form.cardId = options.cardId;
		// form.cardId = "52011640220209453221";
		form.orderId = options.orderId
		form.equipmentState = options.equipmentState
		form.mobile = options.mobile
		form.customerIdnum = options.customerIdnum
		form.userType = options.userType
		if (form.cardId) {
			if (form.cardId.substring(8, 10) == "22") {
				form.isShow = true
			} else {
				form.isShow = false
			}
		}

		console.log("options.orderId", options, getItem('key')['LOGOFF'], "52011328220201990787".substring(8, 10))
		// 获取注销原因
		for (var k = 0; k < getItem('key')['LOGOFF'].length; k++) {
			var obj = {};
			obj['value'] = getItem('key')['LOGOFF'][k]['name']
			obj['label'] = getItem('key')['LOGOFF'][k]['name']
			list.push(obj)
		}
		console.log("list", list)
		/*监听蓝牙回调*/
		uni.$on('bluetoothLink', function (status) {
			queryCardSignCancellation(1, 1, 1).then((item : any) => {
				console.log("item========", item)
				instructAction(item).then(value => {
					console.log('orderStatus=======', value.orderStatus)

				})
			})
		})
	});
	onUnload(() => {
		/*移除监听*/
		uni.$off('bluetoothLink')


	});
	const show = ref(false);

	const showPicker = function () {
		show.value = true;

	};
	// 选择物流
	const confirm = (e) => {
		console.log(e);
		form.ReasonCancellation = e[0].value;
	};

	const toPage = () => {
		//参数说明
		let options = {};
		if (form.equipmentState === '1') {
			navTo("/pages/bluetooth/bluetooth?routeType=5");

		} else {
			queryCardSignCancellation(2, 2, 0).then((item : any) => {
				navTo("/subpackage/after-sale/ETC-log-off/log-off-result");
			})
		}
	};

	/*指令返回*/
	const instructAction = (data) => {

		let cmdArr = data.command.split(",");
		console.log("cmdArr=========", cmdArr);
		bluetoothUtil.transCmd(cmdArr, "10", function (res) {
			console.log("透传");
			console.log(res);
			var arraylenth = res.length;
			var status = res[arraylenth - 1].substring(
				res[arraylenth - 1].length - 4,
				res[arraylenth - 1].length
			);
			console.log("打印指令状态");
			console.log(status);
			var formes = {
				command: data.command,
				response: res.toString(),
				cosRecordId: data.cosRecordId,
				cardId: form.cardId,
				cosType: 9 //1- 开卡. 2- 修改卡内信息  4- 卡签绑定  9- 销卡
			};
			const options = {
				type: 2,
				data: formes,
				method: "POST",
				showLoading: true,
			};
			return new Promise(async (resolve, reject) => {
				const res = await request(writeCardBack, options);
				const data = stringToJson(res.bizContent);
				console.log('data--------------', data)
				if (status === "9000") {
					if (data.orderStatus === 1) {
						instructAction(data)
					} else {
						navTo("/subpackage/after-sale/ETC-log-off/log-off-result");
					}
					resolve(data);
				} else {
					reject("指令执行失败！");
				}
			}).catch((error) => {
				console.log('指令执行失败', error)
				reject(error);
			});
		});
	};

	//注销申请
	const queryCardSignCancellation = (hasCard, hasObu, cosProvider) => {
		if (!form.licenseFrontUrl) {
			msg("请上传行驶证的主页");
			return;
		}
		if (!form.licenseReverseUrl) {
			msg("请上传行驶证的副页");
			return;
		}
		if (!form.ReasonCancellation) {
			msg("请选择注销原因");
			return;
		}
		if (form.orderId) {
			var options = {
				type: 2, //type: 2,JSON格式提交数据(默认表单形式提交)
				data: {
					hasCard: hasCard,
					hasObu: hasObu,
					cosProvider: cosProvider,
					cardId: form.cardId, //卡号
					obuId: form.obuId, //签号
					serviceType: 3, //注销类型 1-卡注销，2-签注销，3-卡签注销
					orderId: form.orderId, //订单编号
					signReason: form.ReasonCancellation,
					licenseFrontUrl: form.licenseFrontUrl,
					licenseReverseUrl: form.licenseReverseUrl,
				}, //请求参数
				method: "POST", //提交方式(默认POST)
				showLoading: true, //是否显示加载中(默认显示)
			};
		} else {
			var options = {
				type: 2, //type: 2,JSON格式提交数据(默认表单形式提交)
				data: {
					hasCard: hasCard,
					hasObu: hasObu,
					cosProvider: cosProvider,
					cardId: form.cardId, //卡号
					obuId: form.obuId, //签号
					serviceType: 3, //注销类型 1-卡注销，2-签注销，3-卡签注销
					signReason: form.ReasonCancellation,
					licenseFrontUrl: form.licenseFrontUrl,
					licenseReverseUrl: form.licenseReverseUrl,
				}, //请求参数
				method: "POST", //提交方式(默认POST)
				showLoading: true, //是否显示加载中(默认显示)
			};
		}
		console.log("options", options)
		return new Promise(async (resolve, reject) => {
			const res = await request(CardSignCancellation, options);
			const data = stringToJson(res.bizContent);
			console.log("注销申请此接口可能有问题", data)
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
	//行驶证图片上传  val 1 正面 2 反面
	const cardFileImageUpdate = (val) => {
		uni.chooseImage({
			count: 1, //只能选取一张照片
			sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ["camera", "album"], //从相册选择
			success: function (res) {
				uploadFile(res.tempFilePaths[0], val, etcCarOcrCard).then((data) => {
					console.log("data", data)
					if (val == 1) {
						form.licenseFrontUrl = data.imageUrl;
					} else {
						form.licenseReverseUrl = data.imageUrl;

					}
				})
			},
		});
	};
	const next = () => {
		if (!form.licenseFrontUrl) {
			msg("请上传行驶证的主页");
			return;
		}
		if (!form.licenseReverseUrl) {
			msg("请上传行驶证的副页");
			return;
		}
		if (!form.ReasonCancellation) {
			msg("请选择注销原因");
			return;
		}
		navTo(`/subpackage/after-sale/ETC-log-off/refund-information-list?mobile=${form.mobile}&&customerIdnum=${form.customerIdnum}&&userType=${form.userType}`);
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		background-color: #EEF7F7;
	}
</style>
<style lang="scss" scoped>
	.upload-box {
		width: 92%;
		margin: 30rpx auto;

		.head-tips {
			text-indent: 2rem;
			font-size: 26rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #666666;
			line-height: 50rpx;
			margin-bottom: 45rpx;

			text {
				color: #ff8000;
			}
		}

		.upload-card {
			margin-bottom: 70rpx;
			height: 260rpx;
			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;

			.card-left {
				.item1 {
					font-size: 34rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #000000;
					margin-bottom: 30rpx;
				}

				.item2 {
					font-size: 24rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #999999;
					margin-bottom: 30rpx;
				}

				.item3 {
					width: 110rpx;
					height: 40rpx;
					background: #ecfbf8;
					border-radius: 6rpx;
					font-size: 20rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					text-align: center;
					line-height: 40rpx;
					color: #0a8f8a;
				}
			}

			image {
				width: 295rpx;
				height: 188rpx;
			}
		}

		.from {
			background-color: #fff;
			margin-top: 30rpx;
			border-radius: 20rpx;
			padding: 10rpx 20rpx;
			box-sizing: border-box;

			::v-deep .u-form-item {
				padding: 0;
				line-height: normal;

				.u-form-item__message {
					margin-bottom: 12rpx;
				}
			}

			.title {
				font-size: 30rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #000000;
				margin: 38rpx 0;
			}

			.from_item {
				display: flex;
				flex-wrap: nowrap;
				justify-content: space-between;
				align-items: center;
				height: 80rpx;
				border-bottom: 1rpx solid #dcdcdc;

				::v-deep .input {
					text-align: right;
					flex: 1;

					input {
						text-align: right;
					}
				}
			}

			.from_item1 {
				display: flex;
				flex-wrap: nowrap;
				flex-direction: column;
				justify-content: space-between;
				padding: 30rpx;
				border-bottom: #dcdcdc 1px solid;

				input {
					text-align: right;
				}

				.textarea {
					background-color: #f1f1f1;
					width: 100%;
					border-radius: 20rpx;
					margin-top: 10rpx;
					text-indent: 1rem;
					height: 180rpx;
					padding: 20rpx;
					box-sizing: border-box;
				}
			}
		}

		.submit {
			height: 80rpx;
			background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
			border-radius: 40rpx;
			font-size: 32rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #ffffff;
			line-height: 80rpx;
			margin: 60rpx auto;
		}
	}

	.picture-wrapper {
		width: 92%;
		margin: 0 auto;
		margin-top: 40rpx;

		.bg {
			background: #ffffff;
			box-shadow: 0rpx 4rpx 13rpx 3rpx rgba(223, 223, 223, 0.8);
			border-radius: 20rpx;
			padding: 40rpx;
			display: flex;
			justify-content: space-between;

			.name {
				font-size: 34rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #000000;
				line-height: 34rpx;
			}

			.value {
				margin-top: 20rpx;
				font-size: 24rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #999999;
				line-height: 24rpx;
			}

			.tip {
				margin-top: 20rpx;
				text-align: center;
				width: 110rpx;
				height: 40rpx;
				background: rgba(33, 190, 177, 0.2);
				border-radius: 6rpx;

				.tip-value {
					font-size: 20rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #0a8f8a;
					line-height: 40rpx;
					opacity: 1;
				}
			}
		}

		.icon {
			width: 294rpx;
			height: 188rpx;
		}
	}

	.list-item {
		width: 100%;
		border-radius: 10rpx;
		margin: 0 auto;
		font-size: 28rpx;
		border: 1rpx solid #ccc;
		padding: 12rpx;
		box-sizing: border-box;
		background-color: white;
		margin-bottom: 28rpx;
	}

	.message {
		font-size: 32rpx;
		margin: 40rpx 0 20rpx 0;
		font-weight: bold;
	}

	.change {
		display: flex;
		text-align: center;
		justify-content: flex-end;
		width: 100%;
	}

	.change>view {
		height: 60rpx;
		background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
		border-radius: 40rpx;
		font-size: 32rpx;
		font-family: Microsoft YaHei;
		color: #ffffff;
		line-height: 60rpx;
		width: 25%;
	}
</style>