<template>
	<z-paging ref="paging" @query="queryList" v-model="dataList" :auto-show-system-loading="true"
		empty-view-text="抱歉，暂时还没有相关数据！">
		<template #top>
			<custom-nav-bar :left="true" title="设备运维"></custom-nav-bar>
		</template>

		<view class="card">
			<uni-collapse v-if="dataList.length">
				<uni-collapse-item title-border="none" :border="false" v-if="dataList[0]">
					<template v-slot:title>
						<view class="cards-title">
							<view>
								<view class="text">基站</view>
							</view>
						</view>
					</template>
					<view class="content">
						<view class="cards-content" v-for="item in dataList[0]">
							<view class="card card-grey">
								<view class="cards-title">
									<view>
										<view>{{ item.deviceCode}}</view>
										<uni-tag class="bg-green" :circle="true" text="在线"
											v-if="dc.dataObj[item.deviceCode]?.online" />
										<uni-tag class="bg-red" :circle="true" text="离线" v-else />
									</view>
									<view class="right">
										<u-button type="success" size="mini" text="刷新"
											:loading="dc.dataObj[item.deviceCode]?.refresh"
											@click="dc.refreshDeviceItem(item.deviceCode, item)" />
									</view>
								</view>
								<view class="cards-content">
									<view class="txt-list">
										<view class="list2">
											<view style="width: 70%;">
												<text>软/硬件版本：</text>
												{{ dataObj[item.deviceCode]?.sv || item.softVer}}
												/
												{{ dataObj[item.deviceCode]?.dv || item.deviceVersion}}
											</view>
											<view style="width: 30%;">
												<text>频段：</text>{{ dataObj[item.deviceCode]?.lf || item.rolaFrequency}}
											</view>
										</view>
									</view>
									<view class="btn-grid">
										<u-button type="primary" size="normal" text="更换基站" @click="changeStation(item)"
											disabled />
										<u-button type="primary" size="normal" text="下发列表"
											v-hasPermi="['iot:dev-ops:gvl']" @click="sendTopoList(item)" />
										<u-button type="primary" size="normal" text="修改频段"
											v-hasPermi="['iot:dev-ops:slf']" @click="SLF(item)" />
										<u-button type="primary" size="normal" text="获取配置"
											v-hasPermi="['iot:dev-ops:gsc']" @click="getStationConfig(item)" />
										<u-button type="primary" size="normal" text="压力校准"
											@click="pressureCalibrationTopo(item)" disabled />
										<u-button type="primary" size="normal" text="重启" @click="stationRestart(item)"
											disabled />
										<u-button type="primary" v-hasPermi="['iot:dev-ops:valve']"
											@click="topoDevice(item)">
											子设备<u-badge type="success" max="999" :value="item.topoCount" />
										</u-button>
										<u-button type="primary" size="normal" text="基站升级"
											v-hasPermi="['iot:dev-ops:upgradeStation']" @click="upgradeStation(item)" />
										<u-button type="primary" size="normal" text="阀门升级"
											@click="upgradeTopoValve(item)" disabled />
									</view>
								</view>
							</view>
						</view>
					</view>
				</uni-collapse-item>
				<uni-collapse-item title-border="none" :border="false" v-if="dataList[1]">
					<template v-slot:title>
						<view class="cards-title">
							<view>
								<view class="text">蝶阀</view>
							</view>
						</view>
					</template>
					<view class="content"></view>
				</uni-collapse-item>
				<uni-collapse-item title-border="none" :border="false" v-if="dataList[2]">
					<template v-slot:title>
						<view class="cards-title">
							<view>
								<view class="text">4G阀门</view>
							</view>
						</view>
					</template>
					<view class="content">
						<view class="cards-content" v-for="item in dataList[2]">
							<view class="card card-grey">
								<view class="cards-title">
									<view>
										<view>{{ item.device.deviceCode}}</view>
										<uni-tag class="bg-green" :circle="true" text="在线"
											v-if="dc.dataObj[item.device.deviceCode]?.online" />
										<uni-tag class="bg-red" :circle="true" text="离线" v-else />
									</view>
									<view class="right">
										<u-button type="success" size="mini" text="刷新"
											:loading="dc.dataObj[item.device.deviceCode]?.refresh"
											@click="dc.refreshDeviceItem(item.device.deviceCode, item)" />
									</view>
								</view>
								<view class="cards-content">
									<view class="txt-list">
										<view class="list2">
											<view style="width: 60%;">
												<text>软/硬件版本：</text>
												{{ dataObj[item.device.deviceCode]?.sv || item.device.deviceCode.softVer}}
												/
												{{ dataObj[item.device.deviceCode]?.dv || item.device.deviceVersion}}
											</view>
											<view style="width: 40%;">
												<text>信号强度：</text>{{ dc.dataObj[item.device.deviceCode]?.comm.rssi || '-' }}
											</view>
											<view style="width: 60%;">
												<text>电量：</text>{{ dc.dataObj[item.device.deviceCode]?.adc.b || '-' }}%
											</view>
											<view style="width: 40%;">
												<text>压力：</text>{{ dc.dataObj[item.device.deviceCode]?.adc.p || '-' }}
											</view>
										</view>
									</view>
									<view class="btn-grid">
										<u-button type="primary" size="normal" text="获取配置"
											@click="getValve4Config(item.device)" disabled />
										<u-button type="primary" size="normal" text="重启"
											@click="restartValve4(item.device)" disabled />
										<u-button type="primary" size="normal" text="升级"
											v-if="item.device.newDeviceVersionId"
											v-hasPermi="['iot:dev-ops:upgradeValve4G']"
											@click="upgradeValve4(item.device)" />
										<u-button type="primary" size="normal" text="压力校准"
											@click="pressureCalibrationValve4(item.device)" disabled />
										<u-button type="primary" size="normal" text="压力查询"
											@click="pressureSelect(item.device)" disabled />
									</view>
								</view>
							</view>
						</view>
					</view>
				</uni-collapse-item>
			</uni-collapse>
		</view>
	</z-paging>

	<!-- 修改基站频段 -->
	<custom-popup ref="refFrequency" :title="rolaFrequency.item?.deviceCode" :footer="true"
		@confirm="loraFrequencyHandler">
		<view class="jz-info" style="width: 80vw;height:150px;padding: 10px;">
			<uni-forms ref="baseForm" style="padding-top: 10px;">
				<uni-forms-item label="原频段" required>
					<uni-data-select v-model="rolaFrequency.old" :localdata="rolaFrequency.range" :clear="false" />
				</uni-forms-item>
				<uni-forms-item label="新频段" required>
					<uni-data-select v-model="rolaFrequency.new" :localdata="rolaFrequency.range" :clear="false" />
				</uni-forms-item>
			</uni-forms>
		</view>
	</custom-popup>
