(function () {
    "use strict";

    angular.module('pos').controller('notaFiscalComplementarController', controller);
    controller.$inject = ['$scope', '$window', '$state', '$stateParams', '$timeout', 'notaFiscalComplementarService'];

    function controller($scope, $window, $state, $stateParams, $timeout, notaFiscalComplementarService) {
       
        //[Inicializa objetos]
        $scope.state = {
            codigo: decodeURIComponent($stateParams.codigo),
            data: [],
            imposto: { index: -1, ativo: false },
            tabCorrente: 0,
            notaComplementar: {}
        };
        
        //[Inicializa funcões]
        $scope.initialize = function () {            
            $scope.refresh();
        };

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

        $scope.getById = function () {
            notaFiscalComplementarService
                .get($scope.state.codigo)
                .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);
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.saveItens = function () {
            notaFiscalComplementarService
                .saveItens($scope.state.data.Itens)
                .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.Itens);

                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.save = function () {
            ;
            notaFiscalComplementarService
                .save($scope.state.data)
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');    

                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.goToNfOrigem = function () {
            $state.go("state-nota-fiscal-detail", {
                numero: $scope.state.data.NotaFiscalOrigem.Numero,
                serie: $scope.state.data.NotaFiscalOrigem.Serie.Codigo
            });
        }

        $scope.toggleImposto = function (index) {
            $scope.state.imposto.index = index;
            $scope.state.imposto.ativo = !$scope.state.imposto.ativo;
        };

        $scope.saveImposto = function (index) {
            notaFiscalComplementarService
                .saveImposto($scope.state.data.Itens[$scope.state.imposto.index].Impostos[index], $scope.state.data.Itens[$scope.state.imposto.index].Id)
                .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.Itens[$scope.state.imposto.index].Impostos[index]);

                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.generateNfe = function () {
            $scope.state.notaComplementar.Id = $scope.state.codigo;
            notaFiscalComplementarService
                .generateNFe($scope.state.notaComplementar)
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else {
                            $state.go("state-nota-fiscal-detail", {
                                numero: response.Data.Numero,
                                serie: response.Data.Serie.Codigo
                            });
                        }
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.refresh = function () {
            $scope.getById();            
        };

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

})();