'use strict';

var EventEditController =  angular.module('events.controllers.edit', [
    'system'
])
    .controller('EventEditController', [
        '$scope',
        '$route',
        '$location',
        '$filter',
        'System',
        'EventService',
        'ClassificationService',
        function ($scope, $route, $location, $filter, System, EventService, ClassificationService) {
            var id = $route.current.params["id"];
            $scope.Labels = {};
            $scope.query = { Alias: 'najave' }

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

            $scope.refresh = function() {
                EventService.getEdit(id)
                    .then(function(data) {
                        $scope.event = data;
                        for (var i = 0; i < $scope.event.Labels.length; i++) {
                            if ($scope.Labels[data.Labels[i].Classification] == undefined) {
                                $scope.Labels[data.Labels[i].Classification] = new Array();
                            }
                            $scope.Labels[data.Labels[i].Classification].push(data.Labels[i].ID);
                        }
                    });
                
                ClassificationService.query($scope.query)
                    .then(function (data) {
                        $scope.classifications = [];
                        for (var i = 0; i < data.classifications.length; i++) {
                            $scope.classifications[i] = {
                                Alias: data.classifications[i].Alias,
                                Labels: []
                            };
                            $scope.classifications[i]['Labels'] = qusion.list.jsTree(data.classifications[i].Labels, "ID", "Name");
                        }
                    });
            };
            
            $scope.treeOptions = {
                selectMode: 2,
                dialogTitle: $scope.$resource('events.select_labels')
            };
            

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

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

                }

                EventService.update($scope.event)
                    .then(function (response) {
                        if (response.Errors) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource('utility.changeNotSave'));
                        } else {
                            $location.url('/events');
                            System.Message.success($scope.$resource('events.EventUpdated'));
                        }
                    });
            };

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

            $scope.refresh();
        }
    ]);