'use strict';

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

CostCenterListCtrl.resolve = {
    cost_centers: ['$q', '$route', 'CostCenter', function ($q, $route, CostCenter) {
        var deferred = $q.defer();

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

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

var CostCenterCtrl = ['$scope', '$routeParams', '$location', 'cost_center', function ($scope, $routeParams, $location, cost_center) {
    $scope.cost_center = cost_center;

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

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

CostCenterCtrl.resolve = {
    cost_center: ['$q', '$route', 'CostCenter', function ($q, $route, CostCenter) {
        var deferred = $q.defer();

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

        CostCenter.get({id: id}, successCb);
        return deferred.promise;
    }]
};
