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

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

    AdminPopCtrl.$inject = ['$scope', 'Base', '$mdDialog', 'Poi'];

    var app = angular.module('s4c.controllers.AdminPopCtrl', ['textAngular']).controller('AdminPopCtrl', AdminPopCtrl);

    function AdminPopCtrl($scope, Base, $mdDialog, Poi) {

        $scope.res = $scope.$root.res;
        $scope.tipoSelecionado = null;
        $scope.perfilSelecionado = null;

        $scope.cancelado = true;
        $scope.isAdding = false;
        $scope.isEdition = false;

        $scope.popPerfil = {};
        $scope.tabs = [];
        $scope.tabs.push({ title: $scope.res('MSI_TIPOSDEINCIDENTE') });
        $scope.tabs.push({ title: $scope.res('TIPO_POI') });


        /**
         * @method onTabSelected
         * @param {*} tab
         */
        $scope.onTabSelected = function (tab) {

            $scope.isAdding = false;
            $scope.isEdition = false;
            $scope.cancelado = true;

            _.each($scope.tipos, function (tipo) {
                tipo.ativo = false;
            });

            _.each($scope.perfis, function (perfil) {
                perfil.ativo = false;
            });

            if (tab.title == $scope.res('MSI_TIPOSDEINCIDENTE')) {
                if (!$scope.tiposIncidentes) {
                    obterTiposIncidentes();
                } else {
                    $scope.tipos = $scope.tiposIncidentes;
                }
            } else if (tab.title == $scope.res('TIPO_POI')) {

                if (!$scope.tiposPois) {
                    Poi.getTipos().then(function (resultado) {
                        ordenaArray(resultado);
                        $scope.tiposPois = resultado;
                        $scope.tipos = $scope.tiposPois;
                        $scope.tiposPesquisa = resultado;
                    });
                } else {
                    $scope.tipos = $scope.tiposPois;
                }
            }
        }

        $scope.tipos;
        /**
         * @method obterTiposIncidentes
         */
        function obterTiposIncidentes() {
            Base.obter('tipo_incidentes').then(function (resultado_tipo) {
                ordenaArray(resultado_tipo);
                $scope.tiposIncidentes = resultado_tipo;
                $scope.tipos = $scope.tiposIncidentes;
                angular.forEach($scope.tipos, function (tipo) {
                    tipo.ativo = false;
                });
                $scope.tiposPesquisa = resultado_tipo;
            });
        }
        obterTiposIncidentes();

        Base.obter('perfil').then(function (resultado_perfil) {
            $scope.perfis = resultado_perfil;
            angular.forEach($scope.perfis, function (perfil) {
                perfil.ativo = false;
                perfil.nome = (perfil.grupoNome ? perfil.grupoNome : '') + (perfil.grupoNome ? ' - ' : '') + perfil.nome;
            });

            ordenaArray(resultado_perfil);
            $scope.perfisPesquisa = resultado_perfil;
        });

        /**
         * @method ordenaArray
         * @param {*} array 
         */
        function ordenaArray(array) {

            array = array.sort(function (a, b) {
                return a.nome > b.nome ? 1 : a.nome < b.nome ? -1 : 0
            });
        }

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

            if (tipo.ativo) {
                return;
            }

            $scope.tipos = _.map($scope.tipos, function (tipo) {
                tipo.ativo = false;
                return tipo;
            });

            tipo.ativo = true;
            $scope.tipoSelecionado = tipo;
            if ($scope.perfilSelecionado != null) {
                getPopPerfisByTipoAndPerfil($scope.tipoSelecionado, $scope.perfilSelecionado);
            }
        }

        /**
         * @method selecionaPerfil
         * @param {*} perfil 
         */
        function selecionaPerfil(perfil) {

            if (perfil.ativo) {
                return;
            }

            $scope.perfis = _.map($scope.perfis, function (perfil) {
                perfil.ativo = false;
                return perfil;
            });

            perfil.ativo = true;
            $scope.perfilSelecionado = perfil;
            if ($scope.tipoSelecionado != null) {
                getPopPerfisByTipoAndPerfil($scope.tipoSelecionado, $scope.perfilSelecionado);
            }

        }

        /**
         * @method getPopPerfisByTipoAndPerfil
         * @param {*} tipo 
         * @param {*} perfil 
         */
        function getPopPerfisByTipoAndPerfil(tipo, perfil) {
            Base.obter('pop_perfis/getPopPerfisByTipoAndPerfil/' + tipo.id + '/' + perfil.id).then(function (resultado) {
                if (resultado == "") {
                    $scope.isAdding = true;
                    $scope.isEdition = false;
                    $scope.popPerfil = {};
                }
                else {
                    $scope.isAdding = false;
                    $scope.isEdition = true;
                    $scope.popPerfil = resultado;
                }
            });
        }

        /**
         * @method salvar
         */
        function salvar() {
            $scope.popPerfil.tipoEntidadeId = $scope.tipoSelecionado.id;
            $scope.popPerfil.perfilId = $scope.perfilSelecionado.id;
            Base.salvar('pop_perfis', $scope.popPerfil).then(function (popPerfilRetorno) {

                $scope.isAdding = false;
                $scope.isEdition = true;
                $scope.popPerfil = popPerfilRetorno;

                $mdDialog
                    .show($mdDialog.alert()
                        .title($scope.res('COMUM_SUCESSO'))
                        .content($scope.res('POP_SALVO'))
                        .ok($scope.res('COMUM_OK')));

            });
        }

        /**
         * @method remover
         */
        function remover() {
            Base.remover('pop_perfis/' + $scope.popPerfil.POPId + '/' + $scope.popPerfil.perfilId).then(function () {
                $scope.popPerfil.POPId = 0;
                $scope.popPerfil.script = null;
                $scope.isAdding = true;
                $scope.isEdition = false;

                $mdDialog
                    .show($mdDialog.alert()
                        .title($scope.res('COMUM_SUCESSO'))
                        .content($scope.res('POP_REMOVIDO'))
                        .ok($scope.res('COMUM_OK')));

            });
        }

        $scope.textoTipoIncidente;
        /**
         * @method executeSearchTipo
         * 
         * @param {*} textoTipo 
         */
        function executeSearchTipo(textoTipo) {

            if (!textoTipo) {
                $scope.tipos = $scope.tiposPesquisa;
                return;
            }

            $scope.tipos = [];
            var texto = textoTipo.toLowerCase();
            angular.forEach($scope.tiposPesquisa, function (tipoPesquisa) {

                var nome = tipoPesquisa.nome.toLowerCase();
                if (nome.indexOf(texto) !== -1) {
                    $scope.tipos.push(tipoPesquisa);
                }
            });
        }

        $scope.textoPerfil;

        /**
         * @method executeSearchPerfil
         * @param {*} textoPerfil 
         */
        function executeSearchPerfil(textoPerfil) {

            if (!textoPerfil) {
                $scope.perfis = $scope.perfisPesquisa;
                return;
            }

            $scope.perfis = [];
            var texto = textoPerfil.toLowerCase();
            angular.forEach($scope.perfisPesquisa, function (perfilPesquisa) {

                var nome = perfilPesquisa.nome.toLowerCase();
                if (nome.indexOf(texto) !== -1) {
                    $scope.perfis.push(perfilPesquisa);
                }
            });
        }

        /**
         * @method cancelar
         */
        function cancelar() {
            $scope.tipoSelecionado.ativo = false;
            $scope.perfilSelecionado.ativo = false;

            $scope.tipoSelecionado = null;
            $scope.perfilSelecionado = null;

            $scope.cancelado = true;
            $scope.isAdding = false;
            $scope.isEdition = false;

            $scope.popPerfil = {};
        }

        angular.extend($scope, {
            selecionaTipo: selecionaTipo,
            selecionaPerfil: selecionaPerfil,
            salvar: salvar,
            remover: remover,
            executeSearchTipo: executeSearchTipo,
            executeSearchPerfil: executeSearchPerfil,
            cancelar: cancelar
        });

    }

}());