dojo.provide("parus.balanceIndexConsDialog");

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

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

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

  intPart: null,

  fracPart: null,

  consItems: null,

  queryConditions: null,

  description: null,

  skipUnsignificantZeroes: false,

  autoSize: true,

  postCreate: function() {
    this.inherited(arguments);

    if (this.description) this.headerPaneText.containerNode.innerHTML = this.description;

    var settings = parus.userdata.get(this.declaredClass);
    if (settings) if (settings.additions) this.skipUnsignificantZeroes = settings.additions.skipUnsignificantZeroes;

    var fc = {
      intPart: this.intPart,
      fracPart: this.skipUnsignificantZeroes ? null : this.fracPart
    };

    dojo.mixin(this.consItemsGrid.views.views[0].structure.cells[0][1], fc);
    dojo.mixin(this.consItemsGrid.views.views[0].structure.cells[0][1]._props, fc);
    dojo.mixin(this.consItemsGrid.views.views[0].structure.cells[0][14], fc);
    dojo.mixin(this.consItemsGrid.views.views[0].structure.cells[0][14]._props, fc);

    dojo.mixin(this.consItemsGrid.structure[0].cells[0][0], fc);
    dojo.mixin(this.consItemsGrid.structure[0].cells[0][0]._props, fc);
    dojo.mixin(this.consItemsGrid.structure[0].cells[0][13], fc);
    dojo.mixin(this.consItemsGrid.structure[0].cells[0][13]._props, fc);

    this.consItemsGridPopupMenu.bindDomNode(this.consItemsGrid.viewsNode);
    parus.balance._createActionsButton(this.consItemsGridPopupMenu, this.headerPane);

    if (settings) {
      parus.gridSettingsDialog.mergeGridSettings(this.consItemsGrid, settings.gridSettings);
      parus.gridSettingsDialog.mergeColorConditions(this.consItemsGrid, settings.colorConditions);
      parus.gridSettingsDialog.mergeMenuSettings(this.consItemsGridPopupMenu, settings.actionSettings, settings.userProcsActions, settings.userReportsActions, settings.userAppsActions);
      parus.gridSettingsDialog.mergeFilterBar(this.consItemsGrid, settings.showFilterBar, settings.filterValues, settings.sortInfo);
    };

    this.consItemsGrid.attr("store", new parus.gridData({
      persistent: true,
      _data: this.consItems
    }));

    this.consItemsGrid.setQuery({});
  },

  print: function() {

    orders = [];
    hiddenCols = [];

    dojo.forEach(this.consItemsGrid.getSortProps() || [], function(sortProp) {
      var idx = dojo.indexOf(["VALUE", "KIND", "AGNABBR", "AGNNAME", "STATE", "CHECKED", "ACCEPTED", "SENT", "RESULT", "CODE", "SRSTATE", "SRCHECKED", "STRVAL", "NUMVAL", "DATEVAL"], sortProp.field);
      var order = (idx >= 0) ? ["20,21,22", 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18][idx] : null;
      if (order) {
        if (sortProp.descending) order = dojo.map(order.split(","), function(o) {
          return o + " desc"
        }).join(",");
        orders.push(order.toString());
      }
    });

    dojo.forEach(this.consItemsGrid.views.views[0].structure.cells[0], dojo.hitch(this, function(column) {
      if (!column.field) return;
      if (column.hidden) hiddenCols.push(column.field);
    }));

    var conditions = this.queryConditions;
    conditions.order = orders.length ? orders.join(",") : "1,2,3,4,5,6,7";

    this.printForm["query"].value = dojo.toJson(conditions);
    this.printForm["handler"].value = parus.database.connected;
    this.printForm["skipzero"].value = this.skipUnsignificantZeroes ? 1 : 0;
    this.printForm["hiddencols"].value = hiddenCols.join(",")
    document.body.appendChild(this.printForm);
    this.printForm.submit();
    document.body.removeChild(this.printForm);
  },

  settingsAdditions: function() {
    return [{
      title: "Скрыть незначащие нули в дробной части числа",

      id: "skipUnsignificantZeroes",

      get: dojo.hitch(this, function() {
        return this.skipUnsignificantZeroes;
      }),

      set: dojo.hitch(this, function(value) {
        this.skipUnsignificantZeroes = !! value;

        var fc = {
          intPart: this.intPart,
          fracPart: this.skipUnsignificantZeroes ? null : this.fracPart
        };

        var str = dojo.clone(this.consItemsGrid.structure);
        var res = [];
        var strItems = str[0].cells ? str[0].cells[0] : str;
        for (var i = 0; i < strItems.length; i++) {
          dojo.mixin(strItems[i], fc);
          res.push(dojo.clone(strItems[i]));
        };
        if (str[0] && str[0].cells) str[0].cells[0] = res;
        else str = res;

        this.consItemsGrid.setStructure(str);
      })
    }]
  }
});