Source: components/admin/controllers/poi.controller.js

/**
 * @ngdoc controllers
 * @name Poi
 * @module s4c.components.admin.controllers.Poi
 *
 * @description
 * `Poi` Controller da tela de Tipo de Poi do módulo de administração
 * 
 * 
 */
(function () {
    'use strict';

    angular.module('s4c.controllers.AdminTipoPoiCtrl', [
        'ngMaterial'
    ])
        .controller('AdminTipoPoiCtrl', AdminTipoPoiCtrl)

    AdminTipoPoiCtrl.$inject = ['$scope', '$mdDialog', 'Poi']

    function AdminTipoPoiCtrl($scope, $mdDialog, Poi) {

        $scope.res = $scope.$root.res;
        $scope.tipos = [];
        Poi.getTipos().then(function (resultado) {
            $scope.tipos = resultado;
            ordenaTipos();
        });

        /**
         * @method adicionarNovoTipo
         */
        function adicionarNovoTipo() {

            var tipo = {};
            tipo.nome = "";
            tipo.status = 1;
            tipo.createdAt = moment();

            $scope.tipos.push(tipo);
        }

        /**
         * @method removerTipo
         * @param {*} tipo 
         */
        function removerTipo(tipo) {

            if (tipo.id) {
                tipo.status = 0;
            } else {
                $scope.tipos.splice($scope.tipos.indexOf(tipo), 1);
            }

        }

        /**
         * @method temNomesRepetidos
         */
        function temNomesRepetidos() {

            var temRepetidos = false;
            var label;
            angular.forEach($scope.tipos, function (tipo1) {

                angular.forEach($scope.tipos, function (tipo2) {

                    if (tipo1.nome == tipo2.nome && tipo1 != tipo2) {
                        temRepetidos = true;
                        return;
                    }

                });
            });

            return temRepetidos;
        }

        /**
         * @method temTiposSemNome
         */
        function temTiposSemNome() {

            var semNome = false;
            angular.forEach($scope.tipos, function (tipo) {

                if (!tipo.nome) {
                    semNome = true;
                    return;
                }

            });

            return semNome;
        }

        /**
         * @method ordenaTipos
         */
        function ordenaTipos() {

            $scope.tipos = $scope.tipos.sort(function (a, b) {

                return a.nome > b.nome ? 1 : a.nome < b.nome ? -1 : 0
            });
        }

        /**
         * @method verificarExistenciaTipo
         */
        function verificarExistenciaTipo() {

            var existe = false;
            var tiposParaRemover = [];
            angular.forEach($scope.tipos, function (tipo1) {

                if (!tipo1.id) {

                    angular.forEach($scope.tipos, function (tipo2) {

                        //Se já tem tipo desativado com o mesmo nome, reativa.
                        if (tipo2.id && tipo2.nome == tipo1.nome && tipo2.status == 0) {
                            tipo2.status = 1;
                            existe = true;
                            tiposParaRemover.push(tipo1);
                        }

                    });
                }
            });

            //Remove os tipos novos pois os desativados ficarão no lugar deles.
            if (existe) {
                angular.forEach(tiposParaRemover, function (tipo) {
                    $scope.tipos.splice($scope.tipos.indexOf(tipo), 1);
                });
            }
        }

        $scope.salvando = false;
        /**
         * @method salvar
         * @param {*} evt 
         */
        function salvar(evt) {

            if ($scope.salvando) {
                return;
            }

            $scope.salvando = true;
            //Verifica se algum novo tipo adicionado tem mesmo nome de um desativado.
            verificarExistenciaTipo();

            //Verificar se tem nomes repetidos
            if (temNomesRepetidos()) {
                $mdDialog.show({
                    targetEvent: evt,
                    scope: $scope.$new(),
                    template: '<md-dialog><md-content>' + $scope.res('TIPO_NOMES_IGUAIS') + '</md-content></md-dialog>'
                });

                $scope.salvando = false;
                return;
            }

            //Verificar se tem tipos sem nome
            if (temTiposSemNome()) {
                $mdDialog.show({
                    targetEvent: evt,
                    scope: $scope.$new(),
                    template: '<md-dialog><md-content>' + $scope.res('TIPO_SEM_NOME') + '</md-content></md-dialog>'
                });

                $scope.salvando = false;
                return;
            }

            ordenaTipos();

            Poi.salvarTipos($scope.tipos)
                .then(function (result) {
                    $scope.salvando = false;
                    $mdDialog.show({
                        targetEvent: evt,
                        scope: $scope.$new(),
                        template: '<md-dialog><md-content>' + $scope.res('TIPO_POI_SALVO') + '</md-content></md-dialog>'
                    });
                });
        }

        angular.extend($scope, {

            salvar: salvar,
            adicionarNovoTipo: adicionarNovoTipo,
            removerTipo: removerTipo

        })
    }

}());