(function (app) {
    app.controller('wechatLoginCtrl', ["$scope","userService","$ionicNavBarDelegate", "storage", "$state", "msg", "$http", "util",
        function ($scope,userService,$ionicNavBarDelegate, storage, $state, msg, $http, util) {
            //$ionicNavBarDelegate.showBackButton(false);
            $scope.wechat_login = function(){
                var scope = "snsapi_userinfo", state = "_" + (+new Date());
                userService.isTest().then(function(res){
                    if(res.data.data){
                        Wechat.auth(scope, state, function (response) {
                            // alert("response:"+JSON.stringify(response));
                            // var response={code:1231};
                           if(response.code){
                               msg.loading('登录中...');
                               userService.wechat_login(response.code).then(function(result){
                                   msg.hide();
                                    // alert("wechat_login success: " + JSON.stringify(result));
                                   if(result.data.data.user){
                                       storage.setObject('user', result.data.data.user);
                                       storage.set('token', result.data.data.token);
                                       $http.defaults.headers.common["Authorization"] = 'Bearer ' + result.data.data.token;
                                       $scope.user=storage.getObject('user');
                                       $state.go('app.home');
                                   }else{
                                       $state.go("loginAccount",{wechat:result.data.data.openid})
                                   }
                               },function(error){
                                    console.log("loginbywx Failed: " + JSON.stringify(error));
                                   $state.go("loginAccount");
                               });
                           }
                        }, function (error) {
                           console.log("Failed: " + JSON.stringify(error));
                        })
                    }else{
                        $state.go("loginAccount",{wechat:'orrk51inhmfmQRXA8VvGEQfow3Es'});
                    }
                } )

            }
    }]);
    app.controller('loginCtrl', ["$scope", "userService","$stateParams","$ionicNavBarDelegate", "storage", "$state", "msg", "$http", "util","$timeout",
        function ($scope, userService,$stateParams,$ionicNavBarDelegate, storage, $state, msg, $http, util, $timeout) {
            //$ionicNavBarDelegate.showBackButton(false);
            $scope.wechat = $stateParams.wechat;
            $scope.vm = {
                mobile: '',
                verify_code: '',
                waitSeconds: "获取验证码"
            };
            $scope.login = function () {
                //msg.loading('登录中...');
                msg.loading();
                userService.login($scope.vm.mobile, $scope.vm.verify_code,$scope.wechat).then(function (result) {
                //userService.login($scope.vm.mobile, $scope.vm.verify_code,124).then(function (result) {
                    msg.hide();
                    storage.setObject('user', result.data.data.user);
                    storage.set('token', result.data.data.token);
                    $http.defaults.headers.common["Authorization"] = 'Bearer ' + result.data.data.token;
                    $scope.user=storage.getObject('user');
                    $state.go('app.home');
                }, function (error) {
                    // msg.hide();
                    // msg.error(error.data.message);
                });
            };

            //获取验证码
            $scope.getVerifyCode = function () {
                // if (!util.isMobile($scope.vm.mobile)) {
                //     msg.text('请输入正确的手机号');
                //     return;
                // }
                if ($scope.vm.waitSeconds!="获取验证码") {
                    msg.text('请稍后再试', true);
                    return;
                }
                $scope.vm.waitSeconds = "正在发送";
                userService.getVerifyCode($scope.vm.mobile).then(function (result) {
                    wait(50);
                    // $scope.returncode = result.data;
                    if(result.data.data.verify_code)msg.success(result.data.data.verify_code);
                }, function (error) {
                    $scope.vm.waitSeconds = "获取验证码";
                    wait(0);
                    // alert(JSON.stringify(error));
                    // msg.error(error.data.message);
                });
            };
            var wait = function (seconds) {
                if (seconds > 0) {
                    $scope.vm.waitSeconds = "" + seconds + "秒";
                } else {
                    $scope.vm.waitSeconds = "获取验证码";
                }
                $timeout(function () {
                    if (seconds >= 1)
                        wait(seconds - 1);
                }, 1000);
            };
            $scope.clear = function(){
                document.getElementById("tel").value = "";
            }
        }]);
  
})(angular.module('app.controllers'));