'use strict';

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

ClientListCtrl.resolve = {
    clients: ['$q', 'Client', function ($q, Client) {
        var deferred = $q.defer();

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

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

var ClientCtrl = ['$scope', '$location', 'client', function ($scope, $location, 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: ['$q', '$route', 'Client', function ($q, $route, Client) {
        var deferred = $q.defer();

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

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