dojo.provide("parus.designerReportTableConstraintAttributeDialog");

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

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

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

dojo.require("parus.designerReportTableIndexDialog");

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

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

    if (store && item) {
      this.numbEdit.attr("value", store.getValue(item, "NUMB"));
      this.indexEdit.attr("value", store.getValue(item, "BALANCEINDEX"));
    }

    if (!this.editMode) this.numbEdit.attr("value", this.executeGetNextNumber());
  },

  getIndexCondition: function() {
    return {
      PRN: this.getParentDialogCondition("PRN") //reportTableRN
    };
  },

  getExecParams: function() {
    return {
      NUMB: this.numbEdit.attr("value"),
      BALANCEINDEX: this.indexEdit.attr("value")
    };
  },

  executeInsert: function(duplicate) {
    var response = parus.database.execute("designer.BalanceTables.Constraints.Attributes.Insert", dojo.mixin(this.getExecParams(), {
      PRN: this.prn
    }));

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

  executeUpdate: function() {
    var response = parus.database.execute("designer.BalanceTables.Constraints.Attributes.Update", dojo.mixin(this.getExecParams(), {
      RN: this.rn
    }));

    return !!response;
  },

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

    return !!response;
  },

  executeGetNextNumber: function() {
    var response = parus.database.execute("designer.BalanceTables.Constraints.Attributes.GetNextNumber", {
      PRN: this.prn
    });

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

});