<template>
	<view class="device-alert-log">
		<view class="nav-bar">
			<view class="left-wrap">
				<view v-if="!isSearch">
					<u-icon name="search" size="23" @click="isSearch = true"></u-icon>
				</view>
				<view v-else style="width: 100%;">
					<!-- #ifndef APP-NVUE -->
					<u-input :customStyle="{ padding: '17rpx 36rpx', background: '#FFFFFF' }"
						v-model="queryParams.alertName" :placeholder="$tt('alert.inputAlertName')" shape="circle"
						@clear="handleClearSearch" clearable>
					<!-- #endif -->
						<!-- #ifdef APP-NVUE -->
						<u--input :customStyle="{ padding: '17rpx 36rpx', background: '#FFFFFF' }"
							v-model="queryParams.alertName" :placeholder="$tt('alert.inputAlertName')" shape="circle"
							@clear="handleClearSearch" clearable>
						<!-- #endif -->
							<template slot="prefix">
								<u-icon name="search" color="rgb(192, 196, 204)" size="26"
									@click="isSearch = false"></u-icon>
							</template>
							<template slot="suffix">
								<view style="display: flex; flex-direction: row; align-items: center;">
									<span
										style="width: 0px; height: 14px; border: 1px solid #000000; opacity: 0.1;"></span>
									<span style="font-size: 14px; font-weight: 400; color: #3378FE; margin-left: 24rpx;"
										@click="handleSearch">{{$tt('common.search')}}</span>
								</view>
							</template>
					<!-- #ifndef APP-NVUE -->
					</u-input>
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					</u--input>
					<!-- #endif -->
				</view>
			</view>
		</view>
		<view class="log-content">
			<view class="item-wrap" v-for="(item, index) in dataList" :key="index" @click="handleDispose(item)">
				<view class="title">
					<view class="name">{{item.deviceName}}</view>
					<view class="status-item"
						:class="{'successBg': item.alertLevel === 1, 'warningBg': item.alertLevel === 2, 'errorBg': item.alertLevel === 3}">
						{{ getaLertLevelDisplay(item) }}
					</view>
				</view>
				<view class="describe">
					<u-row>
						<u-col :span="8">
							<view class="item">{{$tt('alert.alertName')}}：{{item.alertName}}</view>
						</u-col>
						<u-col :span="6">
							<view class="item">
								{{$tt('alert.processState')}}：<text
									:class="{'success': item.status === 1 || item.status === 3, 'warning' : item.status === 2}">
									{{ getaStatusDisplay(item) }}</text>
							</view>
						</u-col>
					</u-row>
					<view class="item">{{$tt('alert.data')}}：<rich-text
							:nodes="getaDataDetailDisplay(item)"></rich-text></view>
					<view class="item">{{$tt('alert.alertTime')}}：{{item.createTime}}</view>
				</view>
				<view class="tools" v-if="item.status === 2">
					<view class="btn"><u-button type="primary" size="mini" :text="$tt('alert.process')"></u-button>
					</view>
				</view>
			</view>
			<u-empty mode="data" :show="total === 0" marginTop="60" :text="$tt('scene.emptyData')"></u-empty>
		</view>
		<u-loadmore :status="loadmoreStatus" v-if="total > queryParams.pageSize"
			:loading-text="$tt('timing.effortLoading')" :loadmoreText="$tt('timing.loadMore')"
			:nomoreText="$tt('timing.nothing')" marginTop="20" @loadmore="loadMoreLogs" />
	</view>
</template>

