<template>
	<view class="page_todo"
		:style="{ paddingTop: statusBarHeight + 160 + 'rpx', paddingBottom: safeAreaBottom + 150 + 'rpx' }">
		<pageHeader></pageHeader>
		<view class="todo_content">
			<!-- 待办事项 -->
			<view class="todo_header">
				<text class="todo_title">待办事项({{ todoList.length }}项)</text>
			</view>
			<!-- 待办事项列表 -->
			<view class="todo_list" v-if="todoList.length > 0">
				<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="item.source === 1 ? '/static/image/todo/xiaoxi.png' : (item.source === 2 ? '/static/image/todo/daiban.png' : '/static/image/todo/shipin.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" v-if="item.source === 1">
								<text class="meta_tag">未读</text>
								<text class="meta_type">{{ truncateText(item.noticeType, 5) }}</text>
								<text class="meta_type">{{ truncateText(item.createdBy, 5) }}</text>
								<text class="meta_type">{{ item.releaseTime }}</text>
							</view>
							<view class="todo_meta" v-if="item.source === 2">
								<text class="meta_tag" :class="getTagStyle('进行中')">{{ '进行中' }}</text>
								<text class="meta_type">{{ truncateText(item.departmentName, 5) }}</text>
								<text class="meta_type">{{ truncateText(item.materialCategoryName, 5) }}</text>
								<text class="meta_type">{{ "剩余" + item.daysRemaining + "天" }}</text>
							</view>
							<view class="todo_meta" v-if="item.source === 3">
								<text class="meta_tag">{{ truncateText(item.taskType, 5) }}</text>
								<text class="meta_type">{{ truncateText(item.workspaceTheme, 5) }}</text>
								<text class="meta_type">{{ item.contentDomain }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<noData v-if="todoList.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 NoData from '@/components/noData.vue';
import workspace from '@/reqApi/workspace.js';
import { useSafeArea } from '@/common/safeArea';
import { truncateText } from '@/common/common.js';

const { statusBarHeight, safeAreaBottom, initSafeArea } = useSafeArea();
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();
		todoList.value = []
		if (res && res.data) {
			todoList.value = res.data
		}
	} catch (error) {
		console.error('获取待办事项列表失败:', error);
	}
};

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

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

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

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

// 待办事项标题
.todo_header {
	display: flex;
	align-items: baseline;
	margin-bottom: 24rpx;
	padding: 0 24rpx;
	height: 64rpx;
	line-height: 64rpx;

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

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

.todo_list {
	flex: 1;
	overflow-y: auto;
	padding: 0 24rpx;

	.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;
			}
		}
	}
}
</style>