dojo.provide("parus.balanceAdvReportSelectDialog");

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");
dojo.require("dijit.TitlePane");
dojo.require("dijit.form.DateTextBox");
dojo.require("dijit.form.CheckBox");

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

dojo.require("parus.balanceAdvReportCompareDialog");

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

  _lastValues: null,

  _canCheckCriteria: true,

  protocol: "",

  applyProgressLabel: "Выполняется подбор строк...",

  postCreate: function() {
    this.inherited(arguments);

    this.criteriaTreeView._createTreeNode = function(args) {
      return new parus.balanceAdvReportCompareCriteriaTreeNode(args);
    };

    this.criteriaTreeView.model = new parus.catalogTreeModel({
      queryid: "BalanceCompareReportRuleCriteriaHier",
      onGetConditions: dojo.hitch(this, function(model, parentNode) {
        return {
          "RULE_CODE": this._lastValues["rule"],
          "CRN": (parentNode ? model.getIdentity(parentNode) || "-1" : "!*")
        }
      })
    });

  },

  onShow: function() {
    parus.catalogActions.refreshCatalogTree(this.criteriaTreeView);
    this.restoreFormLastValues();

  },

  saveFormLastValues: function() {
    this._lastValues = {};
    this._lastValues["groups"] = this.groupsEdit.attr("value");
    this._lastValues["params"] = this.buildParams();
    this._lastValues["userSize"] = this.userSize;
    parus.userdata.set(this.declaredClass + this.protocol, this._lastValues);
  },

  restoreFormLastValues: function() {
    var _lastValues = parus.userdata.get(this.declaredClass + this.protocol) || {}
    dojo.mixin(this._lastValues, _lastValues);
    this.userSize = this._lastValues["userSize"] || null;

    setTimeout(dojo.hitch(this, function() {
      this.groupsEdit.attr("value", this._lastValues["groups"] || null);
    }), 300);

    this.criteriaTreeView.model.fetchAll(dojo.hitch(this, function() {

      var params = this._lastValues["params"];
      if (params) params = params["Conditions"];
      if (params) params = params["Criteria"];

      if (params) if (params.length) {
        var ids = [];
        dojo.forEach(params, function(param) {
          for (key in param)
          ids.push(param[key]);
        });

        setTimeout(dojo.hitch(this, function() {

          (function(node) {
            if (!node) return;
            node._checkBox.attr("checked", dojo.indexOf(ids, parus.parus8.nodeField(node.item, "ID")) != -1)
            dojo.forEach(node.getChildren() || [], arguments.callee);
          })(this.criteriaTreeView.rootNode);

        }), 300);

      };
    }));
  },

  onApply: function() {
    //overriden.
    this.saveFormLastValues();
    return true;
  },

  getSectionGroupsCondition: function() {
    return {
      "RULE_CODE": this._lastValues["rule"] || parus.parus8.CLASS_EmptyData
    };
  },


  buildParams: function() {

    var criteria = [];

    this.criteriaTreeView.model.fetchAll();

    (function(node) {
      if (!node) return;
      if (node._checkBox.checked)
      //ВНИМАНИЕ! В теге - русская буква. Слава программистам Паруса!
      criteria.push({
        "Сriterion": parus.parus8.nodeField(node.item, "ID")
      });
      dojo.forEach(node.getChildren() || [], arguments.callee);
    })(this.criteriaTreeView.rootNode);

    if (!criteria.length) {
      parus.parus8.errorMessage("Не выбраны критерии сравнения.");
      throw new Error("Dialog element is not defined for class " + this.declaredClass + ".");
    };

    return {
      "Conditions": {
        "SectionGroups": this.groupsEdit.value || "",
        "Criteria": criteria
      }
    };
  }
});