<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="index-container">
			<view class="item-container" v-for="item in dataList">
				<view style="display: flex; align-items: center; justify-content: space-between;">
					<view class="flex-text" style="margin: 15px;">
						<view class="flex-text-one" style="color: black;">基站：</view>
						<view>{{ item.deviceCode}}</view>
						<view style="margin-left: 10px;">
							<u-tag text="在线" type="success" plain v-if="dc.dataObj[item.deviceCode]?.online" />
							<u-tag text="离线" type="error" plain v-else />
						</view>
					</view>
					<view style="margin-right: 5px;">
						<u-button type="success" text="刷新" icon="reload" :loading="dc.dataObj[item.deviceCode]?.refresh"
							@click="dc.refreshDeviceItem(item.deviceCode, item)" />
					</view>
				</view>
				<u-gap height="1" bgColor="#D2D2D2"></u-gap>
				<view style="display: flex;justify-content: space-between;align-items: center;">
					<uni-row class="data">
						<uni-col :span="12">
							<view class="flex-text global-font-size-1">
								<view class="flex-text-one">软/硬件版本：</view>
								<view>{{ item.softVer}} / {{ item.deviceVersion}}</view>
							</view>
						</uni-col>
						<uni-col :span="12">
							<view class="flex-text global-font-size-1">
								<view class="flex-text-one">频段：</view>
								<view>{{ item.rolaFrequency}}</view>
							</view>
						</uni-col>
						<uni-col :span="8">
							<u-button type="success" @click="changeStation(item)" disabled>更换基站</u-button>
						</uni-col>
						<uni-col :span="8">
							<u-button type="success" @click="sendList(item)">下发列表</u-button>
						</uni-col>
						<uni-col :span="8">
							<u-button type="success" @click="editRolaFrequency(item)">修改频段</u-button>
						</uni-col>
						<uni-col :span="8">
							<u-button type="success" @click="getConfig(item)" disabled>获取配置</u-button>
						</uni-col>
						<uni-col :span="8">
							<u-button type="success" @click="pressureCalibration(item)" disabled>压力校准</u-button>
						</uni-col>
						<uni-col :span="8">
							<u-button type="success" @click="restart(item)" disabled>重启</u-button>
						</uni-col>
						<uni-col :span="8">
							<u-button type="success" @click="childDevice(item)">
								子设备<u-badge type="primary" max="999" :value="item.topoCount" />
							</u-button>
						</uni-col>
						<uni-col :span="8">
							<u-button type="success" @click="valveUpdate(item)" disabled>阀门升级</u-button>
						</uni-col>
					</uni-row>
				</view>
			</view>
		</view>
	</z-paging>

	<u-popup :show="pop.rolaFrequency" mode="center" @close="close" closeable>
		<view class="jz-info" style="width: 80vw;height:200px;padding: 15px 20px;">
			<u-text :text="rolaFrequency.item.deviceCode"></u-text>
			<uni-forms ref="baseForm" style="padding-top: 10px;">
				<uni-forms-item label="原频段">
					<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>
		<!-- #ifdef H5 -->
		<uni-row style="height: 40px;">
			<uni-col :span="12" style="height: 40px;">
				<u-button type="info" plain :hairline="true" @click="close"
					style="width: 100%!important;height: 40px!important;border-radius: 0!important;">取消</u-button>
			</uni-col>
			<uni-col :span="12" style="height: 40px;">
				<u-button type="primary" plain :hairline="true" @click="editRolaFrequencyHandler"
					style="width: 100%!important;height: 40px!important;border-radius: 0!important;">确定</u-button>
			</uni-col>
		</uni-row>
		<!-- #endif -->
		<!-- #ifdef APP-PLUS -->
		<view style="height: 40px;">
			<u-button type="info" plain :hairline="true" @click="close"
				style="width: 100px!important;height: 40px!important;border: 0!important;float: right;">取消</u-button>
			<u-button type="primary" plain :hairline="true" @click="editRolaFrequencyHandler"
				style="width: 100px!important;height: 40px!important;border: 0!important;float: right;">确定</u-button>
		</view>
		<!-- #endif -->
	</u-popup>
