<template>
	<view class="maincontent contentBg">
		<uni-nav-bar :border="false" @clickLeft="prevPage" fixed="true" statusBar="true" background-color="#121212"
			color="#FFFFFF" :title="Status.navbar.title">
			<template v-slot:left>
				<view>
					<uni-icons type="left" size="24" color="#FFFFFF"></uni-icons>
				</view>
			</template>
			<block slot="right">
				<view class="navbarRight center">
					<image @click.stop="handleRightClick(index,item)" v-for="item,index in Status.navbar.icons"
						class="img" :src="item.src" mode="aspectFit"></image>
				</view>

			</block>
		</uni-nav-bar>
		<view class="tab-bar-wrap" v-show="!Status.Search">
			<scroll-view class="tab-bar" scroll-x="true" scroll-with-animation>
				<view class="tab-container">
					<view v-for="(tab, index) in tabs" :key="index"
						:class="['tab-item', activeTab === index ? 'active' : '']" @click="switchTab(tab,index)">
						{{tab.typeName}}
					</view>
				</view>
			</scroll-view>
			<view class="tab-more" @click="Status.Search=!Status.Search">
				<image src="/static/images/common/search.png" mode="aspectFit" class="more"></image>
			</view>
		</view>
		<view class="searchRow" v-show="Status.Search">
			<view class="ipt">
				<uni-easyinput class="txteare" maxlength="30" v-model="filter.txt" :trim="true" placeholder="搜索设备"
					type="text" :inputBorder="false" :border="false"></uni-easyinput>
			</view>

			<view class="btn" @click.stop="Status.Search=false">取消</view>
			<view class="clear"></view>
		</view>
		<view class="listContent">
			<mescroll-uni class="device-list" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption"
				:down="downOption" :fixed="false">
				<view class="listItem center" v-for="item,index in deviceList">
					<view class="opt center">
						<view class="checkbox center" @click.stop="checkToggle(item,index)"
							:class="{active:checks.find((v,i)=>{return v.id===item.id})}">
							<uni-icons class="icon" type="checkmarkempty" size="17" color="#000000"></uni-icons>
						</view>
					</view>
					<view class="main">
						<view class="device-header">
							<view class="deviceIMG" @click.stop="imgView(item,index)">
								<image :src="item.devicePic" class="IMG" mode="aspectFit"></image>
							</view>
							<view class="device-name">
								<view>设备:{{item.deviceName}}</view>

								<view class="ID">
									ID:{{item.deviceImei?item.deviceImei:item.deviceMac}}
								</view>


								<view class="ID">

									<view class="center ">
										<view :class="{'onlines':item.onlineStatus==1,'offlines':item.onlineStatus==0}">
											{{item.onlineStatus==1?'在线':'离线'}}
										</view>
										<view class="splitLine"></view>

										<view>电量：{{item.battery || '0'}}%</view>
									</view>

									<view @click.stop="Disc(item,index)" class="disc center"
										:class="{active:item.isDisc}">
										<image src="/static/images/common/disc.png" class="discImg" mode="aspectFit">
										</image>
										<text>识别</text>
									</view>
								</view>
							</view>
						</view>
						<view class="device-callpolice"
							v-if="item.communicationMode==0 && item.onlineStatus==1 && item.alarmStatus==1">
							报警中</view>
						<view v-if="item.communicationMode==1 || item.communicationMode==2">
							<view class="device-status" :class="item.bleStatu?'online':'unline'">
								{{item.bleStatu?'已连接':'未连接'}}
							</view>
						</view>
					</view>

				</view>
			</mescroll-uni>
		</view>

		<view :class="{active:checks.length>0}" class="btnOK" @click.stop="OkCheck()">确定</view>
		<global-loading ref="loading" />

		<MsgBox ref="msgPop" />


	</view>
</template>

