<template>
	<view class="container">

		<Ba-ScanViewS ref="scanViewS" style="height: 530;width: 100%;" :load="loadData" @onScan="onScan">
		</Ba-ScanViewS>
		<view
			style="position: absolute;bottom: 150px; left: 0;right: 0;display: flex; justify-content: center;align-items: center;">
			<text style="background-color: white;width: 200px;text-align: center;" v-if="result!==''">{{result}}</text>
		</view>
		<view class="content">
			<view class="btn" @click="goDetailPage"><text style="color: white;">修改信息</text></view>
			<view class="btn" @click="handleSave"><text style="color: white;text-align: center;">默认保存</text></view>
		</view>
		<view class="content">
			<!-- 	<view class="btn" @click="openScanLight">打开闪光灯</view>
			<view class="btn" @click="closeScanLight">关闭闪光灯</view>
			<view class="btn" @click="isLightOn">是否打开闪光灯</view> -->
			<!-- <view class="btn" @click="openAlbumPage">相册识别</view> -->

			<!-- <view class="msg" v-for="(item,index) in msgList">{{item}}</view>  -->
			<!-- 	<view class="btn" @click="goDetailPage"><text style="color: white;">修改信息</text></view>
			<view class="btn" @click="handleSave"><text style="color: white;">默认保存</text></view> -->
			<view class="btn" @click="goDetailPage1"
				style="width: 200px; display: flex; justify-content: center; align-items: center;"><text
					style="color: white;">退出扫描</text></view>
			<!-- 		<view class="btn" @click="takePhoto">拍照（和预览一样）</view>
			<view class="btn" @click="takePhoto2">拍照（全图）</view> -->
		</view>
	</view>
</template>

