<template>
	<ServicePageScaffold title="验收反馈" fallback-url="/pages/mall/consumer/home-service/index">
		<view v-if="detail == null" class="empty-box">
			<text class="empty-text">未找到验收信息</text>
		</view>
		<view v-else>
			<ServicePanel title="验收反馈" subtitle="家属可确认服务完成情况，也可退回整改。">
				<text class="info">服务单号：{{ detail.caseNo }}</text>
				<text class="info">服务对象：{{ detail.elderName }}</text>
				<text class="info">服务项目：{{ detail.serviceName }}</text>
				<text class="info">当前状态：{{ detail.acceptanceStatusText }}</text>

				<view class="block">
					<text class="label">满意度评分</text>
					<view class="rating-row">
						<view v-for="score in scores" :key="score" class="rating-item" :class="rating >= score ? 'rating-active' : ''" @click="rating = score">{{ score }}分</view>
					</view>
				</view>

				<view class="block">
					<text class="label">评价标签</text>
					<view class="tag-row">
						<view
							v-for="item in allTags"
							:key="item"
							class="tag-item"
							:class="selectedTags.indexOf(item) >= 0 ? 'tag-active' : ''"
							@click="toggleTag(item)"
						>
							{{ item }}
						</view>
					</view>
				</view>

				<view class="block">
					<text class="label">反馈说明</text>
					<textarea v-model="feedback" class="textarea" placeholder="填写验收意见、服务感受或需要整改的问题"></textarea>
				</view>

				<view class="action-row">
					<view class="action ghost" @click="submitResult(false)">退回整改</view>
					<view class="action primary" @click="submitResult(true)">确认验收</view>
				</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 ServicePanel from '@/components/homeService/ServicePanel.uvue'
import { fetchConsumerAcceptanceDetail, submitConsumerAcceptance } from '@/services/homeServiceService.uts'
import { HomeServiceAcceptanceType } from '@/types/home-service.uts'
import { getCurrentUser, getCurrentUserId } from '@/utils/store.uts'
import { goToLogin } from '@/utils/utils.uts'

const caseId = ref('')
const detail = ref<HomeServiceAcceptanceType | null>(null)
const rating = ref(5)
const feedback = ref('护理员服务规范，过程说明清楚，老人状态稳定。')
const selectedTags = ref<Array<string>>([])
const scores = [1, 2, 3, 4, 5]
const allTags = ['准时上门', '沟通清楚', '动作规范', '记录完整', '需进一步整改']

async function ensureLogin(): Promise<boolean> {
	const user = await getCurrentUser()
	if (user == null || getCurrentUserId() == '') {
		goToLogin('/pages/mall/consumer/home-service/feedback?id=' + caseId.value)
		return false
	}
	return true
}

onLoad((options) => {
	const id = options['id']
	if (id != null) {
		caseId.value = id as string
		ensureLogin().then((ok) => {
			if (!ok) {
				return
			}
			fetchConsumerAcceptanceDetail(caseId.value).then((res) => {
				if (res != null) {
					detail.value = res
					rating.value = res.rating
					feedback.value = res.feedback
					selectedTags.value = res.tags.slice(0)
				}
			})
		})
	}
})

function toggleTag(tag: string) {
	const index = selectedTags.value.indexOf(tag)
	if (index >= 0) {
		selectedTags.value.splice(index, 1)
	} else {
		selectedTags.value.push(tag)
	}
}

async function submitResult(approved: boolean) {
	if (!(await ensureLogin())) {
		return
	}
	if (caseId.value == '' || feedback.value == '') {
		uni.showToast({ title: '请填写反馈说明', icon: 'none' })
		return
	}
	const result = await submitConsumerAcceptance(caseId.value, approved, rating.value, feedback.value, selectedTags.value)
	if (result != null) {
		uni.showToast({ title: approved ? '已完成验收' : '已退回整改', icon: 'success' })
		setTimeout(() => {
			uni.navigateBack()
		}, 400)
	}
}
</script>

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

.info,
.label,
.rating-item,
.tag-item,
.empty-text {
	font-size: 28rpx;
	line-height: 40rpx;
	color: #16324f;
}

.block {
	margin-top: 24rpx;
}

.rating-row,
.tag-row,
.action-row {
	margin-top: 16rpx;
	flex-direction: row;
	flex-wrap: wrap;
	gap: 16rpx;
}

.rating-item,
.tag-item {
	padding: 16rpx 20rpx;
	border-radius: 18rpx;
	background: #eef2f6;
}

.rating-active,
.tag-active {
	background: #e8f2ff;
	color: #1d4ed8;
}

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

.action {
	flex: 1;
	padding: 24rpx 0;
	border-radius: 18rpx;
	text-align: center;
	font-size: 28rpx;
	font-weight: 700;
}

.ghost {
	background: #fff4e5;
	color: #b45309;
}

.primary {
	background: #1d4ed8;
	color: #ffffff;
}

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