dojo.provide("parus.balanceReportRecalculateValuesDialog");

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");

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

dojo.declare("parus.balanceReportRecalculateValuesDialog", [parus.Dialog], {

  noGrid: false,

  rn: null,

  isSubReport: false,

  applyProgressLabel: "Рассчитываются значения показателей...",

  onShow: function() {
    var defaults = this.getDefaultCalculationTypeValue();
    this.recalculateTypeEdit.attr("value", defaults["CALCTYPECODE"]);
    this.calculateResultSubReportsCheckBox.attr("checked", defaults["CALCRESULTS"]);
  },

  getDefaultCalculationTypeValue: function() {
    var result = {
      "CALCTYPECODE": "",
      "CALCRESULTS": false
    };

    if (!this.rn) {

      var grid = parus.balance.primaryMasterGrid;
      if (this.isSubReport) grid = parus.balance.primaryDetailGrid;
      var store = grid.store;
      var index = grid.selection.getSelectedIndex();
      var item = grid.getItem(index);
      this.rn = store.getValue(item, "RN");

    }

    if (this.rn || grid.selection.getSelectedCount() == 1) {

      var execMethodName = "getReportDefaultsRecalculateType";
      if (this.isSubReport) execMethodName = "getSubReportDefaultsRecalculateType";

      var response = parus.database.execute(execMethodName, {
        RN: this.rn
      });
      if (response) result = {
        "CALCTYPECODE": parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "CALCTYPECODE"),
        "CALCRESULTS": parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "CALCRESULTS") == "1"
      }
    }
    return result;
  },

  onApply: function() {

    var execMethodName = "recalculateReportValues";

    var grid = this.noGrid ? null : parus.balance.primaryMasterGrid;

    if (this.isSubReport) {
      execMethodName = "recalculateSubReportValues";

      grid = this.noGrid ? null : parus.balance.primaryDetailGrid;
    };

    if (grid) {

      var store = grid.store;
      var refreshSubreports = !this.isSubReport;
      var rescount = (this.calculateResultSubReportsCheckBox.checked ? 1 : 0);
      var calctypecode = this.recalculateTypeEdit.value;

      parus.progressDialog.iterateOverGrid(this.applyProgressLabel, grid,

      function(item) {
        parus.database.execute(execMethodName, {
          RN: store.getValue(item, "RN"),
          RESCOUNT: rescount,
          CALCTYPE_CODE: calctypecode
        });
      },

      function() {
        parus.balance.refreshGrid(grid, "RN", store.getValue(grid.selection.getFirstSelected(), "RN"));
        if (refreshSubreports) parus.balance.refreshGrid(parus.balance.primaryDetailGrid, "RN", parus.balance.primaryDetailGrid.store.getValue(parus.balance.primaryDetailGrid.selection.getFirstSelected(), "RN"));
      });

      return true;

    }
    else if (this.rn) {

      parus.database.execute(execMethodName, {
        RN: this.rn,
        RESCOUNT: (this.calculateResultSubReportsCheckBox.checked ? 1 : 0),
        CALCTYPE_CODE: this.recalculateTypeEdit.value
      });

      if (grid) parus.balance.refreshGrid(grid, "RN", this.rn);

    };

    if (!this.isSubReport && grid) parus.balance.refreshGrid(parus.balance.primaryDetailGrid, "RN", parus.balance.primaryDetailGrid.store.getValue(parus.balance.primaryDetailGrid.selection.getFirstSelected(), "RN"));

    return true;
  },

  show: function() {

    if (parus.parus8.optionValue("Balance_ShowCalculationParamsForm") != "0") return this.inherited(arguments)
    else {

      if (this.noGrid) {

        var progressIndicator = new parus.progressDialog({
          message: this.applyProgressLabel
        });
        progressIndicator.show();
        setTimeout(dojo.hitch(this, function() {
          this.onShow();
          this.applied = true;
          this.onApply();
          progressIndicator.close();
          progressIndicator.destroyRecursive();

          this.onClose();
        }), 1000);

      }
      else return (new parus.confirmationDialog({

        message: "Пересчитать значения " + (this.isSubReport ? "подотчета" : "отчета") + "?",

        onApply: dojo.hitch(this, function() {


          var progressIndicator = new parus.progressDialog({
            message: this.applyProgressLabel
          });
          progressIndicator.show();
          setTimeout(dojo.hitch(this, function() {
            this.onShow();
            this.applied = true;
            this.onApply();
            progressIndicator.close();
            progressIndicator.destroyRecursive();

            this.onClose();
          }), 1000);

          return true;

        }),

        onClose: dojo.hitch(this, this.onClose)

      })).show();



    }
  }

});