/**
 * Created by mhashendre on 11/4/17.
 */
angular.module('app.controllers')
  .controller('RejectsSizeWiseCtrl', ["$scope", "$log", "issuesCache", "$ionicSlideBoxDelegate", "eventProtocol", "$state", "propertyCache", "ModalViews", "cacheViews", "$ionicPopup", "dataPersistenceService", "undoOperation", "sparkDataService", "auditProtocol", "$rootScope", "$ionicLoading", "$timeout",
    function ($scope, $log, issuesCache, $ionicSlideBoxDelegate, eventProtocol, $state, propertyCache, ModalViews, cacheViews, $ionicPopup, dataPersistenceService, undoOperation, sparkDataService, auditProtocol, $rootScope, $ionicLoading, $timeout) {

      var _self = this;

      _self.sizelists = []; //issues for selected operation
      _self.rejectslist = [];//final issue list to save
      _self.lastIndex = 0;
      $scope.currentPage = 0;
      $scope.currentDivSize = 0;
      $scope.pageSize = 12;
      $scope.divSize = 2;
      $scope.isSingleSelectOps = true;
      $scope.isSingleSelectRejects = false;

      _self.disableDropDowns = false;


      _self.filter_data = {
        style: "",
        key1: "", // considers as the li
        key2: "",// considers as the so
        buyerId: "",
        categoryId: "",
        sizeId: ""
      };

      _self.planedStyles = [];
      _self.pos = [];
      _self.colours = [];
      _self.buyers = [];
      _self.sizeCats = [];
      _self.sizeList = [];
      _self.pcs = 0;
      _self.selectedOperation = "";
      _self.firstButtonVal = 10;
      _self.secondButtonVal = 15;
      _self.thirdButtonVal = 20;
      _self.fourthButtonVal = 25;

      _self.bundlesizes = [];
      _self.eventType = "";

      _self.focusgranted = false;

      var selectedSizeId = {};
      var selectedOperations = [];

      init = function () {

        cordova.plugins.Keyboard.disableScroll(false);

        angular.forEach(propertyCache.day_start, function (value, key) {
          if (_self.filter_data.hasOwnProperty(key)) {
            if (value != "") {
              _self.filter_data[key] = value;
            }
          }
        });

        angular.forEach(propertyCache.optional_day_start_data, function (value, key) {
          if (_self.filter_data.hasOwnProperty(key)) {
            if (value != "") {
              _self.filter_data[key] = value;
            }
          }
        });

        _self.sizelists = JSON.parse(localStorage.getItem("sizeList"));
        if (_self.sizelists) {
          for (var i = 0; i < _self.sizelists.length; i++) {
            _self.obj = _self.sizelists[i];
            if ($.inArray(_self.obj.buyerId, _self.buyers) == -1) {
              _self.buyers.push(_self.obj.buyerId);
            }
          }
        }

        _self.planedStyles = propertyCache.startedStyles.styleNames;
        // _self.pos = propertyCache.pos.poNames;
        // _self.colours = propertyCache.colours.colorCodes;

        // _self.selectedRejects = [];
        $scope.style = propertyCache.day_start.style;
        // $scope.key2 = propertyCache.day_start.key2;
        // $scope.key1 = propertyCache.day_start.key1;

        _self.bundlesizes = propertyCache.factoryConfigs.default_bundle_sizes.split(',');


        _self.filter_data.sizeId = "";

        self.sizes = [];
        self.sizes.length = 0;
        for (var i = 0; i < _self.sizelists.length; i++) {
          _self.obj = _self.sizelists[i];
          if (_self.obj.categoryId == _self.filter_data.categoryId && _self.obj.buyerId == _self.filter_data.buyerId)
            if ($.inArray(_self.obj.categoryId, _self.sizecats) == -1) {

              // self.dataList = JSON.parse(_self.obj.dataList);
              for (var ii = 0; ii < _self.obj.dataList.length; ii++) {
                self.singleSizeObj = _self.obj.dataList[ii];
                if ($.inArray(self.singleSizeObj.sizeDetail.sizeName, self.sizes) == -1) {
                  var size = {};
                  size.name = self.singleSizeObj.sizeDetail.sizeName;
                  if (self.singleSizeObj.sizeDetail.sizeName == _self.filter_data.sizeId) {
                    size.selected = true;
                  } else {
                    size.selected = false;
                  }
                  self.sizes.push(size);
                  // self.sizes.push(self.singleSizeObj.sizeDetail.sizeName);
                }
              }

            }
        }
        _self.sizeList.length = 0;
        _self.sizeList = self.sizes;

        _self.disableDropDowns = false;

        if(propertyCache.triggeredEvent == "ftt"){
          _self.disableDropDowns = true;
        }

      }


      //init cache for second time
      issuesCache.init();

      _self.goTo = function (name) {
        if (ModalViews.getView(name))
          ModalViews.getView(name).show();
      }

      //watch for add operations changes
      //$scope.$watch(function () {
      //  return issuesCache.selectedOperations;
      //}, function (newValue) {
      //  _self.operations = issuesCache.getSelectedOperations();
      //  $scope.$safeApply($scope);
      //});

      _self.showStylePopup = function () {
        _self.styleBefore = _self.filter_data.style;
        $scope.selectedStyle = {};
        // An elaborate, custom popup

        if(!_self.disableDropDowns) {
          var planPopup = $ionicPopup.show({
            template: '<ion-list><ion-radio ng-repeat="style in rejectssizewisectrl.planedStyles" ng-model="rejectssizewisectrl.filter_data.style" ng-value=" \'{{style}}\' "> ' +
            '<div class="item item-text-wrap">{{style}}</div> </ion-radio> </ion-list>',
            title: 'Select a Style',
            cssClass: "custom-popup",
            scope: $scope,
            buttons: [
              {text: 'Cancel'},
              {
                text: '<b>Done</b>',
                type: 'button-positive',
                onTap: function (e) {
                  if (!_self.filter_data.style) {
                    //don't allow the user to close unless he enters wifi password
                    e.preventDefault();
                  } else {
                    _self.filter_data.key2 = "";
                    _self.filter_data.key1 = "";
                    dataPersistenceService.selectColoursPos(_self.filter_data.style).then(function (result) {
                      console.log("RESULTTTTTTTTTTTTTT -----> ", result);
                      self.pos = [];
                      self.colours = [];
                      for (var i = 0; i < result.rows.length; i++) {
                        if ($.inArray(result.rows.item(i).Po, self.sos) == -1) {
                          if (!self.pos.contains(result.rows.item(i).Po)) {
                            self.pos.push(result.rows.item(i).Po);
                          }
                        }
                        if ($.inArray(result.rows.item(i).Colour, self.lis) == -1) {
                          if (!self.colours.contains(result.rows.item(i).Colour)) {
                            self.colours.push(result.rows.item(i).Colour);
                          }
                        }
                      }
                      _self.pos.length = 0;
                      _self.pos = self.pos;
                      _self.colours.length = 0;
                      _self.colours = self.colours;


                    });
                    return _self.filter_data.style;
                  }
                }
              }
            ]
          });

          planPopup.then(function (res) {
            if (res) {
              _self.filter_data.style = res;
              //select silhouette here
              for (var key in _self.siluhouttes) {
                var nameArray = _self.siluhouttes[key].split("_");
                if (nameArray.length > 1) {
                  var style = nameArray[0];
                  if (_self.filter_data.style == style) {
                    _self.filter_data.siluhoutteid = _self.siluhouttes[key];
                  }
                }
              }
            } else {
              _self.filter_data.style = _self.styleBefore;
            }
          });
        }else{
          var alertPopup = $ionicPopup.alert({
            title: $filter('translate')('Warning'),
            template: '<div class="nf-center nf-valign-wrapper">' +
            '<ion-spinner icon="ripple" class="nf-text-white"></ion-spinner>' +
            '<span>{{"ChangingStyleIsNotAllowed" | translate}}</span></div>'
          });
        }

      };

      _self.showPoPopup = function () {
        _self.soBefore = _self.filter_data.key2;
        $scope.selectedPo = {};
        // An elaborate, custom popup
        var planPopup = $ionicPopup.show({
          template: '<ion-list><ion-radio ng-repeat="key2 in rejectssizewisectrl.pos" ng-model="rejectssizewisectrl.filter_data.key2" ng-value=" \'{{key2}}\' "> ' +
          '<div class="item item-text-wrap">{{key2}}</div> </ion-radio> </ion-list>',
          title: 'Select a PO no',
          cssClass: "custom-popup",
          scope: $scope,
          buttons: [
            {text: 'Cancel'},
            {
              text: '<b>Done</b>',
              type: 'button-positive',
              onTap: function (e) {
                if (!_self.filter_data.key2) {
                  //don't allow the user to close unless he enters wifi password
                  e.preventDefault();
                } else {
                  _self.filter_data.key1 = "";
                  //_self.cached_params.key2 = "";
                  dataPersistenceService.selectColoursStyles(_self.filter_data.key2).then(function (result) {
                    console.log("RESULTTTTTTTTTTTTTT -----> ", result);
                    self.styles = [];
                    self.colours = [];
                    for (var i = 0; i < result.rows.length; i++) {
                      if ($.inArray(result.rows.item(i).Colour, self.colours) == -1) {
                        if (!self.colours.contains(result.rows.item(i).Colour)) {
                          self.colours.push(result.rows.item(i).Colour);
                        }
                      }
                      if ($.inArray(result.rows.item(i).Style, self.styles) == -1) {
                        if (!self.styles.contains(result.rows.item(i).Style)) {
                          self.styles.push(result.rows.item(i).Style);
                        }
                      }
                    }
                    _self.styles.length = 0;
                    _self.styles = self.styles;
                    _self.colours.length = 0;
                    _self.colours = self.colours;


                  });
                  return _self.filter_data.key2;
                }
              }
            }
          ]
        });

        planPopup.then(function (res) {
          if (res) {
            _self.filter_data.key2 = res;
          } else {
            _self.filter_data.key2 = _self.soBefore;
          }
        });
      };

      _self.showColourPopup = function () {
        _self.liBefore = _self.filter_data.key1;
        $scope.selectedColour = {};
        // An elaborate, custom popup
        var planPopup = $ionicPopup.show({
          template: '<ion-list><ion-radio ng-repeat="key1 in rejectssizewisectrl.colours" ng-model="rejectssizewisectrl.filter_data.key1" ng-value=" \'{{key1}}\' "> ' +
          '<div class="item item-text-wrap">{{key1}}</div> </ion-radio> </ion-list>',
          title: _self.title,
          cssClass: "custom-popup",
          scope: $scope,
          buttons: [
            {text: 'Cancel'},
            {
              text: '<b>Done</b>',
              type: 'button-positive',
              onTap: function (e) {
                if (!_self.filter_data.key1) {
                  //don't allow the user to close unless he enters wifi password
                  e.preventDefault();
                } else {
                  dataPersistenceService.selectStylesPos(_self.filter_data.key1).then(function (result) {
                    console.log("RESULTTTTTTTTTTTTTT -----> ", result);
                    self.styles = [];
                    self.pos = [];
                    for (var i = 0; i < result.rows.length; i++) {
                      if ($.inArray(result.rows.item(i).Po, self.pos) == -1) {
                        if (!self.pos.contains(result.rows.item(i).Po)) {
                          self.pos.push(result.rows.item(i).Po);
                        }
                      }
                      if ($.inArray(result.rows.item(i).Style, self.styles) == -1) {
                        if (!self.styles.contains(result.rows.item(i).Style)) {
                          self.styles.push(result.rows.item(i).Style);
                        }
                      }
                    }
                    _self.styles.length = 0;
                    _self.styles = self.styles;
                    _self.pos.length = 0;
                    _self.pos = self.pos;


                  });
                  return _self.filter_data.key1;
                }
              }
            }
          ]
        });

        planPopup.then(function (res) {
          if (res) {
            _self.filter_data.key1 = res;
          } else {
            _self.filter_data.key1 = _self.liBefore;
          }
        });
      };

      _self.showBuyerPopup = function () {
        _self.buyerBefore = _self.filter_data.buyerId;
        $scope.selectedColour = {};
        // An elaborate, custom popup
        var planPopup = $ionicPopup.show({
          template: '<ion-list><ion-radio ng-repeat="buyer in rejectssizewisectrl.buyers" ng-model="rejectssizewisectrl.filter_data.buyerId" ng-value=" \'{{buyer}}\' "> ' +
          '<div class="item item-text-wrap">{{buyer}}</div> </ion-radio> </ion-list>',
          title: _self.title,
          cssClass: "custom-popup",
          scope: $scope,
          buttons: [
            {text: 'Cancel'},
            {
              text: '<b>Done</b>',
              type: 'button-positive',
              onTap: function (e) {
                if (!_self.filter_data.buyerId) {
                  //don't allow the user to close unless he enters wifi password
                  e.preventDefault();
                } else {

                  _self.filter_data.buyerId
                  self.sizecats = [];

                  for (var i = 0; i < _self.sizelists.length; i++) {
                    _self.obj = _self.sizelists[i];
                    if (_self.obj.buyerId == _self.filter_data.buyerId)
                      if ($.inArray(_self.obj.categoryId, _self.sizecats) == -1) {
                        self.sizecats.push(_self.obj.categoryId);
                      }
                  }
                  _self.sizeCats.length = 0;
                  _self.sizeCats = self.sizecats;

                  _self.sizeList.length = 0;

                  return _self.filter_data.buyerId;
                }
              }
            }
          ]
        });

        planPopup.then(function (res) {
          if (res) {
            _self.filter_data.buyerId = res;
          } else {
            _self.filter_data.buyerId = _self.buyerBefore;
          }
        });
      };

      _self.showSizeListPopup = function () {
        _self.sizeBefore = _self.filter_data.categoryId;
        $scope.selectedColour = {};
        // An elaborate, custom popup
        var planPopup = $ionicPopup.show({
          template: '<ion-list><ion-radio ng-repeat="size in rejectssizewisectrl.sizeCats" ng-model="rejectssizewisectrl.filter_data.categoryId" ng-value=" \'{{size}}\' "> ' +
          '<div class="item item-text-wrap">{{size}}</div> </ion-radio> </ion-list>',
          title: _self.title,
          cssClass: "custom-popup",
          scope: $scope,
          buttons: [
            {text: 'Cancel'},
            {
              text: '<b>Done</b>',
              type: 'button-positive',
              onTap: function (e) {
                if (!_self.filter_data.categoryId) {
                  e.preventDefault();
                } else {
                  self.sizes = [];
                  for (var i = 0; i < _self.sizelists.length; i++) {
                    _self.obj = _self.sizelists[i];
                    if (_self.obj.categoryId == _self.filter_data.categoryId && _self.obj.buyerId == _self.filter_data.buyerId)
                      if ($.inArray(_self.obj.categoryId, _self.sizecats) == -1) {

                        // self.dataList = JSON.parse(_self.obj.dataList);
                        for (var ii = 0; ii < _self.obj.dataList.length; ii++) {
                          self.singleSizeObj = _self.obj.dataList[ii];
                          if ($.inArray(self.singleSizeObj.sizeDetail.sizeName, self.sizes) == -1) {
                            var size = {};
                            size.name = self.singleSizeObj.sizeDetail.sizeName;
                            size.selected = false;
                            self.sizes.push(size);
                            // self.sizes.push(self.singleSizeObj.sizeDetail.sizeName);
                          }
                        }

                      }
                  }

                  _self.sizeList.length = 0;
                  _self.sizeList = self.sizes;

                  return _self.filter_data.categoryId;
                }
              }
            }
          ]
        });

        planPopup.then(function (res) {
          if (res) {
            _self.filter_data.categoryId = res;
          } else {
            _self.filter_data.categoryId = _self.sizeBefore;
          }
        });
      };


      _self.setSelectedOperation = function (operation) {
        _self.selectedOperation = operation;
        operation.selected = true;
        _self.filter_data.sizeId = _self.selectedOperation;

        _self.sizeList = [];
        _self.sizeList.length = 0;
        self.sizes = [];
        self.sizes.length = 0;
        for (var i = 0; i < _self.sizelists.length; i++) {
          _self.obj = _self.sizelists[i];
          if (_self.obj.categoryId == _self.filter_data.categoryId && _self.obj.buyerId == _self.filter_data.buyerId)
            if ($.inArray(_self.obj.categoryId, _self.sizecats) == -1) {

              // self.dataList = JSON.parse(_self.obj.dataList);
              for (var ii = 0; ii < _self.obj.dataList.length; ii++) {
                self.singleSizeObj = _self.obj.dataList[ii];
                if ($.inArray(self.singleSizeObj.sizeDetail.sizeName, self.sizes) == -1) {
                  var size = {};
                  size.name = self.singleSizeObj.sizeDetail.sizeName;
                  if (self.singleSizeObj.sizeDetail.sizeName == _self.selectedOperation) {
                    size.selected = true;
                  } else {
                    size.selected = false;
                  }
                  self.sizes.push(size);
                  // self.sizes.push(self.singleSizeObj.sizeDetail.sizeName);
                }
              }

            }
        }
        _self.sizeList = self.sizes;

      }

      _self.operationClick = function (pcsCount) {

        console.log("Selected Operation ", _self.selectedOperation);
        console.log("pcsCount ", pcsCount);

        _self.eventType = propertyCache.triggeredEvent;



        _self.pcs = parseInt(pcsCount);

        if (_self.pcs > 0) {
          if(_self.filter_data.sizeId != "DEFAULT" && _self.filter_data.sizeId != "" && _self.filter_data.sizeId != undefined) {
            if (_self.eventType == "reject") {
              _self.rejectBulkFunction();
            } else if (_self.eventType == "ftt") {
              _self.fttBulkFunction();
            } else if (_self.eventType == "rwtogood") {
              _self.rwtogoodBulkFunction();
            }
            $state.go('app.home');
          }else{
            $ionicLoading.show({
              template: '<div class="nf-center nf-valign-wrapper"><span>{{"SizeIdMustBeSelected" | translate}}</span></div>'
            });
            $timeout(function () {
              $ionicLoading.hide();
            }, 2000);
          }
        } else {
          $ionicLoading.show({
            template: '<div class="nf-center nf-valign-wrapper"><span>{{"BundleSizeShouldBeMoreThan0" | translate}}</span></div>'
          });
          $timeout(function () {
            $ionicLoading.hide();
          }, 2000);
        }

      }

      _self.resetState = function () {
        issuesCache.checkForRecentStyleChange();
        _self.operations = [];
        _self.operations = issuesCache.getSelectedOperations();
        $scope.style = propertyCache.day_start.style;

        angular.forEach(_self.operations, function (op, index) {
          _self.operations[index].selected = false;
        });

        cacheViews.toOperations = -1;

        issuesCache.loadIssue();
        issuesCache.loadRejects();
        $scope.$safeApply($scope);
      }

      _self.handleMultiSelectOperations = function (operation) {
        var index = selectedOperations.indexOf(operation);
        if (index > -1) {
          selectedOperations.splice(index, 1);
          operation.selected = false;
        } else {
          selectedOperations.push(operation);
          operation.selected = true;
        }
      }
      _self.startNew = function () {
        _self.selectedRejects = [];
        _self.currentRejects = [];
        _self.rejectslist = [];
      }


      // _self.rejectClick = function (reject) {
      //   if ($scope.isSingleSelectIssues) {
      //
      //   } else {
      //     _self.handleMultiSelectIssues(reject);
      //   }
      // }

      _self.saveFinalList = function (reject) {

        if (!reject.hasOwnProperty('name'))
          return;

        //this is a new issue
        var newReject = angular.copy(reject);

        newReject.rejects = _self.selectedRejects;
        _self.lastIndex = _self.rejectslist.push(newReject);

      }
      _self.isInList = function (list, newReject) {
        var ret = {};
        angular.forEach(list, function (reject, index) {
          if (newReject.category_id == reject.category_id) {
            ret.index = index;
            ret.reject = reject;
          }
        });
        return ret;
      }

      $scope.numberOfPages = function (array) {
        return Math.ceil(array.length / $scope.pageSize);
      }
      $scope.$on('$ionicView.beforeEnter', function () {
        $scope.$parent.clearAllFabs();

        if (cacheViews.toOperations === 0) {
          _self.resetState();
        }

        init();



      });

      _self.increment = function () {
        _self.pcs++;
      }

      _self.decrement = function () {
        if (_self.pcs > 1) {
          _self.pcs--;
        }
      }

      _self.rejectBulkFunction = function () {

        var $event = eventProtocol.getEvent('reject');
        var rejects = [];

        var size = new Object();
        size.category_id = _self.filter_data.categoryId;
        size.name = _self.filter_data.buyerId;
        size.operation = [_self.selectedOperation != "" ? _self.selectedOperation : _self.filter_data.sizeId];

        // var sizeName = new Object();


        rejects.push(size);

        $event.data.style = _self.filter_data.style;

        $event.data.rejectreason = rejects;
        $event.data.value = _self.pcs;
        $event.count = _self.pcs;

        $rootScope.$broadcast("modelCountUpdate", {type: "reject", value: _self.pcs});

        //send event to database
        //dataPersistenceService.dailyCountPreProcess($event);

        if(propertyCache.factoryConfigs.show_style_info_bar == "true") {
          dataPersistenceService.selectStyleSoLiWiseDailyCount($event).then(function (res) {
            $event.count = uiModel.value;
            dataPersistenceService.updateDailyCount($event);
          }, function (err) {
            $event.count = uiModel.value;
            dataPersistenceService.insertDailyCount($event);
          });
        }else{
          dataPersistenceService.dailyCountPreProcess($event)
        }


        //set undo operation
        var dataToUndoService = new Object();
        dataToUndoService.dataType = 'reject';
        dataToUndoService.jsonSent = $event.data;
        dataToUndoService.categories = rejects;

        undoOperation.setLastEvent(dataToUndoService);

        //send data to remote server
        sparkDataService.sendDataToServer($event);

        ///////////////////audit//////////////////////
        var $audit = auditProtocol.getEvent('reject');
        $audit.data.value = _self.pcs;
        $audit.data.reference.rejectreason = rejects;
        sparkDataService.sendDataToServer($audit)
        /////////////////////////////////////////////////

      }

      _self.rwtogoodBulkFunction = function () {

        dataPersistenceService.selectAllTheDailyCount().then(function (res) {
          // var _self = this;
          var countData = [];
          self.rwtogoodCount = 0;
          for (var i = 0; i < res.rows.length; i++) {
            if (res.rows.item(i).Type == "defects") {
              self.defectCount = res.rows.item(i).Count;
            }
            if (res.rows.item(i).Type == "rwtogood") {
              self.rwtogoodCount = res.rows.item(i).Count;
            }
          }
          self.feasibleRwkCount = self.rwtogoodCount + _self.pcs;
          if (self.defectCount >= self.feasibleRwkCount) {
            var $event = eventProtocol.getEvent(_self.eventType);

            var selectedReworkCategories = [];

            // var dataObj = new Object();
            // dataObj.id = selectedList[i].id;
            // dataObj.name = selectedList[i].name;
            // selectedReworkCategories.push(selectedList[i].name);

            dataPersistenceService.removeAllDefectCategories($event);

            $event.data.categories = selectedReworkCategories;

            $event.data.buyerId = _self.filter_data.buyerId;
            $event.data.categoryId = _self.filter_data.categoryId;
            $event.data.sizeId = _self.selectedOperation != "" ? _self.selectedOperation : _self.filter_data.sizeId;

            $event.data.value = _self.pcs;

            $event.count = _self.pcs;

            //send event to database
            dataPersistenceService.dailyCountPreProcess($event);

            sparkDataService.sendDataToServer($event);

            ///////////////////audit//////////////////////
            // var $audit = auditProtocol.getEvent('rwtogood');
            // $audit.data.reference.categories = selectedReworkCategories;
            // sparkDataService.sendDataToServer($audit)
            /////////////////////////////////////////////////
            $rootScope.$broadcast("modelCountUpdate", {type: "rwtogood", value: $event.data.value});

            var dataToUndoService = new Object();
            dataToUndoService.dataType = 'rwtogood';
            dataToUndoService.jsonSent = $event.data;
            dataToUndoService.categories = selectedReworkCategories;
            undoOperation.setLastEvent(dataToUndoService);

          } else {
            var alertPopup = $ionicPopup.alert({
              title: $filter('translate')('Warning'),
              template: '<div class="nf-center nf-valign-wrapper">' +
              '<ion-spinner icon="ripple" class="nf-text-white"></ion-spinner>' +
              '<span>{{"NoDefectsToRework" | translate}}</span></div>'
            });
          }
        }, function (err) {

        });

      }

      _self.fttBulkFunction = function () {

        var $event = eventProtocol.getEvent(_self.eventType);
        $log.debug("Return event -> eventProtocol  :", $event);

        $event.data.buyerId = _self.filter_data.buyerId;
        $event.data.categoryId = _self.filter_data.categoryId;
        $event.data.sizeId = _self.selectedOperation != "" ? _self.selectedOperation : _self.filter_data.sizeId;

        $event.count = _self.pcs
        $event.data.value = _self.pcs;
        //send event to database
        dataPersistenceService.dailyCountPreProcess($event);

        $rootScope.$broadcast("modelCountUpdate", {type: "ftt", value: $event.data.value});

        //set undo operation
        var dataToUndoService = new Object();
        dataToUndoService.dataType = _self.eventType;
        dataToUndoService.jsonSent = $event.data;
        undoOperation.setLastEvent(dataToUndoService);

        //send data to remote server
        sparkDataService.sendDataToServer($event);

        ///////////////////audit//////////////////////
        // var $audit = auditProtocol.getEvent(uiModel.dataType);
        // sparkDataService.sendDataToServer($audit)
        //////////////////////////////////////////////
        _self.buttonPress = false;

      },

        _self.focusGrant = function () {
            _self.focusgranted = true;
        }

        _self.focusLoose = function () {
          _self.focusgranted = false;
        }

    }]);

