<template>
	<view class="page_mine">
		<notice-header title="我的通知" :showSearch="true" :showFilter="true" :showTabs="true" :tabs="tabs"
			:activeTab="activeTab" tabMode="index" @tabChange="handleTabChange" @typeChange="handleTypeChange" @timeChange="handleTimeChange" @searchChange="handleSearchChange"></notice-header>

		<view class="notice_content">
			<view class="notice_list">
				<scroll-view scroll-y v-for="item in noticeList" :key="item.id" class="notice_item">
					<view class="item_content">
						<view class="notice_title_row">
							<image class="notice_icon" :src="item.icon" mode="aspectFit"></image>
							<text class="notice_title">{{ item.title }}</text>
						</view>
						<view class="notice_meta">
							<view class="status_tag" :class="item.statusClass">
								<text>{{ item.status }}</text>
							</view>
							<text class="meta_text">{{ item.category }}</text>
							<text class="meta_text">{{ item.department }}</text>
							<text class="meta_text">{{ item.time }}</text>
						</view>
					</view>
					<view class="item_bottom">
						<view class="notice_stats">
							<text class="stats_text">已读: {{ item.readCount }}人</text>
							<text class="stats_text">未读: {{ item.unreadCount }}人</text>
						</view>
						<view class="notice_actions">
							<view class="action_btn" :class="getDeleteClass(item)" @click="handleDelete(item.id)">
								<text>删除</text>
							</view>
							<view class="action_btn edit" @click="handleEdit(item.id)">
								<text>编辑</text>
							</view>
							<view class="action_btn detail" @click="handleDetail(item.id)">
								<text>详情</text>
							</view>
						</view>
					</view>
				</scroll-view>

				<no-data v-if="noticeList.length === 0" text="暂无通知" />
			</view>
		</view>

		<common-tabbar></common-tabbar>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import NoticeHeader from '@/components/notice-header.vue';
import NoData from '@/components/no-data.vue';

const tabs = ['未读', '已读', '全部'];
const activeTab = ref(0);

const noticeList = ref([
	{
		id: '1',
		title: '25-26学年第一学期初一期中考试安排通知',
		status: '已发布',
		statusClass: 'status-published',
		category: '教学教务',
		department: '教务处',
		time: '2024-06-03 18:00:00',
		readCount: 1500,
		unreadCount: 50,
		icon: '/static/image/todo/xiaoxi.png'
	},
	{
		id: '2',
		title: '关于开展教师培训的通知',
		status: '已发布',
		statusClass: 'status-published',
		category: '教师培训',
		department: '人事处',
		time: '2024-06-02 10:00:00',
		readCount: 280,
		unreadCount: 15,
		icon: '/static/image/todo/xiaoxi.png'
	},
	{
		id: '3',
		title: '暑期放假安排通知',
		status: '待发布',
		statusClass: 'status-pending',
		category: '学校通知',
		department: '校长办公室',
		time: '2024-06-01 09:00:00',
		readCount: 0,
		unreadCount: 0,
		icon: '/static/image/todo/xiaoxi.png'
	}
]);

const getDeleteClass = (item) => {
	if (item.noticeStatus !== 2) {
		return 'delete disabled';
	}
	return 'delete';
};

const goBack = () => {
	uni.navigateBack();
};

const handleTabChange = (idx) => {
	activeTab.value = idx;
};

const handleTypeChange = () => {};
const handleTimeChange = () => {};
const handleSearchChange = () => {};

const handleDelete = (id) => {
	uni.showModal({
		title: '确认删除',
		content: '确定要删除这条通知吗？',
		success: (res) => {
			if (res.confirm) {
				noticeList.value = noticeList.value.filter(item => item.id !== id);
				uni.showToast({ title: '删除成功', icon: 'success' });
			}
		}
	});
};

const handleEdit = (id) => {
	uni.navigateTo({ url: `/pages/notice/publish/index?id=${id}` });
};

const handleDetail = (id) => {
	const item = noticeList.value.find(item => item.id === id);
	if (item && item.status === '待发布') {
		uni.showToast({ title: '待发布通知暂无法查看详情', icon: 'none' });
		return;
	}
	uni.showToast({ title: `查看详情: ${id}`, icon: 'none' });
};
</script>

<style lang="scss">
.page_mine {
}

.notice_content {
	padding-top: 325rpx;
	padding-bottom: 104px;
}

.notice_list {
	padding: 0rpx 24rpx;
	box-sizing: border-box;
}

.notice_item {
	background-color: #F9FAFF;
	border-radius: 20rpx;
	margin-bottom: 24rpx;
	border: 2rpx solid #E4E7ED;

	.item_content {
		padding: 24rpx;

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

			.notice_icon {
				width: 32rpx;
				height: 32rpx;
				margin-right: 16rpx;
			}

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

		.notice_meta {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			gap: 16rpx;

			.status_tag {
				font-size: 24rpx;
				height: 48rpx;
				width: 96rpx;
				text-align: center;
				line-height: 48rpx;
				border-radius: 8rpx;
				font-weight: 400;

				&.status-pending {
					background: rgba(46, 100, 250, 0.1);
					color: #2E64FA;
				}

				&.status-published {
					background: rgba(82, 196, 26, 0.1);
					color: #2BC644;
				}

				&.status-closed {
					background: rgba(245, 108, 108, 0.1);
					color: #F56C6C;
				}
			}

			.meta_text {
				font-size: 24rpx;
				color: #999999;
				line-height: 48rpx;
			}
		}
	}

	.item_bottom {
		border-top: 2rpx solid #E4E7ED;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding: 24rpx 16rpx;

		.notice_stats {
			display: flex;

			.stats_text {
				font-size: 24rpx;
				color: #999999;

				&:first-child {
					margin-right: 32rpx;
				}
			}
		}

		.notice_actions {
			display: flex;
			justify-content: flex-end;
			gap: 16rpx;

			.action_btn {
				font-size: 24rpx;
				padding: 12rpx 24rpx;
				border-radius: 8rpx;
				border: 2rpx solid transparent;

				&.delete {
					border-color: #F56C6C;
					color: #F56C6C;
					background-color: #FFFFFF;

					&.disabled {
						border-color: #C0C4CC;
						color: #C0C4CC;
						background-color: #FFFFFF;
					}
				}

				&.edit {
					border-color: #2E64FA;
					color: #2E64FA;
					background-color: #FFFFFF;

					&.disabled {
						border-color: #C0C4CC;
						color: #C0C4CC;
						background-color: #FFFFFF;
					}
				}

				&.detail {
					background-color: #2E64FA;
					color: #FFFFFF;

					&.disabled {
						border-color: #C0C4CC;
						color: #C0C4CC;
						background-color: #FFFFFF;
					}
				}
			}
		}
	}

	&:last-child {
		margin-bottom: 0;
	}
}
</style>