app.controller('LoginController', ['$scope', '$http', '$location', '$routeParams', 'alertService', 'config','auth','session','wx',
    function ($scope, $http, $location, $routeParams, alertService, config, auth,session, wx) {

      var tag = $routeParams.tag;

      // 登录页不用显示菜音
      wx.ready(function(){
         wx.hideOptionMenu();
      })

        if(auth.get_bind().then(function(){
                $location.path("/activities");
         }));

        if($routeParams.code){
            session.save('code', $routeParams.code);
        }

        var vm = $scope;
        vm.loginFailed = false;
        vm.alertService = alertService.create();
        vm.findPwdDialog = alertService.create();

        vm.login = function (frm, user) {
            frm.$setSubmitted();
            console.log(user);

            if (!frm.$valid) {
                console.log(frm.$error);
                return;
            }

            console.log(apiUrl + "auth");

            $http.post(
                config.apiUrl + "users/wxbind",
                {login: user.login, password: user.password}
            ).then(
                function(response) {
                    console.log(response.data);
                    vm.loginFailed = (response.data.status != 0);
                    if (!$scope.loginFailed) { //绑定成功
                        vm.alertService.showMessage('提示', response.data.message, function(){
                            if(tag){
                                window.history.back();
                                tag = null;
                            }
                            else{
                                wx.closeWindow();
                            }
                        });
                    } else {
                        vm.alertService.showMessage('出错了', response.data.message);
                    }
                }
            ).catch(function(e){
                vm.alertService.showMessage('出错了', e);
            });
        };

        vm.showBox = function () {
            vm.findPwdDialog.showMessage("提示", "请访问www.trustie.net获取密码，谢谢！");
        }
        vm.goReg = function () {
            $location.path('/reg');
        }
    }]);
