dojo.provide("parus.designerReportTableConstraintDialog");

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.designerTemplateDialog");

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

  editionRN: null,

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

    if (!this.prn) this.prn = this.getParentDialogCondition("PRN"); //constraintRN
    if (this.parentDialog && this.parentDialog.isInstanceOf(parus.editableDictionaryDialog)) this.editionRN = this.parentDialog.getParentDialogCondition("PRN"); //editionRN
    if (store && item) {
      this.nameEdit.attr("value", store.getValue(item, "NAME"));
      this.constrTypeEdit.attr("value", store.getValue(item, "CONSTR_TYPE"));
      this.textEdit.attr("value", store.getValue(item, "TEXT"));
      this.messageEdit.attr("value", store.getValue(item, "MESSAGE"));
      this.templateEdit.attr("value", store.getValue(item, "TEMPLATE"));

      this.activeCheckBox.attr("checked", store.getValue(item, "ACTIVE_SIGN") == "1");
    }

    this.constrTypeEditChanged();
  },

  constrTypeEditChanged: function() {
    var isUnique = this.constrTypeEdit.attr("value") == "0";
    parus.controlActions.setDisabled(this.textEdit, isUnique);

    this.messageEdit.attr("required", !isUnique);
  },

  getTemplatesCondition: function() {
    return {
      PRN: this.editionRN ? this.editionRN : parus.parus8.CLASS_EmptyData
    };
  },

  executeInsert: function(duplicate) {
    var response = parus.database.execute("designer.BalanceTables.Constraints.Insert", {
      RN: duplicate ? this.rn : null,
      PRN: this.prn,
      NAME: this.nameEdit.attr("value"),
      CONSTR_TYPE: this.constrTypeEdit.attr("value"),
      TEXT: this.textEdit.attr("value"),
      MESSAGE: this.messageEdit.attr("value"),
      TEMPLATE: this.templateEdit.attr("value"),
      ACTIVE_SIGN: this.activeCheckBox.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.Constraints.Update", {
      RN: this.rn,
      NAME: this.nameEdit.attr("value"),
      CONSTR_TYPE: this.constrTypeEdit.attr("value"),
      TEXT: this.textEdit.attr("value"),
      MESSAGE: this.messageEdit.attr("value"),
      TEMPLATE: this.templateEdit.attr("value"),
      ACTIVE_SIGN: this.activeCheckBox.attr("checked") ? "1" : "0"
    });

    return !!response;
  },

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

    return !!response;
  }

});