<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.statusText"
					:items="[
						{ label: '责任人', value: detail.ownerName },
						{ label: '整改时限', value: detail.deadline }
					]"
				></ServiceInfoCard>
				<ServiceInfoList
					:items="[
						{ label: '服务对象：', value: detail.elderName },
						{ label: '服务项目：', value: detail.serviceName }
					]"
				></ServiceInfoList>

				<view class="block">
					<text class="label">问题说明</text>
					<textarea v-model="issueSummary" class="textarea" placeholder="填写整改动作、补充说明和复验收提示"></textarea>
				</view>

				<view class="submit-btn" @click="submitRectification">提交整改结果</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 { fetchAdminRectificationDetail, submitAdminRectification } from '@/services/homeServiceService.uts'
import { HomeServiceRectificationType } from '@/types/home-service.uts'

const caseId = ref('')
const detail = ref<HomeServiceRectificationType | null>(null)
const issueSummary = ref('已补充现场留痕与护理动作说明，建议家属重新确认验收。')

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

async function submitRectification() {
	if (caseId.value == '' || issueSummary.value == '') {
		uni.showToast({ title: '请填写整改结果', icon: 'none' })
		return
	}
	const result = await submitAdminRectification(caseId.value, issueSummary.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,
.empty-text {
	font-size: 28rpx;
	line-height: 40rpx;
	color: #16324f;
}

.block {
	margin-top: 24rpx;
}

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

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

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