<template>
	<view>
		<u-navbar title="异常告警记录" title-color="#101010"></u-navbar>
		<view class="dropdown">
			<view class="dropdown-item" @click="show1=true">
				{{time}} <u-icon name="arrow-down" color="#999999"></u-icon>
			</view>
			<view class="dropdown-item" @click="show2=true">
				{{state}} <u-icon name="arrow-down" color="#999999"></u-icon>
			</view>
			<view class="dropdown-item" @click="tabsFrom.show1=true">
				{{device}} <u-icon name="arrow-down" color="#999999"></u-icon>
			</view>
		</view>

		<u-picker mode="time" :defaultTime="defaultTime" v-model="show1" :params="params" @confirm="timeChange"></u-picker>
		
		<u-select v-model="show2" mode="single-column" :list="stateList" @confirm="stateChange"></u-select>
		
		<u-select v-model="show3" mode="single-column" :list="deviceList" @confirm="deviceChange"></u-select>
		<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="records">
			<view class="records-item" v-for="(item, index) in abnormalRecordsList" :key="index"
				@click="gotoUrl('/pages/abnormal/abnormalAlarmDetails?id='+item.id)">
				<view class="icon icon1">
					<image class="img" src="@/assets/img/riFill-temp-cold-fill.svg" mode=""></image>
				</view>
				<!-- <view class="icon icon2">
					<image class="img" src="@/assets/img/riFill-cloud-off-fill.svg" mode=""></image>
				</view>
				<view class="icon icon3">
					<image class="img" src="@/assets/img/outputVoltage.svg" mode=""></image>
				</view> -->
				<view class="title">
					<view class="name">
						{{item.configName}}
					</view>
					<view class="date">
						{{item.createTime}}
					</view>
				</view>

				<view class="equipment">
					<view class="equipment1">
						{{item.meterName}}
					</view>
					<view class="equipment2">
						{{item.installationAddressSimple}}
					</view>
				</view>
				<view class="more">
					<u-icon name="arrow-right" color="#acacac"></u-icon>
				</view>
			</view>
		</view>
		
		<u-divider :isnone="abnormalRecordsList.length==0" nonetext="暂无异常告警记录" border-color="#CFD2D5">
		</u-divider>
	</view>
</template>

