<template>
	<view>
		<view class="status_bar"></view>
		<view class="" style="position: relative;top: 44px;">
			<view style="position: fixed;z-index: 100;">
				<uni-nav-bar @clickLeft="clickLeft" left-icon="back" left-text="返回" title="环境监测系统" right-icon="search"
					@clickRight="clickRight" size="16"></uni-nav-bar>
				<view class="inputs" :style="{'width':width+'rpx'}">
					<input type="text" value="" placeholder="请输入设备ID或设备名称" v-model="imports" @input="searchinp"
						class="inputbox" :clearable="false" />
					<u-icon name="search" size="40" class="icon" @click="search"></u-icon>
				</view>
			</view>
			<image :src="'http://www.hnyfwlw.com:8006/bigdata_app'+'/image/environment/1.png'" mode="" class="image">
			</image>
			<view class="loading" v-if="loadingtf">
				<image src="../../static/images/ajax-loader.gif" mode="" class="img"></image>
			</view>
			<view class="tab-box">
				<view v-for="(item,index) in equipArr" :key="item.type" v-if="item.tf" @click="tabClick(index,item.type)" :class="['tab-item',active==index?'active':'']">
					<text>{{item.name}}</text>
					<text class="bottom-line"></text>
				</view>
			</view>
			<view class="prevents">
				<view class="prevents_item" v-for="item,index in eqlistdata" :key="index" @click="eqdetails(item)">
					<view class="" v-if="side_type == 5">
						<image
							:src="item.is_online==1?'http://www.hnyfwlw.com:8006/bigdata_app/image/prevention/6.png':'http://www.hnyfwlw.com:8006/bigdata_app/image/prevention/7.png'"
							mode="" class="prevents_item_img"></image>
						<view class="prevents_item_top">
							<p>设备名称：{{item.equip_name==""?"无":item.equip_name}}</p>
							<p :class="item.is_online==1?'green':'red'" v-text="item.is_online==1?'在线':'离线'"></p>
						</view>
						<view class="prevents_item_bot">
							<p>设备 ID：{{item.equip_id || item.device_id}}</p>
							<p>最新上报时间：{{item.uptime|timeFormat()}}</p>
						</view>
					</view>
					<view class="" v-if="side_type == 15">
						<image
							src="http://www.hnyfwlw.com:8006/bigdata_app/image/prevention/6.png"
							mode="" class="prevents_item_img"></image>
						<view class="prevents_item_top">
							<p>设备名称：{{item.device_name==""?"无":item.device_name}}</p>
						</view>
						<view class="prevents_item_bot">
							<p>设备 ID：{{item.device_id}}</p>
							<p>最新上报时间：{{item.uptime|timeFormat()}}</p>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="top" v-if="side_type==5">
			<view class="backtop" @click="top" v-if="isTop">
				<image src="../../static/images/1.png" mode="" class="img0"></image>
			</view>
			<view class="more">
				<view class="box" @click="online" v-show="filtrateTF">
					<p>在线</p>
				</view>
				<view class="box" @click="offline" v-show="filtrateTF">
					<p>离线</p>
				</view>
				<view class="box" @click="complete" v-show="filtrateTF">
					<p>全部</p>
				</view>
				<view @click="filtrate">
					<image src="../../static/images/b0bcdb0e3fe8690520f743aa8303bf2.png" mode="" class="img1"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		Debounce
	} from "../../util/anitthro.js"
	export default {
		data() {
			return {
				page: 1,
				size: 10,
				eqlistdata: [],
				isTop: false,
				filtrateTF: false,
				device_status: "",
				width: 0, //顶部搜索栏宽度
				imports: "", //搜索设备id
				loadingtf: false,
				equipArr: [{
						name: '环境监测',
						type: 5, //5 环境监测 15 管式墒情
						list: [],
						pageIndex: 1,
						tf:false,
					}, {
						name: '管式墒情',
						type: 15, //5 环境监测 15 管式墒情
						list: [],
						pageIndex: 1,
						tf:false,
					}
				],
				active:0,//tab选中的下标
				side_type:5,//当前选中的设备类型
			}
		},
		methods: {
			async eqlist() { //设备列表 环境监测
				this.loadingtf = true
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=weather.weather.qxz_page',
					data: {
						page: this.page,
						device_status: this.device_status,
						page_size: 10,
						device_id: this.imports
					}
				})
				this.loadingtf = false
				this.eqlistdata = this.eqlistdata.concat(res.ids)
				console.log(res.ids)
			},
			// weather.weather.nd_qxz_page 管式墒情
			async tubulareqlist() { //设备列表 环境监测
				this.loadingtf = true
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=weather.weather.nd_qxz_page',
					data: {
						page: this.page,
						device_status: this.device_status,
						page_size: 10,
						device_id: this.imports
					}
				})
				this.loadingtf = false
				this.eqlistdata = this.eqlistdata.concat(res.ids)
				console.log(res.ids)
			},
			clickRight() {
				this.width = 600
			},
			clickLeft() {
				uni.switchTab({
					url: "../index/index"
				})
			},
			eqdetails(data) {
				if(this.side_type == 5){
					uni.navigateTo({
						url: "./equipment?shebei=" + JSON.stringify(data)
					})
				}else if(this.side_type == 15){
					uni.navigateTo({
						url: "./gsequipment?shebei=" + JSON.stringify(data)
					})
				}
			},
			filtrate() {
				this.filtrateTF = !this.filtrateTF
			},
			top() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 500
				})
			},
			online() {
				this.eqlistdata = []
				this.page = 1
				this.device_status = 1
				this.eqlist()
				this.filtrateTF = !this.filtrateTF
			},
			offline() {
				this.eqlistdata = []
				this.page = 1
				this.device_status = 0
				this.eqlist()
				this.filtrateTF = !this.filtrateTF
			},
			complete() {
				this.eqlistdata = []
				this.page = 1
				this.device_status = ""
				this.eqlist()
				this.filtrateTF = !this.filtrateTF
			},
			search() { //搜索按钮搜索
				if(this.side_type == 5){
					this.eqlistdata = []
					this.eqlist()
				}else if(this.side_type == 15){
					this.eqlistdata = []
					this.tubulareqlist()
				}
			},
			searchinp() { //自动搜索
				Debounce(() => {
					this.search()
				}, 1000)()
			},
			tabClick(index,type){
				this.active = index
				this.side_type = type
				this.page = 1
				if(type == 5){
					this.eqlistdata = []
					this.eqlist()
				}else if(type == 15){
					this.eqlistdata = []
					this.tubulareqlist()
				}
			}
		},
		onLoad() {
			this.eqlist()
			uni.getStorage({
				key:"jurisdiction",
				success:(res)=>{
					let items = JSON.parse(res.data).filter((item)=>{
						return item.purview_name == "环境监测系统"
					})
					var itemarr = items[0].children
					console.log(itemarr)
					for(var i =0;i<itemarr.length;i++){
						switch (itemarr[i].purview_name){
							case "环境监测":
								this.equipArr[0].tf = true
								break
							case "管式墒情":
								this.equipArr[1].tf = true
								break
						}
					}
				}
			})
		},
		onReachBottom() {
			this.page++
			this.eqlist()
		},
		onPageScroll(e) { //nvue暂不支持滚动监听，可用bindingx代替
			if (e.scrollTop > 200) { //距离大于200时显示
				this.isTop = true
			} else { //距离小于200时隐藏
				this.isTop = false
			}
		},
	}