</template>
<script>
	import * as DevOpsApi from "@/api/dev-ops/index.js"

	export default {
		data() {
			return {
				landId: null,
				dataList: [],
				dc: getApp().dc, // 设备控制公共代码
				pop: {
					rolaFrequency: false,
				},
				rolaFrequency: {
					item: null,
					old: null,
					new: null,
					range: [],
				},
			}
		},
		onLoad(par) {
			const cacheData = uni.getStorageSync("cacheData");
			if (cacheData) {
				this.landId = cacheData.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;
		},
		onShow() {},
		mounted() {},
		methods: {
			queryList(pageNo, pageSize) {
				DevOpsApi.getStationList(this.landId).then(res => {
					this.$refs.paging.completeByTotal(res.rows);
				});
			},
			// 刷新
			refresh(item) {
				console.error("刷新:", item);
			},
			// 更换基站
			changeStation(item) {
				console.error("更换基站:", item);
			},
			// 下发列表
			sendList(item) {
				DevOpsApi.getGVL(item.deviceCode).then(res => {
					this.$toast(res.msg)
				})
			},
			// 修改频段
			editRolaFrequency(item) {
				this.pop.rolaFrequency = true;
				this.rolaFrequency.item = item;
				this.rolaFrequency.old = item.rolaFrequency;
				this.rolaFrequency.new = item.rolaFrequency;
			},
			// 获取配置
			getConfig(item) {
				console.error("获取配置:", item);
			},
			// 压力校准
			pressureCalibration(item) {
				console.error("压力校准:", item);
			},
			// 重启
			restart(item) {
				console.error("重启:", item);
			},
			// 子设备
			childDevice(item) {
				uni.navigateTo({
					url: '/pages/dev-ops/valve?station=' + JSON.stringify(item)
				});
			},
			// 设备升级
			valveUpdate(item) {
				console.error("设备升级:", item);
			},
			// 关闭弹出层
			close() {
				for (var key in this.pop) {
					this.pop[key] = false;
				}
			},

			editRolaFrequencyHandler() {
				// if (this.rolaFrequency.item.rolaFrequency == this.rolaFrequency.rolaFrequency) {
				// 	this.$toast("新旧频段不能相同！")
				// 	return;
				// }
				this.pop.rolaFrequency = false;
				let msg = `您确定要修改【${this.rolaFrequency.item.deviceCode }】的频段为【${this.rolaFrequency.new}】?`;
				this.$showConfirm(msg).then(res => {
					if (res.confirm) {
						DevOpsApi.updateSLF({
							deviceCode: this.rolaFrequency.item.deviceCode,
							oldFrequency: this.rolaFrequency.old,
							newFrequency: this.rolaFrequency.new
						}).then(res => {
							this.$toast(res.msg)
						})
					} else {
						this.pop.rolaFrequency = true;
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.index-container {
		.list {
			margin: 0px 10px;
		}

		.list-size {
			display: flex;
			padding: 5px 5px;
			background: white;
			justify-content: center;
		}

		.search {
			background-color: white;
			padding: 10px;
		}

		.item-container {
			margin: 10px 10px;
			background-color: white;
			padding-bottom: 10px;
			border-radius: 10px;
			box-shadow: 1px 1px 2px 2px rgba(0, 0, 0, 0.05);
		}

		.flex-text {
			display: flex;
			align-items: center;

			.flex-text-one {
				color: #939393;
				font-weight: bold;
			}
		}

		.data {
			margin: 10px 20px !important;
			width: 100%;

			.uni-col {
				margin-bottom: 5px;
			}
		}
	}


	::v-deep.u-popup__content {
		border-radius: 4px;
	}

	::v-deep.u-popup__content__close--top-right {
		top: 10;
		right: 10;
	}

	// 弹出框1
	.tc-box {
		max-width: 86vw;
		min-width: 60vw;
		min-height: 100px;
		padding: 20px;

		.tc-title {
			font-size: 34rpx;
			font-weight: bold;
		}

		.tc-content {
			font-size: 30rpx;
			margin: 10px 0;
		}

		.tc-foot {
			display: flex;
			justify-content: end;

			&>view {
				margin: 0 10px;
				color: #2f7b9f;
			}
		}
	}

	// 弹出框2

	.tc-box {
		max-width: 86vw;
		min-width: 60vw;
		min-height: 100px;
		padding: 20px;

		.tc-title {
			font-size: 34rpx;
			font-weight: bold;
		}

		.tc-content {
			font-size: 30rpx;
			margin: 10px 0;
		}

		.tc-foot-c {
			display: flex;
			text-align: center;

			&>view {
				border: 1px solid #f3f3f3;
				flex: 1;
				line-height: 40px;
			}

			&>view:last-child {
				color: #2f7b9f;
				border-left: none;
			}
		}
	}
</style>