/// <reference path="relatorio.evolucao.de.venda.js" />
(function () {
    "use strict";

    angular.module('pos').controller('clienteConsultaController', controller);
    controller.$inject = ['$scope', '$window', '$state', '$timeout', 'clienteService', 'entidadeAuxiliarService', 'ticketService'];

    function controller($scope, $window, $state, $timeout, clienteService, entidadeAuxiliarService, ticketService) {
        $scope.$parent.initializeLayout('Consulta de Clientes');

        $scope.print = function (showDetails) {
            $timeout(function () {
                window.print();
            }, 2000);
        }; 
        $scope.cliente = []; 
        $scope.itensporgrupo = {
            carregando: false,
            vazio: true,
            data: []
        };
        $scope.items = {
            data: [],
            carregando: false,
            vazio: false,
            count: 0,
            filtro: {
                ativo: false,
                dataCadastro: '',
                dataInicio: null,
                dataFim: null,
                nome: '',
                cpf: '',
                telefone: '',
                colecao: '',
                grupo: '',
                mes: '',
                valorMinimo: '',
                qtdeMinima: '',
                numeroDias: '',
                codigoCliente: ''
            },
        };    
        $scope.state = {
            filiais: [],
            tipoVisualizacao: 0,
            filtro: {
                ativo: true,
            },
        } 
        $scope.getAll = function () {
            $scope.items.filtro.skip = 0;
            $scope.items.count = 0;
            $scope.items.carregando = true;
            $scope.carregamentoCompleto = false;
            $scope.items.vazio = false;
            clienteService
                .GetVendasClientes($scope.items.filtro)
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else {
                            angular.copy(angular.fromJson(response.Data), $scope.items.data);                            
                        }

                        $scope.items.carregando = false; 
                        $scope.items.vazio = ($scope.items.data == 0);
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        }; 
        $scope.toggleFilter = function () {
            $scope.items.filtro.ativo = !$scope.items.filtro.ativo;            
        };
        $scope.refresh = function () {
            $scope.getAll();
        };
        $scope.initialize = function () { 
            $scope.reverse = true;
        };
       
        $scope.sortBy = function (propertyName) {
            $scope.reverse = ($scope.propertyName === propertyName) ? !$scope.reverse : false;
            $scope.propertyName = propertyName;
        };

        $scope.DetailpropertyName = 'ValorCompras';
        $scope.Detailreverse = true;

        $scope.DetailsortBy = function (propertyName) {
            $scope.Detailreverse = ($scope.DetailpropertyName === propertyName) ? !$scope.Detailreverse : false;
            $scope.DetailpropertyName = propertyName;
        };

        $scope.pesquisar = function () {
            $scope.listar = true; 

            $scope.toggleFilter(); 
            $scope.refresh();
        };

        $scope.Limpar = function () {
            $scope.items.filtro = { 
                dataInicio: '',
                dataFim: '',
                nome: '',
                cpf: '',
                telefone: '', 
                mes: ''
            };
        };
         
        $scope.$watch('carregandoInfo', function (newValue, oldValue) {
            if (oldValue == false && newValue == true)
                $scope.$parent.toastr.info("Carregando novos itens...");
        });

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

})();