dojo.provide("parus.balanceUserReportParamsDialog");

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

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

dojo.require("parus.customEdit");
dojo.require("parus.excelFileFormatDialog");
dojo.require("parus.excelColorModeDialog");
dojo.require("parus.balanceUserProcParamsDialog");

dojo.declare("parus.balanceUserReportParamsDialog", [parus.balanceUserProcParamsDialog], {

  queryid: "userRepParams",

  rns: null,

  unitcode: null,

  userreporttype: null,

  postCreate: function() {
    this.inherited(arguments);
    this._storeKey = "parus.balanceUserProcParamsDialog";
    if (this.userreporttype != "4") this.OKButton.attr("dropDown", this.menu);
    if (!(parus.database.attributes.useReportsQueue == "1")) this.QueueButton.domNode.style.display = "none";
  },

  downloadExcel: function() {
    if (!this.validate()) return;
    this._download("xls");
  },

  downloadPDF: function() {
    if (!this.validate()) return;
    this._download("pdf");
  },

  downloadHTML: function() {
    if (!this.validate()) return;
    this._download("html");
  },

  enqueue: function() {

    var response = parus.database.execute("enqueueUserReport", {
      RN: this.rns,
      UNITCODE: this.unitcode,
      USERREPORT: this.userproc,
      PARAMS: parus.xml.toXml({
        "PARAMS": dojo.clone(this.paramValues())
      })
    });

    if (!response) return;

    (new parus.confirmationDialog({
      message: "Пользовательский отчет поставлен в очередь. Получить отчет?",
      onApply: function() {
        setTimeout(function() {
          parus.parus8.enqueuedUserReport(parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NQUEUE"));
        }, 100);
        return true;
      }
    })).show();

    this.close();
  },

  _download: function(extension, colormode) {

    if (parus.parus8._themeEditorMode) {
      alert("В режиме редактирования схемы это действие невозможно.");
      return;
    }

    var f = dojo.hitch(this, function(extension, colormode) {

      var form = document.createElement("form");
      form.method = "POST";
      form.action = parus.database.getUserReportUrl(this.userreport, extension);
      //if ( /*!dojo.isChrome ||*/ (extension == "html"))
      if (!parus.parus8._embeddedMode) form.target = "_blank";

      var input = document.createElement("input");
      input.type = "hidden";
      input.name = "query";
      input.value = parus.xml.toXml({
        query: {
          rn: this.rns,
          unitcode: this.unitcode,
          userreport: this.userproc,
          colormode: colormode,
          params: parus.xml.toXml({
            "PARAMS": dojo.clone(this.paramValues())
          })
        }
      });

      form.appendChild(input);

      document.body.appendChild(form);
      form.submit();
      document.body.removeChild(form);

      this.close();

    });

    if (extension == "xls") {
      if (parus.database.attributes.newoffice == "1") {
        (new parus.excelFileFormatDialog({
          onApply: function() {
            f(this.formatEdit.value, colormode);
            return true;
          }
        })).show();
      } else if (parus.database.attributes.newoffice == "2") f("xlsx", colormode)
      else f("xls", colormode);
    }
    else if ((extension == "pdf" || extension == "html") && (colormode === undefined || colormode === null)) {
      (new parus.excelColorModeDialog({
        onApply: function() {
          f(extension, this.colorModeEdit.value);
          return true;
        }
      })).show();
    }
    else f(extension, colormode);

  }

});