<template>
	<view>
		<u-navbar title="租户断电提醒" title-color="#101010"></u-navbar>
		<u-tabs :list="list" :is-scroll="false" 
		:current="current" @change="change"></u-tabs>

		
		<view class="records">
			<view class="records-item" v-for="(item, index) in abnormalRecordsList" :key="index"
				>
				
				<!-- <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">
						<span v-if="item.level==1" class="level level1 ">一级</span>
						<span v-if="item.level==2"  class="level level2">二级</span>
						
						{{item.content}}
					</view>
					<view class="date" style="display: flex;    justify-content: space-between;">
						<view>{{item.title}}</view>
						<view>{{item.createTime}}</view>
						
					</view>
				</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/energyManage.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,
				
				list: [{
									name: '一级预警',
									level:'1'
								}, {
									name: '二级预警',
									level:'2'
								}],
								current: 0,
								
								
				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.getAlarmConfiguration();
			this.getAbnormalAlarmRecord();
		},
		onReady() {
			
		},
		onReachBottom() {
			if (this.abnormalRecordsList.length < this.recordsTotal) {
				this.myLoadmore();
			}
		},
		methods: {
			// 异常告警记录
			getAbnormalAlarmRecord(bl) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.abnormalRecordsList = [];
					this.pageIndex = 1;
				}
				API.remindList({
					queryDate: this.queryDate,
					configId: this.configId,
					pageIndex: this.pageIndex,
					pageSize: 20,
					companyId: this.companyId,
					queryParent:this.companyId?1:0,
					level:this.list[this.current].level
				}).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().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"
					})
				})
			},
			change(index){
				this.current = index;
				this.getAbnormalAlarmRecord(true);
			},
			// 时间查询
			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);
			},
			// 设备查询
			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;
		justify-content: space-around;
		border-bottom: 1px solid rgba(245, 245, 245, 1);
		.dropdown-item{
			//width: 47.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);
			
			.level{
				color:#fff;
				
				    border-radius: 8rpx;
				    font-weight: 400;
				    padding: 4rpx 8rpx;
				    font-size: 22rpx;
					margin-right: 2px;
			}
			.level1{
				background: red;
			}
			.level2{
				background-color: rgba(255,123,0,1);
			}
			.icon {
				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;

				.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>