<template>
	<ServicePageScaffold title="服务记录" fallback-url="/pages/mall/delivery/home/index">
		<ServicePanel title="历史记录" subtitle="展示服务留痕、验收结果、评价与结算状态。">
			<view v-if="records.length == 0" class="empty-box"><text class="empty-text">暂无服务记录</text></view>
			<view v-for="item in records" :key="item.id" class="record-card" @click="goDetail(item.orderId)">
				<text class="row-title">{{ item.orderNo }} · {{ item.serviceName }}</text>
				<text class="row-text">服务对象：{{ item.elderNameMasked }}</text>
				<text class="row-text">状态：{{ item.statusText }}</text>
				<text class="row-text">时间：{{ item.actualStartTime }} ~ {{ item.actualEndTime }}</text>
				<text class="row-text">结算：{{ item.settlementStatus }} · 评价：{{ item.ratingText }}</text>
			</view>
		</ServicePanel>
	</ServicePageScaffold>
</template>

<script setup lang="uts">
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import ServicePageScaffold from '@/components/homeService/ServicePageScaffold.uvue'
import ServicePanel from '@/components/homeService/ServicePanel.uvue'
import type { DeliveryRecordType } from '@/types/delivery.uts'
import { getDeliveryRecords } from '@/services/deliveryService.uts'
import { requireDeliveryAuth } from '@/utils/deliveryAuth.uts'

const records = ref([] as Array<DeliveryRecordType>)

async function loadData() {
	const authResult = await requireDeliveryAuth({ redirectOnFail: true, toastOnFail: true })
	if (!authResult.ok) {
		return
	}
	records.value = await getDeliveryRecords()
}

function goDetail(orderId: string) {
	uni.navigateTo({ url: '/pages/mall/delivery/records/detail?id=' + orderId })
}

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

<style scoped>
.record-card {
	padding: 24rpx;
	margin-bottom: 16rpx;
	border-radius: 20rpx;
	background: #f8fbfc;
}

.row-title {
	font-size: 30rpx;
	font-weight: 700;
	color: #16324f;
}

.row-text,
.empty-text {
	display: block;
	margin-top: 12rpx;
	font-size: 24rpx;
	line-height: 36rpx;
	color: #5e758c;
}

.empty-box {
	padding: 24rpx;
}
</style>