<template>
	<view class="app" v-if='list'>
		<view class="item" v-for="(item,index) in list.questions" :key="index">
			<view class="title">
				{{Number(index)+1}}、{{item.questionName}}
			</view>
			<u-radio-group v-model="list.answers[index].optionContent" @change="radioGroupChange($event,item)">
				<u-radio @change="radioChange($event,aitem,index)" v-for="(aitem, aindex) in item.options" :key="aindex"
					:name="aitem.optionName">
					{{aitem.optionName}}
				</u-radio>
			</u-radio-group>
		</view>
		<view class="btns" @tap='updata'>
			提交
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		submit
	} from '@/api/questionnaire/index.js'
	export default {
		data() {
			return {
				list: null,
				timer: null,
			};
		},
		methods: {
			updata() {
				this.list.answers.forEach(e => {
					if (!e.optionId) {
						this.$refs.uToast.show({
							title: '请选择完全部选项',
							type: 'error',
						})
						return
					}
				})
				var truelist = this.list.answers.filter(e => e.optionContent == '是')
				if (truelist.length > 0) {
					this.list.evaluateResult = '您可能存在糖尿病高危风险，建议每年至医疗机构进行一次糖尿病筛查。'
				} else if (truelist.length == 0) {
					this.list.evaluateResult = '您没有糖尿病高危风险，请继续保持！'
				}
				clearInterval(this.timer);
				submit(this.list).then(res => {
					this.$refs.uToast.show({
						title: '提交成功',
						type: 'success',
					})
					this.timer = setTimeout(e => {
						uni.redirectTo({
							url: `/questionnaire/prescription/prescription?code=1002&truelistlength=${truelist.length}&list=${encodeURIComponent(JSON.stringify(this.list))}`
						})
					}, 2000)
				})
			},
			// 选中某个单选框时，由radio时触发
			radioChange(e, item, index) {
				this.list.answers[index].optionId = item.id
			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e) {
				// console.log(e);
			}
		},
		onLoad(options) {
			let list = JSON.parse(decodeURIComponent(options.item))
			list.answers = []
			list.questions.forEach(e => {
				e.name = '请选择'
				var obj = {
					"questionId": e.id,
					"optionId": null,
					"optionContent": null,
				}
				list.answers.push(obj)
			})
			this.list = {
				userId: uni.getStorageSync('patientId'),
				surveyId: list.id,
				surveySubject: list.surveySubject,
				evaluateResult: null,
				advice: null,
				questions: list.questions,
				answers: list.answers
			}
		}
	}
</script>

<style lang="scss">
	.app {
		width: 96%;
		margin: 16rpx auto;
		background: #FFFFFF;
		box-shadow: 0rpx 9rpx 31rpx 9rpx rgba(0, 0, 0, 0.03);
		border-radius: 5rpx;
		padding-bottom: 200rpx;

		.btns {
			width: 90%;
			margin: 100rpx auto;
			background-color: #26A888;
			text-align: center;
			line-height: 60rpx;
			height: 60rpx;
			color: #fff;
		}

		.item {
			width: 90%;
			margin: 0 auto;
			padding: 30rpx 0;

			::v-deep .u-radio {
				padding: 20rpx;
			}

			.title {
				padding: 40rpx 0 20rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #333333;
			}

			::v-deep .uni-picker-container {
				width: 100%;
				height: 63rpx;
				background: #A7A7A7;
				line-height: 63rpx;
				opacity: 0.1;
				border-radius: 5rpx;
			}
		}
	}
</style>