<template>
	<view class="todo-page">
		<view class="card">
			<uni-card margin="0" :is-shadow="true">
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" ref="wForm"
					errorType="border-bottom">
					<u-form-item label="培训名称：" prop="NAME" borderBottom>
						<u--input v-if="safe" v-model="safe.NAME" disabled disabledColor="#fff" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="发起时间：" prop="NAME" borderBottom>
						<u--input v-if="safe" v-model="safe.LAUNCH_TIME" disabled disabledColor="#fff"
							border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="结束时间：" prop="NAME" borderBottom>
						<u--input v-if="safe" v-model="safe.END_TIME" disabled disabledColor="#fff"
							border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="发起部门：" prop="NAME" borderBottom>
						<u--input v-if="safe" v-model="safe.Nav_LaunchDepartment.NAME" disabled disabledColor="#fff"
							border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="发起人员：" prop="NAME" borderBottom>
						<u--input v-if="safe" v-model="safe.Nav_LaunchUser.NAME" disabled disabledColor="#fff"
							border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="成绩：" prop="score" borderBottom>
						<u--input v-model="score" disabled disabledColor="#fff" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>
		<u-sticky offset-top="20">
			<view class="sub-form">
				<view class="sub-form-wrap">
					<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom"
						ref="sForm">
						<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
							v-for="(item, index) in papers">
							<u-form-item>
								<view class="uni-flex uni-column">
									<view>{{(index+1)+'. '+ item.Nav_Test.NAME+getType(item.Nav_Test.TYPE)}}</view>
									<view>
										<u-radio-group v-if="item.Nav_Test.TYPE==0" v-model="item.ANSWER1"
											placement="column">
											<u-radio :customStyle="{marginBottom: '4px',marginTop: '4px'}"
												v-for="(item1, index1) in radiolist1" :key="index1" :label="item1.name"
												:name="item1.name" @change="doTrueOrFalse(index1,item)"
												:disabled="readonly" />
										</u-radio-group>
										<u-radio-group v-if="item.Nav_Test.TYPE==1" v-model="item.ANSWER2"
											placement="column">
											<u-radio :customStyle="{marginBottom: '4px',marginTop: '4px'}"
												v-for="(item2, index2) in radiolist2[item.ID]" :key="index2"
												:label="item2.name" :name="item2.name"
												@change="doOptionChange(index2,item)" :disabled="readonly" />
										</u-radio-group>
										<u-checkbox-group v-if="item.Nav_Test.TYPE==2" v-model="item.ANSWER3"
											@change="checkboxChange($event,item)" placement="column">
											<u-checkbox :customStyle="{marginBottom: '4px',marginTop: '4px'}"
												v-for="(item3, index3) in radiolist2[item.ID]" :key="index3"
												:label="item3.name" :name="item3.name" :disabled="readonly">
											</u-checkbox>
										</u-checkbox-group>
									</view>
								</view>
							</u-form-item>
						</uni-card>
					</u--form>
				</view>
			</view>
		</u-sticky>
		<view class="bottom-button">
			<button type="primary" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		guid,
		initFilter,
		initFilterGroup
	} from '../../../../utils/common'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	import config from '../../../../config/common'
	export default {
		data() {
			return {
				model: {
					ORG_ID: "",
				},
				papers: [],
				viewAll: false,
				safe: null,
				UserList: [],
				SelUsrID: null,
				config: null,
				score: 0,
				END_TIME: null,
				readonly: true,
				loading: false,
				radiolist1: [{
						name: '正确',
						disabled: false
					},
					{
						name: '错误',
						disabled: false
					},
				],
				radiolist2: {},
				radiovalue1: "错误",
				checkbox: [],
				TaskID: '',
				ORG_ID: uni.getStorageSync('orgId')
			}
		},
		onLoad(option) {
			this.TaskID = option.taskID ? option.taskID : '';
			this.model.ID = option.ID ? option.ID : '';
			this.loadData();
		},
		methods: {
			loadData() {
				let json = initFilter(this.ORG_ID);
				extendRule(json, 'SAFE_ID', 1, this.model.ID);
				extendInclude(json, 'Nav_User');
				getRequest(json, "/SE/SESafeSurvey/GetUsers").then(res => {
					if (res && res.length > 0) {
						this.UserList = res
						if (this.viewAll) {
							this.SelUsrID = res[0].ID;
							this.getPapers();
						} else {
							let usr = res.find(it => it.ID == uni.getStorageSync('appInfo')?.User?.ID);
							if (usr) {
								this.SelUsrID = usr.ID
								this.getPapers();
							} else {
								uni.$showMsgFunc('当前用户没有问卷!', () => {}, 'error', 1000)
							}
						}
					}
				});
			},
			getPapers() {
				let SelUsrID = this.SelUsrID;
				this.papers = [];
				if (!SelUsrID) {
					return;
				}
				const json = initFilter(this.ORG_ID, "", "")
				extendRule(json, 'SAFE_ID', 1, this.model.ID);
				extendRule(json, 'USER_ID', 1, SelUsrID);
				extendInclude(json, 'Nav_Test');
				extendInclude(json, 'Nav_Safe');
				extendInclude(json, 'Nav_Safe.Nav_LaunchDepartment');
				extendInclude(json, 'Nav_Safe.Nav_LaunchUser');
				extendInclude(json, 'Nav_Safe.Nav_Points');
				extendInclude(json, 'Nav_Safe.Nav_Points.Nav_Point');
				extendInclude(json, 'Nav_Safe.Nav_JoinDepartment');
				extendInclude(json, 'Nav_Safe.Nav_JoinDepartment.Nav_Department');
				extendInclude(json, 'Nav_User');
				extendInclude(json, 'Nav_Department');
				getRequest(json, "/SE/SESafeSurvey/GetPapers").then(res => {
					this.model = res
					if (res && res.Nav_Papers && res.Nav_Papers.length > 0) {
						let papers = res.Nav_Papers.sort((a, b) => {
							return a.Nav_Test.TYPE - b.Nav_Test.TYPE
						});
						let config = res.Nav_Config;
						let safe = res.Nav_Papers[0].Nav_Safe;
						let hasAnswered = false;
						for (let i = 0; i < res.Nav_Papers.length; i++) {
							if (res.Nav_Papers[i].ANSWER !== 0) {
								hasAnswered = true;
							}
						}
						this.papers = papers;
						this.papers.forEach(item => {
							let test = {}
							if (item.Nav_Test.TYPE != 0) {
								this.radiolist2[item.ID] = [];
								this.radiolist2[item.ID].push({
									name: item.Nav_Test.OPTION_A
								}, {
									name: item.Nav_Test.OPTION_B
								}, {
									name: item.Nav_Test.OPTION_C
								}, )
								if (item.Nav_Test.OPTION_D != undefined && item.Nav_Test.OPTION_D != "") {
									this.radiolist2[item.ID].push({
										name: item.Nav_Test.OPTION_D
									})
								}
								if (item.Nav_Test.OPTION_E != undefined && item.Nav_Test.OPTION_E != "") {
									this.radiolist2[item.ID].push({
										name: item.Nav_Test.OPTION_E
									})
								}
							}
						})
						this.config = config;
						this.safe = safe;
						this.readonly = hasAnswered;
						this.score = this.papers[0].SCORE;

					} else {
						uni.$showMsgFunc('未能成功获取此用户的在线试卷，请稍后重试!', () => {}, 'error', 1000)
					}
				})
			},
			getType(type) {
				let ret = '';
				let config = this.config;
				switch (type) {
					case 0:
						ret = `【是非题】(${config && config.C_TEST_SCORE || 1}分)`;
						break;
					case 1:
						ret = `【单选题】(${config && config.S_TEST_SCORE || 1}分)`;
						break;
					case 2:
						ret = `【多选题】(${config && config.M_TEST_SCORE || 1}分)`;
						break;
				}
				return ret;
			},
			doTrueOrFalse(index, item) {
				item.ANSWER = (index + 1);
			},
			doOptionChange(index, item) {
				if (index == 0) {
					item.ANSWER = 1;
				} else if (index == 1) {
					item.ANSWER = 2;
				} else if (index == 2) {
					item.ANSWER = 4;
				} else if (index == 3) {
					item.ANSWER = 8;
				} else if (index == 4) {
					item.ANSWER = 16;
				}
			},
			doCheckChange(index, item) {
				if (index == 0) {
					item.ANSWER = 1;
				} else if (index == 1) {
					item.ANSWER = 2;
				} else if (index == 2) {
					item.ANSWER = 4;
				} else if (index == 3) {
					item.ANSWER = 8;
				} else if (index == 4) {
					item.ANSWER = 16;
				}
			},
			checkboxChange(arr, item) {
				let indexArr = this.findCommonElementsIndices(arr, this.radiolist2[item.ID])
				item.ANSWER3 = arr;
				item.ANSWER = 0;
				indexArr.forEach(index => {
					if (index == 0) {
						item.ANSWER += 1;
					} else if (index == 1) {
						item.ANSWER += 2;
					} else if (index == 2) {
						item.ANSWER += 4;
					} else if (index == 3) {
						item.ANSWER += 8;
					} else if (index == 4) {
						item.ANSWER += 16;
					}
				})
			},
			findCommonElementsIndices(arr1, arr2) {
				const commonElements = [];
				for (let i = 0; i < arr1.length; i++) {
					for (let j = 0; j < arr2.length; j++) {
						if (arr1[i] === arr2[j].name) {
							commonElements.push(j);
						}
					}
				}
				return commonElements;
			},
			submit() {
				let data = {
					Nav_Config: JSON.parse(JSON.stringify(this.config)),
					Nav_Papers: JSON.parse(JSON.stringify(this.papers)),
					TaskID: this.TaskID,
					ORG_ID: this.ORG_ID
				}
				for (let i = 0; i < data.Nav_Papers.length; i++) {
					if (data.Nav_Papers[i].ANSWER == 0) {
						uni.$showMsgFunc(`第${i + 1}题尚未选择答题，请完成所有答题后再进行提交`, () => {}, 'error', 2000)
						return;
					}
				}
				getRequest(data, "/SE/SESafeSurvey/SavePapers").then(res => {
					if (res) {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					}
				})
			},
		}
	}
</script>

<style scoped>
	@import url("../../../../style/css/editTemplate.css");

	.todo-page {
		padding: 16px 16px 70px;
	}
</style>