dojo.provide("parus.designerTableModelTotalDialog");

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

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

  priority: null,
  index: null,
  formula: null,

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

    if (!this.prn) this.prn = this.getParentDialogCondition("PRN"); //tableModelRN
    if (store && item) {
      this.intermediateKindRadio.attr("checked", store.getValue(item, "KIND") == "0");
      this.totalKindRadio.attr("checked", store.getValue(item, "KIND") == "1");

      this.priorityEdit.attr("value", store.getValue(item, "PRIORITY"));
      this.indexEdit.attr("value", store.getValue(item, "BALANCEINDEX"));
      this.formulaEdit.attr("value", store.getValue(item, "FORMULA"));

      this.underDataRadio.attr("checked", store.getValue(item, "LOCATION") == "0");
      this.overDataRadio.attr("checked", store.getValue(item, "LOCATION") == "1");

      if (this.editMode) {
        this.priority = store.getValue(item, "PRIORITY");
        this.index = this.indexEdit.attr("value");
        this.formula = this.formulaEdit.attr("value");
      }
    }

    this.kindChanged(this.totalKindRadio.attr("checked"));
  },

  kindChanged: function(toTotal) {
    //store current values
    if (toTotal) {
      this.index = this.indexEdit.attr("value");
      this.formula = this.formulaEdit.attr("value");
    }

    parus.controlActions.setDisabled(this.indexEdit, toTotal);
    parus.controlActions.setDisabled(this.formulaEdit, toTotal);
    if (toTotal) this.priorityEdit.attr("value", 0);

    //restore previous values
    if (!toTotal) {
      this.indexEdit.attr("value", this.index);
      this.formulaEdit.attr("value", this.formula);

      //начали редактировать запись с Вид = Общий, потом поменяли на Промежуточный -> Уровень должен обновиться
      var priority = this.priority;
      if (!this.editMode || priority == 0) priority = this.executeGetNextPriority();

      this.priorityEdit.attr("value", priority);
    }
  },

  getIndexCondition: function() {
    return {
      PRN: this.prn
    };
  },

  getExecParams: function() {
    return {
      KIND: this.totalKindRadio.attr("checked") ? "1" : "0",
      BALANCEINDEX: this.indexEdit.attr("value"),
      PRIORITY: this.priorityEdit.attr("value"),
      FORMULA: this.formulaEdit.attr("value"),
      LOCATION: this.underDataRadio.attr("checked") ? "0" : "1"
    };
  },

  executeInsert: function(duplicate) {
    var response = parus.database.execute("designer.BalanceTableModels.Totals.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.BalanceTableModels.Totals.Update", dojo.mixin(this.getExecParams(), {
      RN: this.rn
    }));

    return !!response;
  },

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

    return !!response;
  },

  //static
  executeChangePriority: function(store, item, directUp) {
    var response = parus.database.execute("designer.BalanceTableModels.Totals.ChangePriority", {
      RN: store.getValue(item, "RN"),
      DIRECT: directUp ? "-1" : "1"
    });

    return !!response;
  },

  executeGetNextPriority: function() {
    var response = parus.database.execute("designer.BalanceTableModels.Totals.GetNextPriority", {
      PRN: this.prn
    });

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

});