<template>
	<view class="form-container">
		<u-form :model="form" label-width="250px" label-position="top">
			<u-divider></u-divider>
			<view v-for="(parent, index) in firstArray" :key="parent.questionId">
				<text>{{ parent.questionName }}</text>

				<template v-if="secondArray[index] === '是否'">
					<u-radio-group v-model="selectedValues[index]" @change="change">
						<!-- <u-radio :value="parent.value" size="large" label="是"></u-radio> -->
						<u-radio size="large" label="是" name="1"></u-radio>
						<u-radio  size="large" label="否"  name="0"></u-radio>
					</u-radio-group>
				</template>

				<template v-else>
					<u-radio-group v-model="selectedValues[index]">
						<u-radio v-for="child in secondArray[index]" :key="child.questionId" :name="child.value"
							size="large" :label="child.questionName">
						</u-radio>
					</u-radio-group>
				</template>
			</view>

			<u-form-item class="submit-btn-container">
				<u-button type="primary" @click="submitForm">提交</u-button>
				<u-button @click="goBack">取消</u-button>
			</u-form-item>
		</u-form>
	</view>

</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		onLoad,
		onShow,
		onHide,
		onReady
	} from '@dcloudio/uni-app'
	import {
		useRoute
	} from 'vue-router'; // 如果使用 vue-router
	import {
		listQuestionnaireTemplate,
		listElderProfile,
		addLrnlpg
	} from '@/api/elder/pensionAssessment/fwzypgmb/index';
	
	
	const name = ref('');
	const loading = ref(false); // 定义 loading 变量
	const DataList = ref([]); // 定义 DataList 变量
	const total = ref(0); // 定义 total 变量
	const elderList = ref([])
	const firstArray = ref([]);
	const secondArray = ref([]);
	const selectedValues = ref([]);
	const questionTitles = ref([])
	const dataList = ref([])
	let moban=ref({})
	const questionnaireTemplateList = ref([])
	const isSubmitting = ref(false);
	const route = useRoute(); // 使用 vue-router 获取当前路由信息
onLoad((option) => {
		console.log('-----option',JSON.parse(option.item))
		moban.value = JSON.parse(option.item)
		console.log('-----option',moban.value.elderQuestionnairePaperId)
		getList();
	})
	function getList() {
		loading.value = true;
		const receivedName = moban.value.elderQuestionnairePaperId;
		

		// 更新响应式数据
		name.value = receivedName ? decodeURIComponent(receivedName) : '';



		console.log("Received name:", name.value);

		listElderProfile().then((res) => {
			// console.log("老人信息", res.rows[0]);
			elderList.value = res.rows[0]
			// 根据需要处理 res 数据
		}).finally(() => {
			loading.value = false;
		});

		listQuestionnaireTemplate({
			paperId: moban.value.elderQuestionnairePaperId,
			status: 0
		}).then((res) => {
			questionTitles.value = res.rows;
			questionnaireTemplateList.value = res.rows;
			firstArray.value = res.rows.filter(item => item.parentQuestionid === null || item.parentQuestionid ===
				'0');
			secondArray.value = firstArray.value.map(parent => {
				const children = res.rows.filter(item => item.parentQuestionid === parent.questionId);
				if (parent.questionType === '2') {
					return '是否';
				} else {
					return children;
				}
			});
			selectedValues.value = new Array(firstArray.value.length).fill(null);
			console.log(88888,secondArray.value,selectedValues.value, 888,firstArray.value)
			console.log(1111,firstArray.value.map(parent => {
				const children = res.rows.filter(item => item.parentQuestionid === parent.questionId);

				if (parent.questionType === '2') {
					return '是否';
				} else {
					return children;
				}
			}))
			// 更新总数
			total.value = res.total;
		}).finally(() => {
			loading.value = false;
		});

	}
	function change(e){
		console.log('selectedValues[index]---',selectedValues.value,e)
	}
	function submitForm() {
		if (isSubmitting.value) {
			console.log('提交已被锁定，防止多次提交');

			return; // 防止多次提交
		}

		isSubmitting.value = true; // 标记为正在提交
		// 创建一个空的list
		const list = [];
		const params = new URLSearchParams(location.search);

		// 遍历firstArray，生成包含questionId和value的对象
		firstArray.value.forEach((parent, index) => {
			list.push({
				questionId: parent.questionId,
				value: selectedValues.value[index]
			});
		});

		// 打印list以便调试
		console.log('生成的list:', list);
		addLrnlpg({
			list,
			elderName: elderList.value.name,
			birthDate: elderList.value.birthday,
			elderNumber: elderList.value.sn,
			elderProfileId: elderList.value.id,
			elderQuestionnairePaperId: route.query.name,
			gender: elderList.value.gender,
		}).then(res => {
			console.log(res)
		})
	}

	function goBack() {
		console.log('Cancel button clicked');
	};
	onMounted(() => {
	});
</script>