'use strict';

var UserListCtrl = ['$scope', 'users', function ($scope, users) {
    $scope.info = users;
}];

UserListCtrl.resolve = {
    users: ['User', function (User) {
        return User.query({}).$promise;
    }]
};

var UserCtrl = ['$scope', '$location', 'user', function ($scope, $location, user) {
    $scope.user = user;

    $scope.save = function () {
        $scope.user.$save(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/Users');
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

    $scope.delete = function () {
        $scope.user.$delete(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/Users');
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };
    $('#txtName').focus();
}];

UserCtrl.resolve = {
    user: ['$route', 'User', function ($route, User) {
        var id = $route.current.params.id;

        return User.get({id: id}).$promise;
    }]
};
