controller.js 5.32 KB
angular.module('focaModalCotizacion')
    .controller('focaModalCotizacionController', 
        [
            '$filter',
            '$scope',
            '$uibModalInstance',
            'focaModalCotizacionService',
            'idMoneda',
            function($filter, $scope, $uibModalInstance, focaModalCotizacionService, idMoneda) {
                focaModalCotizacionService.getCotizaciones(idMoneda).then(
                    function(res) {console.log(res.data);
                        $scope.moneda = res.data[0];
                        $scope.cotizacion = res.data[0].cotizaciones;
                        $scope.search();
                    }
                );

                // pagination
                $scope.numPerPage = 10;
                $scope.currentPage = 1;
                $scope.filteredCotizacion = [];
                $scope.currentPageCotizacion = [];
                $scope.selectedCotizacion = -1;
                
                //METODOS
                $scope.search = function() {
                    $scope.filteredCotizacion = $filter('filter')(
                        $scope.cotizacion, 
                        {$: $scope.filters}
                    );

                    $scope.lastPage = Math.ceil(
                        $scope.filteredCotizacion.length / $scope.numPerPage
                    );

                    $scope.resetPage();
                };

                $scope.resetPage = function() {
                    $scope.currentPage = 1;
                    $scope.selectPage(1);
                };

                $scope.selectPage = function(page) {
                    var start = (page - 1) * $scope.numPerPage;
                    var end = start + $scope.numPerPage;
                    $scope.paginas = [];
                    $scope.paginas = calcularPages(page);
                    $scope.currentPageCotizacion = 
                        $scope.filteredCotizacion.slice(start, end);
                    $scope.currentPage = page;
                };

                $scope.select = function(cotizacion) {
                    $uibModalInstance.close(cotizacion);
                };

                $scope.cancel = function() {
                    $uibModalInstance.dismiss('cancel');
                };

                $scope.busquedaDown = function(key) {
                    if (key === 40) {
                        primera(key);
                    }
                };

                $scope.busquedaPress = function(key) {
                    if (key === 13) {
                        primera(key);
                    }
                };

                $scope.itemProducto = function(key) {
                    if (key === 38) {
                        anterior(key);
                    }

                    if (key === 40) {
                        siguiente(key);
                    }

                    if (key === 37) {
                        retrocederPagina();
                    }

                    if (key === 39) {
                        avanzarPagina();
                    }
                };

                function calcularPages(paginaActual) {
                    var paginas = [];
                    paginas.push(paginaActual);

                    if (paginaActual - 1 > 1) {
                        
                        paginas.unshift(paginaActual - 1);
                        if (paginaActual - 2 > 1) {
                            paginas.unshift(paginaActual - 2);
                        }
                    }

                    if (paginaActual + 1 < $scope.lastPage) {
                        paginas.push(paginaActual + 1);
                        if (paginaActual + 2 < $scope.lastPage) {
                            paginas.push(paginaActual + 2);
                        }
                    }

                    if (paginaActual !== 1) {
                        paginas.unshift(1);
                    }

                    if (paginaActual !== $scope.lastPage) {
                        paginas.push($scope.lastPage);
                    }

                    return paginas;
                }

                function primera() {
                    $scope.selectedCotizacion = 0;
                }

                function anterior() {
                    if ($scope.selectedCotizacion === 0 && $scope.currentPage > 1) {
                        retrocederPagina();
                    } else {
                        $scope.selectedCotizacion--;
                    }
                }

                function siguiente() {
                    if ($scope.selectedCotizacion <
                            $scope.currentPageCotizacion.length - 1 ) {
                        $scope.selectedCotizacion++;
                    } else {
                            avanzarPagina();
                    }
                }

                function retrocederPagina() {
                    if ($scope.currentPage > 1) {
                        $scope.selectPage($scope.currentPage - 1);
                        $scope.selectedCotizacion = $scope.numPerPage - 1;
                    }
                }

                function avanzarPagina() {
                    if ($scope.currentPage < $scope.lastPage) {
                        $scope.selectPage($scope.currentPage + 1);
                        $scope.selectedCotizacion = 0;
                    }
                }
            }
        ]
    );