app.controller('AppCtrl', ['$rootScope', '$scope', '$http', '$timeout', '$state', function ($rootScope, $scope, $http, $timeout, $state) {
    $scope.load = function () {
        $scope.myPromise = $http({
            url: '../enrol/gobal/login/init.htm'
        }).success(function (data) {
            $scope.loginActive = data.map.loginActive;
            $scope.openTime = data.map.openTime;
            $scope.closeTime = data.map.closeTime;
            $scope.allowed = data.map.allowed;
            $rootScope.year = data.map.ArtYear;
        });
    }
    $scope.load();

    $scope.logout = function () {
        $http({
            url: '../enrol/pc/reg/logout.htm'
        }).success(function (data) {
        });
        window.location = 'index.html';
    }
}])
    .controller('LoginCtrl', ['$rootScope', '$scope', '$http', '$timeout', '$state', function ($rootScope, $scope, $http, $timeout, $state) {
        $scope.login = {
            exam_id: '',
            std_pass: '',
            valid_code: ''
        }

        $scope.ruser = {
            exam_id: '',
            cert_id: '',
            std_email: '',
            valid_code: ''
        };

        $timeout(function () {
            $scope.changeCode('imgCode');
            $scope.changeCode('imgCode1');
        }, 100);

        // function to submit the form after all validation has occurred
        $scope.submitForm = function (isValid) {
        	var userAgent = navigator.userAgent; // 取得浏览器的userAgent字符串
        	var isOpera = userAgent.indexOf("Opera") > -1;
        	// 判断是否IE浏览器
    		if (userAgent.indexOf("compatible") > -1
    			&& userAgent.indexOf("MSIE") > -1 && !isOpera) {
    			alert("系统不支持IE浏览器,报名请用使用【Google浏览器】、【Microsoft Edge浏览器】");
    			return ;
    		}
        	
            $scope.submitted = true
            // check to make sure the form is completely valid
            if (isValid) {
                $scope.myPromise = $http({
                    url: '../enrol/gobal/reg/loginWy.htm',
                    method: 'POST',
                    data: $scope.login
                }).success(function (data) {
                    //console.log(data);
                    if (data.entity == 'firstLogin') {
                    	$state.go('firstLogin', {std: data.entity});
                    } else {
                    	window.location = "art.html";
                    }
                    //window.location = "art.html";
                }).error(function () {
                    $scope.changeCode('imgCode');
                });
            }

        };

        $scope.changeCode = function (elementId) {
            $('#' + elementId).attr('src', '../frame/verification/random.htm?verify=' + Math.random() + '&img_width=120&font_size=30&margin_left=15&font_interval=20&img_height=40&margin_top=25');
        }

        //------------ 重置密码 ---------------------------
        $scope.goForget = function () {
            $state.go("stop");
        }

        $scope.openReset = function () {
            $('#imgCode2').attr('src', '../frame/verification/random.htm?code=' + Math.random() + '&img_width=120&font_size=30&margin_left=15&font_interval=20&img_height=40&margin_top=25');
            $('#resetmodal').modal({
                backdrop: 'static'
            });
        }

        $scope.reset = function (isValid) {
            $scope.resetFormSubmitted = true;
            console.log($scope.ruser);
            if (isValid) {
                $scope.myPromise = $http({
                    url: '../enrol/gobal/reg/reset.htm',
                    method: 'POST',
                    data: $scope.ruser
                }).success(
                    function (data) {
                        if (data.success == false) {
                            // 刷新验证码
                            $scope.errorMsg = data.errorMsg;
                            $("#imgCode2").attr("src", "../frame/verification/random.htm?" + Math.random() + '&img_width=120&font_size=30&margin_left=15&font_interval=20&img_height=40&margin_top=25');
                            return;
                        } else {
                            alert('新密码已经重置为您的身份证号,如有x请用大写');
                            $('#resetmodal').modal('hide');
                            $state.go("login");
                        }
                    });
            }
        };
        //------------ 重置密码结束 ---------------------------


    }]).controller('FirstLoginCtrl', function ($rootScope, $scope, $http, $timeout, $state, toaster) {

    $scope.load = function () {
        $scope.std = {};
        $scope.myPromise = $http.post('../enrol/gobal/std/init.htm').success(function (data) {
            $scope.std = data.entity;
        })
    }
    $scope.load();
    
    //判断浏览器
	function myBrowser() {
		var userAgent = navigator.userAgent; // 取得浏览器的userAgent字符串
		alert(userAgent);
		var isOpera = userAgent.indexOf("Opera") > -1;
		// 判断是否Opera浏览器
		if (isOpera) {
			return "Opera"
		}
		// 判断是否Firefox浏览器
		if (userAgent.indexOf("Firefox") > -1) {
			return "FF";
		} 
		if (userAgent.indexOf("Chrome") > -1) {
			return "Chrome";
		}
		// 判断是否Safari浏览器
		if (userAgent.indexOf("Safari") > -1) {
			return "Safari";
		} 
		// 判断是否IE浏览器
		if (userAgent.indexOf("compatible") > -1
			&& userAgent.indexOf("MSIE") > -1 && !isOpera) {
			return "IE";
		}
		 
	}

    $scope.submitForm = function (isValid) {
        $scope.submitted = true;
        if (isValid) {
            $scope.myPromise = $http.post('../enrol/gobal/std/confirm.htm', $scope.std).success(function (data) {
                var d = dialog({
                    title: '操作提示',
                    content: "完善个人信息成功!",
                    ok: function () {
                        window.location = "art.html";
                    },
                    okValue: '确定',
                    width: 350
                });
                d.showModal();
            })
        }
    }
})

;