<template>
	<ServicePageScaffold title="任务详情" fallback-url="/pages/mall/merchant/home-service/tasks">
		<view v-if="detail == null" class="empty-box">
			<text class="empty-text">未找到任务详情</text>
		</view>
		<view v-else>
			<ServicePanel title="任务状态" subtitle="执行端首批支持签到开始与完成提交两个动作。">
				<ServiceInfoCard
					:title="detail.serviceName"
					:code="detail.caseNo"
					:status-text="detail.statusText"
					:status-tone="detail.statusTone"
					:items="[
						{ label: '服务对象', value: detail.elderName },
						{ label: '上门时间', value: detail.appointmentTime },
						{ label: '签到状态', value: detail.checkInStatus },
						{ label: '执行人员', value: detail.staffName + ' ' + detail.staffPhone },
						{ label: '服务地址', value: detail.address }
					]"
				></ServiceInfoCard>
				<ServiceActionRow
					:actions="[
						{ key: 'check-in', label: '到岗签到', tone: 'teal' },
						{ key: 'record', label: '服务记录', tone: 'ghost' },
						{ key: 'exception', label: '异常上报', tone: 'warn' }
					]"
					@action="handleDetailAction"
				></ServiceActionRow>
				<view class="primary-btn" @click="handleAdvance">{{ detail.actionText }}</view>
			</ServicePanel>

			<ServicePanel title="服务记录">
				<text class="task-info">记录摘要：{{ detail.recordSummary }}</text>
				<text class="task-info">后续将补充签到拍照、服务步骤打卡和上传凭证组件。</text>
			</ServicePanel>

			<ServicePanel title="过程时间线">
				<ServiceTimeline :items="detail.timeline"></ServiceTimeline>
			</ServicePanel>
		</view>
	</ServicePageScaffold>
</template>

<script setup lang="uts">
import { ref } from 'vue'
import { onLoad, onShow } from '@dcloudio/uni-app'
import ServicePageScaffold from '@/components/homeService/ServicePageScaffold.uvue'
import ServiceActionRow from '@/components/homeService/ServiceActionRow.uvue'
import ServiceInfoCard from '@/components/homeService/ServiceInfoCard.uvue'
import ServicePanel from '@/components/homeService/ServicePanel.uvue'
import ServiceTimeline from '@/components/homeService/ServiceTimeline.uvue'
import { advanceWorkerTask, fetchWorkerTaskDetail } from '@/services/homeServiceService.uts'
import { HomeServiceTaskType } from '@/types/home-service.uts'

const taskId = ref('')
const detail = ref<HomeServiceTaskType | null>(null)

async function loadData() {
	if (taskId.value == '') {
		return
	}
	detail.value = await fetchWorkerTaskDetail(taskId.value)
}

async function handleAdvance() {
	if (taskId.value == '') {
		return
	}
	if (detail.value != null && detail.value.status == 'pending_visit') {
		goCheckIn()
		return
	}
	if (detail.value != null && detail.value.status != 'serving') {
		uni.showToast({ title: '当前任务无需更新', icon: 'none' })
		return
	}
	const result = await advanceWorkerTask(taskId.value)
	if (result != null) {
		detail.value = result
		uni.showToast({ title: '任务状态已更新', icon: 'success' })
	}
}

function goCheckIn() {
	if (taskId.value == '') {
		return
	}
	uni.navigateTo({ url: '/pages/mall/merchant/home-service/check-in?id=' + taskId.value })
}

function goRecord() {
	if (taskId.value == '') {
		return
	}
	uni.navigateTo({ url: '/pages/mall/merchant/home-service/service-record?id=' + taskId.value })
}

function goException() {
	if (taskId.value == '') {
		return
	}
	uni.navigateTo({ url: '/pages/mall/merchant/home-service/exception-report?id=' + taskId.value })
}

function handleDetailAction(actionKey: string) {
	if (actionKey == 'check-in') {
		goCheckIn()
	} else if (actionKey == 'record') {
		goRecord()
	} else if (actionKey == 'exception') {
		goException()
	}
}

onLoad((options) => {
	const id = options['id']
	if (id != null) {
		taskId.value = id as string
		loadData()
	}
})

onShow(() => {
	loadData()
})
</script>

<style scoped>
.page {
	min-height: 100vh;
	background: #f3f7f9;
	padding: 24rpx;
	box-sizing: border-box;
}

.task-info,
.empty-text {
	margin-top: 10rpx;
	font-size: 26rpx;
	line-height: 38rpx;
	color: #66788a;
}

.primary-btn {
	margin-top: 24rpx;
	padding: 24rpx 0;
	text-align: center;
	font-size: 30rpx;
	font-weight: 700;
	color: #ffffff;
	background: #0f766e;
	border-radius: 18rpx;
}

.empty-box {
	padding: 120rpx 0;
	align-items: center;
}
</style>