<template>
	<view class="detail-container">
		<view v-if="loading" class="loading"><u-loading mode="circle" text="加载中..." /></view>
		<view v-else-if="taskDetail">
			<view class="title">{{ taskDetail.taskTitle }}</view>
			<view class="row"><text class="label">负责人：</text>{{ taskDetail.workerNickName || '-' }}</view>
			<view class="row"><text class="label">开始时间：</text>{{ taskDetail.beginTime || '-' }}</view>
			<view class="row"><text class="label">结束时间：</text>{{ taskDetail.finishTime || '-' }}</view>
			<view class="row"><text class="label">创建人：</text>{{ taskDetail.createUserNickName || '-' }}</view>
			<view class="row"><text class="label">创建时间：</text>{{ taskDetail.createTime || '-' }}</view>
			<view class="section-title">任务描述</view>
			<mp-html v-if="taskDetail.content" :content="taskDetail.content" />
			<view class="section-title">报工进度</view>
			<view v-if="taskDetail.taskItemVoList && taskDetail.taskItemVoList.length">
				<view v-for="item in taskDetail.taskItemVoList" :key="item.itemId" class="item-block">
					<view class="item-row"><text class="item-label">进度时间：</text>{{ item.signTime || '-' }}</view>
					<view class="item-row"><text class="item-label">进度区间：</text>{{ item.beginTime || '-' }} ~ {{ item.endTime || '-' }}</view>
					<view class="item-row"><text class="item-label">进度内容：</text></view>
					<mp-html v-if="item.content" :content="item.content" />
					<view class="item-row"><text class="item-label">完成时间：</text>{{ item.completedTime || '-' }}</view>
				</view>
			</view>
			<view v-else class="empty-progress">暂无报工进度</view>

			<u-button type="primary" class="add-btn" @click="openAddPopup">新增报工</u-button>

			<uni-popup ref="addPopup" type="bottom" :mask-click="true">
				<view class="add-dialog-bottom">
					<view class="dialog-title">新增报工</view>
					<u-form :model="addForm" ref="addFormRef">
						<u-form-item label="进度区间">
							<view class="period-row">{{ addForm.beginTime }} ~ {{ addForm.endTime }}</view>
						</u-form-item>
						<u-form-item label="进度时间">
							<view class="period-row">{{ addForm.signTime }}</view>
						</u-form-item>
						<u-form-item label="完成时间">
							<view class="period-row">{{ addForm.completedTime }}</view>
						</u-form-item>
						<u-form-item label="进度内容">
							<u-textarea v-model="addForm.content" placeholder="请输入进度内容" autoHeight />
						</u-form-item>
						<u-form-item label="备注">
							<u-textarea v-model="addForm.remark" placeholder="备注（可选）" autoHeight />
						</u-form-item>
					</u-form>
					<view class="dialog-actions">
						<u-button type="primary" @click="submitAdd">提交</u-button>
						<u-button @click="$refs.addPopup.close()">取消</u-button>
					</view>
				</view>
			</uni-popup>
		</view>
		<view v-else class="empty">未查询到任务详情</view>
	</view>
</template>

<script>
import { getTask } from '@/api/oa/task.js'
import { addOaTaskItem } from '@/api/oa/taskItem.js'
import mpHtml from 'uni_modules/mp-html/components/mp-html/mp-html.vue'

