<template>
	<view class="content">
		{{GET_REQUEST_DATA}}
		<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="default" @click="search_bluetooth">搜索蓝牙</button>
			<button @click="senBleLabel">打印</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,
		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:{},
				connectedPrint:{}
				// 巴枪参数
				// broadcase_actions: "com.android.receive_scan_action",
				// broadcast_flag: "data"
			};
		},
		computed: {
			...mapGetters([GET_INFODATA, GET_CONNECTBLEDATA,GET_REQUEST_DATA])
		},
		onShow() {
			// console.log(this.$store.state.connectedPrint)
			// this.connectedPrint = this.$store.state.connectedPrint
			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);
				return this.$store.dispatch(SET_REQUEST_DATA, {name:this.name,mac:this.mac});
				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"
				})
				console.log((this.form.orderItemsList.length*40)/3)
				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); //打印机连接
					}
				})
			},
			//搜索没匹配的蓝牙设备
			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">
	.bluetoothItem {
		width: 100%;
		height: 100%;

		.bluetoothList {
			display: flex;
			flex-direction: column;
			padding: 20rpx;
			border-bottom: 1rpx solid #BEBEBE;
			font-size: 18rpx;
		}
	}
</style>
