<template>
	<view class="page_notice" :style="{paddingTop: statusBarHeight + 310 + 'rpx', paddingBottom: safeAreaBottom + 160 + 'rpx' }">
		<notice-header 
			title="任务总览" 
			mode="taskOverview"
			:showSearch="true" 
			:showFilter="true" 
			:showTabs="true" 
			:showCreateBtn="true"
			:tabs="tabs"
			:activeTab="activeTab" 
			@tabChange="handleTabChange" 
			@academicYearChange="handleAcademicYearChange"
			@categoryChange="handleCategoryChange"
			@materialTypeChange="handleMaterialTypeChange"
			@searchChange="handleSearchChange"
			@create="handleCreate">
		</notice-header>

		<view class="notice_content">
			<view class="notice_list">
				<scroll-view scroll-y v-for="item in taskList" :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 textLength">{{ item.academicYear }}</text>
							<text class="meta_text textLength">{{ item.category }}</text>
							<text class="meta_text">{{ item.time }}</text>
						</view>
					</view>
					<view class="item_bottom">
						<view class="notice_stats">
							<text class="stats_text">提交人: {{ item.submitter }}</text>
							<text class="stats_text">截止: {{ item.deadline }}</text>
						</view>
						<view class="notice_actions">
							<view class="action_btn detail" @click="handleDetail(item.id)">
								<text>详情</text>
							</view>
						</view>
					</view>
				</scroll-view>

				<no-data v-if="taskList.length === 0" centered text="暂无任务" />
			</view>
		</view>

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

<script setup>
import { ref, onMounted } from 'vue';
import NoticeHeader from '@/components/notice-header.vue';
import CommonTabbar from '@/components/commonTabbar.vue';
import NoData from '@/components/no-data.vue';
import { useSafeArea } from '@/common/safeArea';

const { statusBarHeight, safeAreaBottom, initSafeArea } = useSafeArea();

const tabs = ['全部', '待开始', '进行中', '已结束'];
const activeTab = ref(-1);
const academicYearIndex = ref(0);
const categoryIndex = ref(0);
const materialTypeIndex = ref(0);
const word = ref('');
const taskList = ref([]);

const statusMap = {
	0: '待开始',
	1: '进行中',
	2: '已结束'
};

const statusClassMap = {
	0: 'status-pending',
	1: 'status-progress',
	2: 'status-ended'
};

const mockTaskList = [
	{
		id: 1,
		title: '2025年秋季学期教案材料收集',
		status: '待开始',
		statusClass: 'status-pending',
		academicYear: '2025-2026学年',
		category: '日常教学',
		time: '2025-07-28',
		submitter: '张老师',
		deadline: '2025-08-15',
		icon: '/static/image/workspace/icon2.png'
	},
	{
		id: 2,
		title: '期中考试试卷提交任务',
		status: '进行中',
		statusClass: 'status-progress',
		academicYear: '2025-2026学年',
		category: '考试测评',
		time: '2025-07-25',
		submitter: '李老师',
		deadline: '2025-08-30',
		icon: '/static/image/workspace/icon1.png'
	},
	{
		id: 3,
		title: '课件资源库更新任务',
		status: '进行中',
		statusClass: 'status-progress',
		academicYear: '2025-2026学年',
		category: '日常教学',
		time: '2025-07-20',
		submitter: '王老师',
		deadline: '2025-09-01',
		icon: '/static/image/workspace/icon3.png'
	},
	{
		id: 4,
		title: '教研活动材料归档',
		status: '已结束',
		statusClass: 'status-ended',
		academicYear: '2024-2025学年',
		category: '教研活动',
		time: '2025-06-15',
		submitter: '赵老师',
		deadline: '2025-07-01',
		icon: '/static/image/workspace/icon4.png'
	}
];

const fetchTaskList = () => {
	let filteredList = [...mockTaskList];
	
	if (activeTab.value >= 0) {
		filteredList = filteredList.filter(item => {
			const statusIndex = Object.keys(statusMap).findIndex(key => statusMap[key] === item.status);
			return statusIndex === activeTab.value;
		});
	}
	
	if (word.value) {
		filteredList = filteredList.filter(item => 
			item.title.includes(word.value)
		);
	}
	
	taskList.value = filteredList;
};

onMounted(() => {
	initSafeArea();
	fetchTaskList();
});

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

const handleAcademicYearChange = (index) => {
	academicYearIndex.value = index;
	fetchTaskList();
};

const handleCategoryChange = (index) => {
	categoryIndex.value = index;
	fetchTaskList();
};

const handleMaterialTypeChange = (index) => {
	materialTypeIndex.value = index;
	fetchTaskList();
};

const handleSearchChange = (keyword) => {
	word.value = keyword;
	fetchTaskList();
};

const handleCreate = () => {
	uni.navigateTo({
		url: '/pages/materialCollection/submitMaterial/index'
	});
};

const handleDetail = (id) => {
	uni.navigateTo({
		url: `/pages/materialCollection/submitMaterial/index?id=${id}`
	});
};
</script>

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

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

.notice_list {
	flex: 1;
	overflow-y: auto;
	overflow-x: hidden;
	padding: 0 24rpx;
	display: flex;
	flex-direction: column;
}

.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: 4rpx;
				font-weight: 400;
				border-radius: 8rpx;

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

				&.status-progress {
					background: rgba(230, 162, 60, 0.1);
					color: #E6A23C;
				}

				&.status-ended {
					background: rgba(144, 147, 153, 0.1);
					color: #909399;
				}
			}

			.meta_text {
				font-size: 24rpx;
				color: #999999;
				line-height: 48rpx;
			}
			.textLength {
				max-width: 160rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}
	}

	.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;

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

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