angular.module('focaModalPrecioCondicion') .controller('focaModalPrecioCondicionController', [ '$filter', '$scope', '$uibModalInstance', 'focaModalService', 'focaModalPrecioCondicionService', function($filter, $scope, $uibModalInstance, focaModalService, focaModalPrecioCondicionService) { focaModalPrecioCondicionService.getPreciosCondicionesPlazosPagos().then( function(res) { for(var i = 0; i < res.data.length; i++) { var plazosTemp = ''; for(var j = 0; j < res.data[i].plazoPago.length; j++) { plazosTemp += res.data[i].plazoPago[j].dias + ' '; } res.data[i].plazos = plazosTemp.trim(); } $scope.precioCondicion = res.data; $scope.search(); } ); $scope.ingreso = false; $scope.plazosNuevos =[]; $scope.plazoACargar = { item: 1 }; // pagination $scope.numPerPage = 10; $scope.currentPage = 1; $scope.filteredPrecioCondicion = []; $scope.currentPagePrecioCondicion = []; $scope.selectedPrecioCondicion = -1; //METODOS $scope.agregarPlazo = function(key) { if(key === 13) { if(!$scope.plazoACargar.dias) { focaModalService.alert('Ingrese cantidad de días'); return; } var tieneEseDia = $scope.plazosNuevos.filter(function(a) { return a.dias === $scope.plazoACargar.dias; }); if(tieneEseDia.length > 0) { focaModalService.alert('Ya ha ingresado un plazo con esos días'); return; } $scope.plazosNuevos.push($scope.plazoACargar); $scope.plazoACargar = { item: $scope.plazosNuevos.length + 1 }; } }; $scope.volver = function() { $scope.ingreso = false; $scope.plazosNuevos = []; $scope.plazoACargar = { item: $scope.plazosNuevos.length + 1 }; }; $scope.quitarPlazo = function(key) { $scope.plazosNuevos.splice(key, 1); $scope.plazoACargar = { item: $scope.plazosNuevos.length + 1 }; }; $scope.search = function() { $scope.filteredPrecioCondicion = $filter('filter')( $scope.precioCondicion, {$: $scope.filters} ); $scope.lastPage = Math.ceil( $scope.filteredPrecioCondicion.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.currentPagePrecioCondicion = $scope.filteredPrecioCondicion.slice(start, end); $scope.currentPage = page; }; $scope.select = function(precioCondicion) { $uibModalInstance.close(precioCondicion); }; $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.selectedPrecioCondicion = 0; } function anterior() { if ($scope.selectedPrecioCondicion === 0 && $scope.currentPage > 1) { retrocederPagina(); } else { $scope.selectedPrecioCondicion--; } } function siguiente() { if ($scope.selectedPrecioCondicion < $scope.currentPagePrecioCondicion.length - 1 ) { $scope.selectedPrecioCondicion++; } else { avanzarPagina(); } } function retrocederPagina() { if ($scope.currentPage > 1) { $scope.selectPage($scope.currentPage - 1); $scope.selectedPrecioCondicion = $scope.numPerPage - 1; } } function avanzarPagina() { if ($scope.currentPage < $scope.lastPage) { $scope.selectPage($scope.currentPage + 1); $scope.selectedPrecioCondicion = 0; } } } ] );