<template>
	<view>
		<u--input v-model="queryParams.alertName" placeholder="请输入告警名称" prefixIcon="search"
			prefixIconStyle="color: #909399" customStyle="height:50rpx;margin:10rpx 20rpx;background-color: #ffffff">
			<template slot="suffix">
				<u-button :text="$tt('scene.search')" type="primary" size="mini" @click="handleSearch"></u-button>
			</template>
		</u--input>
		<view class="event-wrap">
			<view class="alert-item" v-for="(item, index) in dataList" :key="index">
				<view class="left">
				</view>
				<view class="middle">
					<u--text lineHeight="22" :text="item.alertName"></u--text>
					<view style="width:60px;">
						<u-tag text="提醒通知" type="warning" size="mini" :plain="true" v-if="item.status==1"
							customStyle="width:30px;"></u-tag>
						<u-tag text="轻微问题" type="primary" size="mini" :plain="true" v-if="item.status==2"
							customStyle="width:50px;"></u-tag>
						<u-tag text="严重警告" type="success" size="mini" :plain="true" v-if="item.status==3"
							customStyle="width:80px;"></u-tag>
					</view>
				</view>
				<view class="right">
					<u-button :plain="true" v-if="item.status !== 1" type="primary" icon="edit-pen-fill" size="mini"
						:text="`${item.status === 2 ? $tt('alert.process') : $tt('alert.update')}`"
						@click="handleDispose(item)"></u-button>
				</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" />
		<u-loading-page :loading="loading" bg-color="#eef3f7" loadingText="ZYC.cn"></u-loading-page>
	</view>
</template>

<script>
	import {
		listAlertLog
	} from '@/apis/modules/device.js';
	import {
		getAlertTemplateId
	} from '@/apis/modules/alertLog.js';
	export default {
		name: "device-alertLog",
		props: {
			device: {
				type: Object,
				default: null,
				required: true
			}
		},
		watch: {
			// 兼容小程序
			device: function (newVal, oldVal) {
				console.log(newVal)
				this.deviceInfo = newVal;
				if (newVal.deviceId && newVal.deviceId !== oldVal.deviceId) {
					this.queryParams.deviceId = newVal.deviceId;
					this.getAlertLogList();
				}
			},
		},
		data () {
			return {
				queryParams: {
					alertName: '',
					pageNum: 1,
					pageSize: 10,
					serialNumber: '',
				},
				dataList: [],
				form: {},
				total: 0, // 总条数
				loadmoreStatus: 'loadmore', // 加载更多
				loading: false,
			};
		},
		mounted () {
			const {
				deviceId,
				serialNumber
			} = this.device;
			if (deviceId) {
				this.queryParams.deviceId = deviceId;
				this.queryParams.serialNumber = serialNumber;
				this.getAlertLogList();
			}
		},
		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);
					}
					this.loading = false;
					uni.stopPullDownRefresh();
				});
			},
			// 处理
			handleDispose (item) {
				console.log(item, 'kk');
				// 订阅通知
				// #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
				});
			},
			handleSearch () {
				this.queryParams.pageNum = 1;
				this.getAlertLogList();
			},
			//获取模板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);
					}
				});
			},

			// 上拉加载
			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.type === 1 && this.getList();
					uni.stopPullDownRefresh();
				}, 1000);
			},
		}
	}
</script>
<style>
	page {
		background: #eef3f7;
	}
</style>

<style lang="scss" scoped>
	.event-wrap {
		.log-item {
			box-shadow: 0 1px 0px 0 rgba(0, 0, 0, 0.1);
			border-radius: 5px;
			margin: 10px;
			padding: 10px;
			background-color: #ffffff;
		}

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