<template>
	<view class="page-news">
		<uni-list ref="list" class="listview" :Showscrollbar="true" :enableBackToTop="true" :scroll-y="true" @scrolltolower="loadMore()">
			<uni-refresh class="refresh" @refresh="onrefresh" @pullingdown="onpullingdown"
				:display="refreshing ? 'show' : 'hide'">
				<div class="refresh-view">
					<image class="refresh-icon" :src="refreshIcon" :style="{width: (refreshing || pulling) ? 0: '32px'}"
						:class="{'refresh-icon-active': refreshFlag}"></image>
					<uni-load-more v-if="refreshing" class="loading-icon" status="loading"
						:contentText="loadingMoreText"></uni-load-more>
					<text class="loading-text">{{refreshText}}</text>
				</div>
			</uni-refresh>
				<uni-cell v-for="(item, index) in dataList" :key="item.id">
					<news-item :newsItem="item" @click="goDetail(item)"></news-item>
				</uni-cell>
			<uni-cell>
				<view id="loading-more" class="loading-more">
					<view class="creatView" v-if="isNoData">
						<image class="imsa" src="../../../static/nodata.png" mode=""></image>
						<text class="c-te">暂无消息</text>
					</view>
					<text class="loading-more-text">{{loadingText}}</text>
				</view>
			</uni-cell>

		</uni-list>
	</view>
</template>

<script>
	import uniList from '@/components/uni-list.vue';
	import uniCell from '@/components/uni-cell.vue';
	import uniRefresh from '@/components/uni-refresh.vue';
	import uniLoadMore from '@/components/uni-load-more.vue';
	import newsItem from './detailPage.nvue';
	import {
		getMessageList,
		readAll,
		getReadNum
	} from '@/common/api.js'
	export default {
		components: {
			uniList,
			uniCell,
			uniRefresh,
			uniLoadMore,
			newsItem
		},
		props: {
			nid: {
				type: [Number, String],
				default: ''
			},
		},
		data() {
			return {
				searchContent: "",
				dataList: [],
				navigateFlag: false,
				pulling: false,
				refreshing: false,
				refreshFlag: false,
				refreshText: "",
				isLoading: false,
				loadingText: '加载中...',
				isNoData: false,
				pulling: false,
				angle: 0,
				loadingMoreText: {
					contentdown: '',
					contentrefresh: '',
					contentnomore: ''
				},
				requestParams: {},
				refreshIcon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAB5QTFRFcHBw3Nzct7e39vb2ycnJioqK7e3tpqam29vb////D8oK7wAAAAp0Uk5T////////////ALLMLM8AAABxSURBVHja7JVBDoAgDASrjqj//7CJBi90iyYeOHTPMwmFZrHjYyyFYYUy1bwUZqtJIYVxhf1a6u0R7iUvWsCcrEtwJHp8MwMdvh2amHduiZD3rpWId9+BgPd7Cc2LIkPyqvlQvKxKBJ//Qwq/CacAAwDUv0a0YuKhzgAAAABJRU5ErkJggg=="
			}
		},
		onShow() {
		    this.loadData(true);
		},
		created() {
					 var that = this;
                     /* setInterval(function(){
                        if(uni.getStorageSync("getRealNews")==1){
                        console.log("+++++++++实时告警请求--------------")
							that.requestParams.pageIndex = 1;
					        that.loadData(true);
                        }
                     },5000) */
			this.pullTimer = null;
			this.requestParams = {
				pageIndex: 1,
				pageSize: 10,

			};
			uni.$on("readAll",() =>{
				if(this.isNoData) return
				readAll({
					userId: this.$store.state.Token
				}).then(res =>{
					this.pulling = true;
					this.refreshing = true;
					this.refreshText = "正在刷新...";
					this.clear()
					this.loadData(true);
				})
			})
		},
		onPageScroll(e){ //根据距离顶部距离是否显示回到顶部按钮
		console.log(111111111111)
		    if(e.scrollTop>600){ //当距离大于600时显示回到顶部按钮
		        this.topState = true
		    }else{ //当距离小于600时显示回到顶部按钮
		console.log(111111111111)
		        this.topState = false
		    }
		},
		methods: {
			loadData(refresh) {
				if (this.isLoading) {
					return;
				}
				this.isLoading = true;
				this.isNoData = false;
				getReadNum({
					 userId: this.$store.state.Token
                }).then(res => {
                     if(res==0){
                        uni.hideTabBarRedDot({ //隐藏红点
                          index: 2
                        })
                     }else{
                        uni.showTabBarRedDot({ //显示红点
                          index: 2
                        })
                     }
                }).catch(e => {
                     console.log(e)
                })
				getMessageList({
					...this.requestParams,
					 userId: this.$store.state.Token
				}, (e) => {

					this.isLoading = false;
					if (refresh) {
						this.refreshing = false;
						this.refreshFlag = false;
						this.refreshText = "已刷新";
						if (this.pullTimer) {
							clearTimeout(this.pullTimer);
						}
						this.pullTimer = setTimeout(() => {
							this.pulling = false;
						}, 500);
					}
				}).then(result => {
					let data_list = result;
					if (data_list.length < 10) {
						this.loadingText = "没有更多了~"
						//#ifdef MP-WEIXIN
						this.isLoading = true
						//#endif
					}
					if (refresh) { //如果是刷新页面,清空数据
						this.dataList = data_list;
						this.requestParams.pageIndex = 1;
					} else { //加载更多
						this.dataList = this.dataList.concat(data_list);
					}
					if (this.dataList.length == 0) {
						this.isNoData = true
						this.loadingText = ""
					}

				}).catch(e => {

				})
			},
			loadMore(e) {
				if (this.isNoData) {
					return
				}
				++this.requestParams.pageIndex;
				this.loadData();
				let info = uni.createSelectorQuery().select("#loading-more");  
				    　　　  info.boundingClientRect(function(data) { //data - 各种参数  
				             console.log(data.top)  
				　　    }).exec()　
			},
			clear() {
				this.dataList = [];
				this.loadingText = "加载中..."
				this.requestParams.pageIndex = 1;
			},
			goDetail(detail) {
				var that = this
				uni.navigateTo({
					url:'/pages/webview/newsDetail'
				})
				console.log(detail)
				uni.setStorageSync("newsDetailxjapp",detail)
				// uni.navigateTo({
				// 	url: `/pages/hybrid/index?url=notiveDetail`,
				// 	success: function(res) {
				// 		// 通过eventChannel向被打开页面传送数据
				// 		res.eventChannel.emit('acceptDataFromOpenerPage', {
				// 			value: detail.id
				// 		})
				// 	}
				// })
				detail.readFlag = 1

				getReadNum({
					 userId: that.$store.state.Token
                }).then(res => {
                     if(res==0){
                        uni.hideTabBarRedDot({ //隐藏红点
                          index: 2
                        })
                     }else{
                        uni.showTabBarRedDot({ //显示红点
                          index: 2
                        })
                     }
                }).catch(e => {
                     console.log(e)
                })
			},
			refreshData() {
				if (this.isLoading) {
					return;
				}
				this.pulling = true;
				this.refreshing = true;
				this.refreshText = "正在刷新...";
				this.clear()
				this.loadData(true);
			},
			onrefresh(e) {
				this.isNoData = false
				this.loadingText = "加载中..."
				this.refreshData();
				this.$refs.list.resetLoadmore();
			},
			onpullingdown(e) {
				if (this.refreshing) {
					return;
				}

				this.pulling = false;

				if (Math.abs(e.pullingDistance) > Math.abs(e.viewHeight)) {
					this.refreshFlag = true;
					this.refreshText = "释放立即刷新";
				} else {
					this.refreshFlag = false;
					this.refreshText = "下拉可以刷新";
				}
			},
		}
	}
