<template>
	<view class="page_message" :style="{paddingTop: statusBarHeight + 160 + 'rpx', paddingBottom: safeAreaBottom + 150 + 'rpx' }">
		<pageHeader></pageHeader>
		<view class="message_content">
			<view class="message_header">
				<text class="message_title">系统消息({{ unreadCount }}项)</text>
				<text class="mark_all_read" @click="mark_all_read" v-if="messageList.length > 0">全部已读</text>
			</view>
			<view class="message_list" v-if="messageList.length > 0">
				<view v-for="item in messageList" :key="item.id" class="message_item">
					<view class="message_left">
						<view class="message_info">
							<view class="message_title_row">
								<image src="@/static/image/todo/daiban.png" class="message_icon" mode="aspectFit"></image>
								<text class="message_name">{{ item.taskName }}</text>
							</view>
							<view class="message_meta">
								<view class="meta_left">
									<text class="meta_tag" :class="getTagStyle(item.taskStatus)">{{ item.taskStatus }}</text>
									<text class="meta_type">{{ item.taskRemark }}</text>
									<text class="meta_time">{{ item.taskTime }}</text>
								</view>
								<text class="mark_read" @click="markNoticeRead(item.id)">{{ item.readStatus === 0 ? '标记已读' : '' }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- <view v-if="messageList.length === 0" class="schedule_empty">
				<image src="@/static/image/bg/no_data.png" class="empty_image" mode="aspectFit"></image>
				<text class="empty_text">暂无系统消息</text>
			</view> -->
			<noData v-if="messageList.length === 0" centered text="暂无系统消息"></noData>
		</view>
		<common-tabbar></common-tabbar>
	</view>
</template>

<script setup>
	import { ref, onMounted } from 'vue';
	import { onShow } from '@dcloudio/uni-app';
	import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
	import PageHeader from '@/components/pageHeader.vue';
	import CommonTabbar from '@/components/commonTabbar.vue';
	import workspace from '@/reqApi/workspace.js';
	import { useSafeArea } from '@/common/safeArea';
	import NoData from '@/components/noData.vue';
	import { truncateText } from '@/common/common.js';

	const { statusBarHeight, safeAreaBottom, initSafeArea } = useSafeArea();
	const messageList = ref([]);
	const unreadCount = ref(0);
	const getTagStyle = (status) => {
		if (status === '已驳回') {
			return 'meta_tag_red'
		}
		if (status === '进行中' || status === "已通过") {
			return 'meta_tag_green'
		}
		return ''
	}

	const fetchMessageList = async () => {
		try {
			const res = await workspace.getNoticeList();
			if (res && res.data) {
				messageList.value = res.data
				unreadCount.value = messageList.value.filter((item) => item.readStatus === 0).length;
			}
		} catch (error) {
			console.error('获取系统消息列表失败:', error);
		}
	};
	const markNoticeRead = async (id) => {
		try {
			const res = await workspace.markNoticeRead(id);
			if (res.code === 200) {
				const item = messageList.value.find((item) => item.id === id);
				if (item) {
					item.readStatus = 1;
				}
				unreadCount.value--;
				uni.showToast({
					title: '当前消息标记成功',
					icon: 'success',
					duration: 2000
				});
			}
		} catch (error) {
			console.error('标记系统消息已读失败:', error);
		}
	};
	const mark_all_read = async () => {
		try {
			const res = await workspace.mark_all_as_read();
			if (res.code === 200) {
				messageList.value.forEach((item) => {
					item.readStatus = 1;
				});
				unreadCount.value = 0;
				uni.showToast({
					title: '全部已读成功',
					icon: 'success',
					duration: 2000
				});
			}
		} catch (error) {
			console.error('标记所有消息已读失败:', error);
		}
	};
	onMounted(() => {
		initSafeArea();
	});

	onShow(() => {
		fetchMessageList();
	});
</script>

<style lang="scss">
.page_message {
	min-height: 100vh;
	display: flex;
	flex-direction: column;
	background-color: #FFFFFF;
	box-sizing: border-box;
}

.message_content {
	flex: 1;
	display: flex;
	flex-direction: column;
	overflow: hidden;
	padding-left: 0rpx;
	padding-right: 0rpx;
}

.message_header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin-bottom: 24rpx;
	flex-shrink: 0;
	padding:0 24rpx;
	.message_title {
		font-weight: 500;
		font-size: 32rpx;
		color: #333333;
		height: 64rpx;
		line-height: 64rpx;
	}

	.mark_all_read {
		border-radius: 8rpx;
		border: 2rpx solid #2E64FA;
		background: #FFFFFF;
		font-weight: 400;
		font-size: 24rpx;
		color: #2E64FA;
		width: 136rpx;
		height: 64rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
}

.message_list {
	flex: 1;
	overflow-y: auto;
	padding:0 24rpx;
	.message_item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 16rpx;
		background: #F9FAFF;
		border-radius: 20rpx;
		margin-bottom: 24rpx;
		border: 2rpx solid #E4E7ED;

		&:last-child {
			margin-bottom: 0;
		}

		.message_left {
			display: flex;
			flex: 1;
			align-items: flex-start;
			gap: 16rpx;
		}

		.message_icon {
			flex-shrink: 0;
			margin-top: 4rpx;
			width: 32rpx;
			height: 32rpx;
		}

		.message_info {
			flex: 1;
			overflow: hidden;
		}

		.message_title_row {
			display: flex;
			align-items: center;
			gap: 16rpx;
			margin-bottom: 16rpx;

			.message_name {
				font-size: 28rpx;
				color: #333333;
				font-weight: 500;
				flex: 1;
				width: 0;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}

		.message_meta {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.meta_left {
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				gap: 16rpx;
				flex: 1;
				overflow: hidden;
			}

			.meta_tag {
				font-size: 22rpx;
				padding: 8rpx 12rpx;
				background-color: rgba(74, 108, 247, 0.1);
				color: #4A6CF7;
				border-radius: 4rpx;
			}

			.mark_read {
				font-weight: 400;
				font-size: 24rpx;
				color: #2E64FA;
				flex-shrink: 0;
				margin-left: 16rpx;
			}
			.meta_tag_red {
				background-color: rgba(245, 108, 108, 0.1);
				color: #F56C6C;
			}
			.meta_tag_green {
				background-color: rgba(43, 198, 68, 0.1);
				color: #2BC644;
			}
			.meta_type {
				font-size: 24rpx;
				color: #999999;
			}
			.meta_time {
				margin-top: 5rpx;
				font-size: 24rpx;
				color: #999999;
			}
		}
	}
}

.schedule_empty {
	display: flex;
	flex-direction: column;
	align-items: center;
	font-size: 28rpx;
	color: #999999;
	position: relative;
	height: 100%;
	overflow: hidden;
	.empty_image {
		width: 400rpx;
		height: 400rpx;
	}

	.empty_text {
		position: absolute;
		bottom: 15%;
		left: 50%;
		transform: translate(-50%, -50%);
	}
}
</style>