dojo.provide("parus.balanceCheckReportCheckDialog");

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.balanceCheckReportCheckDialog", [parus.Dialog], {

  grid: null,

  kind: null,

  isSubReport: null,

  rn: null,

  formedRn: null,

  reportRn: null,

  checks: null,

  _lastValues: null,

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

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

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

  },

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

    var allOK = true;

    for (var i = 0; i < this.rn.length; i++) {
      if (allOK) {

        if (!parus.database.execute(execMethodName, {
          RN: this.rn[i],
          RESULT: (this.checkResults.checked ? "1" : "0"),
          USE_AGNCHKLST: (this.useAgnCheckLists.checked ? "1" : "0"),
          CHECKS: this.checks
        })) allOK = false;

      }
    };

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


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

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

    if (allOK) parus.parus8.notificationMessage("Проверка завершена.");

    if (this.grid) {
      parus.balance.refreshGrid(this.grid, "RN", this.rn[0]);
    }

    return true;
  },

  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();
  }

});