<template>
	<page-meta>
		<navigation-bar :title="$tt('navBar.alert')" background-color="#007AFF"></navigation-bar>
	</page-meta>

	<view class="alert-wrap">
		<u-sticky>
			<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="$tt('alert.inputAlertName')" shape="circle" @clear="handleClearSearch"
							clearable>
						<!-- #endif -->
							<!-- #ifdef APP-NVUE -->
							<u--input :customStyle="{ padding: '6rpx 18rpx'}" v-model="queryParams.alertName"
								:placeholder="$tt('alert.inputAlertName')" 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('common.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>
			<view class="tabs-wrap">
				<u-tabs :list="tabList" :scrollable="true" lineWidth="40" lineHeight="2" lineColor="transparent"
					:duration="100" :activeStyle="{ fontSize: '36rpx', color: '#3c9cff', fontWeight: 'bold' }"
					@change="handleTabsClick">
				</u-tabs>
			</view>
		</u-sticky>

		<view class="log-content">
			<view class="item-wrap" v-for="(item, index) in datas" :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">{{$tt('alert.alertName')}}：{{item.alertName}}</view>
					<view class="item">
						{{$tt('alert.processState')}}：<text
							:class="{'success': item.status === 1 || item.status === 3, 'warning' : item.status === 2}">
							{{ getaStatusDisplay(item) }}</text>
					</view>
					<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="list" :show="total === 0" marginTop="60"></u-empty>
		</view>
		<u-loadmore :status="loadmoreStatus" v-if="total > queryParams.pageSize" marginTop="20" />
		<u-loading-page :loading="loading" bg-color="#eef3f7" loadingText="XCWL.cn"></u-loading-page>
	</view>
</template>

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

	export default {
		data () {
			return {
				loading: false,
				isSearch: true, // 是否开启搜索框
				// tabs列表
				tabList: [{
					id: null,
					name: this.$tt('alert.all'),
				}, {
					id: 2,
					name: this.$tt('alert.pending'),
				}, {
					id: 1,
					name: this.$tt('alert.untreated'),
				}, {
					id: 3,
					name: this.$tt('alert.processed'),
				}],
				queryParams: {
					alertName: null,
					status: null,
					pageNum: 1,
					pageSize: 10,
				},
				datas: [], // log列表数据
				total: 0, // 总条数
				loadmoreStatus: 'loadmore',
				tmpIds: [], //模板ids
			};
		},
		onShow () {
			// #ifdef MP-WEIXIN
			// 检查用户是否登录
			if (!this.isLogin()) {
				uni.showModal({
					title: this.$tt('common.tips'),
					content: this.$tt('common.loginTips'),
					success: (res) => {
						if (res.confirm) {
							// 用户点击确定，跳转到登录页面
							uni.navigateTo({
								url: '/pages/login/index'
							});
						} else if (res.cancel) {
							uni.switchTab({
								url: '/pages/tabBar/home/index'
							})
							console.log('用户点击取消');
						}
					}
				});
			}
			// #endif
		},
		created () {
			this.getToken();
			if (this.token != '' && this.token != null) {
				this.getDatas();
				this.getTmplIds();
			}
		},
		methods: {
			getToken () {
				// 本地缓存获取token
				this.token = uni.getStorageSync('token');
				// vuex存储token
				uni.$u.vuex('vuex_token', this.token);
				console.log(this.token)
			},
			//判断是否登录
			isLogin () {
				if (this.token == '' || this.token == null) {
					return false;
				} else {
					return true;
				}
			},
			// 获取列表数据
			getDatas () {
				this.loading = true;
				getAlertList(this.queryParams).then(res => {
					const { code, rows, total } = res;
					if (this.queryParams.pageNum === 1) {
						this.datas = rows;
					} else {
						this.datas = this.datas.concat(rows);
					}
					this.total = total;
					this.loading = false;
					uni.stopPullDownRefresh();
				});
			},
			handleSearch () {
				this.datas = [];
				this.queryParams.pageNum = 1;
				this.getDatas();
			},
			handleClearSearch () {
				this.handleSearch();
			},
			// 单击查询
			handleTabsClick (e) {
				this.queryParams.status = e.id;
				this.datas = [];
				this.queryParams.pageNum = 1;
				this.getDatas();
			},
			// 上拉加载
			onReachBottom () {
				this.loadmoreStatus = 'loading';
				this.queryParams.pageNum = this.queryParams.pageNum + 1;
				if ((this.queryParams.pageNum - 1) * this.queryParams.pageSize > this.total) {
					this.loadmoreStatus = 'nomore';
				} else {
					this.getDatas();
					this.loadmoreStatus = 'loadmore';
				}
			},
			// 下拉刷新
			onPullDownRefresh () {
				this.datas = [];
				this.queryParams.pageNum = 1;
				this.getDatas();
			},
			// 获取告警级别显示
			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.untreated');
				} else if (status === 2) {
					return this.$tt('alert.pending');
				} 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 style="color:#F56C6C">${detailObj.id}</span>，value：<span style="color:#F56C6C">${detailObj.value}</span></div>`
				}
			},
			// 处理
			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=2`
				});
			},
			// 获取模板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);
					}
				});
			},
		}
	};
</script>

<style lang="scss">
	page {
		background: #eef3f7;
	}

	.alert-wrap {
		width: 100%;
		padding-bottom: 20rpx;

		.nav-bar {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 20rpx 0;
			height: 74rpx;
			background: #eef3f7;

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

		.tabs-wrap {
			background: #eef3f7;
			padding: 16rpx 10rpx 10rpx;
		}

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

		.content {
			.alert-item {
				display: flex;
				justify-content: center;
				align-items: center;
				box-shadow: 0 1px 0px 0 rgba(0, 0, 0, 0.1);
				border-radius: 5px;
				margin: 10px;
				padding: 10px;
				background-color: #ffffff;

				.left {
					margin-right: 10px;
				}

				.middle {
					flex: 1;
				}

				.right {
					margin-left: 10px;
				}
			}
		}
	}
</style>