<script>
	import {
		openSqlite,
		executeSql,
		closedb,
		getTable,
		isTable,
		getAllField,
		insertAll,
		addSql,
		getPageList,
		selectList,
		deleteSql,
		updateSql,
		selectSql,
		batchUpdate
	} from "@/utils/database";
	export default {
		data() {
			return {
				loadData: { //配置
					scanTimeSpace: 1500, //时间间隔
					//isShowToast: true,
					// rpCancelBtnText: 'cancel',
					hintText: '',
					// isDirScanMulti: true
				},
				msgList: [],
				result: '',
				detailData: {}
			}
		},

		onLoad(options) {
			const params = JSON.parse(options.params);
			this.detailData = params
			// 使用 params 中的值
			console.log(params); // 输出 'John'
			// console.log(params.age);  // 输出 25
		},
		onBackPress(options) {
			if (options.from === 'navigateBack') {
				return false; // 表示不拦截系统默认行为
			} else {
				// 自定义返回逻辑
				uni.$emit('receiveData', this.detailData.publicId);
				uni.navigateBack({
					delta: 1
				});
				return true; // 表示拦截系统默认行为
			}
		},
		methods: {
			goDetailPage() {
				let self = this
				uni.navigateBack({
					success: function() {
						// 传递数据给上一页
						uni.$emit('data', self.result);
					}
				});
			},
			goDetailPage1() {
				let self = this
				uni.navigateBack({
					success: function() {
						// 传递数据给上一页
						uni.$emit('data1', self.detailData.publicId);
					}
				});
			},
			onScan(e) { //扫码监听
				console.log("onScan：" + e.detail.result);
				// uni.showToast({
				// 	title: e.detail.result,
				// 	icon: 'none'
				// })
				this.msgList.unshift(JSON.stringify(e.detail))
				this.result = e.detail.result
				this.closeScan()
			},
			openScanLight() { //打开闪光灯
				this.$refs.scanViewS.openScanLight();
			},
			closeScanLight() { //关闭闪光灯
				this.$refs.scanViewS.closeScanLight();
			},
			isLightOn() { //是否打开闪光灯
				this.$refs.scanViewS.isLightOn(res => {
					uni.showToast({
						title: 'isLightOn：' + res.isLightOn,
						icon: 'none'
					})
				});
			},
			openScan() { //打开扫描
				this.$refs.scanViewS.openScan();
			},
			closeScan() { //关闭扫描
				this.$refs.scanViewS.closeScan();
			},
			isScanOpen() { //是否打开扫描
				this.$refs.scanViewS.isScanOpen(res => {
					uni.showToast({
						title: 'isScanOpen：' + res.isScanOpen,
						icon: 'none'
					})
				});
			},
			takePhoto() { //拍照
				this.$refs.scanViewS.takePhoto(res => {
					uni.showToast({
						title: 'path：' + res.path,
						icon: 'none'
					})
				});
			},
			takePhoto2() { //拍照
				this.$refs.scanViewS.takePhoto(res => {
					uni.showToast({
						title: 'path：' + res.path,
						icon: 'none'
					})
				}, {
					isFull: true
				});
			},
			openAlbumPage() { //相册识别
				this.$refs.scanViewS.openAlbumPage();
			},
			async handleSave() {
				if (this.result.length === 0) {
					uni.showToast({
						title: "请扫码",
						icon: "none",
						success: function() {

						}
					})
					return
				}
				// this.$refs['uForm1'].validate().then(async () => {
				console.log('aaaaa', this.detailData.fileSavePath)
				// 使用 map 方法提取需要的属性值
				const urls = this.detailData.fileSavePath.map(item => item.url);
				console.log(urls)
				// 使用 join 方法将剩余元素以逗号拼接为字符串
				const fileSavePath = urls.join(',');
				console.log(fileSavePath); // 输出 "Alice, Charlie"
				try {
					// detailData: {
					// 	barCode: '',
					// 	bpQuantity: '',
					// 	gyQuantity: '',
					// 	myQuantity: '',
					// 	note: "",
					// 	fileList: [],
					// },
					let b = await addSql('detail', {
						publicId: this.detailData.publicId,
						barCode: this.result,
						bpQuantity: this.detailData.bpQuantity || 1,
						gyQuantity: this.detailData.gyQuantity || 0,
						myQuantity: this.detailData.myQuantity || 0,
						note: this.detailData.note,
						fileSavePath: fileSavePath
					})
					const self = this
					uni.showToast({
						title: "添加成功",
						icon: "none",
						success: function() {
							self.result = ''
							self.detailData.note = ''
							self.openScan()
							// self.getData(self.baseData.uid)
							// self.detailData.barCode = ''
							// self.detailData.bpQuantity = ''
							// self.detailData.gyQuantity = ''
							// self.detailData.myQuantity = ''
							// self.detailData.fileSavePath = []
							// self.pointblur()
							// // showToast 弹窗关闭后执行跳转
							// self.switchTab(1)
						}
					});
				} catch (e) {
					console.error("添加数据，报错", e)
				}
				// }).catch(errors => {
				// 	console.log(errors)
				// 	uni.$u.toast(errors[0].message)
				// })
			},
		}
	}
</script>

<style lang="scss">
	.container {
		//padding: 30rpx;
		display: flex;
		flex-direction: column;
		flex: 1;
		// flex-direction: column;
		// justify-content: center;


		.btn {
			margin: 20rpx;
			padding: 20rpx;
			// text-align: center;
			background-color: #253a6f;
			// color: red;
			border-radius: 50rpx;
			text-align: center;
		}

		.dataResult {
			display: flex;
			flex-direction: column;
			padding: 20rpx;
			color: #555;
			word-wrap: break-word;
		}
	}

	.content {
		// position: absolute;
		bottom: 300rpx;
		// padding: 30rpx;
		// 
		// display: flex;
		//  left: 50%;
		// transform: translateX(50%);
		flex-direction: row;
		justify-content: center;
		align-items: center;
		// margin:  auto;
	}

	.form-row {
		padding-top: 16rpx;
		padding-bottom: 16rpx;
		padding-left: 16rpx;
		padding-right: 16rpx;
		display: flex;
		flex-direction: row;
		align-items: center;

		.form-key {
			display: inline-block;
			text-align: justify;
			text-justify: distribute-all-lines;
			text-align-last: justify;
			color: #2f2f2f;
			font-size: 28rpx;
			font-weight: 600;
		}

		.form-value {
			padding-left: 16rpx;

			.radio {
				padding: 10rpx 40rpx;
			}
		}
	}

	.msg {
		display: flex;
		word-break: break-all;
	}
</style>