<script>
	import {
		parseUnixTime,
		beforeTimeStamp,
		getWeek
	} from '@/apis/utils'
	import * as API from '@/apis/pagejs/index.js'
	
	export default {
		data() {
			return {
				abnormalRecordsList: [], // 异常告警记录
				queryDate: '',
				configId: '',
				pageIndex: 1,
				recordsTotal: 0,
				companyId: '',
				show1: false, // 时间选择
				defaultTime: '',
				params: {
					year: true,
					month: true,
					day: false,
					hour: false,
					minute: false,
					second: false,
					timestamp: false
				},
				time: '全部时间',
				show2: false, // 状态选择
				state: '全部状态',
				stateList: [],
				show3: false, // 设备选择
				device: '全部设备',
				deviceList: [],
				value1: 1,
				value2: 1,
				value3: 1,
				current2:[0,0],
				tabsFrom: {
					show1: false,
					show1Index: 0,
					show2Index: '',
					show2: false,
					show1Text: "全部类型",
					show2Text: "全部时间",
					
				},
				companyList:[],
				options1: [{
						label: '2024年2月',
						value: 1,
					},
					{
						label: '2024年3月',
						value: 2,
					},
					{
						label: '2024年4月',
						value: 3,
					}
				],
				options2: [{
						label: '设备离线',
						value: 1,
					},
					{
						label: '温度异常',
						value: 2,
					},
					{
						label: '电压异常',
						value: 3,
					},
					{
						label: '功率因数异常',
						value: 4,
					},
				],
				options3: [{
						label: '荆鹏集团',
						value: 1,
					},
					{
						label: '青少年宫',
						value: 2,
					},
					{
						label: '荆州院子',
						value: 3,
					},
				],
			}
		},
		onLoad(op) {
			if(op.id) {
				this.companyId = op.id;
			}
			
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth()+1 >= 10 ? date.getMonth()+1 : '0'+(date.getMonth()+1);
			this.defaultTime = year + '-' + month;
			this.queryDate = year + '-' + month;
			this.time = year + '年' + parseInt(month) + '月';
			
			this.getCompanyInfoList();
			this.getAlarmConfiguration();
			this.getAbnormalAlarmRecord();
		},
		onReady() {
			
		},
		onReachBottom() {
			if (this.abnormalRecordsList.length < this.recordsTotal) {
				this.myLoadmore();
			}
		},
		methods: {
			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:"全部设备"
							}
						]
					})
					
					this.companyList = list;
					
					
			
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			// 异常告警记录
			getAbnormalAlarmRecord(bl) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.abnormalRecordsList = [];
					this.pageIndex = 1;
				}
				API.alarmRecord({
					queryDate: this.queryDate,
					configId: this.configId,
					pageIndex: this.pageIndex,
					pageSize: 20,
					companyId: this.companyId,
					classify: '2'
				}).then((res) => {
					uni.hideLoading();
					this.abnormalRecordsList = [
						...this.abnormalRecordsList,
						...res.data.data
					];
					this.recordsTotal = res.data.recordsTotal;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			myLoadmore() {
				this.pageIndex += 1;
				this.getAbnormalAlarmRecord();
			},
			// 异常查询条件
			getAlarmConfiguration() {
				// uni.showLoading({
				// 	title: "加载中",
				// 	mask: true,
				// })
				API.alarmConfiguration({classify: '2'}).then((response) => {
					//uni.hideLoading();
					var list1 = [];
					var list2 = [];
					if(response.data.alarmConfigList && response.data.alarmConfigList.length != 0) {
						list1 = response.data.alarmConfigList.map(item => {
							return {
								label: item.name,
								value: item.id
							}
						});
					}
					list1.unshift({
						value: '',
						label: '全部状态'
					});
					
					if(response.data.companyInfoList && response.data.companyInfoList.length != 0) {
						list2 = response.data.companyInfoList.map(item => {
							if(item.id == this.companyId) {
								this.device = item.name;
							}
							return {
								label: item.name,
								value: item.id
							}
						});
					}
					list2.unshift({
						value: '',
						label: '全部设备'
					});
					
					this.stateList = list1;
					this.deviceList = list2;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			// 时间查询
			timeChange(params) {
				console.log(params)
				this.time = params.year + '年' + parseInt(params.month) + '月';
				this.queryDate = params.year + '-' + params.month;
				
				this.getAbnormalAlarmRecord(true);
			},
			// 状态查询
			stateChange(e) {
				console.log(e);
				this.configId = e[0].value;
				this.state = e[0].label;
				
				this.getAbnormalAlarmRecord(true);
			},
			selector1confirm(e){
				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.getAbnormalAlarmRecord(true);
			},
			// 设备查询
			deviceChange(e) {
				console.log(e);
				this.companyId = e[0].value;
				this.device = e[0].label;
				
				this.getAbnormalAlarmRecord(true);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.dropdown {
		background-color: #fff;
		position: sticky;
		top: 88rpx;
		z-index: 999;
		padding: 18rpx 46rpx;
		display: flex;
		border-bottom: 1px solid rgba(245, 245, 245, 1);
		.dropdown-item{
			width: 33.33%;
			text-align: center;
			height: 60rpx;
			line-height: 60rpx;
		}
	}

	// 记录
	.records {
		background-color: #fff;
		// margin-top: 80rpx;

		.records-item {
			display: flex;
			align-items: center;
			padding: 24rpx 40rpx;
			border-top: 1px solid rgba(245, 245, 245, 1);

			.icon {
				min-width: 72rpx;
				width: 72rpx;
				height: 72rpx;
				border-radius: 4px;
				display: flex;
				align-items: center;
				justify-content: center;

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

			.icon1 {
				background-color: rgba(255, 227, 218, 1);
			}

			.icon2 {
				background-color: rgba(230, 230, 230, 1);
			}

			.icon3 {
				background-color: rgba(212, 251, 220, 1);
			}

			.title {
				margin-left: 16rpx;
				white-space: pre;
				.name {
					color: rgba(51, 51, 51, 1);
					font-weight: bold;
				}

				.date {
					color: rgba(119, 119, 119, 1);
					font-size: 24rpx;
					margin-top: 4rpx;
				}
			}

			.equipment {
				margin-left: auto;
				margin-right: 24rpx;

				.equipment1 {
					color: rgba(51, 51, 51, 1);
					font-weight: bold;
				}

				.equipment2 {
					color: rgba(119, 119, 119, 1);
					font-size: 24rpx;
					text-align: right;
					margin-top: 4rpx;
				}
			}
		}
	}
</style>