angular.module("app").controller('EnrolInfoCtrl', function($rootScope, $scope, $stateParams, $timeout,$sce) {
	$scope.pay_flag = $stateParams.pay_flag;
	$scope.enrol_id = $stateParams.enrol_id;
	// 通用ajax请求
	if ($scope.pay_flag == undefined || $scope.enrol_id == undefined) {
		$scope.goUrl('main');
		return;
	}
	$scope.check_flag = false;

	$scope.cancelEnrol = function() {
		$rootScope.confirm('确定要取消该专业的报考吗？', function() {
			$rootScope.ajaxRequest('../enrol/std/enrol/cancel.htm', {
				enrol_id : $scope.enrol.enrol_id
			}, function() {
				$rootScope.msg('操作完成！', undefined, function() {
					$scope.goUrl('main');
					return;
				})
			});
		})
	}
	
	
	$scope.msg = function(msg, title, callback) {
		var msgTitle = '提示消息';
		if (title !== undefined) {
			var type = typeof (title);
			if (type == 'string' && title.length > 0)
				msgTitle = title;
			else if (type == 'function') {
				callback = title;
			}
		}
		if (callback == undefined)
			callback = function() {
			}
		var d = dialog({
			title : msgTitle,
			width : '20em',
			content : msg,
			cancel : false,
			ok : callback,
			cancelDisplay : false,
			okValue : '确 定'
		});
		d.showModal();
	}

	$scope.goPay = function() {
		$rootScope.ajaxRequest('../enrol/std/enrol/channel/list.htm', {}, function(array) {
			if (array == undefined || array.length == 0) {
				$rootScope.msg('此考点未开放网络缴费，请联系考点进行缴费确认！');
				return;
			}
			if (array.length > 1) {
				// 多个缴费渠道，显示列表供选择
				return;
			}
			debugger;
			$scope.payChannel = array[0];
			$scope.channelPay();

		}, 'array');
	}
	
	// $scope.PayChannelDialog;
	$scope.channelPay = function() {
		debugger;
		// 先判断是否已经缴费
		$rootScope.ajaxRequest('../enrol/std/enrol/check.htm', {
			enrol_id : $scope.enrol.enrol_id
		}, function(entity) {
			debugger;
//			//如果是交通职院  12752
//			if(entity.school_id == 12752){
//				if(entity.verify_status =='未审核'){
//					$scope.msg('照片材料审核中，请耐心等待！');
//					$rootScope.goUrl('main');
//					return;
//				}else if(entity.verify_status =='审核未通过'){
//					$scope.msg('照片材料审核未通过中，请重新上传照片！');
//					$rootScope.goUrl('main');
//					return;
//				}
//			}
			if (entity.pay_status == 'Active') {
				$rootScope.GobalInitMap.StdEnrol.pay_status = 'Active';
				$rootScope.GobalInitMap.StdEnrol.pay_time = entity.pay_time;
				$rootScope.GobalInitMap.StdEnrol.bill_id = entity.bill_id;
				$rootScope.msg('已经对该报考进行了缴费确认，无需再进行缴费操作！', '缴费提示', function() {
					$rootScope.goUrl('main');
				});
			} else {
				//如果是襄阳的 直接返回
				/*if ($scope.enrol.school_id == 8310 || $scope.enrol.school_id == 12354) {
					$timeout($scope.checkXiangYang(), 5000);
					return;
				}*/
				if ($scope.payChannel.channel_id == 'WeiXin' || $scope.payChannel.channel_id == 'AnxinPay') {
					$scope.PayChannelDialog = dialog({
						title : '请用微信进行扫码支付',
						content : '<img class="ml-5" alt="无法生成微信扫码信息，请稍候重试！" src="' + $scope.payChannel.small_image + '?enrol_id=' + $scope.enrol.enrol_id + '&std_id=' + $rootScope.GobalStdReg.std_id + '&date='+ new Date()+'">',
						cancelDisplay : true,
						width : '300px',
						cancelValue : '取消操作',
						cancel : function() {
							$scope.check_flag = false;
						},
						quickClose : false
					});
					$scope.PayChannelDialog.showModal();
					// $("#imgBankPay").attr('src',
					// $scope.payChannel.small_image +
					// "?enrol_id=" + $scope.enrol.enrol_id);
				} else {
					$scope.PayChannelDialog = dialog({
						title : '请在新页面进行支付操作',
						content : '系统打开了新的页面导航到银行支付页面，请在新页面完成支付操作，如未能打开新页面，请检查浏览器拦截设置。<br/>在完成支付前，请勿刷新本页面',
						cancelDisplay : true,
						width : '300px',
						cancelValue : '取消操作',
						cancel : function() {
							$scope.check_flag = false;
						},
						quickClose : false
					});
					$scope.PayChannelDialog.showModal();
					window.open('../enrol/std/enrol/pay.htm?enrol_id=' + $scope.enrol.enrol_id + '&channel_id=' + $scope.payChannel.channel_id + '&std_id=' + $rootScope.GobalStdReg.std_id);
				}
				
				$scope.check_flag = true;
				$timeout($scope.checkPay(), 3000);

			}
		}, 'entity');

	}

	$scope.checkPay = function() {
		$rootScope.ajaxRequest('../enrol/std/enrol/check.htm', {
			enrol_id : $scope.enrol.enrol_id
		}, function(entity) {
			if (entity.pay_status == 'Active') {
				$scope.PayChannelDialog.close().remove();
				$rootScope.msg('支付成功，你已完成此报考专业的信息确认！', '缴费提示', function() {
					$rootScope.goUrl('main');
				});
			} else {
				if ($scope.check_flag)
					$timeout($scope.checkPay, 3000);
			}
		}, 'entity');
	}

	//检查襄阳是否缴费
	$scope.checkXiangYang = function() {
		$rootScope.ajaxRequest('../enrol/std/enrol/xiangyang/check.htm', {
			enrol_id : $scope.enrol.enrol_id
		}, function(entity) {
			debugger;
			if (entity == true) {
				$rootScope.msg('支付成功，你已完成此报考专业的信息确认！', '缴费提示', function() {
					$rootScope.goUrl('main');
				});
			} else {
				if ($scope.check_flag)
					$timeout($scope.checkXiangYang, 3000);
			}
		}, 'entity');
	}
	
	//推送襄阳第三方缴费数据-考生数据
	$scope.reqXiangYangData = function() {
		var exam_type_t = "";
		if (window.location.href.indexOf("/sk/")) {
			exam_type_t = "53";//技能高考
		} else if (window.location.href.indexOf("/skill/")) {
			exam_type_t = "52";//单招
		}
		$rootScope.ajaxRequest('../enrol/std/enrol/xiangyang/initpay.htm', {
			enrol_id : $scope.enrol.enrol_id,
			exam_type : exam_type_t
		}, function(entity) {
			debugger;
			if (entity.length == 0) {
				$scope.check_flag = true;
				//调用刷新
				$timeout($scope.checkXiangYang(), 8000);//第一次8秒再检查
			} else {
				$rootScope.msg(entity.errorMsg);
			}
		}, 'entity');
	}
	
	$rootScope.ajaxRequest('../enrol/std/enrol/find.htm', {
		enrol_id : $scope.enrol_id
	}, function(enrol) {
		if (enrol.map == undefined) {
			$scope.goUrl('main');
			return;
		}

		enrol.map.aspect_notice = $sce.trustAsHtml( enrol.map.aspect_notice );
		$scope.enrol = enrol.map;
		if ($scope.pay_flag == 'true') {
			//襄阳的除外
			/*if ($scope.enrol.school_id == 8310 || $scope.enrol.school_id == 12354) {
				//襄阳打开这个界面 考生自己关注学校公众号 缴费。这里是刷新定时检测是否缴费了
				//调用襄阳第三方信息
				$scope.reqXiangYangData();
			} else {*/
				$scope.goPay();
			//}
		}
	});
});