<template>
	<view class="task-center">

		<u-subsection :list="tabList" 
		active-color="#ffaa48"
		bg-color="#fff"
		inactive-color="#aaaaaa"
		font-size="16"
		:current="curNow" 
		@change="sectionChange"></u-subsection>

		<!-- <view class="container-tabs">
			<up-tabs :list="tabList" lineWidth="68rpx" :activeStyle="{
		    color: '#ffaa48',
		    fontWeight: 'bold',
		    transform: 'scale(1.05)'
		}" :inactiveStyle="{
		    color: '#555',
		    transform: 'scale(1)'
		}" :itemStyle="{height:'88rpx',padding:'0 52rpx'}" lineColor="#ffaa48" @click="sectionChange"></up-tabs>
		</view> -->

		<!-- 待接受任务列表 -->
		<view v-if="curNow === 0">
			<view v-for="(item,index) in pendingTasks" style="padding:28rpx 36rpx 0;" :key="index">
				<view class="task-card">
					<view class="task-header">
						<view class="task-image">
							<image src="https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/index/point.png" mode="heightFix"></image>
						</view>
						<view class="task-type">{{item.taskType}}</view>
						<view class="task-reward">酬劳 <text> ¥{{item.reward}}</text> </view>
					</view>
					<view class="task-content">
						<view class="task-icon">
							<image :src="item.icon" style="width: 120rpx; height: 120rpx;" mode="aspectFill"></image>
						</view>
						<view class="task-info">
							<view class="task-title">{{item.title}}</view>
							<view class="task-desc">{{item.description}}</view>
							<view class="task-deadline">任务截止日期: {{item.deadline}}</view>
						</view>
					</view>
					<view class="task-footer">
						<u-button shape="circle" plain text="查看详情" @click="viewTaskDetail(item)"></u-button>
						<u-button shape="circle" color="#ffaa48" text="立即接受" @click="acceptTaskHandler(item)"></u-button>
					</view>
				</view>
			</view>
			<view v-if="pendingTasks.length === 0" class="empty-tip">
				<text>暂无待接受任务</text>
			</view>
		</view>
		
		<!-- 已接收任务列表 -->
		<view v-else>
			<view v-for="(item,index) in acceptedTasks" style="padding:28rpx 36rpx 0;" :key="index">
				<view class="task-card">
					<view class="task-header">
						<view class="task-image">
							<image src="https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/index/point.png" mode="heightFix"></image>
						</view>
						<view class="task-type">{{item.taskType}}</view>
						<view class="task-reward">酬劳 <text> ¥{{item.reward}}</text> </view>
					</view>
					<view class="task-content">
						<view class="task-icon">
							<image :src="item.icon" style="width: 120rpx; height: 120rpx;" mode="aspectFill"></image>
						</view>
						<view class="task-info">
							<view class="task-title">{{item.title}}</view>
							<view class="task-desc">{{item.description}}</view>
							<view class="task-deadline">任务截止日期: {{item.deadline}}</view>
							<view class="task-status-tag" :class="{'status-pending': item.status === 'ACCEPTED', 'status-reviewing': item.status === 'SUBMITTED', 'status-rejected': item.status === 'REJECTED', 'status-approved': item.status === 'APPROVED'}">{{getStatusText(item.status)}}</view>
						</view>
					</view>
					<view class="task-footer">
						<u-button shape="circle" plain text="查看详情" @click="viewTaskDetail(item)"></u-button>
						<u-button v-if="item.status === 'REJECTED'" shape="circle" type="error" text="驳回原因" @click="showRejectReason(item)"></u-button>
						<u-button v-if="item.status === 'ACCEPTED'" shape="circle" color="#ffaa48" text="立即上传" @click="uploadTask(item)"></u-button>
						<u-button v-if="item.status === 'SUBMITTED'" shape="circle" disabled text="审核中"></u-button>
						<u-button v-if="item.status === 'REJECTED'" shape="circle" color="#ffaa48" text="重新上传" @click="uploadTask(item)"></u-button>
						<view v-if="item.status === 'APPROVED'" class="task-status task-status-approved">已通过</view>
					</view>
				</view>
			</view>
			<view v-if="acceptedTasks.length === 0" class="empty-tip">
				<text>暂无已接收任务</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getTaskList,
		getTaskDetail,
		acceptTask
	} from "@/api/order/task.js"
	export default {
		data() {
			return {
				tabList: [
					{
						name: '待接受',
						badge: {
							value: 1,
						}
					},
					{
						name: '已接受',
						badge: {
							value: 0,
						}
					},
				],
				curNow: 0,
				pendingTasks: [],
				acceptedTasks: []
			}
		},
		onShow() {
			// 加载任务列表数据
			this.getTaskList()
		},
		methods: {
			sectionChange(index) {
				this.curNow = index;
                this.getTaskList()
			},
			getTaskList() {
				// 调用API获取任务列表
				getTaskList({
                    status : this.curNow
                }).then(res=>{
					if (res && res.code === 200) {
						let rows = res.rows || []
						console.log(rows)
						// 根据status字段分类：0为待接受，1为已接受
						this.pendingTasks = rows.filter(item => item.status === 0).map(item => this.formatTaskItem(item))
						this.acceptedTasks = rows.filter(item => item.status === 1).map(item => this.formatTaskItem(item))
						this.updateBadgeCount()
					}
				})
			},
			// 格式化任务项
			formatTaskItem(item) {
				return {
					id: item.id,
					taskType: item.taskName || '任务',
					reward: item.taskMoney || '0',
					icon: item.taskIcon || 'https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/index/point.png',
					title: item.title || '',
					description: item.theme || '',
					deadline: item.taskEndTime ? this.formatDate(item.taskEndTime) : '',
					status: this.getTaskStatus(item.taskState, item.examineState),
					rejectReason: item.examineText || ''
				}
			},
			// 根据任务进度和审核状态获取任务状态
			getTaskStatus(taskState, examineState) {
				// taskState: 任务进度，examineState: 审核状态
				if (taskState === 0) return 'ACCEPTED';  // 待上传
				if (taskState === 1 && examineState === 0) return 'SUBMITTED'; // 审核中
				if (taskState === 1 && examineState === 1) return 'APPROVED';  // 已通过
				if (taskState === 1 && examineState === 2) return 'REJECTED';  // 驳回
				return 'ACCEPTED'; // 默认状态
			},
			// 格式化日期
			formatDate(dateStr) {
				if (!dateStr) return '';
				let date = new Date(dateStr);
				let year = date.getFullYear();
				let month = (date.getMonth() + 1).toString().padStart(2, '0');
				let day = date.getDate().toString().padStart(2, '0');
				return `${year}-${month}-${day}`;
			},
			updateBadgeCount() {
				// 更新标签页的徽标数量
				this.tabList[0].badge.value = this.pendingTasks.length
				this.tabList[1].badge.value = this.acceptedTasks.length
			},
			viewTaskDetail(task) {
				// 查看任务详情，跳转到任务详情页面
				uni.navigateTo({
					url: `/pages_order/task/taskDetail?id=${task.id}`
				});
			},
			acceptTaskHandler(task) {
				// 接受任务
				uni.showModal({
					title: '接受任务',
					content: `确定接受任务: ${task.title}？`,
					success: res => {
						if (res.confirm) {
							acceptTask({id : task.id}).then(res => {
								if (res && res.code === 200) {
									uni.showToast({
										title: '任务接受成功',
										icon: 'success'
									});
									// 刷新任务列表
									this.getTaskList();
								}
							});
						}
					}
				});
			},
			uploadTask(task) {
				// 上传任务
				uni.navigateTo({
					url: `/pages_order/task/taskUpload?id=${task.id}&status=${task.status}`
				});
			},
			showRejectReason(task) {
				// 显示驳回原因
				if (task.rejectReason) {
					uni.showModal({
						title: '驳回原因',
						content: task.rejectReason,
						showCancel: false
					});
				} else {
					uni.showToast({
						title: '暂无驳回原因',
						icon: 'none'
					});
				}
			},
			getStatusText(status) {
				// 获取状态文本
				const statusMap = {
					'ACCEPTED': '待上传',
					'SUBMITTED': '审核中',
					'REJECTED': '未通过',
					'APPROVED': '已通过'
				};
				return statusMap[status] || status;
			}
		}
	}
