'use strict';
/**
 * @controller DocumentUploadController
 * @description Manipulations with documents.
 */
angular.module('documents.controllers.upload', [
    'system'
])
    .controller('DocumentUploadController', [
        '$scope',
        '$route',
        '$location',
        '$timeout',
        'System',
        'DocumentService',
        'OdjeljenjeService',
        'LibraryService',
        function($scope, $route, $location, $timeout, System, DocumentService,OdjeljenjeService,LibraryService) {

            $(".tooltip").tooltip();
            var libid = $route.current.params["id"] || "";
            $scope.libraryId =  $route.current.params["id"];
            /**
            *@property addToExistig
            *@descriptoin Indicates whether to create a new library or to add documents to an existing library.
            */
            if (libid == "") {
                $scope.addToExistig = $scope.addToExistig == true;
            }
        
            /*Inicijacija radnika*/
            $scope.init = function () {
                LibraryService.get(libid).then(function (data) {
                    $scope.library = data;
                LibraryService.getRadnici($scope.library.OdjelID).then(function (data) {
                    $scope.radnici = data;
                });
                });
               
            };
            $scope.query = $route.current.params || {};
            /**
            * @property libraryTitle
            * @description Title for new library. Default is date in the moment of creating it.
            */
            var now = new Date();
            var month = now.getMonth() + 1;
            $scope.libraryTitle = now.getDate() + '. ' + month + '. ' + now.getFullYear();


            /**
             * @method refreshLibraries
             * @description Loads libraries for the user.
             */
            $scope.refreshLibraries = function () {
               
                DocumentService.libraries().then(function(data) {
                    $scope.libraries = data;
                    for (var i = 0; i < $scope.libraries.length; i++) {
                            if ($scope.query.id == $scope.libraries[i].ID)
                                $scope.library = $scope.libraries[i];
                        }
                    $scope.Title = function (libraries) {
                        return libraries.Title != null;
                    };
                });

                OdjeljenjeService.query().then(function (data) {
                    $scope.odjeljenja = data.odjeljenja;
                });

                OdjeljenjeService.queryOdjeli().then(function (data) {
                    $scope.odjeli = data.odjeli;
                    //$scope.pager = System.Pager.create({ page: $scope.query.page, total: data.total });
                });

                LibraryService.querytype($scope.query).then(function (data) {
                    $scope.librarytypes = data.librarytype;
                   // $scope.pager = System.Pager.create({ page: $scope.query.page, total: data.total });
                });

            };

            /**
            *@property upload
            *@description upload widget will bee initialized here.
            */
            $scope.upload = $('#fileupload').upload({
                extensions: ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'ppt', 'pptx', 'zip']
            }).data("qusion-upload");

            /*Gleda u selektovani tip dokumenta*/
            $scope.$watch('SelectedTip', function (value) {
                if (value) {
                    $scope.TipDokumenta =  value ;
                };
            });
            /*Gleda u selekotvano odjeljenje*/
                $scope.$watch('SelectedOdjel', function (value) {
                    if (value) {
                        $scope.Odjel = value ;
                    };
                });
            /**
            *@method startUpload
            *@description Starts upload process.
            */
             
            $scope.startUpload = function() {
                if ($scope.TipDokumenta != null, $scope.Odjel != null) {
                    var library = {
                        Title: $scope.libraryTitle,
                        Type: "Document",
                        OdjelID: $scope.Odjel,
                        LibraryTypeID: $scope.TipDokumenta
                    };
                    DocumentService.create(library).then(function (value) {
                        $scope.libraryId = value.ID;
                        $scope.libraries.push(value);
                        $timeout(function() {
                            $scope.upload.send();
                        }, 100);
                    });
                } else {
                    $scope.upload.send();
                }
            };


            /**
           * @action goToUpload
           * @description returns to location /documents/upload
           */
            $scope.addToLibrary = function () {
                   $location.url('/documents/upload');
            };

            /**
            *@action done
            *@description Redirect to documents library where documents were added.
            */
            $scope.done = function() {
                if ($scope.libraryId === 'undefined') {
                    $location.url('/documents/' + $scope.libraryId);
                } else {
                    $location.url('/documents/');
                }
            };
            $scope.init();
            $scope.refreshLibraries();
        }
    ]);