dojo.provide("parus.balanceReportDialog");

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

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

dojo.require("parus.customEdit");

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

  _agentCodeLoaded: false,

  _agentNameLoaded: false,

  _dateIsSet: false,

  _docprops: null,

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

    this.noteEdit.attr("value", null);
    this._docprops = {};

    if (parus.balance._docprops.length) {

      this.requsitesPane.domNode.style.margin = "0";
      var tabs = new dijit.layout.TabContainer({
        region: "center",
        "class": "balanceInternalPages"
      });
      tabs.addChild(this.requsitesPane);

      var propsPane = new dijit.layout.ContentPane({
        title: "Свойства",
        "class": "dialogContentPane",
        style: "overflow-y:scroll"
      });
      var propsTable = document.createElement("table");
      propsTable.cellSpacing = "5";
      propsTable.width = "100%";

      dojo.forEach(parus.balance._docprops, dojo.hitch(this, function(docprop) {
        var row = propsTable.insertRow(-1);
        var titleCell = row.insertCell(-1);
        titleCell.appendChild(document.createElement("b")).
        appendChild(document.createTextNode(parus.parus8.nodeField(docprop, "PROMPT") + ":"));

        var editCell = row.insertCell(-1);
        editCell.align = "right";

        var params = parus.customEditProperties(
        parus.parus8.nodeField(docprop, "DICTIONARY"), parus.parus8.nodeField(docprop, "DATATYPE") == "3" ? "4" : parus.parus8.nodeField(docprop, "DATATYPE"), parus.parus8.nodeField(docprop, "SUBTYPE"), parus.parus8.nodeField(docprop, "REQUIRE") == "1", parus.parus8.nodeField(docprop, "STRLEN"), parus.parus8.nodeField(docprop, "INTPART"), parus.parus8.nodeField(docprop, "FRACPART"));

        dojo.mixin(params.params, {
          disabled: parus.parus8.nodeField(docprop, "READONLY") == "1"
        });

        var editWidget = new(params.constructor())(params.params, editCell.appendChild(document.createElement("div")));

        this._docprops[parus.parus8.nodeField(docprop, "RN")] = editWidget;
      }));

      propsPane.containerNode.appendChild(propsTable);
      tabs.addChild(propsPane);


      this.mainContainer.addChild(tabs);

      dojo.style(this.mainContainer.domNode, {
        height: (parus.parus8.styleSize(this.mainContainer.domNode.style.height) + 30) + "px"
      });

    };

    if (parus.balance._masterUserCalcs.length) this.userCalcsButton.domNode.style.display = "";

  },


  agentsChanged: function(sender) {

    if (sender) if (sender.disabled) return;

    if ((this.agentCodeEdit.value == "") && (this.agentNameEdit.value == "")) {
      this.agentCodeEdit.attr("disabled", false);
      this.agentNameEdit.attr("disabled", false);
    }
    else if ((this.agentCodeEdit.value != "") && (sender.dojoAttachPoint == "agentCodeEdit")) {
      if (this._agentCodeLoaded) {
        this._agentCodeLoaded = false;
      }
      else {
        this.agentNameEdit.attr("disabled", true);
        this.agentNameEdit.attr("value", null);
      }
    }
    else if ((this.agentNameEdit.value != "") && (sender.dojoAttachPoint == "agentNameEdit")) {
      if (this._agentNameLoaded) {
        this._agentNameLoaded = false;
      }
      else {
        this.agentCodeEdit.attr("disabled", true);
        this.agentCodeEdit.attr("value", null);
      }
    }

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

      var agentInfo = parus.parus8.rowNodes(parus.database.execute("getAgentInfo", {
        "AGENT_CODE": this.agentCodeEdit.disabled ? "" : this.agentCodeEdit.value,
        "AGENT_NAME": this.agentNameEdit.disabled ? "" : this.agentNameEdit.value
      }))[0];

      if (this.agentCodeEdit.disabled) this.agentCodeEdit.attr("value", parus.parus8.nodeField(agentInfo, "RAGNABBR"));
      if (this.agentNameEdit.disabled) this.agentNameEdit.attr("value", parus.parus8.nodeField(agentInfo, "RAGNNAME"));
      if (this.agentHeadEdit.disabled) this.agentHeadEdit.attr("value", parus.parus8.nodeField(agentInfo, "RHEAD"));

      if ((this.agentCodeEdit.value == "") && (this.agentNameEdit.value == "")) {
        this.agentCodeEdit.attr("disabled", false);
        this.agentNameEdit.attr("disabled", false);
      };


    }), 100);


  },

  formChanged: function() {
    if (this._dateIsSet) {
      this._dateIsSet = false;
      return;
    };
    if (this.formCodeEdit.value != "") {
      var response = parus.database.execute("findNearestPeriodDate", {
        SFORM: this.formCodeEdit.value
      });
      if (response) this.reportDateEdit.attr("value", parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "RESULT"));
    };
  },

  getDatesCondition: function() {
    return {
      "FORM_CODE": this.formCodeEdit.value || parus.parus8.CLASS_EmptyData
    };
  },

  propValues: function() {
    var result = {};

    for (var key in this._docprops)
    result["P" + key] = parus._docPropsValueValueFromWidget(this._docprops[key]);

    return {
      DOCPROPS: result
    };
  },

  _userCalcParams: null,

  paramsForUserCalc: function() {

    var params = this._userCalcParams || {};
    dojo.mixin(params, {
      "SAGENT_CODE": this.agentCodeEdit.value,
      "SAGENT_NAME": this.agentNameEdit.value,
      "DBDATE": this.reportDateEdit.value,
      "SNOTE": this.noteEdit.value,
      "SFORM_CODE": this.formCodeEdit.value,
      "NUSE_HISTORY": this.historySelect.value,
      "NONLY_LAST_HISTORY": (this.historyLastCheckBox.checked ? 1 : 0)
    });
    return params;

  },

  paramsFromUserCalc: function(values) {
    if (values["SAGENT_CODE"]) {
      this.agentCodeEdit.attr("value", values["SAGENT_CODE"]);
      this.agentsChanged(this.agentCodeEdit);
    }
    if (values["SAGENT_NAME"]) {
      this.agentNameEdit.attr("value", values["SAGENT_NAME"]);
      this.agentsChanged(this.agentNameEdit);
    }
    if (values["DBDATE"]) this.reportDateEdit.attr("value", values["DBDATE"]);
    if (values["SNOTE"]) this.noteEdit.attr("value", values["SNOTE"]);
    if (values["SFORM_CODE"]) {
      this.formCodeEdit.attr("value", values["SFORM_CODE"]);
      this.formChanged();
    }
    if (values["NUSE_HISTORY"]) this.historySelect.attr("value", values["NUSE_HISTORY"]);
    if (values["NONLY_LAST_HISTORY"]) this.historyLastCheckBox.attr("checked", values["NONLY_LAST_HISTORY"] = "1");

  },

  userCalcs: function() {

    parus.balance.executeUserCalcs(parus.balance._masterUserCalcs, true, true, dojo.hitch(this, this.paramsForUserCalc), dojo.hitch(this, this.paramsFromUserCalc));

  }

});