dojo.provide("parus.balanceRemarksDialog");

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

dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");

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


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

  store: null,

  queryInfo: null,

  pane: null,

  readonly: false,

  postCreate: function() {
    this.inherited(arguments);
    this.remarksGrid.attr("store", this.store);
    this.remarksGrid.setQuery(this.queryInfo);
    this.remarksGrid.selection.select(0);
    this.remarksGridPopupMenu.bindDomNode(this.remarksGrid.viewsNode);

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

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

    this.noteEdit.attr("value", null);
    setTimeout(dojo.hitch(this, function() {
      this._selected();
    }), 100);
  },

  _selected: function() {
    var item = this.remarksGrid.getItem(this.remarksGrid.selection.getSelectedIndex());
    this.noteEdit.attr("value", this.store.getValue(item, "NOTE"));

    dojo.forEach(this.remarksGridPopupMenu.getChildren(), function(menuItem) {
      var disabled = item ? false : true;
      if (!menuItem.itemAction) disabled = false;
      if (menuItem.declaredClass != "dijit.MenuSeparator") menuItem.attr("disabled", disabled);
    });

    if (this.readonly) {
      this.addRemerkMenuItem.attr("disabled", true);
      this.duplicateRemerkMenuItem.attr("disabled", true);
      this.clearRemarksMenuItem.attr("disabled", true);
    }

    parus.parus8.checkMenuSeparators(this.remarksGridPopupMenu);

  },

  clearRemarks: function() {

    this.pane.clearRemarks(
    dojo.clone(this.queryInfo), dojo.hitch(this, dojo.hitch(this, this._remarkCreated)));
  },

  addRemark: function() {

    this.pane.createRemark(
    dojo.clone(this.queryInfo), dojo.hitch(this, dojo.hitch(this, this._remarkCreated)));

  },

  duplicateRemark: function() {
    var item = this.remarksGrid.getItem(this.remarksGrid.selection.getSelectedIndex());

    var params = {
      NOTE: this.store.getValue(item, "NOTE"),
      CODE: this.store.getValue(item, "CODE")
    };

    dojo.mixin(params, this.queryInfo);

    this.pane.createRemark(
    params, dojo.hitch(this, this._remarkCreated));
  },

  _remarkCreated: function() {

    var q = dojo.clone(this.queryInfo);

    dojo.mixin(q, {
      onComplete: dojo.hitch(this, function() {
        this.remarksGrid.selection.select(this.remarksGrid._by_idx.length - 1);
        this.remarksGrid.updateRow(this.remarksGrid._by_idx.length - 1);
        this.remarksGrid.scrollToRow(this.remarksGrid._by_idx.length - 1);
      })
    });

    this.remarksGrid.setQuery(q);

  },

  settings: function() {
    parus.balance.executeSettings(this.remarksGrid, this.remarksGridPopupMenu, this.declaredClass);
  },

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

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

});