app.controller('LoginCtrl', [ '$rootScope', '$scope', '$http', '$location', '$q', '$timeout', '$sce', function($rootScope, $scope, $http, $location, $q, $timeout, $sce) {

	$scope.currStep = 'LoginName';
	$scope.clientMac = '';
	$scope.currentNum = 0;

//	$scope.launchFullscreen = function(element) {
//		if (document.documentElement.requestFullscreen) {
//			document.documentElement.requestFullscreen();
//		} else if (document.documentElement.mozRequestFullScreen) {
//			document.documentElement.mozRequestFullScreen();
//		} else if (document.documentElement.webkitRequestFullscreen) {
//			document.documentElement.webkitRequestFullscreen();
//		} else if (document.documentElement.msRequestFullscreen) {
//			document.documentElement.msRequestFullscreen();
//		}
//	}

	$scope.isWillCheckPhoneCode = false;
	$scope.checkCodeForm = {
		phone_code : ""
	}
	$scope.sendPhoneCodeSuccess = "";
	$scope.aliPlayer = null;

	$scope.changeLogin = function($event) {
		if ($event.target.value == '' || $event.target.value.length < 3)
			return;
		if ($event.keyCode == 13) {
			$('#login_pass').focus();
		}
	}
	
	$scope.changeLoginName = function($event) {
		if ($event.target.value == '' || $event.target.value.length < 3)
			return;
		if ($event.keyCode == 13) {
			$scope.queryJudge($.trim($event.target.value));
		}
	}

	$scope.showLoginName = function() {
		$scope.group = undefined;
		$scope.room = undefined;
		$scope.judge = undefined;
		$scope.currStep = 'LoginName';
		$timeout(function() {
			$('#login_name').focus();
		}, 100);
	}

	$scope.queryJudge = function(login_name) {
		//
		var login_pass = document.getElementById('login_pass');
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/info/loginname.htm',
			data : {
				login_name : document.getElementById('login_name').value,
				login_pass :   (login_pass== undefined || login_pass == '') ?  '': login_pass.value
			}
		}, function(response) {
			$scope.group = response.map.Room.group;
			$scope.room = response.map.Room;
			$scope.judge = response.map.Judge;
			$scope.disArr = response.map.discipline;

			// 单独评分 手机短信验证
			$scope.isWillCheckPhoneCode = (($scope.ParamMap['ExamJudgeGradeType'] !== undefined && $scope.ParamMap['ExamJudgeGradeType'].param_value == 'Judge') ? true : false);
			if (!$scope.isWillCheckPhoneCode)
				$scope.currStep = 'ConfrimJudge';

		})
	}

	$scope.sendPhoneCode = function() {
		$rootScope.ajaxRequest({
			url : '../exam/judge/sendPhoneCode.htm',
			data : {
				judge_id : $scope.judge.judge_id
			}
		}, function(response) {
			if (response.success) {
				$rootScope.sendPhoneCodeSuccess = true;

				$scope.codeCountDown = 30;
				$scope.countDownFn();
				$scope.checkPhoneCodeSucMsg = "短信验证码发送成功,30秒后未收到可重新发送!";
			} else {
				$scope.checkPhoneCodeErrMsg = response.errorMsg;
			}

		})
	}

	$scope.checkPhoneCode = function() {
		if ($scope.checkCodeForm.phone_code.length == 0) {
			alert('请输入手机验证码!');
			return;
		}
		$rootScope.ajaxRequest({
			url : '../exam/judge/checkPhoneCode.htm',// TODO
			data : {
				phone_code : $scope.checkCodeForm.phone_code
			}
		}, function(response) {
			if (response.success) {
				$scope.isWillCheckPhoneCode = false;
				$scope.currStep = 'ConfrimJudge';
			}
		})
	}

	$scope.countDownFn = function() {
		$timeout(function() {
			console.log($rootScope.codeCountDown);
			$scope.codeCountDown--;
			if ($scope.codeCountDown > 0) {
				$scope.countDownFn();
			}
		}, 1000);
	}
	$scope.showVideo = function(video_file, skips){
		var v = $('#video-player');
		v.html('');
		v.css("background-color","");
		
		//width: 575px;
	// height: 270px;
		
		
		$scope.aliPlayer = new Aliplayer({
		  "id": "video-player",
		  "source": video_file,
		  "width": "575px",
		  "height": "270px",
		  "autoplay": true,
		  "isLive": false,
		  "rePlay": false,
		  "playsinline": true,
		  "preload": true,
		  "controlBarVisibility": "hover",
		  "useH5Prism": true
		}, function (player) {
			player.play();
			player.seek(skips);
			/*
			player.on('autoplay',function(data){
				// 浏览器支持自动播放
				if(!data.paramData){
					// 自动播放
					player.play();
				}else{
					// 提示用户点击播放
				}
				
			})
			*/
			
		  }
		);
	}
	
	$scope.closeVideo = function(){
		$scope.aliPlayer.dispose();
	}

	$scope.changeVideoSource = function(video_file, skips) {
		$scope.aliPlayer.loadByUrl(video_file);
		//自动播放
		$scope.aliPlayer.play();
		$scope.aliPlayer.seek(skips);
	}

	$scope.showGroup = function() {
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/group/list.htm',
			data : {
				exam_date : '2020-02-12'
			}
		}, function(response) {
			$scope.groupArray = response.array;
			$scope.currStep = 'ChooseGroup';
		})
	}

	$scope.chooseGroup = function(group) {
		$scope.group = group;
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/room/list.htm',
			data : {
				exam_date : '2020-02-12',
				ly_group_id : group.ly_group_id
			}
		}, function(response) {
			$scope.roomArray = response.array;
			$scope.currStep = 'ChooseRoom';
		})
	}

	$scope.chooseRoom = function(room) {
		$scope.room = room;
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/judge/list.htm',
			data : {
				ly_room_id : room.ly_room_id
			}
		}, function(response) {
			$scope.judgeArray = response.array;
			$scope.currStep = 'ChooseJudge';
		})
	}

	$scope.chooseJudge = function(judge) {
		$scope.judge = judge;
		$scope.currStep = 'ConfrimJudge';
	}

	$scope.judgeLogin = function() {
		// 生成mac
		var padMac = localStorage.getItem('padMac');
		if(!padMac) {
			padMac = $scope.judge.room_id + '_' + $scope.judge.judge_seq + "_" + $scope.judge.room_id + "_" + $scope.clientMac;
		}
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/login.htm',
			data : {
				ly_room_id : $scope.room.ly_room_id,
				judge_id : $scope.judge.judge_id,
				login_name : $scope.judge.login_name,
//				client_mac : ($scope.judge.room_id + '_' + $scope.judge.judge_seq + "_" + $scope.judge.room_id + "_" + $scope.clientMac)
				client_mac : padMac
			}
		}, function(response) {
			$scope.ExamParamMap = response.map;
			$scope.refreshTime = $rootScope.longMinuteString(new Date());
			var key = "";
			angular.forEach(response.map.Judge.des_type, function(k) {
				if (k == '1')
					key += response.map.Judge.judge_id;
				else if (k == '2')
					key += response.map.Judge.judge_seq;
				else if (k == '3')
					key += response.map.Judge.login_name;
				else if (k == '4')
					key += $scope.room.ly_room_id;
				else if (k == '5')
					key += $scope.room.room_key;
				else if (k == '6')
					key += $scope.room.ly_group_id;
				else if (k == '7')
					key += $scope.room.ly_agent_id;
			});
			// 444111
			if (key.length < 16) {
				var len = 16 - key.length;
				for (var i = 0; i < len; i++)
					key = "0" + key;
			}
			key = key.substring(0, 16);
			response.map.Judge.des_key = key;
			response.map.Judge.real_exam_seq = 0;
			$scope.judge = response.map.Judge;
			$scope.StdArray = [];
			$scope.nextExamSeq();
			$scope.currStep = 'Examing';
			$scope.scoreStdTotal = 0;
		})
	}

	/***************************************************************************
	 * 评分相关函数
	 */

	$scope.getParamValue = function(param_name, default_value) {
		if ($scope.ParamMap == undefined)
			return undefined;
		if ($scope.ParamMap[param_name] == undefined)
			return default_value;
		return $scope.ParamMap[param_name].param_value;
	}

	/***************************************************************************
	 * 查找下一个考生
	 */
	$scope.nextExamSeq = function() {
		if ($scope.judge == undefined)
			return;
		if ($scope.group.judge_cache_num <= $scope.StdArray.length) {
			// 超出等待限制
			$timeout($scope.nextExamSeq, parseInt($scope.getParamValue('NextExamSeqTicketSeconds', '10000'), 10));
			return;
		}
		$scope.last_next_time = new Date();
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/std/next.htm',
			data : $scope.judge,
			error : function() {
				// 网络错误等情况,要考虑延迟的问题
				$timeout($scope.nextExamSeq, parseInt($scope.getParamValue('NextExamSeqTicketSeconds', '5000'), 10));
			}
		}, function(data) {
			$scope.lastRequestTime = data.map.QueryNextTime;
			$scope.jsonValue = angular.toJson(data);
			var std = data.map.ExamStd;
			//debugger;			
			if (data.map.ExVideoList && data.map.ExVideoList.length > 0){
				//std['material_file'] = data.map.ExVideo['video_file'];
				std.ExVideoList = data.map.ExVideoList;
			}
			if (data.map.RealExamSeq) {
				$scope.judge.real_exam_seq = data.map.RealExamSeq;
			}
			if (std && std.std_id) {
				// 是否是新考生
				var flag = true;
				for (var k = 0; k < $scope.StdArray.length; k++) {
					$scope.time_type = $scope.StdArray[k].time_type;
					if ($scope.StdArray[k].std_id - std.std_id == 0) {
						flag = false;
						return;
					}
				}
				if (flag) {
					std.SubjectArray = data.map.SubjectArray;
					var avoid = false;
					angular.forEach(std.SubjectArray, function(sb) {
						$scope.time_type = sb.time_type;
						if (sb.judge_avoid == 'Active') {
							avoid = true;
						} else {
							if (sb.judge_record_score != undefined && sb.judge_record_score != sb.judge_score) {
								sb.judge_score = sb.judge_record_score;
							}
						}
					});
					// 该考生被回避
					if (avoid) {
						angular.forEach(std.SubjectArray, function(sb) {
							sb.judge_avoid = 'Active';
							sb.judge_score = undefined;
							sb.judge_record_score = undefined;
						});
						std.avoid_flag = 'Active';
					} else {
						std.avoid_flag = undefined;

					}
					std.tracksArray = data.map.TracksArry;
					$scope.StdArray.push(std);
					// 如果当前考生不为空,判断是否焦点要自动下移
					if ($scope.currentStd != undefined) {
						// 判断是否给过分
						var score_flag = true;
						angular.forEach($scope.currentStd.SubjectArray, function(sb) {
							if (sb.judge_score == undefined && sb.judge_avoid != 'Active') {
								score_flag = false;
							}
						});
						if (score_flag) {
							// 所有科目已经给过分,光标下移
							//$scope.chooseStd(std);
						}
					} else {
						$scope.chooseStd(std);
					}
					$scope.checkGradeTotol();
				}
				$scope.judge.real_exam_seq = std.real_exam_seq;
				// 有新考生,1秒轮询
				$timeout($scope.nextExamSeq, 500);
				//计算总分
				$scope.getTotalScore(std);
			} else {
				// 没有新考生,按照规定的时间去轮询
				$timeout($scope.nextExamSeq, parseInt($scope.getParamValue('NextExamSeqTicketSeconds', '5000'), 10));

			}

		});
	}

	//多科目 多个视频--切换视频
	$scope.changeSubjVideo = function(ex_video, index) {
		$scope.currentNum = index;
		//debugger;
		var v = document.getElementById('video-player');
		if (v) {
			let skips =  parseInt(ex_video["skip_prepare_secs"]);
			$scope.changeVideoSource(ex_video.video_file,skips);
		}
	}
	
	//计算总分
	$scope.getTotalScore = function(std) {
		if(std && std.SubjectArray.length > 1) {
			std.totalScore = 0;
			for(const subject of std.SubjectArray) {
				std.totalScore += parseInt(subject.judge_score == undefined ? 0 : subject.judge_score);
			}
		}
	}
	
	
	/***************************************************************************
	 * 选择某个考生
	 */
	$scope.chooseStd = function(std) {
		var avoid = false;
		// $scope.disciplineFlag = 'InActive';
		angular.forEach(std.SubjectArray, function(sb) {
			if (sb.judge_avoid == 'Active') {
				avoid = true;
			} else {
				// if (sb.judge_record_score != undefined &&
				// sb.judge_record_score !=
				// sb.judge_score) {
				// sb.judge_score = sb.judge_record_score;
				// }
			}
		});
		// 该考生被回避
		if (avoid) {
			angular.forEach(std.SubjectArray, function(sb) {
				sb.judge_avoid = 'Active';
				sb.judge_score = undefined;
				sb.judge_record_score = undefined;
			});
			std.avoid_flag = 'Active';
		} else {
			std.avoid_flag = undefined;

		}

		$scope.currentStd = std;
		// 首次自动加载初始化视频
		if ($scope.currentStd && $scope.getParamValue('JudgeVideoScore', 'InActive') == 'Active'
			&& $scope.currentStd.separate_score == 'Active') {
			setTimeout(function() {
				var v = document.getElementById('video-player');
				//debugger;
				if (v && std.ExVideoList.length >=1 ) {
					let skips =  parseInt(std.ExVideoList[0]["skip_prepare_secs"]);
					$scope.showVideo(std.ExVideoList[0].video_file, skips);
					$scope.first_load = false;
				}

			}, 2000);
		}
		if (std.SubjectArray.length == 1) {
			$scope.chooseSubject(std.SubjectArray[0]);

		} else {
			$scope.scoreStep = 'subjectPanel';
		}
	}

	/***************************************************************************
	 * 点击列表选择某个考生
	 */
	$scope.clickChooseStd = function(std) {
		var avoid = false;
		$scope.showScore = false;
		$scope.currentScore = undefined;
		
		angular.forEach(std.SubjectArray, function(sb) {
			if (sb.judge_avoid == 'Active') {
				avoid = true;
			} else {
				// if (sb.judge_record_score != undefined &&
				// sb.judge_record_score !=
				// sb.judge_score) {
				// sb.judge_score = sb.judge_record_score;
				// }
			}
		});
		// 该考生被回避
		if (avoid) {
			angular.forEach(std.SubjectArray, function(sb) {
				sb.judge_avoid = 'Active';
				sb.judge_score = undefined;
				sb.judge_record_score = undefined;
			});
			std.avoid_flag = 'Active';
		} else {
			std.avoid_flag = undefined;

		}

		$scope.currentStd = std;
		//开启评委视频评分
		if($scope.getParamValue('JudgeVideoScore', 'InActive') == 'Active' && $scope.currentStd.separate_score == 'Active') {
			if (std.ExVideoList && std.ExVideoList.length > 0) {
				let skips = parseInt(std.ExVideoList[0]["skip_prepare_secs"]);
				$scope.changeVideoSource(std.ExVideoList[0].video_file, skips);
			}
		}

		if (std.SubjectArray.length == 1) {
			$scope.chooseSubject(std.SubjectArray[0]);

		} else {
			$scope.scoreStep = 'subjectPanel';
		}
		if($scope.getParamValue('NineScore', 'InActive') == 'Active') {
			$scope.nineExit();
		}
	}

	/***************************************************************************
	 * 选择评分科目(多科目情况下)
	 */
	$scope.chooseSubject = function(sb) {
		if ($scope.currentStd.avoid_flag == 'Active')
			return;
		$scope.currentSubject = sb;
		for (var i = 0; i < $scope.ExamParamMap['SubjectScore_' + sb.subject_id].length; i++) {
			var score = $scope.ExamParamMap['SubjectScore_' + sb.subject_id][i];
			if (score.master_flag == 'Active') {
				$scope.chooseScore(score)
				break;
			}
		}
		//开启分评
		if($scope.getParamValue('JudgeVideoScore', 'InActive') == 'Active' && $scope.currentStd.separate_score == 'Active' 
			&& $scope.currentStd.SubjectArray.length > 1 ) {
			$scope.aliPlayer.loadByUrl(sb.video_url);
		}
		
		//currentSubject.disciplineFlag    判断是否开打违规
		if(sb.discipline_id){
			$scope.currentSubject.disciplineFlag = 'Active'
		}
		
		$scope.scoreStep = 'scorePanel';
		if($scope.getParamValue('NineScore', 'InActive') == 'Active') {
			$timeout(function() {
				$(".numkeyboard").numkeyboard();
				$(".numkeyboard").focus();
			}, 500);
		}
	}

	/***************************************************************************
	 * 选择评分区间
	 */
	$scope.chooseScore = function(score) {
		$scope.showScore = true;
		var array = new Array();
		for (var value = score.score_max; value >= score.score_min; value = value - (score.score_tick)) {

			var score_value = value;
			if (value < score.score_min)
				score_value = score.score_min;
			array.push({
				score_value : score_value
			});
		}
		$scope.currentScore = score;
		$scope.scoreArray = array;

	}

	/***************************************************************************
	 * 对某个科目进行评分(未提交)
	 */
	$scope.gradeScore = function(score_value, discipline_id) {
		//取消违规
		var obj = angular.extend({}, $scope.judge, {
			judge_score : score_value,
			std_subject_id : $scope.currentSubject.std_subject_id,
			discipline_notice:  $scope.currentSubject.discipline_notice,
			discipline_id: discipline_id
		});
		//多科目计算总分
		if($scope.currentStd.SubjectArray.length > 1) {
			$scope.currentStd.totalScore = 0;
			for(const subject of $scope.currentStd.SubjectArray) {
				if( $scope.currentSubject.subject_id == subject.subject_id) {
					$scope.currentStd.totalScore += parseInt(score_value);
				} else {
					$scope.currentStd.totalScore += parseInt(subject.judge_score === undefined ? 0 : subject.judge_score);
				}
				
			}
		}
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/std/record.htm',
			data : obj
		}, function(response) {
			$scope.currentSubject.judge_score = score_value;
			$scope.checkGradeTotol();
			
			if(score_value == 20 && $scope.currentSubject.discipline_id){
				return ; //如果是单独选择的违规,那么不跳走,方便后续打分
			}
			
			if ($scope.currentStd.SubjectArray.length > 1) {
				$scope.scoreStep = 'subjectPanel';
			}
		})
	}
	
	//取消违规
	$scope.clearDiscipline = function(){
		
		$scope.currentSubject.discipline_id=undefined;
		var obj = angular.extend({}, $scope.judge, {
			judge_score : '',
			std_subject_id : $scope.currentSubject.std_subject_id,
			discipline_id: ''
		});
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/std/record.htm',
			data : obj
		}, function(response) {
			$scope.checkGradeTotol();
			$scope.stdDiscipline();
		})
	}

	/***************************************************************************
	 * 回避或者取消回避
	 */
	$scope.avoidStd = function() {
		var obj = angular.extend({}, $scope.judge, {
			judge_avoid : $scope.currentStd.avoid_flag == 'Active' ? 'InActive' : 'Active',
			std_subject_id : $scope.currentStd.SubjectArray[0].std_subject_id
		});
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/std/avoid.htm',
			data : obj
		}, function(response) {
			$scope.currentStd.avoid_flag = ($scope.currentStd.avoid_flag == 'Active' ? undefined : 'Active');
			angular.forEach($scope.currentStd.SubjectArray, function(sb) {
				sb.judge_score = undefined;
				sb.judge_record_score = undefined;
				sb.judge_avoid = $scope.currentStd.avoid_flag;
			});
			$scope.checkGradeTotol();

		})
	}

	/***************************************************************************
	 * 判断已经评分的数量
	 */
	$scope.checkGradeTotol = function() {
		var total = 0;
		angular.forEach($scope.StdArray, function(std) {
			var flag = true;
			angular.forEach(std.SubjectArray, function(sb) {
				if (sb.judge_avoid != 'Active' && sb.judge_score == undefined)
					flag = false;
			});
			if (flag) {
				std.score_flag = true;
				total++;
			} else {
				std.score_flag = false;
			}
		});
		$scope.scoreStdTotal = total;
	}

	/***************************************************************************
	 * 提交前确认
	 */
	$scope.confirmScore = function() {
		var flag = new Array();
		for (var i = 0; i < $scope.StdArray.length; i++) {
			flag.push($scope.StdArray[i].score_flag);
		}
		var truePosition = flag.lastIndexOf(true);
		var falsePosition = flag.indexOf(false);
		if (truePosition > falsePosition && falsePosition != -1) {
			alert('前面的考生未打分,请打分后在提交分数!');
			$scope.clickChooseStd($scope.StdArray[falsePosition]);
			return;
		}
		var array = new Array();
		for (var i = 0; i < $scope.StdArray.length; i++) {
			if ($scope.StdArray[i].score_flag == true) {
				var confirm_score_value = "";
				for (var k = 0; k < $scope.StdArray[i].SubjectArray.length; k++) {
					var subject = $scope.StdArray[i].SubjectArray[k];
					confirm_score_value = confirm_score_value + (k > 0 ? "、" : "") + subject.subject_name + "【" + (subject.judge_avoid == 'Active' ? "回避" : (subject.discipline_id?'违规'+subject.judge_score:subject.judge_score)) + "】";
				}
				$scope.StdArray[i].confirm_score_value = confirm_score_value;
				array.push($scope.StdArray[i]);
			} else {
				break;
			}
		}
		if (array.length == 0)
			return;
		$scope.confirmStdArray = array;
		if ($scope.getParamValue('ShowJudgeSign', 'InActive') == 'Active') {
			$scope.setStdNumber(array);
		}
		$scope.currStep = 'ConfirmScore';
	}

	/***************************************************************************
	 * 查看分数
	 */
	$scope.lookScore = function() {
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/judge/lookScore.htm',
			data : {
				room_id : $scope.room.ly_room_id,
				judge_id : $scope.judge.judge_id
			}
		}, function(response) {
			$scope.stdScoreArray = response.array;
		});
		$scope.currStep = 'LookScore';
	}

	// 选中上一个考生
	$scope.chooseLastStd = function() {
		$scope.showScore = false;
		$scope.currentScore = undefined;
		$scope.disciplineFlag = 'InActive';
		// 当前选中考生
		var curStd = $scope.currentStd;
		if (!curStd)
			return;
		for (var k = 0; k < $scope.StdArray.length; k++) {
			var std = $scope.StdArray[k];
			if (std.std_id === curStd.std_id) {
				if (k > 0) {
					$scope.currentStd = $scope.StdArray[k - 1];
					if($scope.getParamValue('JudgeVideoScore', 'InActive') == 'Active' && $scope.currentStd.separate_score == 'Active') {
						if ($scope.currentStd.ExVideoList && $scope.currentStd.ExVideoList.length > 0) {
							let skips =  parseInt($scope.currentStd.ExVideoList[0]["skip_prepare_secs"]);
							$scope.changeVideoSource($scope.currentStd.ExVideoList[0].video_file, skips);
						}
					}
					$scope.clickChooseStd($scope.currentStd);
					
					//这里滚动
					$("#stuList")[0].scrollTop = $(".stu-info:eq("+(k-1)+")")[0].offsetTop -81
					
					break;
				} else {
					alert('已经是第一个考生!')
				}
			}

		}
		if($scope.getParamValue('NineScore', 'InActive') == 'Active') {
			$scope.nineExit();
		}
	}

	// 选中下一个考生
	$scope.chooseNextStd = function() {
		$scope.showScore = false;
		$scope.currentScore = undefined;
		$scope.disciplineFlag = 'InActive';
		// 当前选中考生
		var curStd = $scope.currentStd;
		if (!curStd)
			return;
		for (var k = 0; k < $scope.StdArray.length; k++) {
			var std = $scope.StdArray[k];
			if (std.std_id === curStd.std_id) {
				if ((k + 1) < $scope.StdArray.length) {
					$scope.currentStd = $scope.StdArray[k + 1];
					if($scope.getParamValue('JudgeVideoScore', 'InActive') == 'Active' && $scope.currentStd.separate_score == 'Active') {
						if ($scope.currentStd.ExVideoList && $scope.currentStd.ExVideoList.length > 0) {
							let skips = parseInt($scope.currentStd.ExVideoList[0]["skip_prepare_secs"]);
							$scope.changeVideoSource($scope.currentStd.ExVideoList[0].video_file, skips);
						}
					}
					$scope.clickChooseStd($scope.currentStd);
					
					$("#stuList")[0].scrollTop = $(".stu-info:eq("+(k+1)+")")[0].offsetTop -81
					
					break;
				} else {
					// 无下一个考生 提示 TODO
					alert('已经是最后一个考生!')
				}
			}

		}
		if($scope.getParamValue('NineScore', 'InActive') == 'Active') {
			$scope.nineExit();
		}
		
	}

	// aes加密
	$scope.aesValue = function(value) {
		var key = CryptoJS.enc.Utf8.parse($scope.judge.des_key);
		var str = CryptoJS.enc.Utf8.parse(value);
		var base64 = CryptoJS.enc.Base64.stringify(str);
		var encrypted = CryptoJS.AES.encrypt(str, key, {
			mode : CryptoJS.mode.ECB,
			padding : CryptoJS.pad.Pkcs7
		});
		return encrypted.toString();
	}

	/***************************************************************************
	 * 提交分数
	 */
	$scope.submitScore = function() {
		// && !$("#signature")[0].contentWindow.flag
		if ($scope.getParamValue('ShowJudgeSign', 'InActive') == 'Active' && $("#signature")[0].contentWindow.document.getElementById('saveBtn').disabled) {
			alert('提交分数前,请先保存签名!');
			return;
		}
		if ($scope.getParamValue('ShowJudgeSign', 'InActive') == 'Active') {
			$("#signature")[0].contentWindow.saveSignature();
		}
		var std = undefined;
		$scope.commitScoreFlag = true;
		for (var i = 0; i < $scope.confirmStdArray.length; i++) {
			if ($scope.confirmStdArray[i].commit_flag != true) {
				std = $scope.confirmStdArray[i];
				break;
			}
		}
		if (std == undefined) {
			$scope.commitScoreFlag = undefined;
			$scope.currentSubject = undefined;
			$scope.currentScore = undefined;
			$scope.currentStd = undefined;
			// 已经完成评分
			$scope.confirmStdArray = [];
			$scope.currStep = 'Examing';
			if ($scope.StdArray.length > 0) {
				$scope.chooseStd($scope.StdArray[0]);
			}
			return;
		}
		debugger;
		if ($scope.requestingStdId == undefined) {
			$scope.requestingStdId = std.std_id;
			$scope.submitStdScore(std);
		}
		// 1秒钟等待

		$timeout($scope.submitScore, 1000);

	}

	/***************************************************************************
	 * 提交分数,单个考生
	 */
	$scope.submitStdScore = function(std) {
		debugger;
		angular.forEach(std.SubjectArray, function(sb) {
			sb.room_id = $scope.room.ly_room_id;
			sb.agent_id = $scope.judge.agent_id;
			sb.judge_id = $scope.judge.judge_id;
			sb.judge_seq = $scope.judge.judge_seq;
			sb.real_exam_seq = std.real_exam_seq;
			if (sb.judge_avoid == 'Active')
				std.judge_avoid = 'Active';
		});
		var data = angular.extend({}, $scope.judge, {
			score_json : $scope.aesValue(angular.toJson(std.SubjectArray)),
			std_id : std.std_id,
			real_exam_seq : std.real_exam_seq,
			judge_avoid : std.judge_avoid,
			remark : std.remark,
			discipline_id: std.discipline_id
		});
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/std/score.htm',
			data : data,
			error : function() {
				// $scope.submitStdScore(std);
				// 出现评分分差,跳转到打分界面
				$scope.confirmStdArray = [];
				$scope.requestingStdId = undefined;
			}
		}, function(response) {
			std.commit_flag = true;
			$scope.scoreStdTotal = $scope.scoreStdTotal - 1;
			// 移除队列
			for (var i = 0; i < $scope.StdArray.length; i++) {
				if ($scope.StdArray[i].std_id == std.std_id) {
					$scope.StdArray.splice(i, 1);
					break;
				}
			}
			$scope.requestingStdId = undefined;
		})
	}

	$scope.cancelConfirm = function() {
		$scope.currStep = 'Examing';
		//是否开启视频评分
		if($scope.getParamValue('JudgeVideoScore', 'InActive') == 'Active' && $scope.currentStd.separate_score == 'Active') {
			setTimeout(function() {
				var v = document.getElementById('video-player');
				if (v) {
					let skips =  parseInt($scope.currentStd.ExVideoList[0]["skip_prepare_secs"]);
					$scope.showVideo($scope.currentStd.ExVideoList[0].video_file, skips);
					$scope.first_load = false;
				}
				
			}, 1000);
		}
	}

	$scope.showJudge = function() {
		$scope.modifyJudge = angular.extend({}, $scope.judge, {});
		// 显示评分版
		$('#judgeModal').modal('show')
	}

	$scope.changeJudge = function() {
		$rootScope.ajaxRequest({
			url : '../exam/judge/grade/judge/modify.htm',
			data : $scope.modifyJudge
		}, function(response) {
			$scope.judge = response.entity;
			// todo 更优雅的提醒方式
			alert('评委信息已修改!');
			$('#judgeModal').modal('hide')
		})
	}

	$scope.exitJudge = function() {
		$scope.judge = undefined;
		$scope.currentSubject = undefined;
		$scope.currentScore = undefined;
		$scope.currentStd = undefined;
		$scope.scoreStdTotal = 0;
		$scope.StdArray = [];
		$scope.confirmStdArray = undefined;
		$scope.requestingStdId == undefined;
		if ($scope.groupArray != undefined) {
			$scope.chooseRoom($scope.room);
		} else {
			$scope.currStep = 'LoginName';
		}

	}

	$scope.getBatchName = function(tracks) {
		var current_batch = 1;
		angular.forEach($scope.currentStd.tracksArray, function(t) {
			if (t.tracks_batch > current_batch)
				current_batch = t.tracks_batch;
		});
		if (tracks.tracks_batch == 1) {
			return '';
		}
		if (tracks.tracks_batch == 2)
			return '复试:'
		return '三试:'
	}

	$scope.getTracksClass = function(tracks) {
		var current_batch = 1;
		angular.forEach($scope.currentStd.tracksArray, function(t) {
			if (t.tracks_batch > current_batch)
				current_batch = t.tracks_batch;
		});
		if (current_batch == tracks.tracks_batch) {
			return 'text-success';
		}
		return '';
	}

	$scope.login = function() {
		var login_name = document.getElementById('login_name').value;
		if (!login_name) {
			alert('请输入评委登录账号!');
			return;
		}
		$scope.queryJudge($.trim(login_name));
		//$scope.launchFullscreen();
	}

	$rootScope.ajaxRequest({
		url : '../exam/judge/grade/param.htm'
	}, function(response) {
		var map = {};
		for (var i = 0; i < response.array.length; i++) {
			map[response.array[i].param_name] = response.array[i];
		}
		$scope.ParamMap = map;

		if ($scope.getParamValue('JudgeLoginType', 'LoginName') == 'ChooseGroup') {
			$scope.showGroup();
		}
		//是否开启生成Pad随机码
		if($scope.getParamValue('PadRandomCode', 'Active') == 'Active') {
			$scope.generateRandom();
		}
	});

	/***************************************************************************
	 * 获取客户端的MAC地址
	 */
	$rootScope.ajaxRequest({
		url : '../exam/judge/grade/judge/getMac.htm'
	}, function(response) {
		if (response) {
			$scope.clientMac = response.entity;
		}
	});

	/***************************************************************************
	 * 获取提交考生的起止序号
	 */
	$scope.setStdNumber = function(array) {
		$scope.numberBetween = "";
		if(array.length >= 1) {
			$scope.numberBetween += array[0].real_exam_echo;
			$scope.numberBetween += "~" + array[array.length-1].real_exam_echo;
		}
	}
	// 清除签名
	$scope.clearCanvas = function() {
		$("#signature")[0].contentWindow.clearCanvas();
	}
	// 保存签名
	$scope.saveSignature = function() {
		if ($("#signature")[0].contentWindow.document.getElementById('saveBtn').disabled) {
			alert('保存签名前,请先签名!');
			return;
		}
		$("#signature")[0].contentWindow.saveSignature();
	}
	
	//标记违纪
	$scope.stdDiscipline = function(sb) {
		if($scope.currentSubject.disciplineFlag == 'Active'){
			$scope.currentSubject.disciplineFlag = 'InActive';
			//$scope.currentSubject.discipline_id = ''
		}else{
			$scope.currentSubject.disciplineFlag = 'Active';
		}
	}
	
	$timeout(function() {
		$('#login_name').focus();
		// $scope.launchFullscreen();
	}, 100);
	
	
	//***九宫格输分***************************************************************
	//保存录入分数
	$scope.saveJudgeScore = function() {
		var score = $('#score_cur').val();
		//录入前判断
		if(score == '00' || score == '000') {
			score = '0';
		}
		if(score.length == 2 && score.substring(0,1) == '0') {
			score = score.substring(1,2);
		}
		if(score.length == 3 && score.substring(0,1) == '0') {
			score = score.substring(1,3);
		}
		$scope.gradeScore(score);
	}
	
	//返回到科目选择界面
	$scope.backPanel = function() {
		$scope.scoreStep = 'subjectPanel';
		$scope.nineExit();
	}
	//九宫格控制
	$scope.nineExit = function() {
		if($(".auth_keybord")) {
			$(".auth_keybord").css({
				"display" : "none"
			});
		}
	}
	
	//***九宫格输分***************************************************************
	
	/**********************************
	 *   生成32随机码
	 */
	$scope.randomString = function(len) {
		len = len || 32;
		var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
		var maxPos = $chars.length;
		var str = '';
		for (i = 0; i < len; i++) {
			str += $chars.charAt(Math.floor(Math.random() * maxPos));
		}
		return str;
	}
	
	/**********************************
	 *   本地存储随机码
	 */
	$scope.generateRandom = function() {
		if(!window.localStorage){
		    alert("浏览器不支持localstorage");
		    return false;
		}else{
			if(!localStorage.getItem('padMac')) {
				localStorage.setItem('padMac', $scope.randomString());
			}
		}
	}
	
	
	/**********************************
	 *   查看考题
	 */
	$scope.lookTopic = function() {
		$rootScope.ajaxRequest({
			url : '../std/video/topic.htm',
			data: {
				std_id : $scope.currentStd.std_id,
				ly_group_id : $scope.currentSubject.ly_group_id
			}
		}, function(response) {
			$scope.topic = response.array;
			$('#topicModal').modal('show');
		});
	}
	
	
	$scope.showPhoto = function() {
		// 显示评分版
		$('#judgeModal2').modal('show');
	}
	
	
} ]);