<template>
	<ServicePageScaffold title="异常上报" fallback-url="/pages/mall/merchant/home-service/tasks">
			<ServicePanel title="异常上报" subtitle="提交真实异常类型、说明和调度协同信息。">
			<text class="info">任务编号：{{ taskNo }}</text>
			<view class="block">
				<text class="label">异常类型</text>
				<view class="type-wrap">
					<view
						v-for="item in exceptionTypes"
						:key="item"
						class="type-item"
						:class="selectedType == item ? 'active-type' : ''"
						@click="selectedType = item"
					>
						{{ item }}
					</view>
				</view>
			</view>
			<view class="block">
				<text class="label">异常说明</text>
				<textarea v-model="description" class="textarea" placeholder="填写未能执行原因、现场情况、已采取措施"></textarea>
			</view>
			<view class="block">
				<text class="label">处理建议</text>
				<view class="suggestion-box">上报后将同步给调度员与管理端，后续可继续补充整改结果。</view>
			</view>
			<view class="submit-btn warn" @click="submitException">提交异常</view>
		</ServicePanel>
	</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 { fetchWorkerTaskDetail, submitWorkerException } from '@/services/homeServiceService.uts'

const taskId = ref('')
const taskNo = ref('')
const selectedType = ref('服务对象不在家')
const description = ref('现场无人响应，已电话联系家属，等待调度重新确认上门时间。')
const exceptionTypes = ['服务对象不在家', '临时身体不适', '环境不具备执行条件', '物资缺失', '其他']

onLoad((options) => {
	const id = options['id']
	if (id != null) {
		taskId.value = id as string
		fetchWorkerTaskDetail(taskId.value).then((res) => {
			if (res != null) {
				taskNo.value = res.caseNo
			}
		})
	}
})

async function submitException() {
	if (taskId.value == '' || description.value == '') {
		uni.showToast({ title: '请填写异常说明', icon: 'none' })
		return
	}
	const result = await submitWorkerException(taskId.value, selectedType.value, description.value)
	if (result != null) {
		uni.showToast({ title: '异常已上报', icon: 'success' })
		setTimeout(() => {
			uni.navigateBack()
		}, 400)
	}
}
</script>

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

.info,
.label,
.suggestion-box,
.type-item {
	font-size: 28rpx;
	line-height: 40rpx;
	color: #16324f;
}

.block {
	margin-top: 24rpx;
}

.type-wrap {
	margin-top: 16rpx;
	flex-direction: row;
	flex-wrap: wrap;
	gap: 16rpx;
}

.type-item {
	padding: 18rpx 22rpx;
	border-radius: 18rpx;
	background: #f8fbfc;
}

.active-type {
	background: #fff4e5;
	color: #b45309;
}

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

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

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

.warn {
	background: #b45309;
}
</style>