<template>
	<view>
		<u-navbar title="设备状态监测" title-color="#101010"></u-navbar>
		<u-select title="" v-model="tabsFrom.show1" :defaultValue="current2"
		
		 :list="companyList" value-name="id" label-name="name"
		    child-name="childList"
		 mode="mutil-column-auto"
			@confirm="selector1confirm"  >
		</u-select>
		<view class="dropdownMain" >
			<view class="dropdown" >
				
			
			<view class="dropdown-item" @click="tabsFrom.show1=true">
		
				<view class="text">
					{{device}}
				</view>
				<view class="item-icon">
					<u-icon name="arrow-down" color="#999999" v-if="show1==false"></u-icon>
					<u-icon name="arrow-up" color="#999999" v-else></u-icon>
				</view>
			</view>
			<view class="dropdown-item" @click="show3=true">
				<view class="text">
					{{typeName}}
				</view>
				<view class="icon">
					<u-icon name="arrow-down" color="#999999" v-if="show3==false"></u-icon>
					<u-icon name="arrow-up" color="#999999" v-else></u-icon>
				</view>
			</view>
			<view class="dropdown-item" @click="show2=true">
				<view class="text">
					{{statusName}}
				</view>
				<view class="icon">
					<u-icon name="arrow-down" color="#999999" v-if="show2==false"></u-icon>
					<u-icon name="arrow-up" color="#999999" v-else></u-icon>
				</view>
			</view>
		</view>
		
		
		</view>
		
		
		<u-select v-model="show3" mode="single-column" :list="deviceStatusList" @confirm="deviceStatusChange"></u-select>
		
		<u-select v-model="show2" mode="single-column" :list="statusList" @confirm="statusChange"></u-select>

		<view class="main">
			<view class="item" v-for="(item,index) in deviceList" :key="index" @click="toElectronicMonitoring(item)">
				<view class="item-icon" v-if="item.classify == '1'">
					<image class="img" src="@/assets/img/energy1.svg" mode=""></image>
				</view>
				<view class="item-icon" v-if="item.classify == '2'">
					<image class="img" src="@/assets/img/transformer2.svg" mode=""></image>
				</view>
				<view class="item-icon" v-if="!item.classify ">
					<image class="img" src="@/assets/img/transformer2.svg" mode=""></image>
				</view>
				<view class="item-content">
					<view class="content1">
						<view class="name1">
							{{replaceLastTwoWords(item.name)}}
						</view>
						
					</view>
					<view class="content3">
						{{item.installationAddressSimple}}
					</view>
					<view class="content2">
						<view class="text" v-if="!deviceType">
							{{item.deviceTypeN}}
						</view>
						<view class="text" v-else >
							{{item.parkName}}
						</view>
						
						<view class="item-state">
							<view class="dot" :class="item.online ||item.transportType == 2 ? 'on-line' : 'off-line'">

							</view>
							<view class="text">
								{{item.online ||item.transportType == 2 ? '在线' : '离线'}}
							</view>
							<view class="more">
								<u-icon name="arrow-right" size="24" color="#acacac"></u-icon>
							</view>
						</view>
					</view>
					
					<view class="content3" v-if="item.classify == '2'">
						<view class="state-item">
							<view class="state-title">
								运行状态：
							</view>
							<view class="state-value" v-if="item.deviceStatus == '1'">
								正常
							</view>
							<view class="state-value2" v-else>
								异常
							</view>
						</view>
						<view class="state-item">
							<view class="state-title">
								温度状态：
							</view>
							<view class="state-value" v-if="item.temperatureStatus == '1'">
								正常
							</view>
							<view class="state-value2" v-else>
								异常
							</view>
						</view>
						<view class="state-item">
							<view class="state-title">
								烟感状态：
							</view>
							<view class="state-value" v-if="item.smokeStatus == '1'">
								正常
							</view>
							<view class="state-value2" v-else>
								异常
							</view>
						</view>
					</view>
				</view>
			</view>
			
		</view>
		<u-divider :isnone="deviceList.length==0" nonetext="暂无数据" border-color="#CFD2D5">已经到底了</u-divider>
		
	</view>
