<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" v-for="item in dataList">
			<view class="cards-title">
				<view>
					<view style="font-size: 15px;"><em
							style="font-weight: bold;">{{ item.wo.showName }}：</em>{{ 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.device)" />
				</view>
			</view>
			<view class="cards-content">
				<view class="txt-list">
					<view class="list2">
						<view style="width: 60%;">
							<text>电量：</text>
							{{ dc.dataObj[item.device.deviceCode]?.adc.b || '-' }}%
						</view>
						<view style="width: 40%;">
							<text>频段：</text>{{ dataObj[item.device.deviceCode]?.lf || item.device.deviceExtendValve.rolaFrequency}}
						</view>
						<view style="width: 60%;">
							<text>软/硬件版本：</text>
							{{ dataObj[item.device.deviceCode]?.sv || item.device.softVer}} /
							{{ dataObj[item.device.deviceCode]?.dv || item.device.deviceVersion }}
						</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" @click="restart(item)">重启</u-button>
					<u-button type="primary" @click="editRolaFrequency(item)">修改频段</u-button>
					<u-button type="primary" @click="getConfig(item)">获取配置</u-button>
					<!-- <u-button type="primary" @click="" disabled>压力校准</u-button>
					<u-button type="primary" @click="" disabled>桥接</u-button>
					<u-button type="primary" @click="" disabled>删除桥接</u-button> -->
				</view>
			</view>
		</view>
	</z-paging>

	<!-- 修改频段 -->
	<custom-popup ref="refFrequency" :title="rolaFrequency.item?.deviceCode" :footer="true"
		@confirm="editRolaFrequencyHandler">
		<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 plugins from "@/plugins";
	import * as DevOpsApi from "@/api/dev-ops/index.js"

	export default {
		data() {
			return {
				station: null,
				queryParams: {
					landId: null, //地块id
					stationId: null, //基站id
				},
				dataList: [],
				dc: getApp().dc, // 设备控制公共代码
				dataObj: {},
				topics: [],
				rolaFrequency: {
					item: null,
					old: null,
					new: null,
					range: [],
				},
			}
		},
		onLoad(par) {
			if (par.station) {
				this.station = JSON.parse(par.station)
				this.queryParams = {
					landId: this.station.landId,
					stationId: this.station.id,
				}
			} else {
				this.queryParams = {
					landId: -1,
					stationId: -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_valve", function(e) {
				if (e.type == "state") {
					// that.mqttConnected = e.connected;
				} else if (e.type == "msg") {
					if (e.topic.includes("jsy/iot/deviceConfig/2/") || e.topic.includes(
							"jsy/iot/deviceConfig/13/")) {
						const arr = e.topic.split("/");
						const deviceCode = arr[4];
						let obj = that.dataObj[deviceCode] || {};
						if (e.data.type == "gvc") {
							obj = Object.assign(obj, {
								dv: e.data.data.dv,
								sv: e.data.data.sv,
								lf: e.data.data.lf,
							})
						}
						if (e.data.type == "vlf" && e.data.res == 1) {
							obj = Object.assign(obj, {
								lf: e.data.data,
							})
						}

						that.dataObj[deviceCode] = obj;
					}
				}
			});
		},
		onShow() {},
		mounted() {},
		onUnload() {
			getApp().mqttUnsubscribes(this.topics);
			getApp().off("mqtt", "mqtt_devOps_valve")
		},
		methods: {
			queryList(pageNo, pageSize) {
				DevOpsApi.getStationValvelist(this.queryParams.landId, this.queryParams.stationId).then(res => {
					this.$refs.paging.completeByTotal(res.data.valve);
					this.mqttSubscribes(res.data.valve);
				});
			},
			mqttSubscribes(rows) {
				if (rows && rows.length) {
					let topic = [];
					rows.forEach(item => {
						topic.push(
							`jsy/iot/deviceConfig/${item.device.deviceTypeId}/${item.device.deviceCode}`); //获取配置
					});
					this.topics = topic;
					getApp().mqttSubscribes(this.topics);
				}
			},
			// 重启
			restart(item) {
				plugins.modal.confirm(`重启"${item.device.deviceCode}"`).then(() => {
					DevOpsApi.resetStationDevice(item.device.deviceCode).then(res => {
						plugins.modal.msg(res.msg)
					})
				}).catch(() => {});
			},
			// 修改频段
			editRolaFrequency(item) {
				this.$refs.refFrequency.show();
				this.rolaFrequency.item = item.device;
				this.rolaFrequency.old = item.device.deviceExtendValve.rolaFrequency;
				this.rolaFrequency.new = item.device.deviceExtendValve.rolaFrequency;
			},
			// 获取配置
			getConfig(item) {
				plugins.modal.confirm("获取配置").then(() => {
					DevOpsApi.gvc(item.device.deviceCode).then(res => {
						plugins.modal.msg(res.msg)
					})
				}).catch(() => {});
			},
			// 关闭弹出层
			close() {
				for (var key in this.pop) {
					this.pop[key] = false;
				}
			},

			editRolaFrequencyHandler(e) {
				if (!e) {
					return;
				}
				// if (this.rolaFrequency.item.rolaFrequency == this.rolaFrequency.rolaFrequency) {
				// 	plugins.modal.msg("新旧频段不能相同！")
				// 	return;
				// }
				let msg = `您确定要修改【${this.rolaFrequency.item.deviceCode}】的频段为【${this.rolaFrequency.new}】?`;
				plugins.modal.confirm(msg).then(() => {
					DevOpsApi.vlf({
						stationCode: this.station.deviceCode,
						deviceCode: this.rolaFrequency.item.deviceCode,
						oldFrequency: this.rolaFrequency.old,
						newFrequency: this.rolaFrequency.new
					}).then(res => {
						plugins.modal.msg(res.msg)
					})
				}).catch(() => {
					this.pop.rolaFrequency = true;
				});
			},

		}
	}
</script>

<style lang="scss">

</style>