<!--
 * @Title: 
 * @Description: 管理工作台
 * @Author: swp
 * @Date: 2022-08-24 10:49:21
 * @LastEditors: 
 * @LastEditTime: 2022-08-24 10:49:21
-->
<template>
	<view class="container">
		<uni-nav-bar :fixed="true" backgroundColor="#3F9EFF" statusBar="false" height="75px">
			<view style="width:100%;display: flex;flex-direction: row;justify-content: center;align-items: center;">
				<text style="color: #ffffff; font-size: 1rem;font-family: Microsoft YaHei;">{{title}}</text>
			</view>
		</uni-nav-bar>
		<uni-section title="查询" type="line">
			<view class="search-block">
				<view class="adcd-adnm">
					<uni-data-picker placeholder="请选择地区" popup-title="请选择所在地区" :localdata="regionTree"
						v-model="selectedTreeNode" @change="onTreeChange" @nodeclick="onTreeNodeClick"
						@popupopened="onTreePopupOpened" @popupclosed="onTreePopupClosed" :clear-icon="false">
					</uni-data-picker>
				</view>
				<view class="rtu-code">
					<uni-easyinput :styles="inputStyles" @input="searchRtuCodeValClear" prefixIcon="search"
						v-model="searchRtuCodeVal" placeholder="请输入测站编码">
					</uni-easyinput>
				</view>
				<view class="rtu-name">
					<uni-easyinput :styles="inputStyles" @input="searchRtuNameValClear" prefixIcon="search"
						v-model="searchRtuNameVal" placeholder="请输入测站名称">
					</uni-easyinput>
				</view>
				<view class="submit-btn">
					<button type="default" @click="search">查 询</button>
				</view>
			</view>
		</uni-section>
		<view class="count-info-line">
			<view class="rtuCount">
				<span class="name">测站数量: </span>
				<span v-if="countInfo.rtuCount" class="count">{{ countInfo.rtuCount }}</span>
			</view>
			<view class="warnRtuCount">
				<span class="name">异常测站数量: </span>
				<span v-if="countInfo.warningRtuCount" class="count">{{
						                                        countInfo.warningRtuCount }}</span>
			</view>
		</view>
		<view style="padding-left: 0px;padding-right: 0px;">
			<uni-collapse v-model="value">
				<uni-collapse-item title="异常类别筛选条件" :show-animation="true">
					<view class="select-checkbox">
						<uni-data-checkbox v-model="warnKindOption" :localdata="warnKindItems"
							@change="warnKindOptionChange">
						</uni-data-checkbox>
					</view>
				</uni-collapse-item>
			</uni-collapse>
		</view>
		<uni-list>
			<uni-list-item v-for="item in list" :key="item.id">
				<template v-slot:body>
					<view class="list-item-block">
						<!-- 测站名称 -->
						<uni-row>
							<uni-col :span="20">
								<view class="items-line">
									<image v-if="item.runStatusId==1" class="item-title-run-status-icon" mode="widthFix"
										src="/static/images/icon_warning.png">
									</image>
									<image v-else class="item-title-run-status-icon" mode="widthFix"
										src="/static/images/icon_ok.png">
									</image>
									<text class="item-title-rtu-name">{{item.rtuName}}</text>
									<text class="item-title-rtu-code">[{{item.rtuCode}}]</text>
								</view>
							</uni-col>
							<uni-col :span="4">
								<view class="items-line" style="padding-top: 5px;"
									@click="onRtuDetailClick(item.rtuCode)">
									<text
										style="text-decoration-line: underline;font-size: 0.6rem;color:gray;">测站信息</text>
								</view>
							</uni-col>
						</uni-row>

						<!-- 最近上报时间 -->
						<view class="items-line">
							<uni-icons class="input-uni-icon" type="calendar" size="18" color="lightblue" />
							<text class="item-up-time-text">最后上线时间:</text>
							<text class="item-up-time-text">{{item.lastUpTime}}</text>
						</view>
						<view v-if="item.runStatusId==1" class="items-line">
							<uni-icons class="input-uni-icon" type="bars" size="18" color="lightblue" />
							<text class="item-up-time-text">异常类型:</text>
							<text class="item-up-time-text">{{item.warnKindInfo}}</text>
						</view>
						<view class="items-line">
							<uni-icons class="input-uni-icon" type="location" size="18" color="lightblue" />
							<text class="item-adcd-adnm-text">{{item.areaName}}</text>
						</view>
						<!-- 操作 -->
						<view class="item-button-group">
							<view class="item-button" @click="onLocationClick(item)">
								<view class="items-line">
									<uni-icons class="input-uni-icon" type="location" size="18" color="coral" />
									<text class="button-text">导航</text>
								</view>
							</view>
							<view class="item-button" @click="onWarningPageClick(item.rtuCode)">
								<view class="items-line">
									<uni-icons class="input-uni-icon" type="gear" size="18" color="coral" />
									<text class="button-text">异常信息</text>
								</view>
							</view>
							<view class="item-button" @click="onCheckOrderPageClick(item.rtuCode)">
								<view class="items-line">
									<uni-icons class="input-uni-icon" type="gear" size="18" color="coral" />
									<text class="button-text">维修任务</text>
								</view>
							</view>
							<view class="item-button" @click="onInspectionReportClick(item.rtuCode)">
								<view class="items-line">
									<uni-icons class="input-uni-icon" type="reload" size="18" color="coral" />
									<text class="button-text">设备巡检</text>
								</view>
							</view>
							<view class="item-button" @click="onSiteRealDataClick(item)">
								<view class="items-line">
									<uni-icons class="input-uni-icon" type="info" size="18" color="coral" />
									<text class="button-text">实时数据</text>
								</view>
							</view>
							<view v-if="permission.admin || permission.orgAdmin" class="item-button"
								@click="onManuallyOrderClick(item.rtuCode)">
								<view class="items-line">
									<uni-icons class="input-uni-icon" type="email" size="18" color="coral" />
									<text class="button-text">人工派单</text>
								</view>
							</view>
						</view>
					</view>
				</template>
			</uni-list-item>
		</uni-list>
		<uni-group>
			<view class="pagination-block">
				<uni-pagination show-icon :page-size="pageSize" :current="pageCurrent" :total="total"
					@change="pageChange" />
			</view>
		</uni-group>
		<!-- 	</view> -->

	</view>