<script>
	var timeout = null;
	var these = null;
	var eventChannel = null;
	var ble = null;
	var recei = null;
	var pagePath = "pages/common/linkDefence/checkDevice";
	import request, {
		baseURL
	} from '@/utils/request.js';
	import {
		showLoading,
		hideLoading,
		updateLoading
	} from '@/utils/loading.js';
	import {
		MsgSuccess,
		MsgError,
		MsgClose,
		MsgWarning,
		showPop,
		MsgInfo,
		MsgClear,
		MsgPrompt
	} from '@/utils/MsgPops.js';
	import {
		deviceInfo,
		deviceTypeList
	} from '@/api/common/index.js'
	import Common from '@/utils/Common.js';
	import bleTool from '@/utils/BleHelper.js';
	import BleReceive from '@/utils/BleReceive';


	export default {
		data() {
			return {
				filter: {
					txt: ''
				},
				Status: {
					navbar: {
						icons: [{
							src: '/static/images/common/add.png',
							type: 'add'
						}],
						title: '选择设备',
						showBack: true,
						height: 90,

					},
					type: null,
					Search: false

				},

				mescroll: null,
				downOption: {
					auto: false,
					autoShowLoading: false,
				},
				upOption: {
					auto: false,
					noMoreSize: 0,
					offset: 10,
					isLock: false,
					empty: {
						tip: '暂无数据',
						hideScroll: false,
						icon: '/static/images/common/empty.png'
					},
					textNoMore: '没有更多数据了'
				},
				deviceList: [],
				checks: [],
				tabs: [],
				activeTabInfo: '',
				activeTab: 0,
				formData: {
					id: '',
					name: '',
					main: [], //通知设备
					sub: [], //预警设备
					notify: [] //发送设备
				}
			}
		},
		created() {
			this.Status.navbar.height = uni.getSystemInfoSync().statusBarHeight + 44;
		},
		onLoad() {
			this.getTab();
			these = this;
			this.$watch("filter.txt",(newVal,oldVal)=>{
				if(this.mescroll){
					this.mescroll.triggerDownScroll();	
				}				
			});
			eventChannel = this.getOpenerEventChannel();
			eventChannel.on('detailData', (res) => {
				let data = res.data;
				console.log("收到上级页面传参", data);
				this.Status.type = data.type;
				let keys = Object.keys(data.formData);
				for (let i = 0; i < keys.length; i++) {
					let key = keys[i];
					this.formData[key] = data.formData[key];
				}
				if (data.type == 'main') {
					this.checks = data.main;
					this.Status.navbar.title = "选择预警主机";
				} else if (data.type == 'sub') {
					this.checks = data.sub;
					this.Status.navbar.title = "选择布防联机";
				} else if (data.type == 'notify') {
					this.checks = data.notify;
					this.Status.navbar.title = "选择接收设备";
					
				}
				setTimeout(() => {
					if (this.mescroll) {
						this.mescroll.triggerDownScroll();
					}
				}, 0);

			});


			ble = bleTool.getBleTool();

			recei = BleReceive.getBleReceive();
			//蓝牙连接成功的回调
			ble.addRecoveryCallback((res) => {
				// console.log("蓝牙连接成功的回调");
				this.bleRecovery(res);
			}, pagePath);

			//蓝牙断开连接的回调
			ble.addDisposeCallback((res) => {
				console.log("蓝牙断开连接的回调");
				this.bleBreak(res);
			}, pagePath);

			//蓝牙适配器恢复可用的回调，一般是重连设备
			ble.addStateRecoveryCallback(res => {
				// console.log("蓝牙适配器恢复可用的回调");
				this.bleStateRecovery();
			}, pagePath);

			//蓝牙适配器不可用的回调
			ble.addStateBreakCallback(res => {
				// console.error("蓝牙适配器不可用的回调");
				this.bleStateBreak();
			}, pagePath);

			//接收到消息的回调
			ble.addReceiveCallback((receive, device, path, recArr) => {
				// console.error("首页收到消息了");
				let json = recei.ReceiveData(receive, device, path, recArr);
				// console.error("消息内容",json);
				this.updateBleStatu();
			}, pagePath);
			// eventChannel.emit('checkOver', {type:'',arr:[]});//
		},
		methods: {
			imgView(item, index) {
				if (!item.devicePic) {
					return;
				}

				uni.previewImage({
					urls: [item.devicePic]
				});
			},
			checkToggle(item, index) {

				let f = this.checks.find((v, i) => {
					if (v.id === item.id) {
						this.checks.splice(i, 1);
						return true;
					}
					return false;
				});
				if (!f) {
					this.checks.push({
						id: item.id,
						deviceName: item.deviceName,
						deviceImei: item.deviceImei,
						deviceMac: item.deviceMac,
						devicePic: item.devicePic
					});
				}

				console.log("checks=", this.checks);
			},
			bleStateBreak() {
				console.error("蓝牙适配器不可用");
				this.updateBleStatu();
			},
			bleStateRecovery() {
				if (this.isPageShow) {
					console.log("蓝牙适配器恢复可用,重连断开的设备");
					ble.linkAllDevices();
				}
			},
			bleBreak(res) {
				console.error("蓝牙断开连接", res);
				if (res.deviceId) {
					this.updateBleStatu(res.deviceId);
				}
			},
			bleRecovery(res) {
				// console.log("蓝牙连接成功", res);
				if (res.deviceId) {
					this.updateBleStatu(res.deviceId);
				}
			},

			updateBleStatu() { //更新列表的蓝牙连接状态,电池 电量
				if (ble) {

					for (var i = 0; i < this.deviceList.length; i++) {


						let f = null;
						if (ble.data && ble.data.LinkedList) {
							f = ble.data.LinkedList.find(v => {

								if (v.macAddress && v.device && v.device.id) {
									return v.device.id == this.deviceList[i].id;
								}
								return false;
							});
						}
						if (f) {

							this.$set(this.deviceList[i], 'bleStatu', f.Linked);
							if (f.formData) {
								let battary = 0;
								if ('battary' in f.formData) {
									battary = f.formData.battary;
								} else if ('sta_PowerPercent' in f.formData) {
									battary = f.formData.sta_PowerPercent;
								} else if ('sta_battery' in f.formData) {
									battary = f.formData.sta_battery;
								}
								this.$set(this.deviceList[i], 'battery', battary);
							}


						}



					}
					return;
				}
				console.error("ble is null")
			},
			getTab() {
				deviceTypeList({}).then((res) => {
					if (res.code == 200) {
						//console.log("deviceTypeList=" + JSON.stringify(res.data));
						this.tabs = [{
								id: '',
								name: '全部设备',
								typeName: '全部设备'
							},
							...res.data.map(item => ({
								id: item.id,
								name: item.typeName,
								typeName: item.typeName,
								communicationMode: item.communicationMode
							}))
						];
					}
				})
			},

			isChecked(item, index) {
				let f = this.checks.find((v, i) => {
					return v.id == item.id;
				});
				if (f) {
					return true;
				}
				return false;
			},
			OkCheck() {
				if (this.checks.length == 0) {
					return;
				}
				eventChannel.emit('checkOver', {
					type: this.Status.type,
					arr: this.checks
				});
				uni.navigateBack();
			},
			onClick(btn, i, item, index) {
				if (btn.type == 'rename') {
					console.log("执行重命名");
					MsgPrompt(this, btn.type, () => {
						console.log("确定被点击");
					}, false, true);
					return;
				}

				if (btn.type == 'drop') {
					console.log("执行删除");
					MsgError('删除后不可恢复,您确认删除"' + item + '"吗?', '确定', this, () => {
						this.deviceList.splice(index, 1);
					}, true);
					return;
				}
			},
			change(event) {
				console.log('改变事件', event);
			},
			swipeChange(e, index) {
				console.log('当前状态：' + e + '，下标：' + index)
			},
			gotoDetail(item, index) {
				console.log("item=", item);
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			// 下拉刷新
			downCallback() {
				if (this.mescroll) {
					this.mescroll.resetUpScroll(false);
					this.mescroll.scrollTo(0, 0);
				}
				this.getData();
			},
			// 上拉加载 
			upCallback() {
				this.getData();
			},
			getData() {
				var task = () => {

					return new Promise((resolve, reject) => {
						if (!this.Status.type) {
							this.mescroll.endSuccess(0, false);
							resolve();
							return;
						}
						let data = {
							pageNum: this.mescroll.num,
							pageSize: 10,
							deviceType: this.activeTabInfo ? this.activeTabInfo.id : '', // 使用传入的设备类型
							isAsc: 'desc',
							orderByColumn: 'bindingTime',
							deviceName:this.filter.txt
						}
						if (!data.pageNum) {
							this.mescroll.endSuccess(0, false);
							resolve();
							return;
						}

						let method = deviceInfo;
						if (this.Status.type == 'notify') {
							method = () => {
								let rows=this.formData.main;
								rows=rows.concat(this.formData.sub);
								rows=rows.filter(v=>{
									return v.deviceName.toLowerCase().indexOf(this.filter.txt.toLowerCase())>-1;
								})
								let json = {
									code: 200,
									rows: rows
								};							
								
								return Promise.resolve(json);
							}
						}
						method(data).then((res) => {
							if (res.code == 200) {

								const newDevices = res.rows.map(device => ({
									...device,
									showConfirm: false
								}));

								// 如果是第一页或切换分类，替换数据
								if (data.pageNum === 1) {
									this.deviceList = newDevices
								} else {
									//防止后端返回的数据包含已有数据
									for (var i = 0; i < newDevices.length; i++) {
										let device = newDevices[i];
										let f = this.deviceList.find(v => {
											return v.id === device.id;
										});
										if (!f) {
											this.deviceList.push(device);
										}
									}
								}

								this.updateBleStatu();


								// 判断是否加载完成
								let hasNext = true;
								if (res.rows.length < data.pageSize || this.deviceList.length >= this
									.total) {
									hasNext = false;
								} else {
									hasNext = true;
								}
								this.mescroll.endSuccess(res.rows.length, hasNext);
							} else {
								this.mescroll.endSuccess(0, false);
							}
						}).finally(() => {
							resolve();
						});
					});
				}
				clearTimeout(timeout);
				//防止下拉刷新的同时会调用一次上拉加载的问题
				timeout = setTimeout(task, 50);

			},
			prevPage() {
				uni.navigateBack({

				});
			},
			handleRightClick(s, e) {

				if (s === 0) {

				} else if (s === 1) {

				}
			},
		}
	}
</script>

<style>
	.maincontent {
		height: 100vh;
	}

	.listContent {
		height: calc(100% - 270rpx);
		transform: translateY(20rpx);
	}

	.listItem {
		width: 100%;
		min-height: 140rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
	}

	.swipBtn {
		display: flex;
		flex-direction: column;
		flex-wrap: wrap;
		align-content: center;
		justify-content: center;
		align-items: center;
	}

	.uni-swipe .icon,
	/deep/ .uni-swipe .icon {
		width: 35rpx;
		height: 35rpx;
	}

	.uni-swipe .txt,
	/deep/ .uni-swipe .txt {
		color: rgba(255, 255, 255, 0.87);

		font-family: "PingFang SC";
		font-style: Regular;
		font-size: 24rpx;
		font-weight: 400;

		letter-spacing: 0.14rpx;

	}

	.uni-swipe,
	/deep/ .uni-swipe {
		margin-bottom: 20rpx;
		border-radius: 16rpx;
		overflow: hidden;
	}

	.tab-bar-wrap {
		display: flex;
		align-items: center;
		position: relative;
		margin-top: 90rpx;
		height: 66rpx;
	}

	.tab-bar {
		width: 100%;
		color: rgb(255, 255, 255);
		white-space: nowrap;
		/* 禁止换行 */
		overflow: hidden;
		position: relative;

	}

	.tab-container {
		display: flex;
		cursor: pointer;
		align-items: center;
		padding-right: 80rpx;
		/* 预留更多按钮空间 */
	}

	.tab-item {
		font-size: 28rpx;
		padding: 0 30rpx;
		text-align: center;
		position: relative;
	}

	.tab-item.active {
		color: #bbe600;

		height: 66rpx;
		font-weight: bold;
	}

	.tab-item.active::before {
		content: "";
		background-color: #bbe600;
		position: absolute;
		top: 90%;
		left: 35%;
		width: 30%;
		height: 6rpx;
		border-radius: 6rpx;
	}

	.tab-more {
		margin-left: 10rpx;
		/* 与Tab的间距 */
		display: flex;
		align-items: center;
		background: linear-gradient(-88.60deg, rgba(18, 18, 18, 1), rgba(18, 18, 18, 0) 100%);
		height: 66rpx;
	}

	.more {
		width: 30rpx;
		height: 30rpx;
		margin-top: 5rpx;
	}

	.listItem .opt {
		width: 70rpx;
		height: 210rpx;

	}

	.listItem .opt .checkbox {
		width: 35rpx;
		height: 35rpx;
		box-sizing: border-box;
		border: 1px solid rgba(255, 255, 255, 0.4);
		border-radius: 8rpx;
		background-color: #00000000;
	}

	.listItem .opt .checkbox .icon {
		display: none;
	}

	.listItem .opt .checkbox.active {
		background-color: #AED600 !important;
	}

	.listItem .opt .checkbox.active .icon {
		display: block;
	}

	.listItem .main {
		width: calc(100% - 70rpx);
		height: 210rpx;
	}

	.device-header {
		display: flex;
		align-items: center;
		margin-bottom: 15rpx;
		padding: 30rpx 0 10rpx 30rpx;
		background: rgba(26, 26, 26, 1);
	}

	.device-name {
		font-size: 32rpx;
		color: rgba(255, 255, 255, 0.87);
		margin-left: 24rpx;
		line-height: 50rpx;
		width: 75%;
		white-space: nowrap;
	}

	.ID {
		color: rgba(255, 255, 255, 0.6);
		font-size: 26rpx;
		display: flex;
		justify-content: space-between;
		position: relative;

	}

	.deviceIMG {
		width: 100rpx;
		height: 100rpx;
		border-radius: 16rpx;
		position: relative;
		background-color: rgba(42, 42, 42, 0.6);
		display: flex;
		align-items: center;
	}

	.p10 {
		padding-left: 20rpx;
	}

	.splitLine {
		background: linear-gradient(180deg, rgba(0, 0, 0, 0), rgba(255, 255, 255, 1) 44.525%, rgba(255, 254.75, 254.75, 0) 92%);
		opacity: 0.2;
		width: 4rpx;
		height: 25rpx;
		margin: 0rpx 20rpx;
	}

	.disc {
		padding: 5rpx 10rpx;
		margin-right: -30rpx;
	}

	.disc.active {
		background-color: #2a2a2a !important;
		;
	}

	.discImg {
		width: 24rpx;
		height: 24rpx;
		margin-right: 10rpx;
	}

	.IMG {
		width: 68rpx;
		height: 50rpx;
		margin-left: 17%;
		object-fit: contain;
	}

	.device-callpolice {
		width: 122rpx;
		height: 52rpx;
		font-size: 24rpx;
		border-radius: 0px 8px 0px 8px;
		background-color: rgba(224, 52, 52, 1);
		position: absolute;
		top: 0rpx;
		right: -4rpx;
		text-align: center;
		line-height: 52rpx;
		color: #fff;
	}

	.device-status {
		width: 122rpx;
		height: 52rpx;
		font-size: 26rpx;
		border-radius: 0px 8px 0px 8px;
		background-color: rgb(42, 42, 42);
		position: absolute;
		top: 0rpx;
		right: 0rpx;
		text-align: center;
		line-height: 52rpx;
	}

	.online {
		color: rgb(187, 230, 0);
	}

	.unline {
		color: rgba(255, 255, 255, 0.4);
	}

	.btnOK.active {
		background: rgba(187, 230, 0, 1) !important;
		color: rgba(35, 35, 35, 1) !important;
	}

	.btnOK {
		/* 矩形 7 */
		width: 80%;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 90rpx;
		text-align: center;
		color: rgba(255, 255, 255, 0.4);
		background: rgba(26, 26, 26, 1);
		font-family: "PingFang SC";
		font-style: Bold;
		font-size: 28rpx;
		font-weight: 400;
		box-shadow: 0px 0px 2px 0px rgb(255 255 255 / 30%);
		position: fixed;
		z-index: 1;
		bottom: 50rpx;
		left: 10%;
		text-align: center;
	}

	.searchRow {
		width: 100%;
		height: 70rpx;
		margin-top: 90rpx;
		
	}

	
	.searchRow .ipt {
		width: calc(100% - 80rpx);
		height: 100%;
		float: left;
	}

	.searchRow .btn {
		width: 80rpx;
		height: 100%;
		line-height: 70rpx;
		text-align: right;
		color: rgba(255, 255, 255, 0.87);
		font-family: "PingFang SC";
		font-style: Regular;
		font-size: 28rpx;
		font-weight: 400;
		letter-spacing: 0.14rpx;
		float: left;
		
	}


	/deep/ .uni-easyinput__content,
	.uni-easyinput__content {
		background-color: #1A1A1A !important;
		border-radius: 70rpx;
		color: #FFFFFF;
		text-indent: 10rpx;
		box-sizing: border-box;
		font-size: 24rpx;
	}

	/deep/ .uni-textarea-placeholder,
	.uni-textarea-placeholder {
		color: rgba(255, 255, 255, 0.5);

		font-family: "PingFang SC";
		font-style: Regular;
		font-size: 24rpx;
		font-weight: 400;
		letter-spacing: 0.14rpx;
	}
</style>