export default {
	components: { mpHtml },
	data() {
		return {
			taskDetail: null,
			loading: true,
			addForm: {
				beginTime: '',
				endTime: '',
				content: '',
				remark: ''
			}
		}
	},
	onLoad(options) {
		const taskId = options.id
		if (taskId) {
			this.taskId = taskId
			this.fetchDetail(taskId)
		} else {
			this.loading = false
		}
	},
	methods: {
		async fetchDetail(taskId) {
			this.loading = true
			try {
				const res = await getTask(taskId)
				if (res.code === 200 && res.data) {
					this.taskDetail = res.data
				} else {
					uni.showToast({ title: res.msg || '获取详情失败', icon: 'none' })
				}
			} catch (e) {
				uni.showToast({ title: '网络错误', icon: 'none' })
			} finally {
				this.loading = false
			}
		},
		openAddPopup() {
			// 自动获取进度区间
			let begin = '', end = '';
			const items = this.taskDetail?.taskItemVoList || [];
			if (items.length > 0) {
				// 取最后一个item的endTime为下一个周期的beginTime
				const last = items[items.length - 1];
				begin = last.endTime || this.taskDetail.beginTime;
				// 结束时间=begin+timeGap天
				const gap = this.taskDetail.timeGap || 7;
				const bDate = new Date(begin.replace(/-/g, '/'));
				bDate.setDate(bDate.getDate() + gap);
				end = bDate.getFullYear() + '-' + String(bDate.getMonth() + 1).padStart(2, '0') + '-' + String(bDate.getDate()).padStart(2, '0');
			} else {
				begin = this.taskDetail.beginTime;
				end = this.taskDetail.finishTime;
			}
			// 进度时间=beginTime，完成时间=当天
			const today = new Date();
			const todayStr = today.getFullYear() + '-' + String(today.getMonth() + 1).padStart(2, '0') + '-' + String(today.getDate()).padStart(2, '0');
			this.addForm = {
				beginTime: begin,
				endTime: end,
				signTime: begin,
				completedTime: todayStr,
				content: '',
				remark: ''
			};
			this.$refs.addPopup.open();
		},
		async submitAdd() {
			if (!this.addForm.beginTime || !this.addForm.endTime || !this.addForm.content) {
				uni.showToast({ title: '请填写进度内容', icon: 'none' })
				return
			}
			const data = {
				taskId: this.taskId,
				beginTime: this.addForm.beginTime,
				endTime: this.addForm.endTime,
				signTime: this.addForm.signTime,
				completedTime: this.addForm.completedTime,
				content: this.addForm.content,
				remark: this.addForm.remark || null,
				status: 0
			}
			try {
				const res = await addOaTaskItem(data)
				if (res.code === 200) {
					uni.showToast({ title: '新增报工成功', icon: 'success' })
					this.$refs.addPopup.close()
					this.addForm = { beginTime: '', endTime: '', signTime: '', completedTime: '', content: '', remark: '' }
					this.fetchDetail(this.taskId)
				} else {
					uni.showToast({ title: res.msg || '新增失败', icon: 'none' })
				}
			} catch (e) {
				uni.showToast({ title: '网络错误', icon: 'none' })
			}
		}
	}
}
</script>

<style scoped>
.detail-container {
	padding: 32rpx;
}
.title {
	font-size: 36rpx;
	font-weight: bold;
	margin-bottom: 24rpx;
}
.row {
	font-size: 28rpx;
	margin-bottom: 12rpx;
}
.label {
	color: #888;
}
.section-title {
	margin-top: 32rpx;
	font-size: 30rpx;
	font-weight: 600;
	color: #333;
	margin-bottom: 16rpx;
}
.item-block {
	background: #f8f9fa;
	border-radius: 12rpx;
	padding: 20rpx;
	margin-bottom: 20rpx;
}
.item-row {
	font-size: 26rpx;
	margin-bottom: 8rpx;
}
.item-label {
	color: #666;
}
.empty-progress, .empty {
	text-align: center;
	color: #bbb;
	margin: 40rpx 0;
}
.loading {
	text-align: center;
	margin: 80rpx 0;
}
.add-btn {
	margin-top: 40rpx;
	width: 100%;
}
.add-dialog-bottom {
    background: #fff;
    border-radius: 24rpx 24rpx 0 0;
    padding: 40rpx 32rpx 32rpx 32rpx;
    width: 100vw;
    max-width: 750rpx;
    box-sizing: border-box;
}
.dialog-title {
	font-size: 32rpx;
	font-weight: 600;
	margin-bottom: 32rpx;
	text-align: center;
}
.dialog-actions {
	display: flex;
	gap: 24rpx;
	margin-top: 32rpx;
	justify-content: center;
}
.period-row {
	font-size: 28rpx;
	color: #333;
	padding: 12rpx 0 12rpx 8rpx;
}
</style>
