(function() {
	var page = {
		isScroll: true,
		init: function($page, param) {
            var path = config.webapi;
            learun.layer.loading(true, "加载数据中...");
			learun.httpget(path + "/learun/eval/paper", {
				VID: param.VID
			}, (res) => {
                learun.layer.loading(false);
				$page.find('.inSecTitle').html(res.VTitle).end().find('.inSecTime').html(res.VStartTime + ' - ' + res.VStopTime)
				var dataList = res.Eval_Questions;
				var otherHtml = '<div class="shenInput shenInput3">'+
									'<span>其他</span>'+
									'<div class="shenInputBox shenInputBox1">'+
										'<input data-rule="required;" type="text" name="fieldOther" class="inputTxt" placeholder="请填写备注" />'+
									'</div>'+
								'</div>';
				var html = '';
				$.each(dataList, function(i, n){
					html += '<div class="signSec2Box" qid="'+ n.QID+'" iid="" qmust="'+ n.QMust +'" qmin="'+ n.QMin +'" qmax="'+ n.QMax+'">';
					var flag = false;
					if(n.QType == 1){
						html += '<div class="shenInput">'+
							'<div class="shenInputBox">'+
								'<div class="signTxt">'+ n.QTitle +'</div>'+
								'<div class="inputRadioRow">';
						$.each(n.Eval_QuestionItems, function(j, m){
							m.QShowText ? flag = true : '';
							html += '<div class="inputRadioCol4">'+
										'<i class="inputRadio checkBoxs" flag="'+ flag +'" iid="'+ m.IID +'"></i>'+
										'<span>'+ m.ITitle +'</span>'+
									'</div>';
							
						})
					}else if(n.QType == 2){
						html += '<div class="signTextarea">'+
									'<div class="signTextareaBox">'+
										'<div class="signTxt">'+ n.QTitle +'</div>'+
										'<div class="inputRadioRow">';
						$.each(n.Eval_QuestionItems, function(j, m){
							if(m.QShowText){
								flag = true;
							}
							html += '<div class="inputRadioCol2">'+
										'<i class="inputCheck checkBoxs" flag="'+ flag +'" iid="'+ m.IID +'"></i>'+
										'<span>'+ m.ITitle +'</span>'+
									'</div>';
						})	
					}
					html += '</div></div></div>';
					html += flag? otherHtml + '</div>' : '</div>';
				})
				$('.index_box').html(html)
			});


			$page.find('.shenBtn').on('tap', function() {
				var jsonArr= [];
				var qmustFlag = false;
				var flag = false;
				$('.signTxt').removeClass('redEva');
				$('.signSec2Box').each(function(i, n){
					var _this = $(n);
					var qmust = _this.attr('qmust');
					var qmin = _this.attr('qmin') == 'null'? 0 : _this.attr('qmin');
					var qmax = _this.attr('qmax') == 'null'? 0 : _this.attr('qmax');
					var checkBoxs = _this.find('.checkBoxs.active');
					var ind = checkBoxs.length;
					if(qmust == 'true' && ind == 0){
						qmustFlag = true;
						_this.find('.signTxt').addClass('redEva');
					}
					if((qmin && ind < qmin) || (qmax && ind > qmax)){
						flag = true;
						_this.find('.signTxt').addClass('redEva');
					}
					var UText = _this.find('.shenInput3 .inputTxt').val();
					var qid = $(this).attr("qid");
					checkBoxs.each(function(j, m){
						var txt = $(m).attr('flag') == 'true'? UText : '';
						jsonArr.push({QID: qid, IID: $(m).attr("iid"), UText: txt})
					})
				})
				// console.log(jsonArr)
				if(qmustFlag){
					learun.layer.toast('有必选项未选择');
					return
				}
				if(flag){
					learun.layer.toast('有选择项少选或多选');
					return
				}
				// return
				learun.httpget(path + "/learun/eval/savepaper",
				   {
				       VID: param.VID,
				       LessonNo: param.LessonNo,
				       EmpNo: param.EmpNo,
				       postarray: jsonArr
				   }, (res) => {
                       learun.layer.toast('操作成功');
                       learun.nav.closeCurrent();
                       var prepage = learun.nav.getpage('EvaluationTeach');
                       prepage.grid.reload();
				   });
			})

			//单选框
			$page.on('tap', '.shenInputBox .inputRadio', function() {
				if (!$(this).hasClass('active')) {
					// var iid = $(this).attr('iid');
					$(this).addClass('active').parents('.shenInputBox').find('.inputRadio').not($(this)).removeClass('active');
					judgeRideo($(this))
				}
			})

			//多选框
			$page.on('tap', '.inputCheck', function() {
				var parents = $(this).parents('.signSec2Box');
				var qmin = parents.attr('qmin') == 'null'? 0 : parents.attr('qmin');
				var qmax = parents.attr('qmax') == 'null'? 0 : parents.attr('qmax');
				var ind = parents.find('.inputCheck.active').length;
				if ($(this).hasClass('active')) {
					if(qmin && ind <= qmin){
						learun.layer.toast('该选项最少选择'+ qmin + '项');
						return
					}
					$(this).removeClass('active')
				} else {
					if(qmax && ind >= qmax){
						learun.layer.toast('该选项最多选择'+ qmax + '项');
						return
					}
					$(this).addClass('active');
				}
				// var iids = '';
				// parents.find('.inputCheck.active').each(function(i, n){
				// 	var iid = $(n).attr('iid');
				// 	iids += i== 0? iid : ',' + iid;
				// })
				// parents.attr('iid', iids)
				judgeRideo($(this))
			})
			//是否为其他选项判断
			function judgeRideo(obj) {
				var parents = obj.parents('.signSec2Box');
				var inputs = parents.find('.shenInput3');

				if (parents.find("i[flag='true']").hasClass('active')) {
					inputs.css({
						display: 'block'
					});
				} else {
					inputs.css({
						display: 'none'
					}).find('input').val('');
				}
			}


		},
		destroy: function(pageinfo) {

		}
	};
	return page;
})();
