(function () {
    "use strict";

    angular.module('pos').controller('operacaoVendaController', controller);
    controller.$inject = ['$scope', '$window', 'operacaoVendaService'];

    function controller($scope, $window, operacaoVendaService) {
        $scope.$parent.initializeLayout('Operações de Venda');

        $scope.state = {
            data: [],
            carregando: false,
            vazio: true
        };

        $scope.initialize = function () {
            $scope.refresh();
        };
        $scope.historyBack = function () {
            $window.history.back();
        }

        $scope.getAll = function () {
            $scope.state.carregando = true;
            $scope.state.vazio = true;
            operacaoVendaService
                .getAll()
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else
                            angular.copy(angular.fromJson(response.Data), $scope.state.data);

                        $scope.state.carregando = false;
                        $scope.state.vazio = !(response.Data && response.Data.length > 0);
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.refresh = function () {
            $scope.getAll();
        };
        $scope.initialize();
    }

})();

