(function () {
    "use strict";

    angular.module('pos').controller('relatorioEvolucaoDeVendaPorVendedorController', controller);
    controller.$inject = ['$scope', '$window', '$state', 'relatorioService', '$timeout'];

    function controller($scope, $window, $state, relatorioService, $timeout) {
        $scope.$parent.initializeLayout('Relatório');

        $scope.printPdf = function (showDetails) {
            $timeout(function () {
                window.print();
            }, 2000);
        };

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

        function getDateWithoutTime(date) {
            var dateObj = new Date(date);
            var dateWithoutTime = dateObj.toLocaleDateString();

            return dateWithoutTime;
        }

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

        $scope.chartTicket = function () {
            $scope.state.tickets.chart.carregando = true;
            $scope.state.tickets.chart.vazio = false;
            relatorioService
                .chartEvolucaoVendasByVendedor($scope.state.tickets.filtro)
                .then(
                    function (response) {
                        if (response.Status < 0) {
                            $scope.$parent.toastr.error(response.Message, 'Carregando gráfico');
                            console.error(response.Exception);
                        }
                        else {
                            angular.copy(angular.fromJson(response.Data), $scope.state.tickets.chart.data);
                            $scope.state.tickets.chart.vazio = $scope.$parent.tools.buildChart('#chart-ticket', '', response.Data, true);
                        }
                        $scope.state.tickets.chart.carregando = false;
                    },
                    function (error) {
                        $scope.$parent.toastr.error('Ao carregar o gráfico.', 'Comunicação com o servidor');
                        console.error(error);
                        $scope.state.tickets.chart.carregando = false;
                    }
                );
        };

        $scope.toggleFilter = function () {
            $scope.state.tickets.filtro.ativo = !$scope.state.tickets.filtro.ativo;
        };
        $scope.clearFilter = function () {
            $scope.state.tickets.filtro = {
                emissao: {
                    de: new Date((new Date()).setHours(0, 0, 0, 0)),
                    ate: null
                },
                codigoTerminal: '',
                periodoFechamento: '',
                codigoVendedor: '',
                ativo: false,
            }
            $scope.refresh();
        };

        $scope.refresh = function () {

            if ($scope.state.tickets.filtro.emissao.de) {
                var emissaoDe = $scope.state.tickets.filtro.emissao.de;
                var emissaoDeNoTime = getDateWithoutTime(emissaoDe);
                $scope.state.tickets.filtro.emissao.de = emissaoDeNoTime;
            }

            if ($scope.state.tickets.filtro.emissao.ate) {
                var emissaoAte = $scope.state.tickets.filtro.emissao.ate;
                var emissaoAteNoTime = getDateWithoutTime(emissaoAte);
                $scope.state.tickets.filtro.emissao.ate = emissaoAteNoTime;
            }

            $scope.chartTicket();
        };

        $scope.initialize();
    }

})();