</template>

<script>
	import
	navBtns
	from "@/api/home.js";
	import {
		role
	} from "@/api/role.js";
	import http from '@/http/api.js';
	export default {
		name: '',
		components: {

		},
		data() {
			return {
				title: '管理工作台',
				permission: {
					'admin': false,
					'orgAdmin': false,
					'companyAdmin': false,
					'companyServciePerson': false,
				},
				currentRole: '',
				barcurrent: 1,
				pageSize: 10,
				pageCurrent: 1,
				total: 0,
				list: [],
				regionTree: [],
				regionCode: '',
				selectedTreeNode: '',
				searchRtuNameVal: '',
				searchRtuCodeVal: '',
				query: {},
				inputStyles: {
					color: '#808080',
					borderColor: '#d3d3d3'
				},
				countInfo: {},
				loading: false,
				value: [],
				warnKindItems: [{
					'value': 0,
					'text': '全部'
				}, {
					'value': 1,
					'text': '离线'
				}, {
					'value': 2,
					'text': '时钟异常'
				}, {
					'value': 3,
					'text': '雨量漏报'
				}, {
					'value': 4,
					'text': '水位漏报'
				}, {
					'value': 5,
					'text': '5分钟上报延时'
				}, {
					'value': 6,
					'text': '雨量小时上报延时'
				}, {
					'value': 7,
					'text': '水位小时上报延时'
				}, {
					'value': 8,
					'text': '雨量疑似异常值'
				}],
				warnKindOption: 0,
			}
		},
		computed: {

		},
		onLoad(option) {
			if (this.$u.func.checkLogin()) {
				if (this.userInfo.role_name === role.admin) {
					this.permission.admin = true;
					this.currentRole = role.admin;
				} else if (this.userInfo.role_name === role.orgAdmin) {
					this.permission.orgAdmin = true;
					this.currentRole = role.orgAdmin;
				} else if (this.userInfo.role_name === role.companyAdmin) {
					this.permission.companyAdmin = true;
					this.currentRole = role.companyAdmin;
				} else if (this.userInfo.role_name === role.companyServciePerson) {
					this.permission.companyServciePerson = true;
					this.currentRole = role.companyServciePerson;
				}
				this.getRegionTree();
			} else {
				this.$u.func.logout();
			}
		},
		onShow() {
			if (this.$u.func.checkLogin()) {
				if (this.userInfo.role_name !== this.currentRole) {
					this.permission.admin = false;
					this.permission.orgAdmin = false;
					this.permission.companyAdmin = false;
					this.permission.companyServciePerson = false;
					if (this.userInfo.role_name === role.admin) {
						this.permission.admin = true;
						this.currentRole = role.admin;
					} else if (this.userInfo.role_name === role.orgAdmin) {
						this.permission.orgAdmin = true;
						this.currentRole = role.orgAdmin;
					} else if (this.userInfo.role_name === role.companyAdmin) {
						this.permission.companyAdmin = true;
						this.currentRole = role.companyAdmin;
					} else if (this.userInfo.role_name === role.companyServciePerson) {
						this.permission.companyServciePerson = true;
						this.currentRole = role.companyServciePerson;
					}
					this.getRegionTree();

				}
			} else {
				this.$u.func.logout();
			}
		},
		methods: {
			// 分页触发
			pageChange(e) {
				this.pageCurrent = e.current;
				this.getPage()
			},
			//行政区划选择
			onTreeNodeClick(node) {
				console.log(JSON.stringify(node))
			},
			onTreePopupOpened(e) {
				console.log(JSON.stringify(e))
			},
			onTreePopupClosed(e) {
				console.log(JSON.stringify(e))
			},
			onTreeChange(e) {
				console.log(JSON.stringify(e))
				let nodes = e.detail.value;
				if (nodes.length > 0) {
					let node = nodes[nodes.length - 1];
					this.regionCode = node.value;
					this.query = {};
					this.query['adCode'] = this.regionCode;
					this.searchRtuNameVal = '';
					this.searchRtuCodeVal = '';
					this.pageCurrent = 1;
					this.getPage();
					this.getCountInfo(this.regionCode);
				} else {
					this.regionCode = '';
					this.query = {};
					this.searchRtuNameVal = '';
					this.searchRtuCodeVal = '';
					this.pageCurrent = 1;
					this.getPage();
					this.getCountInfo(this.regionCode);
				}
			},
			//录入查询
			search() {
				this.pageCurrent = 1;
				//	console.log(JSON.stringify(res))
				let params = {};
				if (this.searchRtuCodeVal.length > 0) {
					params['rtuCode'] = this.searchRtuCodeVal;
				}
				if (this.searchRtuNameVal.length > 0) {
					params['rtuName'] = this.searchRtuNameVal;
				}
				this.getPage(params);
			},
			searchRtuNameValClear(e) {
				if (e == null || e.length == 0) {
					this.searchRtuNameVal = '';
					if (this.searchRtuNameVal.length == 0) {
						let params = {};
						if (this.searchRtuCodeVal.length > 0) {
							params['rtuCode'] = this.searchRtuCodeVal;
						}
						this.getPage(params);
					}
				}
			},
			searchRtuCodeValClear(e) {
				if (e == null || e.length == 0) {
					this.searchRtuCodeVal = '';
					if (this.searchRtuCodeVal.length == 0) {
						let params = {};
						if (this.searchRtuNameVal.length > 0) {
							params['rtuName'] = this.searchRtuNameVal;
						}
						this.getPage(params);
					}
				}
			},
			onBackPress() {
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord();
				// #endif
			},
			onRtuDetailClick(rtuCode) {
				uni.navigateTo({
					url: '/pages/rtu-manage/rtudetail?rtuCode=' + rtuCode
				})
			},
			onWarningPageClick(rtuCode) {
				uni.navigateTo({
					url: '/pages/warning/rtuwarninglist?rtuCode=' + rtuCode
				})
			},
			onLocationClick(item) {
				let lat = item.lat;
				let lng = item.lng;
				let name = item.rtuName;
				let add = item.locationDesc;
				uni.openLocation({
					latitude: Number(lat),
					longitude: Number(lng),
					name: name,
					address: "",
					success() {}
				})
			},
			onCheckOrderPageClick(rtuCode) {
				uni.navigateTo({
					url: '/pages/check-order/rtu/rtucheckorderlist?rtuCode=' + rtuCode
				})
			},
			onInspectionReportClick(rtuCode) {
				console.log("onInspectionReportClick")
				uni.navigateTo({
					url: '/pages/equipment-inspection/rtuinspectionreportview?rtuCode=' + rtuCode
				})
			},
			onSiteRealDataClick(item) {
				uni.navigateTo({
					url: '/pages/rtu-manage/rturealdatadetail?rtuCode=' + item.rtuCode + '&isRain=' + item.isRain +
						'&isRiver=' + item.isRiver + '&isRes=' + item.isRes + '&isGround=' + item.isGround,
				})
			},
			onManuallyOrderClick(rtuCode) {
				uni.navigateTo({
					url: '/pages/check-order/manuallyaddcheckorder?rtuCode=' + rtuCode
				})
			},
			warnKindOptionChange: function(evt) {
				let params = {};
				this.getPage(params);
			},
			getSiteData(val) {
				let htmltext = '';
				if (val.isRain === 1) {
					htmltext = htmltext + "降水：";
					if (val.drp) {
						htmltext = htmltext + val.drp;
					} else {
						htmltext = htmltext + "--";
					}
				}
				if (val.isRiver == 1) {
					if (htmltext.length > 0) {
						htmltext = htmltext + ' / ';
					}
					htmltext = htmltext + '水位：';
					if (val.rz) {
						htmltext = htmltext + val.rz;
					} else {
						htmltext = htmltext + "--";
					}
				} else if (val.isRes == 1) {
					if (htmltext.length > 0) {
						htmltext = htmltext + ' / ';
					}
					htmltext = htmltext + '水位：';
					if (val.z) {
						htmltext = htmltext + val.z;
					} else {
						htmltext = htmltext + "--";
					}
				}
				if (val.isGround == 1) {
					if (htmltext.length > 0) {
						htmltext = htmltext + ' / ';
					}
					htmltext = htmltext + '墒情含水层：';
					if (val.ground) {
						htmltext = htmltext + val.ground;
					} else {
						htmltext = htmltext + "--";
					}
				}
				return htmltext;
			},
			getRegionTree() {
				var that = this;
				http.request({
					url: '/galaxy-business/baseinfo/region/tree',
					method: 'GET',
				}).then(res => {
					if (res.data != null) {
						that.regionTree = res.data;
						that.regionCode = that.regionTree[0].id;
						that.selectedTreeNode = that.regionTree[0].id;
						that.query = {};
						that.query['adCode'] = that.regionCode;
						that.getPage();
						that.getCountInfo(that.regionCode);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			getCountInfo(adCode) {
				let that = this;
				http.request({
					url: '/galaxy-business/rtu/manage/count',
					method: 'GET',
					data: {
						adCode
					}
				}).then(res => {
					that.countInfo = res.data;
				})
			},
			getPage(params = {}) {
				this.loading = true
				if (this.warnKindOption == 0) {} else {
					params['warnKind'] = this.warnKindOption;
				}
				const current = this.pageCurrent;
				const size = this.pageSize;
				let postData = Object.assign(params, this.query);
				var that = this;
				http.request({
					url: '/galaxy-business/rtu/manage/page',
					method: 'GET',
					params: {
						current,
						size,
					},
					data: postData,
				}).then(res => {
					//	console.log(JSON.stringify(res.data))
					if (res.data.records != null) {
						that.list = res.data.records;
					}
					that.total = res.data.total;
					this.loading = false
				}).catch(err => {
					console.log(err)
					this.loading = false
				})
			},
		}
	}
</script>

<style>
	/* page {
		background-color: rgb(240, 242, 244);
	} */
</style>

<style lang="scss" scoped>
	$nav-height: 75px;

	.select-checkbox {
		margin-top: 0px;
		margin-bottom: 5px;
		padding-left: 15px;
	}

	.count-info-line {
		padding-top: 5px;
		padding-bottom: 5px;
		padding-left: 10px;
		padding-right: 10px;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.count-info-line .rtuCount {

		display: flex;
		flex-direction: row;
		align-items: center;

		.name {
			color: dimgray;
			font-size: 0.7rem;
		}

		.count {
			margin-left: 5px;
			color: dodgerblue;
			font-size: 0.7rem;
		}
	}

	.count-info-line .warnRtuCount {
		margin-left: 10px;
		display: flex;
		flex-direction: row;
		align-items: center;

		.name {
			color: dimgray;
			font-size: 0.7rem;
		}

		.count {
			margin-left: 5px;
			color: red;
			font-size: 0.7rem;
		}
	}
</style>