<template>
	<view class="page padding">
		<view class="padding bg-white radius">
			<view class="text-bold">{{ checkData?.name || '加载中...' }}</view>
			<view class="margin-top">
				<rich-text :nodes="checkData?.point || ''"></rich-text>
			</view> 
			<view class="margin-top">
				<u-radio-group
					v-model="radiovalue1"
					placement="row"
					@change="groupChange"
				>
					<u-radio
						:customStyle="{marginBottom: '8px'}"
						v-for="(item, index) in radiolist1"
						:key="index"
						:label="item.name"
						:name="item.name"
						@change="radioChange"
					>
					</u-radio>
				</u-radio-group>
			</view>
			<view>
				<up-textarea v-model="value1" placeholder="请输入备注信息" ></up-textarea>
		</view>
		</view>
		<button class="bg-blue round margin-top-xl" @click="handleSubmit">提交</button>
	</view>
</template>

<script setup>
	import { ref, reactive } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import { enterCheckPlan,submitCheckResult } from '@/request/api.js';
	
	// 页面参数
	const oneTableId = ref('');
	// 检查项数据
	const checkData = ref(null);
	
	// 单选框 result: 1.正常 2.异常 3.不涉及
	const radiolist1 = reactive([
	  {
	    name: '正常',
	    value: 1,
	    disabled: false,
	  },
	  {
	    name: '异常',
	    value: 2,
	    disabled: false,
	  },
	  {
	    name: '不涉及',
	    value: 3,
	    disabled: false,
	  },
	]);
	
	// 选中的结果值
	const radiovalue1 = ref('');
	
	const groupChange = (n) => {
	  console.log('groupChange', n);
	};
	
	const radioChange = (n) => {
	  console.log('radioChange', n);
	};
	
	// 备注
	const value1 = ref('');
	
	// 提交检查结果
	const handleSubmit = async () => {
		// 验证是否选择了检查结果
		if (!radiovalue1.value) {
			uni.showToast({ title: '请选择检查结果', icon: 'none' });
			return;
		}
		
		// 获取选中项对应的 value 值
		const selectedItem = radiolist1.find(item => item.name === radiovalue1.value);
		const resultValue = selectedItem ? selectedItem.value : null;
		
		if (!resultValue) {
			uni.showToast({ title: '请选择检查结果', icon: 'none' });
			return;
		}
		
		try {
			const params = {
				taskId: checkData.value?.taskId,
				result: resultValue,
				remark: value1.value
			};
			console.log('提交参数：', params);
			
			const res = await submitCheckResult(params);
			console.log('提交结果：', res);
			
			if (res.code === 0) {
				uni.showToast({ title: '提交成功', icon: 'success' });
				// 提交成功后可以返回上一页或刷新数据
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			} else {
				uni.showToast({ title: res.msg || '提交失败', icon: 'none' });
			}
		} catch (error) {
			console.error('提交失败：', error);
			uni.showToast({ title: '提交失败', icon: 'none' });
		}
	};
	
	// 获取检查项数据
	const getCheckData = async () => {
		try {
			const res = await enterCheckPlan(oneTableId.value);
			console.log('检查项数据：', res);
			if (res.code === 0) {
				checkData.value = res.data;
			}
		} catch (error) {
			console.error(error);
		}
	};
	
	// 页面加载时获取参数并调用接口
	onLoad((options) => {
		console.log('接收到的参数：', options);
		if (options.id) {
			oneTableId.value = options.id;
			getCheckData();
		}
	});
</script>

<style lang="scss" scoped>
	.page {
		min-height: 100vh;
		background: #EBF2FC;
	}
</style>