<template>
	<ServicePageScaffold title="结算归档" fallback-url="/pages/mall/admin/home-service/application-management/index">
		<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.archiveStatusText"
					:items="[
						{ label: '账期', value: detail.billingPeriod }
					]"
				></ServiceInfoCard>
				<ServiceInfoList
					:items="[
						{ label: '服务对象：', value: detail.elderName }
					]"
				></ServiceInfoList>

				<view class="amount-card">
					<text class="amount-line">服务总额：{{ detail.totalAmount }}</text>
					<text class="amount-line">长护险/补贴：{{ detail.insuranceAmount }}</text>
					<text class="amount-line">个人自付：{{ detail.selfPayAmount }}</text>
				</view>

				<view class="archive-box">
					<text class="archive-text">已包含执行记录、验收反馈、结算单与电子档案索引，提交后将标记为已归档。</text>
				</view>

				<view class="submit-btn" @click="submitArchive">确认归档</view>
			</ServicePanel>
		</view>
	</ServicePageScaffold>
</template>

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

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

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

async function submitArchive() {
	if (caseId.value == '') {
		return
	}
	const result = await submitAdminSettlementArchive(caseId.value)
	if (result != null) {
		detail.value = result
		uni.showToast({ title: '已完成归档', icon: 'success' })
	}
}
</script>

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

.amount-line,
.archive-text,
.empty-text {
	font-size: 28rpx;
	line-height: 40rpx;
	color: #16324f;
}

.amount-card,
.archive-box {
	margin-top: 24rpx;
	padding: 24rpx;
	border-radius: 20rpx;
	background: #f8fbfc;
}

.amount-line + .amount-line {
	margin-top: 12rpx;
}

.archive-text {
	color: #66788a;
}

.submit-btn {
	margin-top: 32rpx;
	padding: 26rpx 0;
	border-radius: 20rpx;
	background: #15803d;
	text-align: center;
	font-size: 30rpx;
	font-weight: 700;
	color: #ffffff;
}

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