 <template>
 	<view class="content">
 		<view class="tabs">
 			<u-tabs :list="tabList" @click="change" lineWidth="30" lineColor="#02A7F0" :activeStyle="{
					color: '#02A7F0', 
					fontWeight: 'bold',
					transform:'scale(1.05)'
				}" :inactiveStyle="{
					color: '#000000',
					transform:'scale(1)'}" itemStyle="height: 50px; width: 33.3%;padding:0;border-bottom:1px solid #a0a0a0"></u-tabs>

 		</view>
 		<u-skeleton :animate="true" rows="17" title :loading='loading_skeleton'>
 			<scroll-view scroll-y="true" :style="'height:'+wh+'px;'" @scrolltolower="scrolltolower">
 				<u-swipe-action btn-width="500">
 					<u-swipe-action-item :options="options1" @click="click(item.id)"
 						v-for="(item,index) in productList" :key="index">
 						<view class="list-items">
 							<view class="list-item" @click="toText(item)">
 								<view class="left">
 									<!-- 左 -->
 									<view class="icon">
 										<u-icon v-if="item.type!==0" name="lock-opened-fill" color="#3c9cff"
 											size="25"></u-icon>
 										<u-icon v-else name="eye-fill" color="#3c9cff" size="25"></u-icon>
 									</view>
 									<!-- 中 -->
 									<view class="msg">
 										<view class="id">
 											{{item.deviceId}}
 										</view>
 										<view class="type">
 											<text style="margin-right: 5px;">{{item.testTime|date}}</text>
 											<text>信号值：{{item.signalNum}}</text>
 										</view>
 									</view>

 								</view>
 								<!-- 后 -->
 								<view class="button" @click.stop="textBtn(item)">
 									<u-button v-if="item.type==0" type="primary" size="small">风鸣</u-button>
 									<u-button v-else type="primary" size="small">开门</u-button>
 								</view>
 							</view>

 						</view>

 					</u-swipe-action-item>
 					<!-- 	<view v-else class="noData" style="text-align: center;">
						无数据
					</view> -->
 				</u-swipe-action>
 			</scroll-view>
 		</u-skeleton>


 		<view class="btn">
 			<u-button size="large" type="primary" @click="search"> 点击进行蓝牙设备搜索</u-button>
 		</view>
 		<view class="scan" @click="scan">
 			<img src="@/static/scan.png" alt="" style="width: 100rpx;height: 80rpx;">
 		</view>

 	</view>
 </template>
 <script>
 	export default {

 		data() {
 			return {
 				loading_skeleton: false,
 				gatewayFlag: 0,

 				options1: [{
 					text: '删除',
 					style: {
 						backgroundColor: '#f56c6c'
 					}
 				}],
 				wh: '',
 				active: 0,
 				tabList: [{
 						name: '未检测',
 						badge: {
 							value: null,
 						}
 					}, {
 						name: '检测失败',
 						badge: {
 							value: null,
 						}
 					}, {
 						name: '检测成功',
 						badge: {
 							value: null,
 						}
 					},

 				],
 				parameter: {
 					testProjectId: null,
 					pageSize: 1000,
 					pageNo: 1,
 					status:2


 				},
 				total: 0,
 				productList: [

 				]
 			}
 		},


 		methods: {
 			scrolltolower() {
 				// console.log('触底了');
 				if (this.parameter.pageNo * this.parameter.pageSize >= this.total) {
 					return uni.$showMsg('数据加载完毕')

 				}
 				this.parameter.pageNo++
 				this.getBluetoothList()
 			},
 			toText(item) {
 				console.log(item);
 				uni.showModal({
 					title: '温馨提示',
 					content: "点击确定进入测试阶段",
 					success: res => {
 						if (res.confirm) {
 							// this.show1 = false
 							// this.$u.toast(`点击了确定`)
 							let currentSite = JSON.stringify(item)
 							// console.log(item);
 							if (item.type === 0) {
 								console.log('猫眼');
 								console.log(item);
 								uni.navigateTo({
 									url: "/pages/cat-eye-test/cat-eye-test?item=" + currentSite
 								})
 							} else if (item.type == 1 && item.subType == 0 || item.subType == null) {
 								uni.navigateTo({
 									url: "/pages/lock-test/lock-test?item=" + currentSite

 								})
 							} else {
 								uni.navigateTo({
 									url: "/pages/door-lock-module/door-lock-module?item=" +
 										currentSite
 								})
 							}

 						} else {
 							// this.$u.toast(`点击了取消`)
 						}
 					}
 				})

 			},
 			async textBtn(item) {
 				uni.showLoading({
 					mask: true,
 					title: '命令执行中',
 				})
 				if (item.type === 0) {
 					console.log('我是猫眼');

 					const {
 						data: res
 					} = await uni.$http.post('/api/v1/test/bluetooth/testCam', {
 						deviceId: item.deviceId
 					})
 					if (res.code === 200) {
 						uni.$showMsg('风鸣测试成功')
 					} else {
 						uni.$showMsg('测试失败')
 					}

 				} else {
 					const {
 						data: res
 					} = await uni.$http.post('/api/v1/test/bluetooth/openDoor', {
 						deviceId: item.deviceId
 					})
 					if (res.code === 200) {
 						this.$u.toast(`开门测试成功`)
 					} else {
 						uni.$showMsg('测试失败')
 					}
 				}
 			},
 			change(index) {

 				// this.current = index;
 				// console.log(index.index);
 				let current = index.index
 				if (current == 0) {
 					this.productList = []
 					this.parameter.pageNo = 1
 					this.parameter.status = 2
 					// delete this.parameter.status
 					this.getBluetoothList()
 					console.log('待检查');

 				} else if (current == 1) {
 					this.productList = []
 					this.parameter.pageNo = 1
 					this.parameter.status = 0
 					this.getBluetoothList()

 				} else {
 					this.productList = []
 					this.parameter.pageNo = 1
 					this.parameter.status = 1
 					this.getBluetoothList()

 				}



 			},

 			async scan() {
 				uni.showLoading({
 					title: "正在获取蓝牙设备",
 					icon: 'none'
 				})
 				console.log('扫一扫');
 				let arr = []
 				const {
 					data: res
 				} = await uni.$http.post('/api/v1/test/testResult/queryByCondition', {
 					testProjectId: uni.getStorageSync("testProjectId"),
 					pageSize: 100,
 					pageNo: 1,
 					status: 2
 				}, )
 				uni.hideLoading()
 				let that = this
 				if (res.code == 200) {
 					arr = res.data.records
 					console.log(arr);
 					uni.scanCode({
 						scanType: ['barCode', 'qrCode'],
 						success: function(res) {
 							console.log('条码类型：' + res.scanType);
 							console.log('条码内容：' + res.result);
 							console.log(arr, 'arr的数据');
 							let arr1 = arr.find((item) => {
 								return item.deviceId == res.result.toLowerCase()
 							})
 							console.log(arr1, 'arr1的msg');
 							let scanDeviced = JSON.stringify(arr1)
 							if (!arr1) {
 								return that.$u.toast(`未获取到设备信息`)
 							} else {
 								if (arr1.type == 0) {
 									console.log('我是猫眼');
 									uni.navigateTo({
 										url: "/pages/cat-eye-test/cat-eye-test?item=" +
 											scanDeviced
 									})
 								} else if (arr1.type == 1 && arr1.subType == 0 || arr1.subType ==
 									null) {
 									console.log('我是门锁');
 									uni.navigateTo({
 										url: "/pages/lock-test/lock-test?item=" + scanDeviced
 									})
 								} else {
 									console.log('我是模块');
 									uni.navigateTo({
 										url: "/pages/door-lock-module/door-lock-module?item=" +
 											scanDeviced
 									})
 								}
 							}
 							// 		if (arr1) {

 							// 			uni.showToast({
 							// 				icon: "error",
 							// 				title: "未找到该设备"
 							// 			})

 							// 		} else {

 							// 			if (arr1.type == 0) {
 							// 				console.log(2);
 							// 				uni.navigateTo({
 							// 					url: "/pages/cat-eye-test/cat-eye-test?item=" +
 							// 						scanDeviced
 							// 				})
 							// 			} else if (arr1.type == 1 && arr1.subType == 0 || arr1.subType ==
 							// 				null) {

 							// 				uni.navigateTo({
 							// 					url: "/pages/lock-test/lock-test?item=" + scanDeviced
 							// 				})
 							// 			} else {
 							// 				uni.navigateTo({
 							// 					url: "/pages/door-lock-module/door-lock-module?item=" +
 							// 						scanDeviced
 							// 				})
 							// 			}
 							// 		}
 						}
 					});
 				} else {
 					this.$u.toast(`请重新获取蓝牙设备`)
 				}
 			},
 			search() {
 				this.searchBluetooth().then(() => {
 					this.parameter.pageNo = 1
 					this.productList = []
 					this.getBluetoothList()
 				})
 				// console.log('点击了搜索');
 				// this.productList = []
 				// this.parameter.pageNo = 1
 				// this.getBluetoothList()

 			},
 			// 右滑删除
 			click(index) {
 				console.log('click', index);
 				uni.showModal({
 					title: '温馨提示',
 					content: '确定要删除吗？',
 					success: async res => {
 						if (res.confirm) {
 							// this.show1 = false
 							// this.$u.toast(`点击了确定`)
 							uni.showLoading({
 								mask: true,
 								title: '正在删除中'
 							})

 							let {
 								data: res
 							} = await uni.$http.delete(`/api/v1/test/testResult/${index}`)
 							console.log(res);
 							if (res.code == 200) {
 								this.productList = []
 								this.getBluetoothList().then(() => {
 									this.getNum()
 								})


 							} else {
 								this.$u.toast(`删除失败`)
 							}
 						} else {
 							// this.$u.toast(`点击了取消`)
 						}
 					}
 				})
 			},
 			async getBluetoothList() {
 				if (!uni.getStorageSync('testProjectId')) {
 					return this.$u.toast(`未查询到测试项目，请前往后台添加测试项目`)
 				}
 				this.loading_skeleton = true
 				console.log(11);
 				this.gatewayFlag = 0
 				// uni.showLoading({
 				// 	mask: true,
 				// 	title: '数据加载中'
 				// })
 				const {
 					data: res
 				} = await uni.$http.post('/api/v1/test/testResult/queryByCondition', this.parameter)
 				uni.hideLoading()
 				console.log(res);
 				if (res.code === 200) {
 					// this.getNum()
 					// let badge1 = res.data.records.filter((item) => {
 					// 	return item.status = 2
 					// })
 					// console.log(badge1);
 					// this.total = res.data.total
 					// if (this.parameter.status == 2) {
 					// 	this.tabList[0].badge.value = res.data.total
 					// } else if (this.parameter.status == 0) {
 					// 	this.tabList[1].badge.value = res.data.total
 					// } else if (this.parameter.status == 1) {
 					// 	this.tabList[2].badge.value = res.data.total
 					// }

 					uni.$showMsg('数据加载成功')
 					this.loading_skeleton = false
 					this.productList = []
 					this.productList = [
 						...this.productList,
 						...res.data.records
 					]
 				} else {
 					// uni.$showMsg('数据加载失败')
 					console.log('数据加载失败');
 					this.loading_skeleton = false
 				}

 				// console.log(this.productList);

 			},
 			async searchBluetooth() {

 				this.productList = []
 				uni.showLoading({
 					mask: true,
 					title: "网关扫描中",
 					icon: 'none'
 				})
 				console.log(this.parameter.testProjectId);
 				const {
 					data: res
 				} = await uni.$http.post('/api/v1/test/testProject/bluetooth/search', {
 					testProjectId: this.parameter.testProjectId
 				})
 				if (res.code == 200) {
 					this.gatewayFlag = 1
 					uni.hideLoading()
 					// this.$u.toast(`网关扫描成功`)
 				} else {
 					this.$u.toast(`网关失败，点击按钮重新扫描`)
 				}
 				console.log(res);
 			},
 			async getNum() {
 				if (!uni.getStorageSync('testProjectId')) {
 					return this.$u.toast(`未查询到测试项目，请前往后台添加测试项目`)
 				}
 				const {
 					data: res
 				} = await uni.$http.post('/api/v1/test/testResult/queryByCondition', {
 					testProjectId: uni.getStorageSync("testProjectId"),
 					pageSize: 100,
 					pageNo: 1

 				}, )
 				this.gatewayFlag = 1
 				let badge1 = res.data.records.filter((item) => {
 					return item.status == 2
 				})

 				console.log(res.data.total, 'sea')
 				let badge2 = res.data.records.filter((item) => {
 					return item.status == 1
 				})

 				let badge3 = res.data.records.filter((item) => {
 					return item.status == 0
 				})
 				this.tabList[0].badge.value = badge1.length
 				this.tabList[1].badge.value = badge3.length
 				this.tabList[2].badge.value = badge2.length

 			}

 		},
 		onShow() {
 			this.parameter.testProjectId = uni.getStorageSync('testProjectId')
 			this.getNum().then(() => {
 				if (this.gatewayFlag == 1) {
 					this.getBluetoothList()
 				}

 			})
 		},
 		onLoad() {

 			let btn = uni.createSelectorQuery().in(this).select('.btn')
 			btn.boundingClientRect(data => {
 				let btnH = data.height
 				// console.log(this.searchH)
 				console.log(sysInfo.windowHeight - btnH - 50);
 				this.wh = sysInfo.windowHeight - btnH - 50
 				// console.log(this.wh);
 			}).exec()
 			const sysInfo = uni.getSystemInfoSync()
 			console.log(this.parameter.testProjectId);
 			// this.getNum()
 			// this.searchBluetooth().then(() => {
 			// 	if (this.gatewayFlag = 1) {
 			// 		this.getBluetoothList()
 			// 	}
 			// })


 		},

 	}
 </script>
 <style lang="scss">
 	// .tabs {
 	// 	height: 100rpx;
 	// 	display: flex;
 	// 	justify-content: space-around;
 	// 	align-items: center;
 	// 	border-bottom: 1px solid #a0a0a0;

 	// 	// background-color: red;
 	// 	.tab-item {
 	// 		border: 3px solid transparent;
 	// 		padding-bottom: 10rpx;
 	// 		// background-color: red;



 	// 		&.active {
 	// 			border-bottom: 3px solid #24acf2;
 	// 			color: #1296db;

 	// 		}
 	// 	}

 	// }
 	.tabs {
 		.u-tabs__wrapper__nav__line {
 			display: none;
 			// margin-left: -40rpx;
 		}
 	}

 	.btn {
 		position: fixed;
 		bottom: 0;
 		width: 100%;


 	}

 	.scan {
 		z-index: 9999;
 		position: fixed;
 		width: 120rpx;
 		height: 120rpx;
 		background-color: #3c9cff;
 		bottom: 200rpx;
 		right: 20vw;
 		border-radius: 50%;
 		display: flex;
 		justify-content: center;
 		align-items: center;


 	}

 	.u-page__tag-item {
 		margin-right: 40px;
 		margin-top: 10px;
 	}

 	.u-demo-block__content {
 		flex-direction: row;
 		flex-wrap: wrap;
 		align-items: center;
 	}

 	.list-item {
 		width: calc(100vw - 20px);
 		// margin: 28rpx 10px;
 		// padding-bottom: 10rpx;
 		padding: 28rpx 10px;
 		display: flex;
 		justify-content: space-between;
 		align-items: center;
 		border-bottom: 1px solid #a0a0a0;

 		.left {
 			display: flex;
 			justify-content: center;
 			align-items: center;

 			.id {
 				font-size: 14px
 			}

 			.type {
 				text {
 					margin-right: 5rpx;
 					color: #a0a0a0;
 					font-size: 12px
 				}
 			}

 			.icon {
 				margin-right: 10rpx;
 			}

 		}
 	}
 </style>