<template>
	<view class="content">
		<view v-if="GET_REQUEST_DATA.length > 0" v-for="(item,index) in GET_REQUEST_DATA" :key="index"
			style="width: calc(96% - 40rpx);padding: 20rpx;background-color: #fff;margin: 20rpx auto;border-radius: 20rpx;display: flex;justify-content: space-between;">
			<view style="font-size: 32rpx;">当前设备名称:{{item.name}}</view>
			<view style="color: #FD4B09;font-size: 32rpx;"><text style="color: #00f700;margin-right: 10rpx;">•</text>已连接
			</view>
		</view>
		<view v-if="GET_REQUEST_DATA.length == 0"
			style="width: calc(96% - 40rpx);padding: 20rpx;background-color: #fff;margin: 20rpx auto;border-radius: 20rpx;display: flex;justify-content: space-between;">
			<view style="font-size: 32rpx;">当前设备状态</view>
			<view style="color: #c2c2c2;font-size: 32rpx;"><text
					style="color: #FD4B09;margin-right: 10rpx;">•</text>设备未连接</view>
		</view>
		<view>
			<view style="color: #c2c2c2;width: 100%;text-align: center;">已配对蓝牙设备
			</view>
			<view class="bluetoothList" v-for="(item,index) in GET_CONNECTBLEDATA" :key="index"
				@tap="confirm_bluetooth(item)">
				<view>设备名称:{{item.name}}</view>
				<view style="color: #FD4B09;">连接</view>
			</view>
			<!-- <button @click="senBleLabel">打印</button> -->
			<view class="bluetoothItem" v-if="GET_INFODATA">
				<view style="width: calc(96% - 40rpx);padding: 20rpx;background-color: #fff;margin: 20rpx auto;border-radius: 20rpx;display: flex;justify-content: space-between;margin-bottom: -40rpx;border-bottom-left-radius: 0;border-bottom-right-radius: 0;padding-bottom: 20rpx;">
					搜索到的蓝牙设备
					<view>
						<u-button class="custom-style" color="#fff" type="primary" iconColor="#FD4B09" :hairline="false" size="small" icon="search" @click="search_bluetooth" text="搜索蓝牙"></u-button>
					</view>
				</view>
				<view class="bluetoothList" v-for="(item,index) in GET_INFODATA" :key="index"
					@tap="confirm_bluetooth(item)">
				<view>设备名称:{{item.name}}</view>
				<view style="color: #FD4B09;">连接</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,
		GET_REQUEST_DATA
	} from "@/store/gettersType.js";
	import {
		SET_CONNECTBLEDATA,
		SET_REQUEST_DATA
	} from '@/store/actionsType.js';
	// #ifdef APP-PLUS
	const HanyinPlugin = uni.requireNativePlugin('Hanyin-Plugin'); //汉印
	// #endif
	let print;
	import store from 'store';
	import {
		DX
	} from '@/common/dateFormat.js';
	export default {
		data() {
			return {
				bArray: [], //用于搜索蓝牙去重用的
				no_match_list: [], //没有配对的蓝牙列表
				match_list: "", //已连接蓝牙打印机
				val: "",
				dateTimer: "",
				valArr: [],
				form: {},
				amountNumber: {},
				// 巴枪参数
				// broadcase_actions: "com.android.receive_scan_action",
				// broadcast_flag: "data"
			};
		},
		computed: {
			...mapGetters([GET_INFODATA, GET_CONNECTBLEDATA, GET_REQUEST_DATA])
		},
		onShow() {
			uni.hideLoading();
			//检查是否已连接蓝牙
			this.$check_bluetooth_connect();
			// console.log('onShow');
		},
		onHide() {
			// console.log('onHide');
		},
		onUnload() {
			// console.log('onUnload');
		},
		onLoad(options) {
			_this = this;
			_this.form = JSON.parse(options.data)
			// console.log(JSON.parse(options.data))
			_this.amountNumber = {
				deliveryAll: 0, //送货总数量
				cost: 0, //成本
				grossProfit: 0, //毛利
				delivery: 0, //送货
				saleAll: 0, //销售数量
				amount: 0
			}
			for (let item of _this.form.orderItemsList) {
				_this.amountNumber.saleAll += Number(item.storageInQuantity)
				_this.amountNumber.cost += Number(item.storageInQuantity) * Number(item.purchasePrice)
				_this.amountNumber.delivery += Number(item.actualQuantity)
				_this.amountNumber.deliveryAll += Number(item.actualQuantity)
				_this.amountNumber.amount += Number(item.amount)
				_this.amountNumber.grossProfit += Number(item.amount) - (item.purchasePrice ? Number(item
					.storageInQuantity) * Number(item.purchasePrice) : 0)
			}
			_this.form.purchaseAmount = _this.amountNumber.amount
			_this.$init_bluetooth();
		},
		methods: {
			...mapActions([SET_CONNECTBLEDATA]),
			senBleLabel() {
				// console.log(this.$Mock.order_data);
				if (!print) return;
				this.$Mock.printTemplate.pagConfig = 0
				// console.log(this.$Mock.printTemplate.pagConfig)
				let data = []
				let i = 0
				for (let item of this.form.orderItemsList) {
					data.push({
						"top": Number(i * 40 + 90),
						"left": "0",
						"textAlign": "start",
						"width": "270",
						"fontSize": 9,
						"text": item.cname,
						"type": "text",
						"fontWeight": "400",
						"height": "20"
					})
					data.push({
						"top": Number(i * 40 + 110),
						"left": "140",
						"textAlign": "start",
						"width": "40",
						"fontSize": 8,
						"text": Number(item.storageInQuantity),
						"type": "text",
						"fontWeight": "400",
						"height": "20"
					})
					data.push({
						"top": Number(i * 40 + 110),
						"left": "160",
						"textAlign": "start",
						"width": "50",
						"fontSize": 8,
						"text": item.price,
						"type": "text",
						"fontWeight": "400",
						"height": "20"
					})
					data.push({
						"top": Number(i * 40 + 110),
						"left": "210",
						"textAlign": "start",
						"width": "60",
						"fontSize": 8,
						"text": item.amount,
						"type": "text",
						"fontWeight": "400",
						"height": "20"
					})
					i++
				}
				data.push({
					"top": Number(i * 40 + 90),
					"left": "20",
					"textAlign": "start",
					"width": "270",
					"fontSize": 9,
					"text": "合计:",
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 90),
					"left": "140",
					"textAlign": "start",
					"width": "40",
					"fontSize": 8,
					"text": this.amountNumber.saleAll,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 90),
					"left": "210",
					"textAlign": "start",
					"width": "60",
					"fontSize": 8,
					"text": this.form.orderAmount,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})


				data.push({
					"top": Number(i * 40 + 110),
					"left": "20",
					"textAlign": "start",
					"width": "270",
					"fontSize": 9,
					"text": "产品合计:",
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 110),
					"left": "70",
					"textAlign": "start",
					"width": "70",
					"fontSize": 8,
					"text": this.amountNumber.amount,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 110),
					"left": "140",
					"textAlign": "start",
					"width": "60",
					"fontSize": 8,
					"text": "合计金额:",
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 110),
					"left": "190",
					"textAlign": "start",
					"width": "80",
					"fontSize": 8,
					"text": DX(this.amountNumber.amount),
					"type": "text",
					"fontWeight": "400",
					"height": "40"
				})


				data.push({
					"top": Number(i * 40 + 130),
					"left": "20",
					"textAlign": "start",
					"width": "270",
					"fontSize": 9,
					"text": "订货电话:" + this.form.clientAttn,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 150),
					"left": "20",
					"textAlign": "start",
					"width": "270",
					"fontSize": 9,
					"text": "制单人:" + this.form.client,
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				data.push({
					"top": Number(i * 40 + 150),
					"left": "140",
					"textAlign": "start",
					"width": "80",
					"fontSize": 8,
					"text": "收货人(签字):",
					"type": "text",
					"fontWeight": "400",
					"height": "20"
				})
				print.startPrint({
					SALES_SLIP: this.form.sysNo,
					CUSTOMER_NAME: this.form.corpsName,
					SALES_DATE: this.form.businesDate.slice(0, 10)
				}, {
					pageHeight: (this.form.orderItemsList.length * 40) / 3
				}, data);
			},
			// 连接打印机
			confirm_bluetooth(item) {
				let {
					name,
					mac
				} = item;
				//判断蓝牙是否打开
				this.$check_bluetooth_open().then(res => {
					// console.log(res);
					//进行打印机连接
					if (res) {
						print = new printConnect(item,0); //打印机连接
					}
				})
			},
			//搜索没匹配的蓝牙设备
			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);
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bluetoothList {
		width: calc(96% - 40rpx);
		padding: 20rpx;
		background-color: #fff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		display: flex;
		justify-content: space-between;
	}
	.custom-style {
			color: #ff0000 !important;
		}
	.bluetoothItem {
		width: 100%;
		height: 100%;
	
		.bluetoothList {
			width: calc(96% - 40rpx);
			padding: 20rpx;
			background-color: #fff;
			margin: 20rpx auto;
			border-radius: 20rpx;
			display: flex;
			justify-content: space-between;
		}
	}
</style>
