<template>
	<view>
		<view class="card" v-for="(item, index) in list" :key="index" @click="goDetails(item.id)">
			<view class="lf-row-between lf-m-b-20">
				<view class="lf-color-black lf-flex">
					<text class="hot" v-if="item.is_read == 0"></text>
					<text class="gray" v-else></text>
					<text class="lf-font-28 lf-m-l-10">收到一条新消息</text>
				</view>
				<view class="lf-color-gray lf-font-22">{{timer(item.created_at*1000) || ''}} </view>
			</view>
			<view class="lf-font-24 lf-color-555">{{item.title}}</view>
		</view>
		<!-- 加载 -->
		<view class="loading-more">
			<text v-if="list.length" :class="{'loading-more-text': loadingClass}">{{ loadingText }}</text>
			<lf-nocontent v-else></lf-nocontent>
		</view>
		<!-- 回到顶部 -->
		<u-back-top :scroll-top="pageScrollTop" :custom-style="{background: 'rgba(51, 51 51, 0.3)'}"></u-back-top>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				list: [1, 2],
				loadingClass: true,
				loadingText: '正在加载中',
				page: 1,
				isPage: false
			}
		},
		onLoad(){
			this.getMessageList()
		},
		methods: {
			timer(value, fmt) {
				if(!value) return;
				let newTime = new Date(value)
				if(!fmt){
					fmt = 'yyyy-MM-dd hh:mm';
				}
				if(/(y+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (newTime.getFullYear() + '').substr(4 - RegExp.$1.length));
				}
				let o = {
					'M+': newTime.getMonth() + 1,
					'd+': newTime.getDate(),
					'h+': newTime.getHours(),
					'm+': newTime.getMinutes(),
					's+': newTime.getSeconds()
				};
				
				function padLeftZero(str) {
				  return ('00'+str).substr(str.length);
				}
				
				// 遍历这个对象
				for (let k in o) {
					if (new RegExp(`(${k})`).test(fmt)) {
						// console.log(`${k}`)
						let str = o[k] + '';
						fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
					}
				}
				return fmt;
			},
			goDetails(id) {
				this.$http(this.API.API_CHECKNEWS,{id: id}).then(res => {
					this.$url('/pages/message/detail?news_id='+item.id)
				}).catch(err => {
					
				})
			},
			getMessageList() {
				this.$http(this.API.API_MESSAGELIST,{page: this.page}).then(res => {
					let isPage = res.data.next_page_url == null?false:true;
					this.isPage = isPage;
					if(!isPage){
						this.loadingClass = false;
						this.loadingText = '没有更多数据啦~';
					}
					if(this.page == 1){
						this.list = res.data.data;
					}else{
						this.list.push(...res.data.data);
					}
					uni.stopPullDownRefresh();
					console.log(this.list)
				}).catch(err => {
					
				})
			}
		},
		onReachBottom(){
			if(this.isPage){
				this.page = this.page + 1;
				this.getMessageList();
			}
		},
		onPullDownRefresh(){
			this.getMessageList()
		}
	}
</script>

<style>
	page{
		background-color: #F6F6F6;
	}
</style>
<style lang="scss" scoped="scoped">
	.card{
		margin: 0 auto;
		margin-top: 30rpx;
		width: 686rpx;
		height: max-content;
		background-color: #FFFFFF;
		padding: 20rpx;
		border-radius: 20rpx;
		box-sizing: border-box;
		.hot{
			display: inline-block;
			margin-right: 10rpx;
			width: 15rpx;
			height: 15rpx;
			border-radius: 50rpx;
			background-color: #FF0000;
		}
		.gray {
			display: inline-block;
			margin-right: 10rpx;
			width: 15rpx;
			height: 15rpx;
			border-radius: 50rpx;
			background-color: #999;
		}
	}
	.lf-font-22{
		font-size: 22rpx;
	}
</style>
