<template>
	<view class="containers">
		<view class="steps">
			<uni-steps :options="steps" :active="stepActive" active-color="#044ed7" />
		</view>
		<view class="asks">
			<view v-for="(item,index) in askList">
				<view>{{index}}{{item.title}}({{item.type==1?'单选':'多选'}})</view>
				<view v-if="item.type==1">
					<uni-data-checkbox v-model="result[index]" @change="e=>radioAnswer(e,index)"
						:localdata="item.answer" :wrap='true' :map="{text:'content',value:'content'}" />
				</view>
				<view v-if="item.type==2">
					<uni-data-checkbox v-model="resultArr[index]" @change="e=>chenckAnswer(e,item,index)"
						:localdata="item.answer" :wrap='true' multiple :map="{text:'content',value:'content'}" />
				</view>
			</view>
		</view>
		
		<view disabled class="justify-between submitBtn">
			<text>需要正确率达{{rate}}%</text>
			<text @click="submitAnswer">提交</text>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: 1,
				steps: [{
					title: '填报'
				}, {
					title: '测验'
				}, {
					title: '完成'
				}],
				stepActive: 1,
				studyInfo: {},
				rate: 0,
				askList: [],
				correctNum: {},
				answerList: [],
				result: {},
				resultArr: [
					[],
					[],
					[],
					[],
					[],
					[],
					[],
					[],
					[]
				],
				formData: {}

			}
		},
		onLoad(options) {
			let that = this
			this.type = options.type
			
			this.getFormdata()
			this.getStudy()
			
		},
		methods: {
			getFormdata(){
				let res = uni.getStorageSync('formdata')
				this.formData = res
			},
			async getStudy() {
				let that = this
				this.util.request({
					api: '/api/mobile/visit/get-ask',
					data: {
						type: that.type
					},
					utilSuccess: function(res) {
						that.studyInfo = res
						that.askList = res.asks
						that.rate = parseInt(res.rate)
					},
					utilFail: function(res) {}
				})
			},
			// 获取选择的答案
			radioAnswer(item, index) {
				this.correctNum[index] = item.detail.data.result
			},
			chenckAnswer(e, item, index) {
				let answer = item.answer
				let correctLength = 0
				let data = e.detail.data
				for (var k of answer) {
					if (k.result == 1) {
						correctLength++
					}
				}
				//  多选少于正确答案数量 返回0 错误
				if (data.length < correctLength) {
					console.log(correctLength)
					this.correctNum[index] = 0
					return
				} else {
					for (var m of data) {
						if (m.result == 0) {
							this.correctNum[index] = 0
						} else {
							this.correctNum[index] = 1
						}
					}
				}
			},
			// 匹配答案正确率
			submitAnswer() {
				let correctLength = 0
				for (var key in this.correctNum) {
					if (this.correctNum[key] == 1) {
						correctLength++
					}
				}
				let per = parseFloat(correctLength / this.askList.length) * 100
				if (per < this.rate) {
					this.util.alert("正确率不达标")
				} else {
					this.submitForm()
				}

			},
			submitForm() {
				let that = this
				this.util.request({
					api: '/api/mobile/visit/visit-save',
					method: "POST",
					data: that.formData,
					utilSuccess: function(res) {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none'
						})
						uni.removeStorageSync('formdata')
						uni.navigateTo({
							url:'/pages/visit/successform'
						})
					},
					utilFail: function(res) {
						uni.showToast({
							title: res.errmsg,
							duration: 2000,
							icon: 'none'
						})
					}
				})
			}

		}
	}
</script>

<style scoped>
	.containers {
		background-color: #fff;
		padding: 20rpx;
		padding-bottom: 60rpx;
		position: relative;
		overflow: hidden;
	}

	.asks {
		min-height: 80vh;
		overflow-y: auto;
	}

	.asks>view,
	.asks>view>view {
		margin-bottom: 20rpx
	}

	.containers>.submitBtn {
		position: fixed;
		bottom: 0;
		width: 100%;
	}

	.containers>.submitBtn text {
		display: inline-block;
		padding: 0 10px;
		text-align: center;
		width: 70%;
		background-color: #f8f8f8;
		font-size: 36rpx;
		border-radius: 10rpx;
		padding: 28rpx;
	}

	.containers>.submitBtn text:first-child {
		border-radius: 10rpx 0 0 10rpx;
	}

	.containers>.submitBtn text:last-child {
		width: 30%;
		background-color: #044ed7;
		color: #fff;
		display: inline-block;
		border-radius: 0 10rpx 10rpx 0;
	}

	.steps {
		margin-bottom: 40rpx
	}
</style>