</script>

<style scoped >
	.creatView {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;

	}
	.c-te{
		color: #0581FD;
		padding:30rpx;
		font-size: 30rpx;
	}
	.imsa {
		margin-top:260rpx;
		width: 387rpx;
		height: 200rpx;
	}
	.no-data {
		flex: 1;
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 10;
	}

	.page-news {
		flex: 1;
		flex-direction: column;
		position: absolute;

		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
	}

	.listview {
		position: absolute;
		left: 0;
		background-color: #F9F9F9;
		top: 0;
		right: 0;
		bottom: 0;
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: column;
		/* #endif */
		/* #ifndef MP-ALIPAY */
		flex-direction: column;
		/* #endif */
	}

	.refresh {
		justify-content: center;
	}

	.refresh-view {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		width: 750rpx;
		height: 64px;
		flex-direction: row;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: center;
	}

	.refresh-icon {
		width: 32px;
		height: 32px;
		transition-duration: .5s;
		transition-property: transform;
		transform: rotate(0deg);
		transform-origin: 15px 15px;
	}

	.refresh-icon-active {
		transform: rotate(180deg);
	}

	.loading-icon {
		width: 28px;
		height: 28px;
		margin-right: 5px;
		color: gray;
	}

	.loading-text {
		margin-left: 2px;
		font-size: 16px;
		color: #999999;
	}

	.loading-more {
		align-items: center;
		justify-content: center;
		padding-top: 14px;
		padding-bottom: 14px;
		text-align: center;
	}

	.loading-more-text {
		font-size: 28upx;
		color: #999;
	}
</style>
