'use strict';
/**
 * Implements directive for qusion.table component.
 */
var qnKartaselector = angular.module('karta.directives.selector', [])
    .directive('qnKartaselector', [
       
        function () {
            return {
                require: '?ngModel',
                replace: true,
                scope: {
                    options: "=qnKartaselector",
                    ui: "&ui",
                    selectedKarte: "=ngModel"
                },
                templateUrl: '/areas/qusion/scripts/templates/karta/selector.html',
                controller: function ($scope, $timeout, KartaService, ClassificationService, LabelService, LabelFactory) {
                      $scope.selectedLables = 0;
                      console.log($scope.options);
                      $scope.alias = "lokacije";


                      $scope.loadKarte = function () {

                          KartaService.query($scope.query)
                              .then(function(data) {
                                  $scope.karte = data.karte;
                              });
                      };
                    
                      $scope.loadLabels = function() {
                          LabelService.top({ alias: $scope.alias }).then(function (data) {
                              $scope.labels = qusion.list.hierarchy(data.labels);

                              $timeout(function () {
                                  $("#treeLabels").treetable({
                                      expandable: true,
                                      column: 1,
                                      expanderTemplate: '<a href="laktasi.net">&nbsp;</a>',
                                      indenterTemplate: '<span class="indenter">&nbsp</span>',
                                      onNodeCollapse: function () {
                                          LabelFactory.removeExpanded(this.id);
                                      },
                                      onNodeExpand: function (e) {
                                          LabelFactory.addExpanded(this.id);
                                      }
                                  });

                                  var expanded = LabelFactory.allExpanded();
                                  for (var i = 0; i < expanded.length; i++) {
                                      $("#treeLabels").treetable("expandNode", expanded[i]);
                                  }
                              }, 100);
                          });
                      };

                      $scope.selectedLabel = function (value) {
                          if (value) {
                              if (value != 0)
                                  $scope.query = {Category: value.Alias};
                              else {
                                  $scope.query = {};
                              }
                              $scope.selectedlabels = [];
                              $scope.loadKarte(value);
                          }
                      };

                    

                      $scope.ui.dialogSelect = {
                          width: 600,
                          create: function () {
                              $(".ui-widget-header").hide();
                              $scope.selectionComplete = false;
                              $scope.dialogSelect = $(this);
                          },
                          close: function () {
                              if ($scope.selectionComplete == false) {
                                
                              }
                          }
                      };

                      $scope.slektujKarte = function () {
                          $scope.dialogSelect.dialog("open");
                      };
                      $scope.loadLabels();
                      $scope.loadKarte();
                },

                  link: function (scope, element, attr, ngModel) {
                      scope.selectedKarte = [];

                      var getValue = function () {
                          var model = scope.$eval(attr.ngModel);
                          return model;
                      };

                      scope.$watch(getValue, function (value) {
                          if (value) {
                              scope.selectedKarte = value;
                          }
                      });

                      scope.doneSelection = function () {
                          scope.selectedKarte = [];

                          for (var i = 0; i < scope.selectedItems.length; i++) {
                              scope.selectedKarte.push(scope.selectedItems[i].ID);
                          }

                          scope.selectionComplete = true;
                          ngModel.$setViewValue(scope.selectedKarte);
                          scope.dialogSelect.dialog("close");
                      };

                      scope.cancelSeletion = function () {
                          scope.dialogSelect.dialog("close");
                      };

                  }
              };
          }
    ]);



