(function(){
     angular.module("app.home")
        .controller("mainCtrl",["$scope","OptionService","UtilService","$interval","HttpService",mainCtrl]);

    function mainCtrl($scope,OptionService,UtilService,$interval,HttpService){
        //loading加载
        $scope.showLoading = function(){
            document.getElementById("sysLoad").style.display = "block";
        }
        $scope.hideLoading = function(){
            document.getElementById("sysLoad").style.display = "none";
        }

        var code = "";
        $scope.getImgInfo = function(){
            var selectChar = new Array(1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','j','k','l','m','n','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');  
            code = "";
            var len = selectChar.length;
            for(var i=0;i<4;i++) {      
               var charIndex = Math.floor(Math.random()*len);      
               code +=selectChar[charIndex];      
            }
            document.getElementById("imgMa").src = "/api/login/getvalidcode?code="+code;
        }

        $scope.noCode = 0;
        $scope.closeCode = function(){
            $scope.noCode = 0;
        }

        $scope.noActive = 0;
        $scope.closeWin = function(){
            $scope.noActive = 0;
        }

        $scope.demoSuccess = false;

        $scope.sendtype = 1;
        $scope.send = function(){
            var par = {
                phone: $scope.phone
            };
            var verPar = {
                phone: {
                    key: 'cellphone',
                    val: $scope.phone
                }
            }
            $scope.tip = formJudge(verPar);
            if ($scope.tip.succ != true) { 
                return false;
            }
            var testPM = HttpService.getInfo("demo/freetrial/checkexist?phone="+$scope.phone,{},true);
            testPM.then(function(data){
                if(data == true){
                    $scope.noActive = 1;
                    $scope.errorText = "您已提交过申请，请不要重复提交";
                }else{
                    $scope.noCode = 1;
                    $scope.yema = "";
                    $scope.getImgInfo();
                }
            });   
        }

        $scope.sendMessage = function(){
            if(angular.uppercase($scope.yema) == '' || angular.uppercase($scope.yema) == undefined){
                $scope.tip = {
                    yema:{
                        status:true,
                        txt:"必填"
                    }
                };
                return false;
            }
            if(angular.uppercase($scope.yema)!=angular.uppercase(code)){
                $scope.tip = {
                    yema:{
                        status:true,
                        txt:"验证码有误，请重新输入"
                    }
                };
                $scope.getImgInfo();
                return false;
            }
            $scope.noCode = 0;
            var sendPM = HttpService.getInfo("messagecode/sendMessagecode?phone="+$scope.phone,{},true);
            sendPM.then(function(data){
                if(data.content == 'success' || data == 'success'){
                    $scope.sendtype = 2;
                    $scope.time= 120000;
                    $one_minute = $interval(function(){
                        $scope.time -= 1000;
                        if($scope.time == 0){
                            $scope.sendtype = 1;
                        }
                    },1000);
                }
            },
            function(data){
                if(data.code == -4002 || data.code == -4003 || data.code == -4004){
                    $scope.noActive = 1;
                    $scope.errorText = "今日请求次数过多，请稍后再试。";
                }
            });
        }

        $scope.save = function(){
            var par = {
                name: $scope.name,
                phone: $scope.phone
            };
            var verPar = {
                name: {
                    key: 'username',
                    max:32,
                    val: $scope.name
                },
                phone: {
                    key: 'cellphone',
                    val: $scope.phone
                }
            }
            $scope.tip = formJudge(verPar);
            if($scope.code == '' || $scope.code == undefined ){
                if ($scope.tip.succ == true) {
                    $scope.tip.succ = false;
                }
                $scope.tip.code = {
                    status:true,
                    txt:"必填"
                }
            }
            if ($scope.tip.succ != true) { 
                return false;
            }
            $scope.showLoading();
            var demoPM = HttpService.postInfo("demo/freetrial?code="+$scope.code,par,true);
            demoPM.then(function(data){
                $scope.hideLoading();
                if(data.code == -4005){
                    $scope.noActive = 1;
                    $scope.errorText = "验证码不正确";
                }else if(data.code == -4001){
                    $scope.noActive = 1;
                    $scope.errorText = "验证码过期，请重新获取";
                }else if(data.code == 200){
                    $scope.demoSuccess = true;
                }else{
                    $scope.noActive = 1;
                    $scope.errorText = "申请提交失败，请重新提交";
                }
            });
        }

        $scope.keyEvt= function(e){
            if(e.keyCode == 13){
                $scope.save();
            }
        }
    }
})();