dojo.provide("parus.designerTableModelFormulaDialog");

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.editableDictionaryDialog");
dojo.require("parus.editDictionaryDialog");

dojo.require("parus.designerRecalculateTypeDialog");

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

  kind: null,

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

    if (!this.prn) this.prn = this.getParentDialogCondition("PRN"); //tableModelRN
    this.kind = this.getParentDialogCondition("KIND");

    if (store && item) {
      this.recalculateTypeEdit.attr("value", store.getValue(item, "RECALCTYPE"));
      this.userProcEdit.attr("value", store.getValue(item, "USERPROC"));
      this.dateFromEdit.attr("value", parus.formats.stringToDate(store.getValue(item, "BEGINDATE")));
      this.dateToEdit.attr("value", parus.formats.stringToDate(store.getValue(item, "ENDDATE")));
    }
  },

  executeInsert: function() {
    var response = parus.database.execute("designer.BalanceTableModels.Formulas.Insert", {
      PRN: this.prn,
      KIND: this.kind,
      RECALCTYPE: this.recalculateTypeEdit.attr("value"),
      USERPROC: this.userProcEdit.attr("value"),
      BEGINDATE: parus.formats.dateToString(this.dateFromEdit.attr("value")),
      ENDDATE: parus.formats.dateToString(this.dateToEdit.attr("value"))
    });

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

  executeUpdate: function() {
    var response = parus.database.execute("designer.BalanceTableModels.Formulas.Update", {
      RN: this.rn,
      KIND: this.kind,
      RECALCTYPE: this.recalculateTypeEdit.attr("value"),
      USERPROC: this.userProcEdit.attr("value"),
      BEGINDATE: parus.formats.dateToString(this.dateFromEdit.attr("value")),
      ENDDATE: parus.formats.dateToString(this.dateToEdit.attr("value"))
    });

    return !!response;
  },

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

    return !!response;
  }

});