<template>
	<view class="content">
		<view class="head">
			<view class="header-wrap">
				<view class="index-header">
					<uni-icons class="fanhui" custom-prefix="iconfont" type="icon-fanhui"
					@click="leftClick"></uni-icons>
					<view class="input-wrap">
							<text class="iconfont">组盘</text>
					</view>
					<view class="map-wrap"></view>
				</view>
			</view>
			<view class="blank"></view>
		</view>
		<view>
			<view class="bluetoothConnected">
				<view class="bluetoothList" v-for="(item,index) in GET_CONNECTBLEDATA" :key="index" @tap="confirm_bluetooth(item)">
					<view class="bluetoothList-name">名称:{{item.name}}</view>
					<view class="bluetoothList-mac">地址:{{item.mac}}</view>
				</view>
			</view>
			<button type="primary" plain="true" @click="search_bluetooth">搜索蓝牙</button>
			<button type="primary" plain="true" @click="closeBT()">断开连接</button>
			<button type="primary" plain="true" @click="handlePrint()">测试打印</button>
			<view class="bluetoothItem" v-if="GET_INFODATA">
				<view class="bluetoothList" v-for="(item,index) in GET_INFODATA" :key="index" @tap="confirm_bluetooth(item)">
					<view class="bluetoothList-name">名称:{{item.name}}</view>
					<view class="bluetoothList-mac">地址:{{item.mac}}</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	// import printConnect from "@/common/print.js"; //引入打印机模板文件 
	let _this = null;
	import {
		mapGetters,
		mapActions
	} from 'vuex';
	import {
		GET_INFODATA,
		GET_CONNECTBLEDATA
	} from "@/store/gettersType.js";
	import {
		SET_CONNECTBLEDATA
	} from '@/store/actionsType.js';
	// #ifdef APP-PLUS
	const modal = uni.requireNativePlugin('modal');
	// const HPRT = uni.requireNativePlugin('DCloud-RichAlert');
	const printModule = uni.requireNativePlugin('PrintModuleCPCL');
	// #endif
	let print;
	export default {
		data() {
			return {
				title: '',
				localtion: '',
				isStart: true,
				bArray: [], //用于搜索蓝牙去重用的
				no_match_list: [], //没有配对的蓝牙列表
				match_list: "", //已连接蓝牙打印机
				val: "",
				dateTimer: "",
				valArr: [],
				url: '',
				item: {
					name: "",
					mac: "",
				},
				
			}
		},
		computed: {
			...mapGetters([GET_INFODATA, GET_CONNECTBLEDATA])
		},
		onLoad(options) {
			_this = this;
			this.$init_bluetooth();
		},
		methods: {
				leftClick: function() {
					setTimeout(() => {
						uni.navigateBack();
						// uni.redirectTo({
						// 	url: '/pages/sample/group',
						// })
					}, 30);
					// this.$emit('change', this.value)
				},
			...mapActions([SET_CONNECTBLEDATA]),
			// 连接打印机
			confirm_bluetooth(item) {
				// let {
				// 	name,
				// 	mac
				// } = item;
				uni.showLoading({
					title: "连接中...",
					mask: true
				})
				let mac = item.mac;
				// console.log("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",item)
				try {
					printModule.connectionBT({
						'address': mac
					}, result => {
						const msg = JSON.stringify(result);

						let tmp = "连接成功"
						if (result.result !== 0) {
							let tmp = "连接失败" + msg
						}
						modal.toast({
							message: tmp,
							duration: 6
						});
						uni.hideLoading()
						printModule.setDisConnectBTListener((ret) => {
							modal.toast({
								message: '蓝牙断开',
								duration: 6
							});
						})
					})
				} catch (e) {
					console.log(e)
				}
			},
			//搜索没匹配的蓝牙设备
			search_bluetooth(address) {
				let _this = this;
				//检查蓝牙是否开启
				this.$check_bluetooth_open().then(ores => {
					if (ores) {
						console.log(ores);
						//搜索蓝牙
						_this.$search_bluetooth().then(bres => {
							console.log(bres);
							if (bres.code) {
								_this.$search_pipei().then(pres => {
									console.log(pres);
								})
							}
						})
					}
				})
			},
			handlePrint() {
				printModule.printAreaSize({
					'height': '500',
					'number': '1'
				}, result => {})
				printModule.printBarCode({
					'x_pos': '0',
					'y_pos': '20',
					'code_type': '128',
					'ratio': '1',
					'height': '250',
					'width': '4',
					'rotation': 'BARCODE',
					'undertext': true,
					'number': '4',
					'offset': '5',
					"textAlign": "right",
					'code_data': '2023121717170002'
				});
				printModule.printForm()
				printModule.print()
			},
		
			closeBT() {
				printModule.closeBT();
			},
		
	
		
		}
	}
</script>

<style>
	button {
		margin-top: 30upx;
		margin-bottom: 30upx;
	}

	.button-sp-area {
		margin: 0 auto;
		width: 60%;
	}

	.content {
		text-align: center;
		height: 400upx;
	}

	.wrapper {
		flex-direction: column;
		justify-content: center;
	}

	.button {
		width: 200px;
		margin-top: 30px;
		margin-left: 20px;
		padding-top: 20px;
		padding-bottom: 20px;
		border-width: 2px;
		border-style: solid;
		border-color: #458B00;
		background-color: #458B00;
	}

	.text {
		font-size: 30px;
		color: #666666;
		text-align: center;
	}
</style>

<style lang="scss">
	.bluetoothItem {
		width: 100%;
		height: 100%;
	
		.bluetoothList {
			display: flex;
			flex-direction: column;
			padding: 20rpx;
			border-bottom: 1rpx solid #BEBEBE;
			font-size: 28rpx;
		}
	}
</style>

<style lang="scss">
	$color-base: #0039a6;
	$words-color-base: #333333;
	$words-color-light: #999999;

	.header-wrap {
		width: 100%;
		position: fixed;
		top: 0;
		z-index: 999;

		.index-header {
			height: 88upx;
			line-height: 88upx;
			padding: 0 30upx;
			padding-top: 40upx;
			background-color: $color-base;
			font-Size: 28upx;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.fanhui {
				color:#fff !important;
				font-size: 28px;
				padding-top: 5px;
				font-weight: 700;
			}
			.lanya {
				color:#fff !important;
				font-size: 28px;
				padding-top: 5px;
			}
			.map-wrap {
				padding-top: 5px;
			}
		}
	}

	.blank {
		height: 126upx;
	}
</style>