<template>
	<div class="layout-navBars-breadcrumb-user-news">
		<div class="head-box">
			<div class="head-box-title">通知</div>
			<!--					<div class="head-box-btn" v-if="state.newsList.length > 0" @click="onAllReadClick">全部已读</div>-->
		</div>
		<el-scrollbar class="mt5 content-box">
			<template v-if="state.newsList.length > 0">
				<div class="content-box-item" v-for="(v, k) in state.newsList" :key="k" @click="linkNews(v)">
					<div class="text-no-wrap">{{ v.title }} ({{ v.circularTypeText }})</div>
					<div class="content-box-msg">
						<div class="text-ellipsis2" v-html="v.content"></div>
					</div>
					<div class="content-box-time">{{ dayjs(v.circularTime).format('YYYY-MM-DD HH:mm:ss') }}</div>
				</div>
			</template>
			<el-empty description="暂无通知" v-else></el-empty>
		</el-scrollbar>
	</div>
</template>

<script setup lang="ts" name="layoutBreadcrumbUserNews">
import { onMounted, onUnmounted, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { muquery } from '@/api/auxiliary/notice';
import dayjs from 'dayjs';
import { ElMessage } from 'element-plus';
import mittBus from '@/utils/mitt';

const emit = defineEmits(['hideNws']);

const state = reactive({
	newsList: [],
});
// 全部已读点击
const onAllReadClick = () => {
	state.newsList = [];
};

// 获取数量
const getNumAndList = async () => {
	try {
		const [muqueryRes] = await Promise.all([muquery({ circularState: 2, IsRead: false, PageIndex: 1, PageSize: 999 })]);
		state.newsList = muqueryRes.result?.items ?? [];
	} catch (e) {
		console.log(e, '获取消息数量失败');
	}
};
// 点击消息通知
const router = useRouter();
const linkNews = (v: any) => {
	if (!router.hasRoute('auxiliaryNoticeRead')) {
		ElMessage.warning('当前用户未配置通知详情页面');
		return;
	}
	emit('hideNws');
	router.push({
		name: 'auxiliaryNoticeRead',
		params: {
			id: v.id,
			isRead: 1,
			tagsViewName: '通知详情',
		},
	});
};
onMounted(() => {
	getNumAndList();
	mittBus.on('CircularRecord', (data) => {
		// 小红点数量消息通知
		console.log(data, '小红点数量消息通知');
		getNumAndList();
	});
});
onUnmounted(() => {
	mittBus.off('CircularRecord');
});
</script>

<style lang="scss" scoped>
.layout-navBars-breadcrumb-user-news {
	height: 325px;
	.head-box {
		display: flex;
		border-bottom: 1px solid var(--el-border-color-lighter);
		box-sizing: border-box;
		color: var(--el-text-color-primary);
		justify-content: space-between;
		height: 35px;
		align-items: center;

		.head-box-btn {
			color: var(--el-color-primary);
			font-size: 13px;
			cursor: pointer;
			opacity: 0.8;

			&:hover {
				opacity: 1;
			}
		}
	}

	.content-box {
		font-size: 13px;
		.content-box-item {
			padding: 12px 10px 5px 10px;
			border-radius: 5px;
			&:hover {
				background-color: var(--el-color-primary-light-9);
				cursor: pointer;
			}
			&:last-of-type {
				padding-bottom: 12px;
			}

			.content-box-msg {
				color: var(--el-text-color-secondary);
				margin-top: 5px;
				margin-bottom: 5px;
			}

			.content-box-time {
				color: var(--el-text-color-secondary);
			}
		}
	}

	.foot-box {
		height: 35px;
		color: var(--el-color-primary);
		font-size: 13px;
		cursor: pointer;
		opacity: 0.8;
		display: flex;
		align-items: center;
		justify-content: center;
		border-top: 1px solid var(--el-border-color-lighter);

		&:hover {
			opacity: 1;
		}
	}

	:deep(.el-empty__description p) {
		font-size: 13px;
	}
}
</style>
