(function () {
    'use strict';
    angular
        .module('controleApp')
        .controller('alertasController', alertasController);

    alertasController.$inject = ['$scope', 'Consulta', 'Comando', 'paginationService'];

    function alertasController($scope, Consulta, Comando, paginationService) {
        /*jshint validthis:true*/
        this.toggleModal = {};
        this.notificationMessages = {};

        $scope.take = 10;
        $scope.skip = 0;
        $scope.type = "Data";
        $scope.orderAsc = false;

        $scope.OrderBy = OrderBy;
        $scope.ExcluirAlerta = ExcluirAlerta;
        paginationService.Consultar = Consultar;

        function Consultar(take, skip) {
            $(".loading-div").fadeIn();
            $scope.take = take;
            $scope.skip = skip;
            var obj = {
                take: take,
                skip: skip,
                typeOrder: $scope.type,
                orderAsc: $scope.orderAsc
            };
            Consulta.ConsultaAlertas(obj, successCallBack, errorCallBack);
        }

        function successCallBack(data) {
            $scope.Alertas = data.data.ListaAlertas;
            paginationService.Total = data.data.QtdAlertas;
            paginationService.AtualizarRodapePaginacao();
            $(".loading-div").fadeOut();

            if (localStorage.MensagemSucesso) {
                $scope.showSuccess(localStorage.MensagemSucesso);
                localStorage.removeItem("MensagemSucesso");
            }
        }

        function errorCallBack(error) {
            $scope.showError(error.statusText);
            $(".loading-div").fadeOut();
        }

        function OrderBy(type) {
            $(".loading-div").fadeIn();
            $scope.type = type;
            $scope.orderAsc = !$scope.orderAsc;
            var obj = {
                take: $scope.take,
                skip: $scope.skip,
                typeOrder: $scope.type,
                orderAsc: $scope.orderAsc
            };
            Consulta.ConsultaAlertas(obj, successCallBack, errorCallBack);
        }

        function ExcluirAlerta() {
            $(".loading-div").fadeIn();
            var obj = {codigo: $scope.alertasCtrl.toggleModal.CodigoAlerta};
            $scope.alertasCtrl.toggleModal.hideModal();

            Comando.ExcluirAlerta(obj, function (data) {
                if (data.data.IsSucesso) {
                    $scope.showSuccess(data.data.Mensagem);
                    paginationService.Paginar();
                } else {
                    $scope.showError(data.data.Mensagem);
                    $(".loading-div").fadeOut();
                }
            }, function (error) {
                $scope.showError(error.statusText);
                $(".loading-div").fadeOut();
            });

        }
    }
})();
