app.controller("alertasController", function ($scope, $modal, $rootScope, httpRequestFactory, auxiliosFactory, notificationFactory, ngProgress, dataService) {

    $scope.emExecucao = false;
    

    $scope.selRow = function (row) {

        if (row == null)
            return;

        $scope.selectedRow = row;
    };


    $scope.carregarAlertas = function () {
        var getSuccessCallback = function (data, status) {
            
            $scope.lstItems = data;
            ngProgress.complete();
            $scope.showAlertas = true;
        };

        var errorCallback = function (data, status, headers, config) {
            ngProgress.reset();
        };
                
        httpRequestFactory.getData('../api/alertas').success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };
       

    $scope.mostrarAlertas = function () {
        var row = { lst: $scope.lstItems };
        $scope.showAlertas = false;
        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalAlertas.html',
            controller: 'modalController',
            backdrop: 'static',
            keyboard: false,
            size: 'lg',
            resolve: {
                items: function () { return row; }
            }

        });

        modalInstance.result.then(function (item) {
            location.reload();
        }, function () {
            
        });
    };

    
    $scope.tratarDaNotificacao = function (info) {
        
        $scope.selRow(info);
        if (info.TipoNotificacao == 2) //COMPROMISSO_REGISTAR
        {
            $scope.escolherDadosDoBem();
            return;
        }
        if (info.TipoNotificacao == 3) //CL_PATRIMONIAL_INVALIDA
        {
            $scope.listarClassificacoesInvalidas();
            return;
        }
        if (info.TipoNotificacao == 4)
        {
            $scope.listarSemClassificacoesPat();
            return;
        }
        if (info.TipoNotificacao == 6) {
            //CL_CIBE_INVALIDA
            var w = window.open("../api/mapas/ClassCIBEInvalidas");
            $(w.document.body).html('<p align="center">Por Favor Aguarde. Este Processo pode demorar um pouco...</p>');
            return;
        }
        if (info.TipoNotificacao == 7) //SEM_CONTA_SNC
        {
            var w = window.open("../api/mapas/ContasSNCInvalidas");
            $(w.document.body).html('<p align="center">Por Favor Aguarde. Este Processo pode demorar um pouco...</p>');
            return;
        }
        
        var getSuccessCallback = function (data, status) {
            notificationFactory.success(data);
            $scope.carregarAlertas();
            $scope.emExecucao = false;
            ngProgress.complete();
            
        };

        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
            $scope.emExecucao = false;
            ngProgress.reset();

        };

        $scope.emExecucao = true;
        auxiliosFactory.tratarDaNotificacao(info).success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };
    

    //$scope.escolherDadosDoBem = function () {
    //    var row = {
    //        FkClassificacao: 0,
    //        FkEntidade: 0,
    //        FkEdificio: 0,
    //        FkPiso: 0,
    //        FkSala: 0
    //    };
    //    $scope.showAlertas = false;
    //    var modalInstance = $modal.open(
    //    {

    //        templateUrl: 'views/modalNovoSimpl.html',
    //        controller: 'modalController',
    //        backdrop: 'static',
    //        keyboard: false,
            
    //        resolve: {
    //            items: function () { return row; }
    //        }

    //    });

    //    modalInstance.result.then(function (item) {
            
            
    //        $scope.selectedRow.Dto.Fk_ClassificacaoInventario = item.FkClassificacao;
    //        $scope.selectedRow.Dto.FK_Sala = item.FkSala;
    //        $scope.selectedRow.Dto.FK_Edificio = item.FkEdificio;
    //        $scope.selectedRow.Dto.FK_Piso = item.FkPiso;
            
    //        $scope.trataNotificacao($scope.selectedRow);

    //    }, function () {

    //    });
    //};


    $scope.escolherDadosDoBem = function (row) {
        if (row==null)       
            row = $scope.selectedRow.Dto;
        
        row.Pk_Inventario = 0;
        row.NumInventario = 0;
        //Aquisicao: { Valor: 0, Fk_CodAquisicao: 0, Fk_TipoDespesaAquisicao: 0 },
        row.op = "Novo";
        row.NumRepetir = 0;

        var infoGestor = httpRequestFactory.getDataSync('../api/responsavel/utilizador/idResponsavel');
        if (infoGestor != null) {
            row.Fk_Responsavel = infoGestor.Id;
            row.NomeResponsavel = infoGestor.ValorStr;
        }

        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalNovoCadastroSimpl.html',
            controller: 'modalController',
            size: 'lg',
            resolve: {
                items: function () { return row; }
            }
        });

        modalInstance.result.then(function (item) {
            //console.log($scope.selectedRow.Dto);

            $scope.trataNotificacao($scope.selectedRow);


        }, function () {

        });

    };


    $scope.listarClassificacoesInvalidas = function () {
        var row = {
            lst: $scope.LstClInvalida,
            info: $scope.selectedRow
        };
        $scope.showAlertas = false;
        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalClassificacoesPatInvalidas.html',
            controller: 'modalController',
            backdrop: 'static',
            keyboard: false,
            //size: 'lg',
            resolve: {
                items: function () { return row; }
            }

        });

        modalInstance.result.then(function (item) {
            $scope.carregarAlertas();
        }, function () {

        });
    };

    
    $scope.listarSemClassificacoesPat = function () {
        var row = {
            lst: $scope.LstSemCl,
            info: $scope.selectedRow
        };
        $scope.showAlertas = false;
        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalSemClassPat.html',
            controller: 'modalController',
            backdrop: 'static',
            keyboard: false,
            //size: 'lg',
            resolve: {
                items: function () { return row; }
            }

        });

        modalInstance.result.then(function (item) {
            
            $scope.carregarAlertas();
        }, function () {
            
        });
    };

    $scope.LstSemCl = [];
    $scope.carregarSemClPatrimonial = function () {
        
        var getSuccessCallback = function (data, status) {
            $scope.LstSemCl = data;
            ngProgress.complete();
            $scope.showAlertas = true;
        };

        var errorCallback = function (data, status, headers, config) {
            ngProgress.reset();
        };

        httpRequestFactory.getData('../api/contabilidade/classpat/validar/semclassf').success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };

    $scope.LstClInvalida = [];
    $scope.carregarClPatrimonialInvalida = function () {

        var getSuccessCallback = function (data, status) {
            $scope.LstClInvalida = data;
            ngProgress.complete();
            $scope.showAlertas = true;
        };

        var errorCallback = function (data, status, headers, config) {
            ngProgress.reset();
        };

        httpRequestFactory.getData('../api/contabilidade/classpat/validar/classfinvalida').success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };


    $scope.trataNotificacao = function (info) {
        //console.log(info);
        if (info.TipoNotificacao == 6) {
            //CL_CIBE_INVALIDA
            var w = window.open("../api/mapas/ClassCIBEInvalidas");
            $(w.document.body).html('<p align="center">Por Favor Aguarde. Este Processo pode demorar um pouco...</p>');
            return;
        }

        var getSuccessCallback = function (data, status) {
            notificationFactory.success(data);
            $scope.carregarAlertas();
            $scope.emExecucao = false;
            ngProgress.complete();

        };

        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
            $scope.emExecucao = false;
            ngProgress.reset();

            if (info.TipoNotificacao == 2) //COMPROMISSO_REGISTAR
            {
                
                $scope.escolherDadosDoBem($scope.selectedRow.Dto);
                
            }
        };

        $scope.emExecucao = true;
        auxiliosFactory.tratarDaNotificacao(info).success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };

    $scope.ContaPatInicial = '';
    $scope.ContaPatFinal = '';
    $scope.alteraContasPatrimoniais = function () {

        if ($scope.ContaPatInicial == '') {
            notificationFactory.error("A conta patrimonial inicial não pode ser vazia");
            return;
        }
        if ($scope.ContaPatFinal == '') {
            notificationFactory.error("A conta patrimonial que pretende alterar não pode ser vazia");
            return;
        }

        $scope.emExecucao = true;

        var getSuccessCallback = function (data, status) {
            $scope.emExecucao = false;
            notificationFactory.success("Concluido");
            ngProgress.complete();
            $scope.carregarClPatrimonialInvalida();
        };

        var errorCallback = function (data, status, headers, config) {
            $scope.emExecucao = false;
            notificationFactory.error(data);
            ngProgress.reset();
        };

        var dados = {};
        httpRequestFactory.postData('../api/auxilio/alteraContaPatrimonial/' + $scope.ContaPatInicial + '/' + $scope.ContaPatFinal, dados).success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };

    $scope.ignoraAlerta = function (info) {

        $scope.emExecucao = true;

        var getSuccessCallback = function (data, status) {
            $scope.carregarAlertas();
            $scope.emExecucao = false;
            notificationFactory.success(data);
            ngProgress.complete();
        };

        var errorCallback = function (data, status, headers, config) {
            $scope.emExecucao = false;
            notificationFactory.error(data);
            ngProgress.reset();
        };
                
        httpRequestFactory.postData('../api/alertas/ignora', info).success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };

    $scope.reativarAlertas = function () {

        $scope.emExecucao = true;

        var getSuccessCallback = function (data, status) {
            $scope.carregarAlertas();
            $scope.emExecucao = false;
            notificationFactory.success(data);
            ngProgress.complete();
        };

        var errorCallback = function (data, status, headers, config) {
            $scope.emExecucao = false;
            notificationFactory.error(data);
            ngProgress.reset();
        };

        var dados = {};
        httpRequestFactory.postData('../api/alertas/reativar', dados).success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };

    


});