</template>

<script>
	import * as API from '@/apis/pagejs/index.js'
	import * as API_electricityMeter from '@/apis/pagejs/electricityMeter.js'
	import * as API_water from '@/apis/pagejs/water.js'
	export default {
		data() {
			return {
				companyList: [], // 商户
				merchantList1: [],
				merchantList2: [],
				
				deviceList: [],
				show1: false,
				show2: false,
				show3: false,
				typeName:"电表",
				deviceType:"",
				device: '全部设备',
				statusName: '全部状态',
				deviceTypeList: [],
				deviceStatusList: [{
						value: '',
						label: '电表'
					},
					{
						value: '1',
						label: '水表'
					},
					
				],
				statusList: [{
						value: '',
						label: '全部状态'
					},
					{
						value: '1',
						label: '离线'
					},
					{
						value: '2',
						label: '温度异常'
					},
					{
						value: '3',
						label: '烟感异常'
					},
					{
						value: '4',
						label: '设备异常'
					}
				],
				tabsFrom: {
					show1: false,
					show1Index: 0,
					show2Index: '',
					show2: false,
					show1Text: "全部类型",
					show2Text: "全部时间",
					
				},
				status: '',
				deviceType: '',
				companyId:"",
				pageIndex: 1,
				recordsTotal:0,
				utabsone:true,
				companyType:0,
				incomeExpenditureObj:{},
				opCompanyId: '',
				childList:[],
				companyListMain:[],
				current2:[0,0],
				childInfo:{},
				companyInfo:{},
				popShow: false,
				current: 0,
			}
		},
		computed:{
			childListName(){
				return this.childInfo.name;
			},
			
		},
		onLoad(op) {
			if(op.companyId){
				this.companyId=op.companyId
				this.opCompanyId=op.companyId
			}
			this.getCompanyInfoList();
			this.getDeviceStatusMonitoring();
		},
		onReady() {
			
			//this.getDeviceTypeList();
		},
		onReachBottom() {
			if (this.deviceList.length < this.recordsTotal) {
				this.myLoadmore();
			}
		},
		methods: {
			merchantChangeApi() {
			
				this.popShow=false
				
				//this.getList(true)
				
				this.getDeviceStatusMonitoring(1);
				
			},
			titleCk() {
				if(this.childList.length>1){
					this.tabsFrom.show1 = true
				}
				
			},
			selector1confirm(e) {
				//this.getHomePageKwh()
				 var index = [e[0].i,e[1].i]
				 
				console.log(e)
				this.companyId=e[1].value
				this.device=e[1].label

				this.current2=index
				
				//this.childInfo=this.childList[this.current2];
				uni.showLoading()
				
				this.merchantChangeApi()
			},
			change(index) {
				this.current = index;
				this.current2=0;
				this.childList=[]
				var company= this.merchantList1[index]
				if(company.id!=''){
					this.childList=company.childList;
					if(this.childList&&this.childList.length){
						this.companyId = this.childList[0].id;
						this.childInfo=this.childList[0]
					}else{
						this.childList=[]
						this.companyId =company.id
						this.childInfo=company
					}
					
					
				}else{
					this.companyId =''
					
				}
				this.companyInfo=company;
				
				this.merchantChangeApi()
			},
			merchantChange(item, index) {
				if(this.current==0){
					this.current=1;
				}
				var m = company;
				var n = this.merchantList1[this.current];
				this.merchantList1[this.current] = m;
				this.merchantList2[index] = n;
				//this.$forceUpdate()
				this.utabsone=false
				this.companyId = company.id;
				
				this.companyInfo=company
				
				
				if(company.id!=''){
					this.childList=company.childList;
					if(this.childList&&this.childList.length){
						this.companyId = this.childList[0].id;
						this.childInfo=this.childList[0]
					}else{
						this.childList=[]
						this.companyId =company.id
						this.childInfo=company
					}	
				}else{
					this.companyId =''	
				}
				
				this.merchantChangeApi()
				this.popShow = false;
				
			},
			getCompanyInfoList() {
				this.companyKwhList = [];
			
				API.deviceCompanyList().then((response) => {
					var list = response.data.companyInfoList;
					this.companyListMain=response.data.companyInfoList;
					// if(list.length==1&&list[0].childList&&list[0].childList.length){
					// 	list=list[0].childList
					// }
					
					for(var i in list){
						var obj=list[i]
						
						if(!list.childList){
							list.childList=[]
						}
						
						list[i].childList.unshift({
							id:obj.id,
							name:obj.name,
							sp:1
						})
						
					}
					list.unshift({
						id:'',
						name:"全部",
						childList:[
							{
								id:'',
								name:"全部设备"
							}
						]
					})
					for(var i in list){
						var obj=list[i]
						
						if(this.opCompanyId){
							for(var j in list[i].childList){
								var item=list[i].childList[j]
								
								if(item.id==this.opCompanyId){
									//this.current2=[i,j]
									this.device=item.name
								}	
							}
						}
						
					}
					
					this.companyList = list;
					
					
			
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			myLoadmore() {
				this.pageIndex += 1;
				this.getDeviceStatusMonitoring();
			},
			//设备查询
			deviceChange(e) {
				// console.log(e);
				this.deviceType = e[0].value;
				this.device = e[0].label;
				
				this.getDeviceStatusMonitoring(1);
			},
			deviceStatusChange(e){
				this.typeName =e[0].label;
				this.deviceType= e[0].value;
				this.getDeviceStatusMonitoring(1);
			},
			//状态查询
			statusChange(e) {
				// console.log(e);
				this.status = e[0].value;
				this.statusName = e[0].label;
				
				this.getDeviceStatusMonitoring(1);
			},
			getDeviceStatusMonitoringApi(bl) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.deviceList = [];
					this.pageIndex = 1;
				}
				API.deviceStatusMonitoring({
					pageIndex: this.pageIndex,
					pageSize: 20,
					deviceType: this.deviceType,
					status: this.status,
					companyId:this.companyId,
					classify:"1"
				}).then((response) => {
					uni.hideLoading();
					
					this.deviceList = [
						...this.deviceList,
						...response.data.data
					];
					
					this.recordsTotal = response.data.recordsTotal;
					
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			getDeviceStatusMonitoringWaterApi(bl) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.deviceList = [];
					this.pageIndex = 1;
				}
				API_water.deviceStatusMonitoring({
					pageIndex: this.pageIndex,
					pageSize: 20,
					deviceType: this.deviceType,
					status: this.status,
					companyId:this.companyId,
					classify:"1"
				}).then((response) => {
					uni.hideLoading();
					
					this.deviceList = [
						...this.deviceList,
						...response.data.data
					];
					
					this.recordsTotal = response.data.recordsTotal;
					
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			getDeviceStatusMonitoring(bl) {
				if(this.deviceType=="1"){
					this.getDeviceStatusMonitoringWaterApi(bl)
				}else{
					this.getDeviceStatusMonitoringApi(bl)
				}
			},
			//查询设备类型list
			getDeviceTypeList() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API_electricityMeter.findByName({
					name: '设备类型'
				}).then((res) => {
					uni.hideLoading();
					var list = [];
					list = res.data.map(item => {
						return {
							label: item.name,
							value: item.value
						}
					})
					list.unshift({
						label: '全部设备',
						value: ''
					})
					this.deviceTypeList = list;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			toElectronicMonitoring(item) {
				var key=""
				if(this.deviceType=="1"){
					key="Water"
				}
				uni.navigateTo({
					url: '/pages/equipmentDataMonitoring/electronicMonitoring'+key+'?id=' + item.id + '&name=' + item.name +
						'&companyId=' + item.companyId
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		//padding-bottom: 100px;
	}
	.dropdownMain {
		position: sticky;
		top: 88rpx;
		background-color: #fff;
		z-index: 999;
	}
	.dropdown {
		
		
		
		
		padding: 18rpx 46rpx;
		display: flex;
		border-bottom: 1px solid rgba(245, 245, 245, 1);

		.dropdown-item {
			width: 50%;
			text-align: center;
			height: 60rpx;
			line-height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.text {
				max-width: 80%;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}

	.main {
		background-color: #fff;
		padding: 0 32rpx;

		.item:last-of-type {
			border: none;
		}

		.item {
			padding: 32rpx 0;
			display: flex;
			
			border-bottom: 1px solid rgba(245, 245, 245, 1);

			.item-icon {
				width: 72rpx;
				height: 72rpx;
				border-radius: 4px;
				background-color: rgba(219, 234, 255, 1);
				display: flex;
				align-items: center;
				justify-content: center;

				.img {
					width: 48rpx;
					height: 48rpx;
				}
			}

			.item-content {
				flex: 1;
				margin-left: 16rpx;
				.content3 {
					color: rgba(119, 119, 119, 1);
					font-size: 24rpx;
				}
				
				.content1 {
					display: flex;
					align-items: center;

					.name1 {
						color: rgba(51, 51, 51, 1);
						font-weight: bold;
					
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.name2 {
						color: rgba(119, 119, 119, 1);
						font-size: 24rpx;
						margin-left: 16rpx;
					}
				}

				.content2 {
					margin-top: 4rpx;

					display: flex;
					align-items: center;
					justify-content: space-between;

					.text {
						color: rgba(119, 119, 119, 1);
						font-size: 24rpx;
					}

				}

				.content3 {
					margin-top: 8rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding-right: 16rpx;

					.state-item {
						display: flex;

						color: rgba(51, 51, 51, 1);
						font-size: 24rpx;

						.state-value {
							color: #00B962;
						}

						.state-value2 {
							color: #FF6923;
						}
					}
				}
			}
		}

		.item-state {
			margin-left: auto;
			display: flex;
			align-items: center;

			.dot {

				width: 16rpx;
				height: 16rpx;
				border-radius: 999px;


			}

			.text {
				margin: 0 8rpx;
				color: rgba(51, 51, 51, 1);
				font-size: 24rpx;
			}

			.off-line {

				background-color: rgba(255, 123, 0, 1);

			}

			.on-line {
				background-color: rgba(0, 185, 98, 1);
			}
		}
	}
	
	.tabs-box {
		background-color: #fff;
		display: flex;
		align-items: center;
		
		padding-right: 32rpx;
		justify-content: space-between;
		border-bottom: 1px solid rgba(232, 232, 232, 1);
	
		.tabs {
			width: 97%;
		}
		.more{
			padding-right: 12rpx;
		}
	
		.icon {
			margin-left: auto;
			margin-right: 24rpx;
		}
	}
	.tabs-len-1{
		
	}
	.tabs-len-2{
		/deep/.u-tab-item {
			// max-width: 39% !important;
			// flex: none !important;
		}
	}
	.tabs-len-3{
		/deep/.u-tab-item {
			max-width: 39% !important;
			flex: none !important;
		}
	}
	.tabs-len-4{
		/deep/.u-tab-item {
			max-width: 26% !important;
			flex: none !important;
		}
	}
	/deep/.u-tab-item:first-child {
		width: 20% !important;
		flex: none !important;
	}
	
	.popup-tabs {
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-right: 32rpx;
		color: rgba(51, 51, 51, 1);
		font-size: 32rpx;
		padding-top: 32rpx;
	
		.tabs {
			width: 88%;
		}
	}
	
	.tabs-options {
		display: flex;
		padding: 64rpx 32rpx 0;
		flex-wrap: wrap;
	
		.item {
			width: 25%;
			margin-bottom: 40rpx;
		}
	}
	.electronicMonitoring-title{
			display: flex;
			    align-items: center;
			padding: 0px 32rpx;
			    justify-content: space-between;
				height: 120rpx;
				font-size: 36rpx;
	background-color: rgba(22,119,255,1);
	color:#fff;
	
	
		}
</style>