(function () {
    "use strict";

    angular.module('pos').controller('saidaController', controller);
    controller.$inject = ['$scope', '$window', '$timeout', 'saidaService'];

    function controller($scope, $window, $timeout, saidaService) {
        $scope.$parent.initializeLayout('Saídas de Mercadorias');

        //[Inicializa objetos]
        $scope.state = {
            data: [],
            carregando: false,
            vazio: false,
            count: 0,
            filtro: {
                ativo: false,
                chaves: '',
                codigo: '',
                filialDestino: '',
                terceiro: '',
                emissao: { de: '', ate: '' },
                valorTotal: { de: null, ate: null },
                skip: 0,
                take: 20
            }
        };

        //[Inicializa funcões]
        $scope.initialize = function () {
            ;
            $scope.refresh();
        };
        $scope.historyBack = function () {
            $window.history.back();
        };

        $scope.getAll = function () {
            $scope.state.filtro.skip = 0;
            $scope.state.carregando = true;
            $scope.state.vazio = false;
            saidaService
                .getAll($scope.state.filtro)
                .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 = ($scope.state.data == 0);
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };
        $scope.count = function () {
            saidaService
                .count($scope.state.filtro)
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else
                            $scope.state.count = response.Data;
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };
        $scope.getMore = function () {
            $scope.state.filtro.skip += $scope.state.filtro.take;
            saidaService
                .getAll($scope.state.filtro)
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else
                            $scope.state.data = $scope.state.data.concat(response.Data);
                    },
                    function (err) {
                        $scope.$parent.toastr.error(err);
                     }
                );
        };
        $scope.refresh = function () {
            $scope.getAll();
            $scope.count();
        };
        $scope.clearFilter = function () {
            $scope.state.filtro.chaves = '';
            $scope.state.filtro.codigo = '';
            $scope.state.filtro.filialDestino = '';
            $scope.state.filtro.terceiro = '';
            $scope.state.filtro.emissao.de = '';
            $scope.state.filtro.emissao.ate = '';
            $scope.state.filtro.valorTotal.de = null;
            $scope.state.filtro.valorTotal.ate = null;

            $scope.refresh();
        };
        $scope.toggleFilter = function () {
            $scope.state.filtro.ativo = !$scope.state.filtro.ativo;
            if ($scope.state.filtro.ativo)
                $timeout(function () { $('[ng-model$="state.filtro.chaves"]').focus(); });
        };

        /*Inicializa funções*/
        $scope.initialize();
    }

})();