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

    function acvitationCtrl($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.registerId = UtilService.getCookie("regId");
        $scope.registerEmail= UtilService.getCookie("regEmail");
        $scope.flag = UtilService.getCookie("regFlag");

        var Uuser = UtilService.getUser();
        $scope.activeStatus = Uuser.activeStatus;
        $scope.acvitation =   UtilService.getCookie("ryioAcvitation"); //0 success 1 failure

        //判断状态
        $scope.findStatus = function(){
            $scope.showLoading();
            $http.get("/api/reged/findbyemail?email="+$scope.registerEmail).success(function(data){
                $scope.hideLoading();
                $scope.activeStatus = data.content.activeStatus;

                Uuser.activeStatus = $scope.activeStatus;
                UtilService.setUser(Uuser);

                if(data.content.status > -3 && $scope.flag != 1){
                    window.location.href = "success.html";
                }
                if($scope.activeStatus == true && $scope.flag == 1){
                    window.location.href = "success.html";
                }
            })
        }
        $scope.findStatus();

        $scope.hasReged = true;
        $scope.loginEmail = function(){
            var mail = $scope.registerEmail.split("@");
            window.open("http://mail."+mail[1]);
        }

        $scope.sendtype = 1;
        $scope.sendAgain = function(){
            $scope.showLoading();
            $http.get("/api/reged/sendeactivemail/"+$scope.registerId+"/"+$scope.flag).success(function(data){
                $scope.hideLoading();
                if(data.content == "success"){
                    $scope.sendtype = 2;
                    $scope.acvitation = 0;
                    $scope.time= 99000;
                    $one_minute = $interval(function(){
                        $scope.time -= 1000;
                        if($scope.time == 0){
                            $scope.sendtype = 1;
                        }
                    },1000);
                }else if(data.content == "actived"){
                    window.location.href = "success.html";
                }else{
                    $scope.acvitation = 1;
                }
            });
        }

        $scope.changeEmail = function(){
            $scope.winFlag = 1;
            $scope.oldemail = $scope.registerEmail;
        }
        $scope.closeWin = function(){
            $scope.winFlag = 0;
        }
        $scope.saveNewEmail = function(){
            var par = {
                oldemail: $scope.oldemail,
                newemail: $scope.newemail
            };
            var verPar = {
                oldemail: {
                    key: 'email',
                    max:48,
                    val: $scope.oldemail,
                    txt:"邮箱最长48位"
                },
                newemail: {
                    key: 'email',
                    max:48,
                    val: $scope.newemail,
                    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.newemail == undefined){
                if ($scope.tip.succ == true) {
                    $scope.tip.succ = false;
                }
                $scope.tip.newemail = {
                    status:true,
                    txt:"必填"
                }
            }
            if($scope.newemail == ""){
                if ($scope.tip.succ == true) {
                    $scope.tip.succ = false;
                }
                $scope.tip.newemail = {
                    status:true,
                    txt:"必填"
                }
            }
            if($scope.newemail != undefined && $scope.newemail != "" && !reg.test($scope.newemail)){
                if ($scope.tip.succ == true) {
                    $scope.tip.succ = false;
                }
                $scope.tip.newemail = {
                    status:true,
                    txt:"请输入正确的邮箱格式"
                }
            }
            if ($scope.tip.succ != true) {
                return false;
            }
            $scope.errorMessage = "";
            $scope.showLoading();
            $http.get("/api/reged/modifyemail/"+$scope.registerId+"?oldemail="+$scope.oldemail+"&newemail="+$scope.newemail).success(function(data){
                $scope.hideLoading();
                if(data.code== -1){
                    $scope.tip.newemail = {
                        status:true,
                        txt:"该新邮箱已存在，请更换新邮箱"
                    }
                }else if(data.code== 250){
                    $scope.tip.newemail = {
                        status:true,
                        txt:"发送邮件失败"
                    }
                }else if(data.code == 251){
                    $scope.tip.newemail = {
                        status:true,
                        txt:"注册邮箱已激活，不可再修改"
                    }
                }else if(data.content.id==null){
                    $scope.tip.newemail = {
                        status:true,
                        txt:"注册邮箱错误，请修改"
                    }
                }else{
                    UtilService.setCookie("regFlag",$scope.flag,7);
                    UtilService.setCookie("regEmail",$scope.newemail,7);
                    $scope.registerEmail = $scope.newemail;
                    window.location.href = "activation.html";
                }
            });
        }
        $scope.keyEvt= function(e){
            if(e.keyCode == 13){
                $scope.save();
            }
        }
    }
})();
