new Vue({
	el: "#app",
	data: {
		questions: [],
		questioncode: "03deb476eb634436b4d72b9965539065",
		answers: [],
		count: 0,
		toUserId: "",
		messageId:"",
	},
	mounted: function() {
		var vm = this
		var query = GetRequest();
		vm.messageId = query.messageId;
		var oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
		vm.toUserId = oauthInfo.id
		vm.getQuestions()
	},
	methods: {
		getQuestions: function() {
			var vm = this
			jiuzhenAPI.getSatisfactionQuestionws({
				surveyTemplateCode: vm.questioncode
			}).then(function(res) {
				vm.questions = res.obj.questions
			})
		},
		checkRadio: function(q_sort, a_sort) {
			var vm = this
			q_sort = q_sort - 1
			var answer = $.extend(true, {}, vm.questions[q_sort])
			a_sort = a_sort - 1
			vm.pushAnswers(answer, q_sort, a_sort)
		},
		pushAnswers: function(answer, q_sort, a_sort) {
			var vm = this
			var options = answer.options[a_sort]
			answer.options = options
			//未答题
			if(answer.isAnswer == undefined) {
				vm.questions[q_sort].isAnswer = true
				vm.questions[q_sort].aid = vm.count
				vm.answers.push(answer)
				vm.count++
			} else {
				//已答题
				var count = vm.questions[q_sort].aid
				vm.answers[count] = answer
			}
		},
		sumbit: function() {
			var vm = this
			var all = vm.questions.length
			if(vm.answers.length < all) {
				var answer = vm.answers.concat()
				var answer1 = _.sortBy(answer, function(item) {
					return item.sort;
				})
				for(var i = 0; i < all; i++) {
					var j = i + 1
					if(answer1[i].sort != j) {
						toastr.warning("第" + j + "题未作答！")
						break;
					}
				}
			} else {
				var jsonData = {
					"userId": vm.toUserId,
					"surveyCode": vm.questioncode,
					"questions": vm.answers,
					"messageId": vm.messageId,
				}
				jiuzhenAPI.postSatisfactionAnswers({
					jsonData: JSON.stringify(jsonData)
				}).then(function(res) {
					toastr.success("提交答案成功。谢谢配合！")
				    setTimeout(function(){vm.close()},3000)
				}).catch(function(err) {
					console.log(err)
				})
			}
		},
		close: function() {
			var index = top.layer.getFrameIndex(window.name);
			top.layer.close(index);
		}
	}
});