<!-- 消息通知 -->
<template>
	<view class="notice">
		<u-navbar back-text="" title="" back-icon-color="#FFFFFF" :background="{ background: '#3D5D4C' }" :border-bottom="false"></u-navbar>
		<z-paging
			ref="paging"
			v-model="noticeList"
			@query="queryList"
		>
			<!-- 选项卡 -->
			<view class="notice-tab" slot="top">
				<u-tabs :list="tabList" :is-scroll="false" :current="current" @change="tabChange" bg-color="#f2f2f2" inactive-color="#000000" active-color="#709078" :bold="false" bar-width="40" bar-height="6" :active-item-style="{color: '#000000'}" is-scroll=""></u-tabs>
			</view>
			
			<view class="notice-list">
				<view class="notice-list-item" v-for="(item, index) in noticeList" :key="index" @click="jumpPage('/pages/notification/noticeDetails/noticeDetails', { id: item.id })">
					<view class="notice-list-item-title">{{ item.name }}</view>
					<view class="notice-list-item-content">{{ item.content }}</view>
					<view class="notice-list-item-bottom">
						<view>发布于{{ item.releasTime }}</view>
						<view class="is-read" v-if="item.isRead == 1">已读</view>
						<view class="no-read" v-else>未读</view>
					</view>
				</view>
			</view>
		</z-paging>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current: 0,
				tabList: [
					{ value: 2, name: '培训通知', count: 0 },
					{ value: 3, name: '就业消息', count: 0 },
					{ value: 0, name: '系统通知', count: 0 }
				],
				tabValue: 2,
				pageNum: 1, 
				pageSize: 10,
				noticeList: []
			}
		},
		onShow() {
			this.getClassifyUnreadNum();
			this.getNoticeListByType(this.pageNum, this.pageSize)
		},
		methods: {
			/**
			 * 查询每类通知未读的条数
			 */
			getClassifyUnreadNum() {
				this.$u.api.noticeApi.getClassifyUnreadNumApi().then(res => {
					if (res.code === 200) {
						this.tabList[0].count = res.data.trainUnreadCount
						this.tabList[1].count = res.data.empUnreadCount
						this.tabList[2].count = res.data.sysUnreadCount
					}
				})
			},
			/**
			 * 获取
			 * @param {Object} pageNum
			 * @param {Object} pageSize
			 */
			getNoticeListByType(pageNum, pageSize) {
				const params = {
					type: this.tabValue,
					pageNum,
					pageSize
				}
				this.$u.api.noticeApi.getNoticeListByTypeApi(params).then(res => {
					if (res.code === 200) {
						this.$refs.paging.complete(res.rows);
					}
				})
			},
			/**
			 * tab切换
			 * @param { Number } cur
			 */
			tabChange(cur) {
				this.current = cur
				this.tabValue = this.tabList[cur].value;
				this.getNoticeListByType(1, 10);
			},
			/**
			 * 下拉分页组件触发
			 * @param {Number} pageNum
			 * @param {Number} pageSize
			 */
			queryList(pageNum, pageSize) {
				this.pageNum = pageNum
				this.pageSize = pageSize
				this.getNoticeListByType(pageNum, pageSize);
			},
			/**
			 * 跳转到指定页面
			 * @param {Object} url
			 * @param {Object} params
			 */
			jumpPage(url, params) {
				this.$u.route({ url, params })
			}
		}
	}
</script>

<style lang="scss" scoped>
@import './scss/notification.scss';
</style>