<template>
	<div>
		<image class="img" :src="`${imgPath}static/userui/scan/back.png`" />
		<div class="divBox">
			<div class="divScan">
				<div class="divScanTitle">请扫码车身的二维码进行解锁</div>
				<div class="box">
					<div class="corner-final tl"></div>
					<div class="corner-final tr"></div>
					<div class="corner-final bl"></div>
					<div class="corner-final br"></div>
					<camera mode="scanCode" device-position="back" style="width: 100%; height: 100%;"
						@scancode="scanCode" :flash="openLight" :initdone="(isInitDone = true)">

					</camera>
				</div>
				<div v-if="showWorn" style="text-align: center;margin-top: 10px;">
					<label style="background-color: #61D246;border-radius: 15px;padding: 5px 10px;color: white;">
						<uni-icons type="checkbox-filled" size="16" color="white" />
						扫码成功
					</label>
				</div>
			</div>
			<div class="divBottom">
				<div class="divBtn" @click="openCode">
					<image :src="`${imgPath}static/userui/scan/code.png`" />
					<div>输入车辆编号</div>
				</div>
				<div class="divBtn" @click="clickLight">
					<image :src="`${imgPath}static/userui/scan/light.png`" />
					<div>{{openLight=="off"?"打开":"关闭"}}手电筒</div>
				</div>
			</div>
		</div>
	</div>

	<!--  -->
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		showModelMessage,
		getUrlParams,jkcBaseDecode
	} from "@/utils/tools.js";

	import {
		callOrdereApi
	} from "@/utils/api.js";
	import config from '@/utils/config';
	import {
		addOrder
	} from "@/utils/usermap.js";

	const imgPath = config.imgPath;

	let bikeCode = null;
	const openLight = ref("off");
	const showWorn = ref(false);
	const oUser = JSON.parse(jkcBaseDecode(uni.getStorageSync("wechat_user")));
	let type = null;
	let errdata=null;

	onLoad((options) => {
		type = options["type"];
	});

	function openCode() {
		uni.navigateTo({
			url: "/pages/user/scan/inputcode"
		})
	}

	function scanCode(res) {
		const {
			result
		} = res.detail;
		if (!result || result.indexOf("number") == -1) {
			if(errdata==result) return;
			errdata=result;
			showModelMessage("无效的车辆二维码");
			return;
		}
		if (bikeCode) return;
		const options = getUrlParams(result);
		bikeCode = options["number"];
		if (type == "ride") {
			addOrder(bikeCode, (res) => {
				console.log("00000000000000000000", res);
				if (!res) {
					setTimeout(() => {
						bikeCode = null;
					}, 1000);
					return;
				} else {
					showWorn.value = true;
				}
			})
		}
	}


	function clickLight() {
		openLight.value = openLight.value === "off" ? "on" : "off";
		uni.vibrateShort();
	}
</script>

<style scoped>
	@import url("scancode.css");
</style>