app.controller('EditUserInfoController', ['$scope', 'config','$http', 'auth','$location','$routeParams','alertService','rms','common','$timeout','wx', function($scope, config, $http, auth, $location, $routeParams,alertService,rms,common,$timeout,wx){
//    common.checkLogin();

    $scope.replaceUrl = function(url){
        return url;
    };

    var vm = $scope;

    //单弹框
    vm.alertService_1 = alertService.create();

    //双弹框
    vm.alertService_2 = alertService.create();

    vm.getuserinfo = function(){
        $http.get(config.apiUrl + 'users/get_userinfo?token='+auth.token()).then(
            function(response) {
                console.log(response.data);
                vm.user = response.data.data;
                vm.lastname = vm.user.lastname;
                vm.mail = vm.user.mail;
                vm.sex = vm.user.gender;
            }
        );
    };

    vm.getuserinfo();

    vm.unbind = function(){
        vm.alertService_2.showMessage('提示', '是否确认解除绑定', function() {
            $http.post(config.apiUrl + "users/user_unbind",
                {token: auth.token()}
            ).then(function (response) {
                    if (response.data.status == 0) {
                        vm.alertService_1.showMessage('提示', '解除绑定成功', function () {
                            wx.closeWindow();
                        });
                    }
                    else {
                        vm.alertService_1.showMessage('提示', response.data.message);
                    }
                });
        });
    };

    vm.cancel = function(){
        vm.alertService_2.showMessage('提示', '是否确认取消', function() {
            wx.closeWindow();
        });
    };

    vm.confirm = function(frm){
        frm.$setSubmitted();

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

        if(vm.lastname == ""){
            vm.alertService_1.showMessage('提示', '姓名不能为空');
            return;
        }

        if(vm.mail == ""){
            vm.alertService_1.showMessage('提示', '邮箱不能为空');
            return;
        }

//        if(!(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.test(vm.mail))){
//            vm.alertService_1.showMessage('提示', '邮箱不合法');
//            return;
//        }

        $http.post(config.apiUrl + "users/edit_userinfo",
            {token: auth.token(),lastname: vm.lastname, sex: vm.sex, mail: vm.mail}
        ).then(function(response){
                if(response.data.status == 0)
                {
                    vm.alertService_1.showMessage('提示', '修改成功',function(){
                        wx.closeWindow();
                    });
                }
                else{
                    vm.alertService_1.showMessage('提示', response.data.message);
                }
            });
    };

}]);