'use strict';

var AccountListCtrl = ['$scope', 'accounts', function ($scope, accounts) {
    $scope.info = accounts;
}];
AccountListCtrl.resolve = {
    accounts:['$q', '$route', 'Account', function ($q, $route, Account) {
        var deferred = $q.defer();

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

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

var AccountCtrl = ['$scope', '$location', 'account', 'account_types', 'cost_centers', function ($scope, $location, account, account_types, cost_centers) {
    $scope.account = account;

    $scope.account_types = account_types;
    $scope.cost_centers = cost_centers;

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

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

}]

AccountCtrl.resolve = {
    account:['$q', '$route', 'Account', function ($q, $route, Account) {
        var deferred = $q.defer();

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

        Account.get({id: id}, successCb);
        return deferred.promise;
    }],
    account_types:['$q', 'AccountType', function ($q, AccountType) {
        var deferred = $q.defer();

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

        AccountType.query({}, successCb);
        return deferred.promise;
    }],
    cost_centers:['$q', 'CostCenter', function ($q, CostCenter) {
        var deferred = $q.defer();

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

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