'use strict';

var DocumentMainPoKlasifikacijamaController  = angular.module('documents.controllers.poklasifikacijama', [
    'system'
])
    .controller('DocumentMainPoKlasifikacijamaController', [
        '$scope',
        '$route',
        '$location',
        'System',
        'DocumentService',
        'LibraryService',
        'OdjeljenjeService',
        'ClassificationService',
        function ($scope,$route, $location, System, DocumentService, LibraryService,OdjeljenjeService, ClassificationService) {
            $scope.Status = "Aktivan";
            $scope.query = $route.current.params || {};
            var id = $route.current.params["LibraryID"] || {}
            
            $scope.isCurrentCategory = function (library) {
                return category.ID == $route.current.params["Category"];
            };

            $scope.reset = function () {
                $location.search(($scope.query = {}));
            };

            $scope.switchSidebar = function (view) {
                $scope.sidebarView = view;
            };

            $scope.isSearch = function() {
                return Object.keys($scope.query).length !== 0;
            };

            $scope.documents = [];

            $scope.refresh = function () {
                DocumentService.getSettings()
                  .then(function (data) {
                      $scope.settings = data;
                  });
               
                  DocumentService.query($scope.query)
                  .then(function (data) {
                      $scope.documents = data.documents;
                      $scope.predicateNaziv = 'Title';
                      $scope.pager = System.Pager.create({ page: $scope.query.page, size: $scope.settings.GeneralPageSize, total: data.total });
                  });

                LibraryService.get(id).then(function (data) {
                    $scope.library = data;
                });

                System.Page.title($scope.$resource("documents.poklasifikacijama"));
            };

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

            $scope.search = function (q, reset) {
                reset ? $scope.query = q : angular.extend($scope.query, q);
                $location.search($scope.query);
                $scope.refresh();
            };


            $scope.all = function () {
                $location.url('/documentsk');
            };

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

            $scope.PoTipu = function () {
                $location.url('/documentst');
            };

            $scope.PoOdjeljenjima = function () {
                $location.url('/documentso');
            };
     
            $scope.$watch("pager.page", function() {
                if (!$scope.pager || !$scope.pager.page) return;
                $scope.query.page = $scope.pager.page;
                $scope.search($scope.query, false);
            });

            
            $scope.Ext = function (value) {
                return System.Ext.icon(value);
            };

            $scope.refresh();
            $scope.getClassifications();
        }
    ]);