dojo.provide("parus.balanceIndexConsComparationTemplatesDialog");

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");

dojo.require("parus.parus8");
dojo.require("parus.dialog");
dojo.require("parus.balanceIndexConsComparation");

dojo.declare("parus.balanceIndexConsComparationTemplatesDialog", [parus.Dialog], {

  postCreate: function() {

    this.inherited(arguments);

    if ((dojo.indexOf(parus.balance.availableUnits, "Users") == -1)) {
      this.clearAllTemplatesMenuItem.destroyRecursive();
      var structure = dojo.clone(this.templatesGrid.structure);
      structure[0].cells[0] = dojo.filter(structure[0].cells[0], function(s) {
        return (s.field != 'user');
      });
      this.templatesGrid.attr("structure", structure);
    };

    this.templatesGrid.attr("store", this._getStore());

    this.templatesGrid.setQuery({});
    this.templatesGrid.selection.select(0);
    this.templatesGridPopupMenu.bindDomNode(this.templatesGrid.viewsNode);

    parus.balance._createActionsButton(this.templatesGridPopupMenu, this.headerPane);

    var settings = parus.userdata.get(this.declaredClass);
    if (settings) {
      parus.gridSettingsDialog.mergeMenuSettings(this.templatesGridPopupMenu, settings.actionSettings, settings.userProcsActions, settings.userReportsActions, settings.userAppsActions);
      parus.gridSettingsDialog.mergeGridSettings(this.templatesGrid, settings.gridSettings);
      parus.gridSettingsDialog.mergeColorConditions(this.templatesGrid, settings.colorConditions);
      parus.gridSettingsDialog.mergeFilterBar(this.templatesGrid, settings.showFilterBar, settings.filterValues, settings.sortInfo);
    };

    setTimeout(dojo.hitch(this, function() {
      this._selected();
    }), 100);
  },

  _getStore: function() {
    var dt = parus.parus8.consComparationData ? parus.parus8.consComparationData.dataType : null;
    var r = parus.parus8.consComparationData ? parus.parus8.consComparationData.result : null;
    return new parus.gridData({
      persistent: true,
      _data: parus.balanceIndexConsComparation.getTemplates(dt, r)
    })
  },

  _selected: function() {
    var item = this.templatesGrid.getItem(this.templatesGrid.selection.getSelectedIndex());

    dojo.forEach(this.templatesGridPopupMenu.getChildren(), dojo.hitch(this, function(menuItem) {
      var disabled = item ? false : true;
      if (!menuItem.itemAction) disabled = false;

      if (menuItem == this.deleteTemplateMenuItem) disabled = disabled || !! (this.templatesGrid.store.getValue(item, "user"));

      if (menuItem.declaredClass != "dijit.MenuSeparator") menuItem.attr("disabled", disabled);
    }));
    parus.parus8.checkMenuSeparators(this.templatesGridPopupMenu);

  },

  deleteTemplate: function() {

    (new parus.confirmationDialog({

      message: "Удалить запись (записи)?",

      onApply: dojo.hitch(this, function() {

        var store = this.templatesGrid.store;
        var item = this.templatesGrid.selection.getFirstSelected();
        for (var i = 0; i < this.templatesGrid.selection.getSelectedCount(); i++) {
          parus.balanceIndexConsComparation.deleteTemplate(store.getValue(item, "name"));
          item = this.templatesGrid.selection.getNextSelected(item);
        };
        parus.parus8.consComparationDataModified();
        this.templatesGrid.attr("store", this._getStore());
        this.templatesGrid.setQuery({});
        this.templatesGrid.selection.select(0);
        return true;
      })
    })).show();


    //    var item = this.templatesGrid.getItem(this.templatesGrid.selection.getSelectedIndex());
  },

  clearTemplates: function() {
    (new parus.confirmationDialog({

      message: "Удалить все шаблоны?",

      onApply: dojo.hitch(this, function() {

        parus.balanceIndexConsComparation.clearTemplates(false);
        parus.parus8.consComparationDataModified();
        this.templatesGrid.attr("store", this._getStore());
        this.templatesGrid.setQuery({});
        this.templatesGrid.selection.select(0);
        return true;
      })
    })).show();

  },

  clearAllTemplates: function() {

    (new parus.confirmationDialog({

      message: "Удалить все шаблоны всех пользователей?",

      onApply: dojo.hitch(this, function() {

        parus.balanceIndexConsComparation.clearTemplates(true);
        parus.parus8.consComparationDataModified();
        this.templatesGrid.attr("store", this._getStore());
        this.templatesGrid.setQuery({});
        this.templatesGrid.selection.select(0);
        return true;
      })
    })).show();
  },

  settings: function() {
    parus.balance.executeSettings(this.templatesGrid, this.templatesGridPopupMenu, this.declaredClass);
  },

  destroy: function() {
    this.store = null;
    this.inherited(arguments);
  },

  gridFilterChanged: function(values, sortInfo) {
    parus.balance.gridFilterChanged(this.declaredClass, values, sortInfo);
  }

});