</template>
<script>
	import * as DevOpsApi from "@/api/dev-ops/index.js"

	export default {
		data() {
			return {
				landId: null,
				dataList: [],
				dc: getApp().dc, // 设备控制公共代码
				dataObj: {},
				topics: [],
				rolaFrequency: {
					item: null,
					old: null,
					new: null,
					range: [],
				},
			}
		},
		onLoad(par) {
			if (getApp().ijs.land) {
				this.landId = getApp().ijs.land.id;
			} else {
				this.landId = -1;
			}

			// 频段
			let list = [];
			for (let i = 410; i < 491; i++) {
				let lfObj = {
					value: i,
					text: i.toString()
				}
				list.push(lfObj);
			}
			this.rolaFrequency.range = list;
			const that = this;
			getApp().on("mqtt", "mqtt_devOps_station", function(e) {
				if (e.type == "state") {
					// that.mqttConnected = e.connected;
				} else if (e.type == "msg") {
					if (e.topic.includes("jsy/iot/deviceConfig/1/")) {
						const arr = e.topic.split("/");
						const deviceCode = arr[4];
						let obj = that.dataObj[deviceCode] || {};
						if (e.data.type == "gsc") {
							obj = Object.assign(obj, {
								dv: e.data.data.dv,
								sv: e.data.data.sv,
								lf: e.data.data.lf,
							})
						}
						if (e.data.type == "lf") {
							obj = Object.assign(obj, {
								lf: e.data.data,
							})
						}
						that.dataObj[deviceCode] = obj;
					}
				}
			});
		},
		onShow() {},
		mounted() {},
		onUnload() {
			getApp().mqttUnsubscribes(this.topics);
			getApp().off("mqtt", "mqtt_devOps_station")
		},
		methods: {
			queryList(pageNo, pageSize) {
				DevOpsApi.getList(this.landId).then(res => {
					// console.error("res:", res);
					let station = [],
						butterflyValve = [],
						valve = [];
					if (res.data.station) {
						station = res.data.station;
					}
					if (res.data.butterflyValve) {
						butterflyValve = res.data.butterflyValve;
					}
					if (res.data.valve) {
						valve = res.data.valve;
					}
					let data = [];
					if (station.length == 0 && butterflyValve.length == 0 && valve.length == 0) {
						data = [];
					} else {
						data = [station, butterflyValve, valve];
					}
					this.$refs.paging.completeByTotal(data);
					this.mqttSubscribes(data);
				});
			},
			mqttSubscribes(rows) {
				if (rows && rows.length) {
					let topics = [];
					rows.forEach((x, index) => {
						let topic = "jsy/iot/deviceConfig";
						x.forEach(item => {
							if (index == 0) {
								topics.push(`${topic}/${item.deviceTypeId}/${item.deviceCode}`);
							} else {
								topics.push(
									`${topic}/${item.device.deviceTypeId}/${item.device.deviceCode}`);
							}
						});
					});
					this.topics = topics;
					getApp().mqttSubscribes(this.topics);
				}
			},
			/************************************************** 基站 **************************************************/
			// 更换基站
			changeStation(item) {
				console.error("更换基站:", item);
			},
			// 下发列表
			sendTopoList(item) {
				this.$showConfirm("下发列表").then(res => {
					if (res.confirm) {
						DevOpsApi.gvl(item.deviceCode).then(res => {
							this.$toast(res.msg)
						})
					}
				})
			},
			// 修改频段
			SLF(item) {
				this.$refs.refFrequency.show();
				this.rolaFrequency.item = item;
				this.rolaFrequency.old = item.rolaFrequency;
				this.rolaFrequency.new = item.rolaFrequency;
			},
			// 获取配置
			getStationConfig(item) {
				this.$showConfirm("获取配置").then(res => {
					if (res.confirm) {
						DevOpsApi.gsc(item.deviceCode).then(res => {
							this.$toast(res.msg)
						})
					}
				})
			},
			// 压力校准
			pressureCalibrationTopo(item) {
				console.error("压力校准:", item);
			},
			// 重启
			restartStation(item) {
				console.error("重启:", item);
			},
			// 子设备
			topoDevice(item) {
				uni.navigateTo({
					url: '/pages/dev-ops/valve?station=' + JSON.stringify(item)
				});
			},
			// 升级
			upgradeStation(item) {
				this.$showConfirm(`升级"${item.deviceCode}"到"${item.newVersionName}"版本？`).then(res => {
					if (res.confirm) {
						DevOpsApi.upgradeStation(item.deviceCode, item.newDeviceVersionId).then(res => {
							this.$toast(res.msg)
						})
					}
				})
			},
			// 设备升级
			upgradeTopoValve(item) {
				console.error("设备升级:", item);
			},


			/************************************************** 基站接口 **************************************************/
			loraFrequencyHandler(e) {
				if (!e) {
					return;
				}
				// if (this.rolaFrequency.item.rolaFrequency == this.rolaFrequency.rolaFrequency) {
				// 	this.$toast("新旧频段不能相同！")
				// 	return;
				// }
				let msg = `您确定要修改【${this.rolaFrequency.item.deviceCode }】的频段为【${this.rolaFrequency.new}】?`;
				this.$showConfirm(msg).then(res => {
					if (res.confirm) {
						DevOpsApi.slf({
							deviceCode: this.rolaFrequency.item.deviceCode,
							oldFrequency: this.rolaFrequency.old,
							newFrequency: this.rolaFrequency.new
						}).then(res => {
							this.$toast(res.msg)
						})
					} else {
						this.pop.rolaFrequency = true;
					}
				})
			},

			/************************************************** 4G阀门 **************************************************/
			// 获取配置
			getValve4Config(item) {
				console.error("获取配置:", item);
			},
			// 重启
			restartValve4(item) {
				console.error("重启:", item);
			},
			// 设备升级
			upgradeValve4(item) {
				console.error("设备升级:", item);
				this.$showConfirm(`升级"${item.deviceCode}"到"${item.newVersionName}"版本？`).then(res => {
					if (res.confirm) {
						DevOpsApi.upgradeValve4G(item.deviceCode, item.newDeviceVersionId).then(res => {
							this.$toast(res.msg)
						})
					}
				})
			},
			// 压力校准
			pressureCalibrationValve4(item) {
				console.error("压力校准:", item);
			},
			// 压力查询
			pressureSelect(item) {
				console.error("压力查询:", item);
			},



			/************************************************** 公共 **************************************************/
			// 关闭弹出层
			close() {
				for (var key in this.pop) {
					this.pop[key] = false;
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.card.card-grey {
		margin: 10px 0 0;
	}
</style>