'use strict';

var LoginCtrl = ['$scope', '$http', '$location', '$routeParams', '$cookieStore', function ($scope, $http, $location, $routeParams, $cookieStore) {
    $scope.submit = function () {
        var data = {username: $scope.username, password: $scope.password};
        $http.
            post('/api/Login', data).
            success(function () {
                var came_from = $routeParams.came_from;
                if (typeof came_from === 'undefined' || came_from === "" || came_from === "/Login" || came_from === "/Logout") {
                    came_from = "/";
                }
                $location.path(came_from).search({});
            }).
            error(function (data, status, headers, config) {
                $scope.$emit('notification',{Type: 'Danger', Message: data});
            });
    };
    $('#username').focus();
}];

var LogoutCtrl = ['$scope', '$http', '$location', function ($scope, $http, $location) {
    $http.
        post('/Logout').
        success(function () {
            $scope.$emit('notification',{Type: 'Success', Message: 'Logged out'});
            $location.path('/')
        }).
        error(function (errorMessage) {
            $scope.$emit('notification',{Type: 'Danger', Message: errorMessage});
        });
}];
