'use strict';

function EmployeeListCtrl($scope, employees) {
    $scope.info = employees;
}
EmployeeListCtrl.resolve = {
    employees:function ($q, $route, Employee) {
        var deferred = $q.defer();

        var successCb = function (result) {
            deferred.resolve(result);
        };

        Employee.query({}, successCb);
        return deferred.promise;
    }
};

function EmployeeCtrl($scope, $routeParams, $location, employee, cost_centers) {
    $scope.employee = employee;

    $scope.cost_centers = cost_centers;

    $scope.save = function () {
        $scope.employee.$save(function (u, putResponseHeaders) {
            $scope.toasts.push({Type:'Success', Message:u.Code});
            $location.path('/Employees')
        }, function (data, status) {
            $scope.toasts.push({Type:'Error', Message:data.data});
        });
    };

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

}

EmployeeCtrl.resolve = {
    employee:function ($q, $route, Employee) {
        var deferred = $q.defer();

        var id = $route.current.params.id;
        var successCb = function (result) {
            deferred.resolve(result);
        };

        Employee.get({id: id}, successCb);
        return deferred.promise;
    },
    cost_centers:function ($q, $route, CostCenter) {
        var deferred = $q.defer();

        var successCb = function (result) {
            deferred.resolve(result);
        };

        CostCenter.query({}, successCb);
        return deferred.promise;
    }
};
