<template>
	<z-paging ref="msgpPging" v-model="dataList" @query="queryList" loading-more-no-more-text="没有更多消息了">
		<template v-slot:top>
			<up-navbar title="消息" bgColor="transparent" leftIcon='' :title-style="{fontWeight:'bold'}">
			</up-navbar>
			<div class="nav-header" :style="{height:computeNavHeight}"></div>
		</template>
		<ItemMessage v-for="(item,index) in dataList" :item="item" :index="index" :key="item.notice_user_id"
			@modify="handleModify">
		</ItemMessage>
	</z-paging>
</template>

<script>
	import {
		getMsgCountUrl,
		// getMessageListUrl,
		modifyMsgUrl
	} from '@/common/config/api.js'
	import ItemMessage from '@/components/ItemMessage.vue'
	export default {
		components: {
			ItemMessage
		},
		data() {
			return {
				dataList: []
			}
		},
		onShow() {
			this.getMsgCount();
		},
		computed: {
			computeNavHeight() {
				console.log(uni.$u.sys());
				return uni.$u.sys().statusBarHeight + 44 + 'px'
			}
		},
		methods: {
			handleModify(id) {
				modifyMsgUrl({
						noticeUserId: id
					})
					.then(res => {
						console.log('阅读消息', res);
						this.$refs.msgpPging.reload();
						this.getMsgCount()
					})
					.catch(err => {
						console.log(err);
					})
			},
			// 获取未读消息数量
			getMsgCount() {
				getMsgCountUrl({
						params: {
							pageNum: 1,
							pageSize: 1,
							userId: this.vuex_user.id,
							status: 0
						},
						custom: {
							catch: true
						}
					})
					.then(res => {
						console.log('未读消息数', res);
						this.$refs.msgpPging.reload();
						if (res.code === 200) {
							const count = String(res.total);
							if (count > 0) {
								uni.setTabBarBadge({
									index: 1,
									text: count > 99 ? '99+' : count
								})
							} else {
								uni.removeTabBarBadge({
									index: 1
								})
							}
						} else {
							uni.removeTabBarBadge({
								index: 1
							})
						}
					})
					.catch(err => {
						console.log(err);
					})
			},
			queryList(pageNo, pageSize) {
				// this.$refs.msgpPging.complete([1, 2, 3, 4, 5]);
				getMsgCountUrl({
						params: {
							pageNum: 1,
							pageSize: 1,
							userId: this.vuex_user.id,
							status: 0
						},
					})
					.then(res => {
						console.log('消息列表: ', res);
						this.$refs.msgpPging.complete(res);
					})
					.catch(err => {
						console.log('err: ', err);
						this.$refs.msgpPging.complete(false);
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F6F8FD;
	}

	.nav-header {
		background-image: url('@/static/images/nav-bg.png');
		background-size: cover;
		background-position: center;
	}
</style>