<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="基于真实服务单补充频次、周期和执行说明。">
				<ServiceInfoList
					:items="[
						{ label: '服务单号：', value: detail.caseNo },
						{ label: '服务对象：', value: detail.elderName },
						{ label: '服务类型：', value: detail.serviceName }
					]"
				></ServiceInfoList>

				<view class="block">
					<text class="label">方案标题</text>
					<input v-model="planTitle" class="input" placeholder="请输入方案标题" />
				</view>

				<view class="block">
					<text class="label">服务频次</text>
					<input v-model="serviceFrequency" class="input" placeholder="例如 每周 3 次" />
				</view>

				<view class="block">
					<text class="label">服务周期</text>
					<input v-model="serviceCycle" class="input" placeholder="例如 2026-05-14 至 2026-05-21" />
				</view>

				<view class="block">
					<text class="label">执行建议</text>
					<view class="advice-box">{{ detail.executorAdvice }}</view>
				</view>

				<view class="block">
					<text class="label">收费说明</text>
					<view class="advice-box">{{ detail.billingSummary }}</view>
				</view>

				<view class="block">
					<text class="label">方案摘要</text>
					<textarea v-model="planSummary" class="textarea" placeholder="填写服务目标、执行重点和验收口径"></textarea>
				</view>

				<view class="submit-btn" @click="submitPlan">提交方案</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 ServiceInfoList from '@/components/homeService/ServiceInfoList.uvue'
import ServicePanel from '@/components/homeService/ServicePanel.uvue'
import { fetchAdminServicePlanDetail, submitAdminServicePlan } from '@/services/homeServiceService.uts'
import { HomeServicePlanType } from '@/types/home-service.uts'

const caseId = ref('')
const detail = ref<HomeServicePlanType | null>(null)
const planTitle = ref('')
const serviceFrequency = ref('')
const serviceCycle = ref('')
const planSummary = ref('')

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

async function submitPlan() {
	if (caseId.value == '' || planTitle.value == '' || serviceFrequency.value == '' || serviceCycle.value == '' || planSummary.value == '') {
		uni.showToast({ title: '请补全方案信息', icon: 'none' })
		return
	}
	const result = await submitAdminServicePlan(caseId.value, planTitle.value, serviceFrequency.value, serviceCycle.value, planSummary.value)
	if (result != null) {
		uni.showToast({ title: '方案已提交', icon: 'success' })
		setTimeout(() => {
			uni.navigateBack()
		}, 400)
	}
}
</script>

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

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

.block {
	margin-top: 24rpx;
}

.input,
.textarea {
	margin-top: 16rpx;
	width: 100%;
	padding: 24rpx;
	box-sizing: border-box;
	background: #f8fbfc;
	border-radius: 20rpx;
	font-size: 28rpx;
	color: #23384d;
}

.textarea {
	height: 260rpx;
}

.advice-box {
	margin-top: 16rpx;
	padding: 22rpx 24rpx;
	background: #eef2f6;
	border-radius: 18rpx;
	color: #66788a;
}

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

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