<template>
	<view class="device-alert-log">
		<u-sticky offset-top="45" zIndex="98" bgColor="#eef3f7">
			<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: '6rpx 18rpx'}" v-model="queryParams.alertName"
							placeholder="请输入告警名称" shape="circle" @clear="handleClearSearch" clearable>
						<!-- #endif -->
							<!-- #ifdef APP-NVUE -->
							<u--input :customStyle="{ padding: '6rpx 18rpx'}" v-model="queryParams.alertName"
								placeholder="请输入告警名称" shape="circle" @clear="handleClearSearch" clearable>
							<!-- #endif -->
								<template slot="prefix">
									<u-icon name="search" size="22" @click="isSearch = false"></u-icon>
								</template>
								<template slot="suffix">
									<u-button :text="$tt('scene.search')" type="primary" shape="circle" size="mini"
										@click="handleSearch"></u-button>
								</template>
						<!-- #ifndef APP-NVUE -->
						</u-input>
						<!-- #endif -->
						<!-- #ifdef APP-NVUE -->
						</u--input>
						<!-- #endif -->
					</view>
				</view>
			</view>
		</u-sticky>
		<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"
						:class="{'success': item.alertLevel === 1, 'warning': item.alertLevel === 2, 'error': item.alertLevel === 3}">
						{{ getaLertLevelDisplay(item) }}
					</view>
				</view>
				<view class="describe">
					<view class="item">告警名称：{{item.alertName}}</view>
					<view class="item">
						处理状态：<text
							:class="{'success': item.status === 1 || item.status === 3, 'warning' : item.status === 2}">
							{{ getaStatusDisplay(item) }}</text>
					</view>
					<view class="item">数据：<rich-text :nodes="getaDataDetailDisplay(item)"></rich-text></view>
					<view class="item">告警时间：{{item.createTime}}</view>
				</view>
				<view class="tools" v-if="item.status === 2">
					<view class="btn"><u-button type="primary" size="mini" text="处理"></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="努力加载中..."
			loadmoreText="点击我加载更多" nomoreText="实在没有了" 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: `/pages/tabBar/alert/edit?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 '提醒通知';
				} else if (alertLevel === 2) {
					return '轻微问题';
				} else if (alertLevel === 3) {
					return '严重警告';
				}
			},
			// 获取告警状态显示
			getaStatusDisplay (item) {
				const { status } = item;
				if (status === 1) {
					return '不需要处理';
				} else if (status === 2) {
					return '未处理';
				} else if (status === 3) {
					return '已处理';
				}
			},
			// 获取告警数据内容
			getaDataDetailDisplay (item) {
				const { detail } = item;
				if (detail) {
					const detailObj = JSON.parse(detail);
					return `<div>id：<span style="color:#F56C6C">${detailObj.id}</span>，value：<span style="color:#F56C6C">${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">
	page {
		background: #eef3f7;
	}

	.device-alert-log {
		.nav-bar {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx;
			height: 74rpx;

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

		.log-content {
			width: 100%;

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

				&:first-child {
					margin-top: 0;
				}

				.title {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					border-bottom: 1px solid #eaecef;
					padding: 20rpx 0;

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

					.status {
						font-size: 24rpx;
					}
				}

				.describe {
					font-size: 28rpx;

					.item {
						display: flex;
						margin: 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;
		}
	}
</style>