<script>
	import { listAlertLog } from '@/apis/modules/device.js';
	import { getAlertTemplateId } from '@/apis/modules/alertLog.js';

	export default {
		name: "DeviceAlertLog",
		props: {
			device: {
				type: Object,
				default: null,
				required: true
			}
		},
		watch: {
			// 兼容小程序
			device: function(newVal, oldVal) {
				this.deviceInfo = newVal;
				if (newVal.deviceId && newVal.deviceId !== oldVal.deviceId) {
					this.queryParams.serialNumber = newVal.serialNumber;
					this.getAlertLogList();
				}
			},
		},
		data() {
			return {
				isSearch: true, // 是否开启搜索框
				queryParams: {
					alertName: null,
					serialNumber: null,
					pageNum: 1,
					pageSize: 10,
				},
				dataList: [],
				total: 0, // 总条数
				loadmoreStatus: 'loadmore', // 加载更多
				tmpIds: null, // 模版id
			};
		},
		mounted() {
			const { deviceId, serialNumber } = this.device;
			if (deviceId) {
				this.queryParams.serialNumber = serialNumber;
				this.getAlertLogList();
			}
			this.getTmplIds(); // 获取模版id
		},
		methods: {
			getAlertLogList() {
				listAlertLog(this.queryParams).then((res) => {
					if (res.code === 200) {
						if (this.queryParams.pageNum == 1) {
							this.dataList = res.rows;
						} else {
							this.dataList = this.dataList.concat(res.rows);
						}
						this.total = res.total;
						setTimeout(() => {
							if ((this.queryParams.pageNum - 1) * this.queryParams.pageSize >= this.total) {
								this.loadmoreStatus = 'nomore';
							} else {
								this.loadmoreStatus = 'loadmore';
							}
						}, 1000);
					}
					uni.stopPullDownRefresh();
				});
			},
			handleSearch() {
				this.dataList = [];
				this.queryParams.pageNum = 1;
				this.getAlertLogList();
			},
			handleClearSearch() {
				this.handleSearch();
			},
			// 处理
			handleDispose(item) {
				// 订阅通知
				// #ifdef MP-WEIXIN
				uni.requestSubscribeMessage({
					tmplIds: [this.tmpIds],
					success(res) {
						console.log(res);
					},
					fail(err) {
						console.error(err); //失败
					}
				});
				// #endif
				uni.navigateTo({
					url: `/pagesB/alert/detail?alertLogId=${item.alertLogId}&source=1`
				});
			},
			// 获取模板id
			getTmplIds() {
				getAlertTemplateId().then(res => {
					if (res.code == 200) {
						if (res.msg != '') {
							this.tmpIds = res.msg;
						} else {
							console.log('模板id为空！');
						}
					} else {
						console.log(res.msg);
					}
				});
			},
			// 获取告警级别显示
			getaLertLevelDisplay(item) {
				const { alertLevel } = item;
				if (alertLevel === 1) {
					return this.$tt('alert.notice');
				} else if (alertLevel === 2) {
					return this.$tt('alert.minor');
				} else if (alertLevel === 3) {
					return this.$tt('alert.warning');
				}
			},
			// 获取告警状态显示
			getaStatusDisplay(item) {
				const {
					status
				} = item;
				if (status === 1) {
					return this.$tt('alert.noNeed');
				} else if (status === 2) {
					return this.$tt('alert.Unprocessed');
				} else if (status === 3) {
					return this.$tt('alert.processed');
				}
			},
			// 获取告警数据内容
			getaDataDetailDisplay(item) {
				const { detail } = item;
				if (detail) {
					const detailObj = JSON.parse(detail);
					return `<div>id：<span>${detailObj.id}</span>，value：<span>${detailObj.value}</span></div>`
				}
			},
			// 上拉加载
			loadMoreLogs() {
				this.loadmoreStatus = 'loading';
				this.queryParams.pageNum = ++this.queryParams.pageNum;
				// 模拟网络请求
				setTimeout(() => {
					if ((this.queryParams.pageNum - 1) * this.queryParams.pageSize >= this.total) {
						this.loadmoreStatus = 'nomore';
					} else {
						this.loadmoreStatus = 'loading';
						this.getAlertLogList();
					}
				}, 1000);
			},
			// 下拉刷新
			onPullDownRefresh() {
				this.dataList = [];
				this.queryParams.pageNum = 1;
				// 模拟网络请求
				setTimeout(x => {
					this.getList();
					uni.stopPullDownRefresh();
				}, 1000);
			},
		}
	}
</script>

<style lang="scss">
	.device-alert-log {
		width: 100%;

		.nav-bar {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			height: 74rpx;
			margin-top: 15rpx;

			.left-wrap {
				flex: 1;
				display: flex;
				flex-direction: row;
				align-items: center;
			}
		}

		.log-content {
			width: 100%;
			margin-top: 30rpx;
			z-index: 10;

			.item-wrap {
				box-shadow: 0 2rpx 0px 0 rgba(0, 0, 0, 0.1);
				border-radius: 20rpx;
				padding: 10rpx 20rpx;
				background-color: #ffffff;

				&:not(:last-child) {
					margin-bottom: 20rpx;
				}

				.title {
					position: relative;
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					border-bottom: 1px solid #eaecef;
					padding: 20rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 32rpx;


					.name {
						flex: 1;
						font-size: 32rpx;
						margin-right: 20rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.status-item {
						position: absolute;
						right: -20rpx;
						top: -10rpx;
						width: 64px;
						height: 24px;
						// background: #486FF2;
						font-weight: 400;
						font-size: 20rpx;
						color: #FFFFFF;
						line-height: 28rpx;
						font-style: normal;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						border-top-right-radius: 20rpx;
						border-bottom-left-radius: 20rpx;
					}
				}

				.describe {
					font-size: 28rpx;
					margin: 20rpx;

					.item {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 26rpx;
						color: #000000;
						line-height: 40rpx;
						text-align: left;
						font-style: normal;
						text-transform: none;
						display: flex;
						padding: 16rpx 0;
						width: 100%;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}

				.tools {
					display: flex;
					flex-direction: row;
					justify-content: flex-end;
					margin-bottom: 10rpx;
				}
			}
		}

		.success {
			color: #5ac725;
		}

		.warning {
			color: #f9ae3d;
		}

		.error {
			color: #f56c6c;
		}

		.successBg {
			background: #5ac725;
		}

		.warningBg {
			background: #f9ae3d;
		}

		.errorBg {
			background: #f56c6c;
		}
	}
</style>