(function () {
    "use strict";

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

    function controller($scope, $window, vendedorService) {
        $scope.$parent.initializeLayout('Vendedores');

        $scope.state = {
            data: [],
            carregando: false,
            vazio: true,
            filtro: { ativo: false, nome: "", codigo: "" }
        };

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



        $scope.getAll = function () {
            $scope.state.carregando = true;
            $scope.state.vazio = true;
            vendedorService
                .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.toggleFilter = function () {
            $scope.state.filtro.ativo = !$scope.state.filtro.ativo;
        };

        $scope.filterByName = function (item) {
            return (item.Nome.toUpperCase().indexOf($scope.state.filtro.nome.toUpperCase()) >= 0) || $scope.state.filtro.nome == "";
        };

        $scope.clearFilter = function () {
            $scope.state.filtro.nome = "";
        };

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

        };

        $scope.initialize();
    }

})();