'use strict';

/** 
 * @controller NewsletterMainController
 * @property scope 
 * @property location
 * @property route
 * @property System
 * @property NewslettersService
 */

var NewsletterDetailsController =angular.module('newsletters.controllers.newsletters', [
    'system'
])
    .controller('NewsletterDetailsController', [
        '$scope',
        '$location',
        '$route',
        'System',
        'NewslettersService',
        function ($scope, $location, $route, System, NewslettersService) {
            $scope.Status = $route.current.params["Status"] || "Aktivan";
            $scope.selection = {};
            var id = $route.current.params["id"] || "";
            $scope.query = $route.current.params["id"] || "";
            $scope.sidebarView = 'basic';
            $(".tooltip").tooltip();

    
            /**
            * @method refresh
            * @description refreshes page and loads all initial data
            */
            $scope.refresh = function () {
                NewslettersService.query().then(function (data) {
                    $scope.newsletters = data.newsletters;
                });

            };


            /*Dodatne informacije*/

            $scope.setView = function (view) {
                $scope.view = view;
            };

            /**
            * @action novi newsletter
            */
            $scope.newNewsletter = function () {
                    $location.url('/newsletters/create');
            };


          
            /**
           * @method search
           * @description searches newsletters 
           */
            $scope.search = function (q, reset) {
                reset ? $scope.Status = q : angular.extend($scope.Status, q);
                $location.search($scope.Status);
                $scope.refresh();
            };

            /**
            * @method reset
            * @description resets search to its initial value
            */
            $scope.reset = function () {
                $location.search(($scope.Status = {}));
            };


           
            $scope.switchSidebar = function (view) {
                $scope.sidebarView = view;
            };

            /**
            * edit newslettera
            */
            $scope.edit = function (newsletter) {
                $location.url('/newsletters/' + newsletter.ID  +  '/edit');
            };

         
            /**
            * @method remove
            * @param id
            * @description Removes newsletter by its id
            */
            $scope.remove = function (newsletter, index) {
                var answer = confirm($scope.$resource("utility.areYouShure"));
                if (answer) {
                    NewslettersService.remove({ id: newsletter.ID }).then(function (response) {
                        if (!response.Errors) {
                            $scope.newsletters.splice(index, 1);
                            $scope.refresh();
                            System.Message.success($scope.$resource('utility.deleted'));
                            //$location.url('/radnici/' + radnik.OdjelID);
                        } else {
                            System.Message.error($scope.$resource('utility.changeNotSave'));
                        }
                    });
                }
            };
            


            /**
           * @method setLockStatus
           * @param id
           * @description Set newsletter parametat Locket to true or false. Inidcates whether the newsletter can access the system.
           */
            $scope.setLock = function (newsletter) {
                NewslettersService.setLockStatus(newsletter.ID).then(function (data) {
                    newsletter.Status= data.Status;
                    if (data.Status.Name == "Aktivan") {
                        System.Message.success($scope.$resource("users.locked"));
                    } else {
                        System.Message.success($scope.$resource("users.unlocked"));
                    }
                });
            };


            
            
            $scope.$watch("pager.page", function () {
                if (!$scope.pager || !$scope.pager.page) return;
                $scope.query.page = $scope.pager.page;
                $scope.search($scope.query, false);
            });

            $scope.refresh();

        }
    ]);