<template>
	<view class="page_todo">
		<page-header></page-header>
		<view class="todo_content">
			<!-- 待办事项 -->
			<view class="todo_header">
				<text class="todo_title">待办事项({{ todoList.length }}项)</text>
			</view>
			<!-- 待办事项列表 -->
			<view class="todo_list">
				<view v-for="item in todoList" :key="item.id" class="todo_item">
					<view class="todo_left">
						<view class="todo_info">
							<view class="todo_title_row">
								<image src="/static/image/todo/xiaoxi.png" class="todo_icon" mode="aspectFit"></image>
								<text class="todo_name">{{ item.title }}</text>
								<uni-icons type="right" size="18" color="#999999"></uni-icons>
							</view>
							<view class="todo_meta">
								<text class="meta_tag" :class="getTagStyle(item.status)">{{ item.status }}</text>
								<text v-for="(tag, idx) in item.categoryTags" :key="idx" class="meta_type">{{ tag }}</text>
								<text class="meta_time">{{ item.time }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="todoList.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>
		</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/page-header.vue';
	import workspace from '@/reqApi/workspace.js';

	const todoList = ref([]);

	const getTagStyle = (status) => {
		if (status === '已驳回') {
			return 'meta_tag_red'
		}
		if (status === '进行中' || status === "已通过") {
			return 'meta_tag_green'
		}
		return ''
	}

	const fetchTodoList = async () => {
		try {
			const res = await workspace.getTodoList();
			if (res && res.data) {
				todoList.value = res.data.map(item => {
					const categoryTags = [];
					if (item.departmentName) categoryTags.push(item.departmentName);
					if (item.materialCategoryName) categoryTags.push(item.materialCategoryName);
					return {
						id: item.id,
						title: item.title,
						status: item.source === 2 ? '进行中' : (item.notSubmitNum > 0 ? `待提交：${item.notSubmitNum}` : (item.rejectNum > 0 ? `已驳回：${item.rejectNum}` : '')),
        				btnText: item.source === 1 ? '查看通知' : '查看任务',
						categoryTags,
						time: item.daysRemaining !== undefined ? `剩余${item.daysRemaining}天` : '',
						tag: '',
					};
				});
			}
			} catch (error) {
			console.error('获取待办事项列表失败:', error);
		}
	};

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

<style lang="scss">
.page_todo {
	// min-height: 100vh;
	// background-color: #F5F7FA;
	// padding-bottom: 120rpx;
}

.todo_content {
	padding: 140rpx 24rpx 24rpx 24rpx;
}
// 待办事项标题
.todo_header {
	display: flex;
	align-items: baseline;
	margin-bottom: 24rpx;

	.todo_title {
		font-weight: 500;
		font-size: 32rpx;
		color: #333333;
	}

	.todo_count {
		font-size: 26rpx;
		color: #999999;
		margin-left: 8rpx;
	}
}

.todo_list {
	.todo_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;
		}

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

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

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

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

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

			.todo_tag {
				font-size: 24rpx;
				padding: 6rpx 12rpx;
				border-radius: 4rpx;
				flex-shrink: 0;

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

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

			.meta_tag {
				font-size: 22rpx;
				padding: 8rpx 12rpx;
				background-color: rgba(74, 108, 247, 0.1);
				color: #4A6CF7;
				border-radius: 4rpx;
			}
			.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 {
				font-size: 24rpx;
				color: #999999;
			}
		}
	}
}

.schedule_empty {
	display: flex;
	flex-direction: column;
	align-items: center;
	font-size: 28rpx;
	color: #999999;
	position: relative;

	.empty_image {
		width: 400rpx;
		height: 400rpx;
	}

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