dojo.provide("parus.designerTableModelIndexDialog");

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

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

dojo.require("parus.designerIndexDialog");
dojo.require("parus.designerIndexFormulaDialog");

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

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

    if (!this.prn && this.parentDialog && this.parentDialog.dictionary && this.parentDialog.dictionary.code == "DesignerBalanceTablesNotIncludedIndexes") this.prn = this.getParentDialogCondition("MODEL_PRN");

    if (store && item) {
      this.indexEdit.attr("value", store.getValue(item, "BALANCEINDEX"));
      this.priorityEdit.attr("value", store.getValue(item, "PRIORITY"));
      this.agrFuncSelect.attr("value", store.getValue(item, "AGR_FUNC"));

      if (this.editMode) {
        var isNumber = store.getValue(item, "BITYPE") == "1";
        parus.controlActions.setDisabled(this.agrFuncSelect, !isNumber);
      }
    }

    this.indexEdit.onApplyDictionary = dojo.hitch(this, this.indexChanged);
  },

  indexChanged: function(dialog) {
    //changed by dictionary dialog
    if (dialog) {
      var isNumber = dialog.getFieldValue("BITYPE") == "1";
      parus.controlActions.setDisabled(this.agrFuncSelect, !isNumber);
    }
    //changed by control text
    else if (!this.indexEdit.showingDictionary && !this.indexEdit.attr("value")) { //avoid triggering callback when dialog dictionary changes
      parus.controlActions.setDisabled(this.agrFuncSelect, true);
    }
  },

  executeInsert: function(duplicate) {
    var response = parus.database.execute("designer.BalanceTableModels.Indexes.Insert", {
      PRN: this.prn,
      BALANCEINDEX: this.indexEdit.attr("value"),
      PRIORITY: this.priorityEdit.attr("value"),
      AGR_FUNC: this.agrFuncSelect.attr("value")
    });

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

  executeUpdate: function() {
    var response = parus.database.execute("designer.BalanceTableModels.Indexes.Update", {
      RN: this.rn,
      BALANCEINDEX: this.indexEdit.attr("value"),
      PRIORITY: this.priorityEdit.attr("value"),
      AGR_FUNC: this.agrFuncSelect.attr("value")
    });

    return !!response;
  },

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

    return !!response;
  },

  //static
  executeInsertList: function(prn, selectedList) {
    var response = parus.database.execute("designer.BalanceTableModels.Indexes.InsertList", {
      PRN: prn,
      RN: selectedList
    });

    return response;
  },

  //static
  executeInsertGroup: function(prn, group) {
    var response = parus.database.execute("designer.BalanceTableModels.Indexes.InsertGroup", {
      PRN: prn,
      GROUP: group
    });

    return response;
  },

  //static
  executeChangePriority: function(prn, priority) {
    var response = parus.database.execute("designer.BalanceTableModels.Indexes.ChangePriority", {
      PRN: prn,
      PRIORITY: priority
    });

    return !!response;
  }

});