'use strict';

var ContentEditController = angular.module('contents.controllers.edit', [
    'system'
])
    .controller('ContentEditController', [
        '$scope',
        '$route',
        '$location',
        '$filter',
        'System',
        'ContentService',
        function ($scope, $route, $location, $filter, System, ContentService) {
            var id = $route.current.params["id"];      

            $(".tooltip").tooltip();

            $scope.refresh = function() {
                ContentService.getEdit(id)
                    .then(function (data) {
                        $scope.content = data;
                    });
            };

            $scope.update = function () {

                ContentService.update($scope.content)
                    .then(function (response) {
                        if (response.Errors) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource("contents.created_error"));
                        } else {
                            $location.url('/pages');
                            System.Message.success($scope.$resource("contents.updated_successfully"));
                        }
                    });
            };

            $scope.back = function () {
                $location.url('/pages');
            };

            $scope.refresh();
        }
    ]);