'use strict';

var EventDetailsController = angular.module('events.controllers.event', [
    'system'
])
    .controller('EventDetailsController', [
        '$scope',
        '$location',
        '$route',
        '$window',
        'System',
        'EventService',
        'OdjeljenjeService',
        'ClassificationService',
        function ($scope, $location, $route, $window, System, EventService, OdjeljenjeService, ClassificationService) {
            var id = $route.current.params["id"];
            $scope.radnik = { EventID: id };

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

            $scope.refresh = function () {
                EventService.get(id).then(function (event) {
                    $scope.event = event;
                });
            };

            $scope.load = function() {
                OdjeljenjeService.allRadnici().then(function (data) {
                    $scope.radnici = data;
                });

                ClassificationService.getByAlias("team").then(function(data) {
                    $scope.teams = data.Labels;
                });
            };

            $scope.addRadnik = function() {
                $scope.dialogAddRadnik.dialog("open");
            };
            
            $scope.addTeam = function () {
                $scope.dialogAddTeam.dialog("open");
            };

            $scope.createRadnik = function() {
                EventService.createRadnik($scope.radnik).then(function (response) {
                    if (response.Errors) {
                        $scope.errors = response.Errors;
                        System.Message.error($scope.$resource('utility.changeNotSave'));
                    } else {
                        $scope.refresh();
                        $scope.dialogAddRadnik.dialog("close");
                        System.Message.success($scope.$resource('events.UserCreated'));
                    }
                });
            };
            
            $scope.createTeam = function () {
                EventService.createTeam($scope.radnik).then(function (response) {
                    if (response.Errors) {
                        $scope.errors = response.Errors;
                        System.Message.error($scope.$resource('utility.changeNotSave'));
                    } else {
                        $scope.refresh();
                        $scope.dialogAddTeam.dialog("close");
                        System.Message.success($scope.$resource('events.UserCreated'));
                    }
                });
            };

            $scope.dialogAddRadnik = { };

            $scope.ui.dialogAddRadnik = {
                width: 600,
                create: function() {
                    $scope.dialogAddRadnik = $(this);
                }
            };
            
            $scope.dialogAddTeam = {};

            $scope.ui.dialogAddTeam = {
                width: 600,
                create: function () {
                    $scope.dialogAddTeam = $(this);
                }
            };
            
            /**
            *@method cancelAddTeam Close dialog and clears dialog data.
            */
            $scope.cancelAddTeam = function () {
                $scope.load();
                $scope.dialogAddTeam.dialog("close");
            };
            
            /**
            *@method cancelAddRadnik Close dialog and clears dialog data.
            */
            $scope.cancelAddRadnik = function () {
                $scope.load();
                $scope.dialogAddRadnik.dialog("close");
            };
            
            $scope.changeRadnik = function (radnik) {
                $scope.radnik = radnik;
                $scope.dialogChangeRadnik.dialog("open");
            };

            $scope.updateRadnik = function () {
                EventService.updateRadnik($scope.radnik).then(function (response) {
                    if (response.Errors) {
                        $scope.errors = response.Errors;
                        System.Message.error($scope.$resource('utility.changeNotSave'));
                    } else {
                        $scope.refresh();
                        $scope.dialogChangeRadnik.dialog("close");
                        System.Message.success($scope.$resource('events.UserUpdated'));
                    }
                    
                });
            };

            $scope.dialogChangeRadnik = {};

            $scope.ui.dialogChangeRadnik = {
                width: 600,
                create: function () {
                    $scope.dialogChangeRadnik = $(this);
                }
            };
            
            /**
            *@method cancelChangeRadnik Close dialog and clears dialog data.
            */
            $scope.cancelChangeRadnik = function () {
                $scope.load();
                $scope.dialogChangeRadnik.dialog("close");
            };

            $scope.removeRadnik = function (eventRadnik, index) {
                if ($window.confirm($scope.$resource('utility.areYouShure'))) {
                    EventService.removeRadnik({ id: eventRadnik.Id }).then(function() {
                        $scope.event.EventRadnik.splice(index, 1);
                        $scope.refresh();
                        System.Message.success($scope.$resource('utility.deleted'));
                    });
                }

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