'use strict';

/** 
 * @controller QuestionEditController
 * @property scope 
 * @property route
 * @property location
 * @property System
 * @property PollService
 * @property Resource
 */

var QuestionEditController = angular.module('polls.controllers.questionedit', [
    'system'
])
    .controller('QuestionEditController', [
        '$scope',
        '$location',
        '$route',
        'System',
        'PollService',
        function ($scope, $location, $route, System, PollService, ClassificationService) { 
            var id = $route.current.params["id"];
            $scope.query = { };           
            $(".tooltip").tooltip();

            $scope.refresh = function() {
                PollService.questionForEdit(id).then(function (data) {
                    $scope.question = data;
                });
            };
            

            /**
            * @action back returns to location /polls/PollID
            */
            $scope.back = function () {
                $location.url('/polls/' + $scope.question.PollID);
            };

            /**
            * @method SaveAndPublish
            * @description updates existing blog question
            */
            $scope.SaveAndPublish = function () {
                PollService.updateQuestion($scope.question).then(function (response) {
                    if (response.Errors) {
                        $scope.errors = response.Errors;
                        System.Message.error($scope.$resource('polls.QuestionCreatedError'));
                    } else {
                        $location.url('/polls/' + $scope.question.PollID);
                        System.Message.success($scope.$resource('polls.QuestionUpdated'));
                    }
                });
            };

            $scope.refresh();
        }
    ]);