</script>

<style lang="scss">
	.task-center {
		background-color: #f5f5f7;
		min-height: 100vh;

		.task-card {
			background-color: #FFFFFF;
			border-radius: 16rpx;
			margin-bottom: 30rpx;
			overflow: hidden;

			.task-header {
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;
				background-color: #FFF4E599;
				padding: 15rpx;

				.task-image {
					margin-right: 30rpx;
					display: flex;
					align-items: center;

					image {
						height: 50rpx;
						width: 50rpx;
					}
				}

				.task-type {
					color: #A94F20;
					font-size: 26rpx;
					background-color: #FFF4E5;
					padding: 10rpx 30rpx;
					border-radius: 30rpx;
				}

				.task-reward {
					font-size: 26rpx;
					margin-left: auto;

					text {
						color: #FF5722;
						font-weight: bold;
						margin-left: 10rpx;
						font-size: 30rpx;
					}
				}
			}

			.task-content {
				display: flex;
				margin-bottom: 30rpx;
				padding: 0 30rpx 0 30rpx;

				.task-icon {
					margin-right: 30rpx;
				}

				.task-info {
					flex: 1;
					position: relative;

					.task-title {
						font-size: 32rpx;
						font-weight: bold;
						color: #333;
						margin-bottom: 10rpx;
					}

					.task-desc {
						font-size: 28rpx;
						color: #666;
						margin-bottom: 10rpx;
					}

					.task-deadline {
						font-size: 24rpx;
						color: #999;
						margin-bottom: 10rpx;
					}
					
					.task-status-tag {
						display: inline-block;
						font-size: 24rpx;
						padding: 6rpx 20rpx;
						border-radius: 20rpx;
						margin-top: 10rpx;
						
						&.status-pending {
							background-color: #E6F7FF;
							color: #1890FF;
						}
						
						&.status-reviewing {
							background-color: #FFF7E6;
							color: #FA8C16;
						}
						
						&.status-rejected {
							background-color: #FFF1F0;
							color: #F5222D;
						}
						
						&.status-approved {
							background-color: #F6FFED;
							color: #52C41A;
						}
					}
				}
			}

			.task-footer {
				padding-bottom: 30rpx;
				display: flex;
				justify-content: center;
				align-items: flex-end;
				gap: 20rpx;
				flex-wrap: wrap;

				.u-button {
					width: 200rpx;
					height: 60rpx;
					font-size: 28rpx;
				}

				.task-status {
					height: 60rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 200rpx;
					font-size: 28rpx;
					
					&.task-status-approved {
						color: #52C41A;
					}
				}
			}
		}

		.empty-tip {
			text-align: center;
			padding: 60rpx 0;
			color: #999;
			font-size: 28rpx;
		}
	}
</style>