<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力商协会发展
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdsxh.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德商协会系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.maiwd.cn
+----------------------------------------------------------------------
| 消息通知 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container">
		<!-- 标题栏 -->
		<title-bar title="消息通知"></title-bar>
		<!-- 内容区 -->
		<view class="container-main" v-if="loadEnd">
			<view class="main-list" v-if="noticeList.length">
				<view class="list-item" v-for="(item, index) in noticeList" :key="index" @click="toDetails(item.id)">
					<view class="item-title">{{item.title}}</view>
					<view class="item-date">{{item.createtime}}</view>
					<view class="item-point" v-if="item.is_read != 1"></view>
				</view>
			</view>
			<empty top="30%" title="暂无相关内容~" v-else></empty>
		</view>
		<!-- 底部导航 -->
		<tab-bar></tab-bar>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 分类查询参数
				page: 1,
				limit: 20,
				hasMore: false,
				// 消息通知列表
				noticeList: [],
			}
		},
		onLoad() {
			uni.showLoading({
				title: "加载中"
			})
			this.getNoticeList(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onShow() {
			if (this.loadEnd) {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
				this.page = 1
				this.getNoticeList()
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.getNoticeList(() => {
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.getNoticeList()
			}
		},
		methods: {
			// 获取消息通知列表
			getNoticeList(fn) {
				if (fn) fn()
				this.$util.request("mine.notice.list", {
					page: this.page,
					limit: this.limit,
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						let list = res.data.data
						this.hasMore = this.page < res.data.total / this.limit ? true : false
						this.noticeList = this.page == 1 ? list : [...this.noticeList, ...list];
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					console.error('获取帮助列表 ', error)
				})
			},
			// 跳转详情页面
			toDetails(id) {
				this.$util.toPage({
					mode: 1,
					path: "/pages/mine/notice/details?id=" + id
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		.container-main {
			padding: 32rpx;

			.main-list {
				.list-item {
					position: relative;
					border-radius: 16rpx;
					background: #FFF;
					padding: 32rpx;
					margin-top: 32rpx;

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

					.item-title {
						color: #5A5B6E;
						font-size: 28rpx;
						font-weight: 600;
						line-height: 40rpx;
					}

					.item-date {
						margin-top: 8rpx;
						color: #979797;
						font-size: 24rpx;
						line-height: 34rpx;
					}

					.item-point {
						position: absolute;
						top: 16rpx;
						right: 16rpx;
						width: 16rpx;
						height: 16rpx;
						background: #FF4646;
						border-radius: 50%;
					}
				}
			}
		}
	}
</style>