(function(){
     angular.module("app.login")
        .controller("forgetCtrl",["$scope","UtilService","$http","$interval","$location",forgetCtrl]);

    function forgetCtrl($scope,UtilService,$http,$interval,$location){
    	//loading加载
        $scope.showLoading = function(){
            document.getElementById("sysLoad").style.display = "block";
        }
        $scope.hideLoading = function(){
            document.getElementById("sysLoad").style.display = "none";
        }
        $scope.hasReged = true;
        $scope.forgettype = 'forget';
        $scope.newNoRegister = false;
    	$scope.save = function(){
            var par = {
                email: $scope.email
            };
            var verPar = {
                email: {
                    key: 'email',
                    max:48,
                    val: par.email,
                    txt:"邮箱最长48位"
                }
            }
            var reg = /^([a-zA-Z0-9]+[_|\_|\.|\-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.|\-]?)*[a-zA-Z0-9]+\.[a-zA-Z]{1,63}$/;
            $scope.tip = formJudge(verPar);
            if($scope.email == undefined || $scope.email == ""){
                if ($scope.tip.succ == true) {
                    $scope.tip.succ = false;
                }
                $scope.tip.email = {
                    status:true,
                    txt:"必填"
                }
            }
            if($scope.email != undefined && $scope.email != "" && !reg.test($scope.email)){
                if ($scope.tip.succ == true) {
                    $scope.tip.succ = false;
                }
                $scope.tip.email = {
                    status:true,
                    txt:"请输入正确的邮箱格式"
                }
            }
            if($scope.email == 'admin@reyun.com' || $scope.email == 'demo@reyun.com'){
                if ($scope.tip.succ == true) {
                    $scope.tip.succ = false;
                }
                $scope.tip.email = {
                    status:true,
                    txt:"该账号密码不可修改"
                }
            }
            if ($scope.tip.succ != true) { 
                return false;
            }
            $scope.errorMessage = "";
            $scope.showLoading();
            $http.get("/api/reged/sendforgetpwdemail?email="+$scope.email).success(function(data){
                $scope.hideLoading();
                if(data.content.message == "账号不存在"){
                    $scope.tip = {
                        email:{
                            status:true,
                            txt:"邮箱未注册过账号"
                        }
                    };
                }else if(data.content.message == "success"){
                    $scope.forgettype = 'success';
                }else if(data.content.message == "newuser not actived"){
                    $scope.registerId = data.content.id;
                    $scope.noActive = 1;
                    UtilService.setCookie("regId",data.content.id,7);
                    UtilService.setCookie("regEmail",$scope.email,7);
                    UtilService.setCookie("regFlag",0,7);
                }else if(data.content.message == "new subAccount"){
                    $scope.noActive = 1;
                    $scope.newNoRegister = true;
                }else{
                    $scope.tip = {
                        email:{
                            status:true,
                            txt:"邮件发送失败"
                        }
                    };
                }
            });
        }

        $scope.noActive = 0;
        $scope.closeWin = function(){
            $scope.noActive = 0;
            $scope.loginSend = 'yes';
        }
        $scope.loginSend = 'yes';
        $scope.sendAgain = function(){
            $scope.loginSend = 'no';
            /*var sendPM = HttpService.getInfo("reged/sendeactivemail/"+$scope.registerId+"/"+0);
            sendPM.then(function(data){
                window.location.href = "activation.html";   
            });*/
            $http.get("/api/reged/sendeactivemail/"+$scope.registerId+"/"+0).success(function(data){
                window.location.href = "activation.html"; 
            });
        }

        $scope.loginEmail = function(){
            var mail = $scope.email.split("@");
            window.open("http://mail."+mail[1]);
        }
        $scope.keyEvt= function(e){
            if(e.keyCode == 13){
                $scope.save();
            }
        }    
          
    }
})();