'use strict';

var AccountListCtrl = ['$scope', 'accounts', function ($scope, accounts) {
    $scope.info = accounts;
}];
AccountListCtrl.resolve = {
    accounts: ['Account', function (Account) {
        return Account.query({}).$promise;
    }]
};

var AccountCtrl = ['$scope', '$location', '$modal', 'account', 'account_types', 'cost_centers', function ($scope, $location, $modal, 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: 'Danger', 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: 'Danger', Message: data.data});
        });
    };

    $scope.confirm = function () {
        var modalInstance = $modal.open({
            backdrop: true,
            templateUrl: '/template/modal/confirm.html',
            controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
                $scope.title = "Delete Account";
                $scope.body = "Are you sure? This cannot be undone.";
                $scope.isDelete = true;
                $scope.ok = function () {
                    $modalInstance.close();
                };
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            }]
        });
        modalInstance.result.then(function () {
            $scope.delete();
        });
    };
    $('#txtName').focus();
}];

AccountCtrl.resolve = {
    account: ['$route', 'Account', function ($route, Account) {
        var id = $route.current.params.id;
        return Account.get({id: id}).$promise;
    }],
    account_types: ['AccountType', function (AccountType) {
        return AccountType.query({}).$promise;
    }],
    cost_centers: ['CostCenter', function (CostCenter) {
        return CostCenter.query({}).$promise;
    }]
};