</script>

<style lang="scss">
	page{
		background: $uni-bg-color-grey;
	}
	.inputs {
		height: 54rpx;
		background-color: #E4E4E4;
		border-radius: 27rpx;
		position: absolute;
		right: 20rpx;
		top: 20rpx;
		transition: width 0.5s;
		overflow: hidden;
		padding-top: 8rpx;
		box-sizing: border-box;

		.inputbox {
			width: 85%;
			text-indent: 1rem;
			font-size: 26rpx;
		}

		.icon {
			position: absolute;
			top: 8rpx;
			right: 26rpx;
		}
	}

	/deep/.uni-icons {
		font-size: 40rpx !important;
	}

	.image {
		position: fixed;
		top: 84px;
		width: 100%;
		height: 160rpx;
		z-index: 555;
	}

	.loading {
		position: fixed;
		top: 440px;
		width: 95%;
		left: 2.5%;
		text-align: center;
		z-index:9;
		.img {
			width: 300rpx;
			height: 40rpx;
		}
	}
	.tab-box {
		position: fixed;
		top: 160px;
		display: flex;
		justify-content: space-around;
		font-size: 30rpx;
		line-height: 80rpx;
		background-color: #FFFFFF;
		width: 100%;
		z-index: 2;
		.tab-item {
			cursor: pointer;
			position: relative;
		};
		.tab-item.active {
			.bottom-line {
				bottom: 0;
				position: absolute;
				display: inline-block;
				width: 90rpx;
				height: 6rpx;
				left: 0;
				right: 0;
				margin: auto;
				background: $uni-color-success;
			}
		}
	}
	.prevents {
		width: 100%;
		position: absolute;
		top: 170px;

		.prevents_item {
			width: 95%;
			margin: 0 auto 30rpx;
			border-radius: 10rpx;
			box-shadow: 0 0 10rpx #bcb9ca;
			padding: 20rpx 40rpx 20rpx 80rpx;
			box-sizing: border-box;
			position: relative;
			background-color: #fff;
			.prevents_item_img {
				width: 30rpx;
				height: 50rpx;
				position: absolute;
				top: -4rpx;
				left: 30rpx;
			}

			.prevents_item_top {
				display: flex;
				justify-content: space-between;
				height: 60rpx;
				border-bottom: 2rpx solid #F4F4F4;
				line-height: 60rpx;
				font-size: 26rpx;

				.red {
					color: #ff0000;
				}

				.green {
					color: #7DBB91;
				}
			}

			.prevents_item_bot {
				margin-top: 20rpx;
				font-size: 26rpx;
				color: #BDBDBD;
			}
		}
	}

	.top {
		position: fixed;
		right: 10px;
		bottom: 40px;
		z-index: 100;

		image {
			width: 100rpx;
			height: 100rpx;
		}

		.backtop {
			display: flex;
			justify-content: flex-end;
			margin-bottom: 10rpx;
		}

		.more {
			display: flex;
		}

		.box {
			width: 80rpx;
			height: 80rpx;
			background-color: rgba(161, 161, 161, 0.45);
			border-radius: 50%;
			text-align: center;
			line-height: 80rpx;
			box-sizing: border-box;
			margin: 14rpx 10rpx 0 0;
			color: #fff;
		}
	}
</style>
