app.controller("destaquesController", function ($scope, $http, $modal,destaquesFactory, dataService,notificationFactory) {


    $scope.lstDestaques = [];
    $scope.selectedDestaque = null;
    $scope.carregarDestaques = function () {
        var getSuccessCallback = function (data, status) {
            $scope.lstDestaques = data;
            if ($scope.lstDestaques.length > 0)
                $scope.selectedDestaque=$scope.lstDestaques[0];

        };

        var errorCallback = function (data, status, headers, config) {
            //notificationFactory.error("Sem Dados");
        };

        destaquesFactory.getDestaques().success(getSuccessCallback).error(errorCallback);
    };
       

    $scope.selDestaque = function (row) {
        $scope.selectedDestaque = row;
    };

    $scope.novoDestaque = function () {
        var dt=new Date();
        var row={
            Id:0, 
            Titulo:'Titulo', 
            Conteudo: '',
            Mostrar: false,
            Data: dataService.devDataHoje()
        }

        var getSuccessCallback = function (data, status) {
            notificationFactory.success(data);
            $scope.carregarDestaques();
        };

        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        destaquesFactory.novoDestaque(row).success(getSuccessCallback).error(errorCallback);
    };

    $scope.atualizarDestaque = function (row) {
        if (row == null)
            return;

        var getSuccessCallback = function (data, status) {
            notificationFactory.success(data);
            //$scope.carregarDestaques();
        };

        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        destaquesFactory.atualizarDestaque(row).success(getSuccessCallback).error(errorCallback);
    };

    $scope.apagarDestaque = function (row) {
        if (row == null)
            return;

        var getSuccessCallback = function (data, status) {
            notificationFactory.success(data);
            $scope.carregarDestaques();
        };

        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        destaquesFactory.deleteDestaque(row).success(getSuccessCallback).error(errorCallback);
    };

    $scope.confirmarRemover = function (row) {

        if (row == null)
            return;

        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalConfirmarApagar.html?v=' + new Date().getTime(),
            controller: 'modalController',
            resolve: {

                items: function () { return row; },

                //  isNew: false
            }
        });

        modalInstance.result.then(function (item) {

            $scope.apagarDestaque(item);

        }, function () {

        });

    };

    $scope.htmlDestaques = "";
    $scope.maisDestaques = false;
    $scope.carregarDestaquesMostrar = function () {
        var getSuccessCallback = function (data, status) {
            $scope.lstDestaques = data;

            for (i=0; i<$scope.lstDestaques.length; i++)
            {
                $scope.htmlDestaques += $scope.lstDestaques[i].Conteudo;
            }
            if ($scope.htmlDestaques.length > 0)
                $scope.maisDestaques = true;
        };

        var errorCallback = function (data, status, headers, config) {
            //notificationFactory.error("Sem Dados");
        };

        destaquesFactory.getDestaquesMostrar().success(getSuccessCallback).error(errorCallback);
    };

   

    $scope.mostrarMaisDestaques = function () {
        
        var w = window.open("", 'Destaques', 'height = 400, width = 400, titlebar=1, left=10, top:10');
        w.document.title = "Destaques";
        $(w.document.body).html($scope.htmlDestaques);
    };

});