'use strict';

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

ClientListCtrl.resolve = {
    clients: ['Client', function (Client) {
        return Client.query({}).$promise;
    }]
};

var ClientCtrl = ['$scope', '$location', '$modal', 'client', function ($scope, $location, $modal, client) {
    $scope.client = client;

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

    $scope.delete = function () {
        $scope.client.$delete(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/Clients');
        }, 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 Client";
                $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();
}];

ClientCtrl.resolve = {
    client: ['$route', 'Client', function ($route, Client) {
        var id = $route.current.params.id;
        return Client.get({id: id}).$promise;
    }]
};
