'use strict';

/** 
 * @controller NewsletterEditController
 * @property scope 
 * @property route
 * @property location
 * @property filter
 * @property System
 * @property NewslettersService
 * @property Resource
 */

angular.module('newsletters.controllers.newsletteredit', [
    'system'
])
    .controller('NewsletterEditController', [
        '$scope',
        '$route',
        '$location',
        '$filter',
        'System',
        'NewslettersService',
        function ($scope, $route, $location, $filter, System, NewslettersService) {
            var id = $route.current.params["id"];
            $(".tooltip").tooltip();


            /**
            * @method init
            * @description This load all initial data.
            */
            $scope.init = function () {   
                NewslettersService.get(id)
                    .then(function (data) {
                        $scope.newsletter = data;
                    });
            };


            /**
            * @method update
            * @description NewslettersService updates Newsletter data after successful edit
            */
            $scope.update = function () {
                NewslettersService.update($scope.newsletter)
                    .then(function (response) {
                        if (response.Errors) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource('utility.changeNotSave'));
                        } else {
                            System.Message.success($scope.$resource('newsletters.NewsletterUpdated'));
                            $location.url('/newsletters');
                        }
                    });
            };

            /**
            * @method view
            * @description Set current view on page.
            */
            $scope.setView = function (view) {
                $scope.view = view;
            };

            /**
            *@method changeImge
            *@descritoion Open dialog for changing newsletter profile image.
            */
            $scope.changeImge = function () {
                $scope.uploadDialog.dialog("open");
            };

         

            /**
            *@method cancelEdit Close dialog and clear edit dialog.
            */
            $scope.cancelEdit = function () {
                $scope.upload.clear();
                $scope.uploadDialog.dialog("close");
            };

            /**
            * @action back returns to location /newsletter
            */
            $scope.back = function () {
                $location.url('/newsletters');
            };

           

            $scope.init();
        }
    ]);