<!-- 扫一扫 -->
<template>
	<view>
		<view class="scanCode_box">
			<view class="title">请扫描设备二维码</view>
			<camera class='camera' mode="scanCode" @error="cameraError" @scancode='scancode' frame-size='large'>
				<cover-view class='animation' :animation="animation"></cover-view>
			</camera>
			<view @click="manualFun()" class="manual">手动选择设备</view>
		</view>
	</view>
</template>
<script>
	let animation = uni.createAnimation({});

	export default {
		data() {
			return {
				animation,
				form: {},
			}
		},
		onLoad(option) {
			if (option.form) {
				this.form = JSON.parse(decodeURIComponent(option.form));
			}
		},
		onShow() {
			this.donghua()
		},
		methods: {
			donghua() {
				let that = this;
				let scode = true
				setInterval(function() {
					if (scode) {
						animation.translateY(250).step({
							duration: 3000
						})
						scode = !scode;
					} else {
						animation.translateY(-10).step({
							duration: 3000
						})
						scode = !scode;
					}
					that.animation = animation.export()

				}.bind(this), 3000)
			},
			//手动选择设备
			manualFun() {
				uni.redirectTo({
					url: '/pages_safetyExamine/examineManage/examineAddContent?form=' + encodeURIComponent(JSON.stringify(this
						.form)) + '&pageType=3'
				});
			},
			scancode(e) {
				let self = this;
				let text = decodeURIComponent(e.detail.result)
				let list = text.split("?")[1].split("&");
				let codeData = {};
				list.forEach((item) => {
					codeData[item.split("=")[0]] = item.split("=")[1];
				})
				if (!codeData.type) {
					if (codeData.type != 10) {
						uni.showToast({
							title: '请扫描设备二维码',
							icon: "none",
							mask: true,
							duration: 2000
						});
					} else {
						self.code = codeData.code;
						self.type = codeData.type;
						let joinHazardIds = [];
						if (this.form.hazardIds) {
							joinHazardIds = this.form.hazardIds.split(',')
							joinHazardIds.push(self.code)
							this.form.joinHazardIds = joinHazardIds
						} else {
							this.form.joinHazardIds = self.code
						}
						uni.navigateTo({
							url: '/pages_safetyExamine/views/examineManage/examineAddTow?form=' + encodeURIComponent(JSON
								.stringify(
									this.form)) + '&joinHazardIds=' + self.code
						});
					}
				} else {
					uni.showToast({
						title: '请扫描正确的二维码',
						icon: "none",
						mask: true,
						duration: 2000
					});
				}
			}
		}

	}
</script>
<style>
	.scanCode_box {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		background-color: #000000;
		position: fixed;
		align-items: center;
	}

	.title {
		font-size: 34rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 48rpx;
		margin-top: 256rpx;
		text-align: center;
	}

	.camera {
		width: 433rpx;
		height: 434rpx;
		border-radius: 6rpx;
		margin: 30rpx;
		overflow: hidden;
	}

	.animation {
		position: absolute;
		top: 10rpx;
		left: 0rpx;
		width: 430rpx;
		height: 2rpx;
		background-color: #4CD964;
		border-radius: 50%;
	}

	.manual {
		width: 300rpx;
		height: 100rpx;
		background: #0183FA;
		border-radius: 60rpx 60rpx 60rpx 60rpx;
		font-size: 34rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 100rpx;
		text-align: center;
		position: fixed;
		bottom: 76rpx;
		left: 226rpx;
	}
</style>