/**
 * Created by mhashendre on 11/3/2016.
 */

angular.module('app.controllers')
  .controller('AddPlanAdvanceBehindMasterDataCtrl', function ($scope, $state, ionicMaterialInk, ionicMaterialMotion, $log, dataPersistenceService, $timeout, sparkDataService, propertyCache, auditProtocol, $ionicLoading, initService, WifiService, $rootScope, ModalViews, eventProtocol) {

    ionicMaterialInk.displayEffect();

    var _self = this;

    _self.viewName = "add_plan_advance_behind_master_data";

    _self.styleInfo = {
      style: "",
      so: "",
      li: "",
      smv: 0,
      kickoff: 0.01,
      quantity:"",
      daynumber:0,
      styletype : ""
      }


    _self.exit = function () {
      if (ModalViews.getView(_self.viewName)) {
        ModalViews.getView(_self.viewName).hide();
      }
    }

    _self.click = function (buttonclick) {
      $log.info("saving styleinfo data " + buttonclick);
      console.log("Style info ------------>>>> ", _self.styleInfo)

      var $event = eventProtocol.getEvent("styleInfo");
      $event.data.style = angular.copy(_self.styleInfo.style);
      $event.data.so = angular.copy(_self.styleInfo.so);
      $event.data.li = angular.copy(_self.styleInfo.li);
      $event.data.smv = angular.copy(_self.styleInfo.smv);
      $event.data.kickoff = angular.copy(_self.styleInfo.kickoff);
      $event.data.quantity = angular.copy(_self.styleInfo.quantity);
      $event.data.daynumber = angular.copy(_self.styleInfo.daynumber);
      $event.data.style_type = angular.copy(_self.styleInfo.styletype);


      var event = new Object();
      event.style = _self.styleInfo.style;
      event.po = _self.styleInfo.so;
      event.colour = _self.styleInfo.li;

      dataPersistenceService.addStyles_Po_Colour(event).then(function (result) {
        dataPersistenceService.selectAllFromStyles_po_colours().then(function (result) {
          self.styles = [];
          self.pos = [];
          self.colours = [];
          for (var i = 0; i < result.rows.length; i++) {

            if ($.inArray(result.rows.item(i).Style, self.styles) == -1) {
              self.styles.push(result.rows.item(i).Style);
            }

            if ($.inArray(result.rows.item(i).Po, self.pos) == -1) {
              self.pos.push(result.rows.item(i).Po);
            }

            if ($.inArray(result.rows.item(i).Colour, self.colours) == -1) {
              self.colours.push(result.rows.item(i).Colour);
            }

          }
          propertyCache.planedStyles.styleNames = self.styles;
          propertyCache.pos.poNames = self.pos;
          propertyCache.colours.colorCodes = self.colours;
          console.log("Property cache set ---------> ", propertyCache.planedStyles.styleNames);
          console.log("Property cache set ---------> ", propertyCache.planedStyles);

        });
      });

      sparkDataService.sendDataToServer($event);

      _self.exit();

    }


  });

