dojo.provide("parus.designerReportTableDialog");

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

dojo.require("parus.parus8");
dojo.require("parus.dialog");
dojo.require("parus.dictEdit");
dojo.require("parus.formats");
dojo.require("parus.controlActions");

dojo.require("parus.editableDictionaryDialog");
dojo.require("parus.editDictionaryDialog");

dojo.require("parus.designerTableModelsDictionaryDialog");

dojo.declare("parus.designerReportTableDialog", [parus.editDictionaryDialog], {

  onLoadParams: function(store, item, editMode) {
    this.inherited(arguments);

    if (!this.prn) this.prn = this.getParentDialogCondition("PRN"); //editionRN
    if (store && item) {
      this.tableEdit.attr("value", store.getValue(item, "CODE"));
      this.nameEdit.attr("value", store.getValue(item, "NAME"));
      this.priorityEdit.attr("value", store.getValue(item, "PRIORITY"));
      this.signTotalsCheckBox.attr("checked", store.getValue(item, "SIGN_TOTALS") == "1");
      this.sourceToResultCheckBox.attr("checked", store.getValue(item, "SOURCE_TO_RESULT") == "1");
      this.sourceToUnionCheckBox.attr("checked", store.getValue(item, "SOURCE_TO_UNION") == "1");

      if (this.editMode) {
        this.tableEdit.attr("disabled", true);
      }
    }

    this.tableEdit.onApplyDictionary = dojo.hitch(this, this.tableChanged);
  },

  tableChanged: function(dialog) {
    //changed by dictionary dialog
    if (dialog) this.nameEdit.attr("value", dialog.getFieldValue("NAME"));
    //changed by control text
    else if (!this.tableEdit.showingDictionary && !this.tableEdit.attr("value")) //avoid triggering callback when dialog dictionary changes
    this.nameEdit.attr("value", "");
  },

  signTotalsChanged: function() {
    var checked = this.signTotalsCheckBox.attr("checked");

    parus.controlActions.setDisabled(this.sourceToResultCheckBox, !checked);
    parus.controlActions.setDisabled(this.sourceToUnionCheckBox, !checked && !this.sourceToResultCheckBox.attr("checked"));
  },

  sourceToResultChanged: function() {
    parus.controlActions.setDisabled(
    this.sourceToUnionCheckBox, !this.sourceToResultCheckBox.attr("checked"));
  },

  executeInsert: function(duplicate) {
    var response = parus.database.execute("designer.BalanceTables.Insert", {
      RN: duplicate ? this.rn : null,
      PRN: this.prn,
      TABLEMODELS: this.tableEdit.attr("value"),
      NAME: this.nameEdit.attr("value"),
      PRIORITY: this.priorityEdit.attr("value"),
      SIGN_TOTALS: this.signTotalsCheckBox.attr("checked") ? "1" : "0",
      SOURCE_TO_RESULT: this.sourceToResultCheckBox.attr("checked") ? "1" : "0",
      SOURCE_TO_UNION: this.sourceToUnionCheckBox.attr("checked") ? "1" : "0"
    });

    if (response) return parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NRN");
  },

  executeUpdate: function() {
    var response = parus.database.execute("designer.BalanceTables.Update", {
      RN: this.rn,
      TABLEMODELS: this.tableEdit.attr("value"),
      NAME: this.nameEdit.attr("value"),
      PRIORITY: this.priorityEdit.attr("value"),
      SIGN_TOTALS: this.signTotalsCheckBox.attr("checked") ? "1" : "0",
      SOURCE_TO_RESULT: this.sourceToResultCheckBox.attr("checked") ? "1" : "0",
      SOURCE_TO_UNION: this.sourceToUnionCheckBox.attr("checked") ? "1" : "0"
    });

    return !!response;
  },

  executeDelete: function(store, item) {
    var response = parus.database.execute("designer.BalanceTables.Delete", {
      RN: store.getValue(item, "RN")
    });

    return !!response;
  }

});