<template>
	<scroll-view class="page" scroll-y="true">
		<view v-if="detail == null" class="empty-box">
			<text class="empty-text">未找到对应服务单</text>
		</view>
		<view v-else>
			<ServicePanel title="服务进度" subtitle="申请到执行的关键状态先以 mock 流程展示。">
				<ServiceInfoCard
					:title="detail.serviceName"
					:code="detail.caseNo"
					:status-text="detail.statusText"
					:status-tone="detail.statusTone"
					:items="[
						{ label: '上门时间', value: detail.serviceTime },
						{ label: '当前进度', value: '第 ' + detail.currentStep + ' / ' + detail.totalSteps + ' 步' },
						{ label: '执行人员', value: detail.staffName + ' ' + detail.staffPhone }
					]"
				></ServiceInfoCard>
			</ServicePanel>

			<ServicePanel title="服务对象信息">
				<ServiceInfoList
					:items="[
						{ label: '申请人：', value: detail.applicantName },
						{ label: '服务对象：', value: detail.elderName + '，' + detail.age + ' 岁' },
						{ label: '联系电话：', value: detail.phone },
						{ label: '服务地址：', value: detail.address },
						{ label: '需求说明：', value: detail.summary }
					]"
				></ServiceInfoList>
				<view v-if="detail.status == 'pending_acceptance'" class="feedback-btn" @click="goFeedback">去验收反馈</view>
			</ServicePanel>

			<ServicePanel title="过程留痕" subtitle="后续可替换为真实时间线与上传凭证。">
				<ServiceTimeline :items="detail.timeline"></ServiceTimeline>
			</ServicePanel>
		</view>
	</scroll-view>
</template>

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

const caseId = ref('')
const detail = ref<HomeServiceCaseType | null>(null)

async function loadData() {
	if (caseId.value == '') {
		return
	}
	detail.value = await fetchConsumerHomeServiceCaseDetail(caseId.value)
}

function goFeedback() {
	if (caseId.value == '') {
		return
	}
	uni.navigateTo({ url: '/pages/mall/consumer/home-service/feedback?id=' + caseId.value })
}

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

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

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

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

.feedback-btn {
	margin-top: 24rpx;
	padding: 24rpx 0;
	text-align: center;
	font-size: 28rpx;
	font-weight: 700;
	color: #ffffff;
	background: #1d4ed8;
	border-radius: 18rpx;
}

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