dojo.provide("parus.balanceCheckReportDialog");

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

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

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

  grid: null,

  kind: null,

  isSubReport: null,

  rn: null,

  formedRn: null,

  reportRn: null,

  checks: null,

  _lastValues: null,

  applyProgressLabel: "Проверка значений показателей...",

  onShow: function() {
    this._lastValues = parus.userdata.get(this.declaredClass) || {};

    this.checkResults.attr("checked", this._lastValues["checkResults"]);
    this.useAgnCheckLists.attr("checked", this._lastValues["useAgnCheckLists"]);

    this.showReportCheckBox.attr("checked", this._lastValues["showReportCheckBox"]);
    this.protocolChanged();
    this.showResultReportCheckBox.attr("checked", this._lastValues["showResultReportCheckBox"]);
    this.userSize = this._lastValues["userSize"] || null;

  },

  onApply: function() {
    var execMethodName = "checkReport";
    if (this.isSubReport) execMethodName = "checkSubReport";


    var params = {
      RESULT: (this.checkResults.checked ? "1" : "0"),
      USE_AGNCHKLST: (this.useAgnCheckLists.checked ? "1" : "0"),
      CHECKS: this.checks
    };

    var showReportCheckBox = this.showReportCheckBox.checked;

    var grid = this.grid;
    var rn = this.rn;
    var rn0 = rn[0];

    var kind = this.kind;
    var isSubReport = this.isSubReport;
    var showResultReportCheckBox = this.showResultReportCheckBox.checked;
    var checks = this.checks;
    var allOK = true;

    parus.progressDialog.iterateOverArray(this.applyProgressLabel, rn, function(item) {
      dojo.mixin(params, {
        RN: item
      });

      if (allOK) {
        if (!parus.database.execute(execMethodName, params)) allOK = false;
      }
    },

    function() {
      if (allOK) {
        if (showReportCheckBox) parus.balance.balanceShowCheckReportProtocol(grid, kind, isSubReport, showResultReportCheckBox, rn, checks);
        else parus.parus8.notificationMessage("Проверка завершена.");
      }
      if (grid) parus.balance.refreshGrid(grid, "RN", rn0);
    }

    );

    this._lastValues["checkResults"] = this.checkResults.attr("checked");
    this._lastValues["useAgnCheckLists"] = this.useAgnCheckLists.attr("checked");

    this._lastValues["showReportCheckBox"] = this.showReportCheckBox.attr("checked");
    this._lastValues["showResultReportCheckBox"] = this.showResultReportCheckBox.attr("checked");

    this._lastValues["userSize"] = this.userSize;

    parus.userdata.set(this.declaredClass, this._lastValues);

    return true;
  },

  showProtocol: function() {
    parus.balance.balanceShowCheckReportProtocol(this.grid, this.kind, this.isSubReport, this.showResultReportCheckBox.checked, this.rn, this.checks);
  },

  selectCheckList: function() {

    var dialog;

    var dictionaryInfo = parus.dictionaries["BalanceReportsChecks"];
    dojo.mixin(dictionaryInfo, {
      multiselect: true,
      dictionary: dictionaryInfo,
      gridConditions: {
        REPORTS: this.reportRn.join(";"),
        BLREPFORMED: this.formedRn.join(";"),
        USE_AGNCHKLST: (this.useAgnCheckLists.checked ? "1" : "0"),
        onComplete: dojo.hitch(this, function() {
          setTimeout(function() {
            dialog.grid.selection.selectRange(0, dialog.grid.rowCount - 1);
          }, 1);
        })
      },

      onApply: dojo.hitch(this, function(dialog) {
        this.checks = dialog.getValue();
        return true;
      })
    });

    dialog = new parus.dictionaryDialog(dictionaryInfo);
    dialog.show();
  },

  protocolChanged: function() {
    if (this.showReportCheckBox.checked) {
      this.showResultReportCheckBox.attr("disabled", false);
      this.showResultReportCheckBoxText.style.color = "";
      dojo.removeClass(this.showResultReportCheckBoxText, "balanceReadOnlyHeader");
    }
    else {
      this.showResultReportCheckBox.attr("disabled", true);
      this.showResultReportCheckBox.attr("checked", false);
      dojo.addClass(this.showResultReportCheckBoxText, "balanceReadOnlyHeader");
    };
  }

});