'use strict';

/** 
 * @controller ArticleCreateController
 * @property scope 
 * @property location
 * @property filter
 * @property System
 * @property ArticleService
 * @property Resource
 */

var ArticleCreateController = angular.module('articles.controllers.create', [
    'system'
])
    .controller('ArticleCreateController', [
        '$scope',
        '$location',
        '$filter',
        'System',
        'ArticleService',
        'ClassificationService',
         'ImageService',
         'DocumentService',
        function ($scope, $location, $filter, System, ArticleService, ClassificationService) {
            System.Page.title("Articles - Create new article");
            $scope.classifications = { category: [] };
            $scope.treeObject = {};
            $scope.article = {};

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

            $scope.ImageSelectorOpiton = {
                multipleSeletcion: false
            };

            $scope.LibrarySelectorOpiton = {
                multipleSeletcion: false
            };

            $scope.DocumentSelectorOpiton = {
                multipleSeletcion: false
            };

            $scope.KartaSelectorOpiton = {
                multipleSeletcion: false
            };

            ClassificationService.getByAlias('tag').then(function (response) {
                $scope.tags = response;
            });

            ClassificationService.getByAlias('vijesti').then(function (response) {
                $scope.categories = qusion.list.jsTree(response.Labels, "ID", "Name");
            });

            ClassificationService.getByAlias('lokacije').then(function (response) {
                $scope.zcategories = qusion.list.jsTree(response.Labels, "ID", "Name");
            });
           
            /**
            * @method create
            * @description ArticleService creates new article
            */
            $scope.create = function () {
                $scope.article.Labels = [];

                var keys = (function (obj) {
                    var k = [], x;
                    for (x in obj) {
                        k.push(x);
                    }
                    return k;
                })($scope.classifications);

                for (var i = 0; i < keys.length; i++) {
                    for (var j = 0; j < $scope.classifications[keys[i]].length; j++) {
                        $scope.article.Labels.push($scope.classifications[keys[i]][j]);
                    }
                }

                ArticleService.create($scope.article)
                    .then(function (response) {
                        if (response.Errors) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource("utility.changeNotSave"));
                        } else {
                            $location.url('/articles');
                            System.Message.success($scope.$resource("articles.ArticleSaved"));
                        }
                    });
            };

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


            $scope.treeOptions = {
                selectMode: 2,
                dialogTitle: $scope.$resource('articles.select_dialog_categories')
            };

        }
    ]);