dojo.provide("parus.balanceLoadReportsDialog");

dojo.require("dojo.io.iframe");

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

  catalogTree: null,

  kind: null,

  onShow: function() {
    this.restoreFormLastValues();
  },

  onClose: function() {
    this.saveFormLastValues();
  },

  saveFormLastValues: function() {

    this._lastValues["canOverWrite"] = this.canOverWrite.checked ? 1 : 0;
    this._lastValues["canSkipValues"] = this.canSkipValues.checked ? 1 : 0;
    this._lastValues["canSkipSubreports"] = this.canSkipSubreports.checked ? 1 : 0;
    this._lastValues["recalcReports"] = this.recalcReports.checked ? 1 : 0;
    this._lastValues["distrByCatalog"] = this.distrByCatalog.checked ? 1 : 0;
    this._lastValues["showReport"] = this.showReport.checked ? 1 : 0;
    this._lastValues["agent"] = this.agentEdit.attr("value") || "";
    this._lastValues["userSize"] = this.userSize;
    parus.userdata.set(this.declaredClass, this._lastValues);

  },

  restoreFormLastValues: function() {
    this._lastValues = parus.userdata.get(this.declaredClass) || {};
    this.canOverWrite.attr("checked", this._lastValues["canOverWrite"]);
    this.canSkipValues.attr("checked", this._lastValues["canSkipValues"]);
    this.canSkipSubreports.attr("checked", this._lastValues["canSkipSubreports"]);
    this.recalcReports.attr("checked", this._lastValues["recalcReports"]);
    this.showReport.attr("checked", this._lastValues["showReport"]);
    this.distrByCatalog.attr("checked", this._lastValues["distrByCatalog"]);
    this.agentEdit.attr("value", this._lastValues["agent"]);
    this.userSize = this._lastValues["userSize"] || null;
  },

  _performLoad: function(catalog) {

    this.catalogInput.value = catalog;

    this.postForm.action = parus.database.getLoadReportsUrl();
    this.postForm.method = "POST";
    this.postForm.encoding = "multipart/form-data";

    this.postForm.appendChild(this.fileData);

    this.postForm["canOverWrite"].value = this.canOverWrite.checked ? "on" : "off";
    this.postForm["canSkipValues"].value = this.canSkipValues.checked ? "on" : "off";
    this.postForm["canSkipSubreports"].value = this.canSkipSubreports.checked ? "on" : "off";
    this.postForm["recalcReports"].value = this.recalcReports.checked ? "on" : "off";
    this.postForm["distrByCatalog"].value = this.distrByCatalog.checked ? "on" : "off";
    this.postForm["showReport"].value = this.showReport.checked ? "on" : "off";
    this.postForm["agent"].value = this.agentEdit.attr("value") || "";
    this.postForm["kind"].value = this.kind;

    dojo.io.iframe.send({

      method: "POST",
      form: this.postForm,
      handleAs: "html",

      handle: dojo.hitch(this, function(response, ioArgs) {
        if (this.showReport.checked) {


          if (parus.parus8._embeddedMode) {
            var id = ["customDownloadReport"].join("_");
            var textID = "T" + (+(new Date));
            var title = "Протокол загрузки отчетов";

            mill.session.setValue(textID, response.body.innerHTML, false);

            var url = dojo.moduleUrl("parus", "embeddedContent.html") + "?loggedUserHandler=" + parus.database.connected + "&params=" + parus.parus8.hexEncodeParams([textID, title, 1, ""]);

            parus.embeddedMode.openTab((+(new Date)), title, url);

          }
          else {


            var contentPane;

            var borderContainer = new dijit.layout.BorderContainer({
              design: "headline",
              gutters: false,
              "class": "balanceTemplateContentPane",
              style: "height:100%;width:100%;padding:0"
            });

            var headingContentPane = parus.balance._createHeadingPane("Протокол загрузки отчетов");

            borderContainer.addChild(headingContentPane);

            var templateGridPane = new dijit.layout.ContentPane({
              region: "center",
              content: response.body.innerHTML
            });

            borderContainer.addChild(templateGridPane);

            contentPane = new dijit.layout.ContentPane({
              title: parus.balance.formatPageTitle("Протокол<br/>загрузки отчетов"),
              closable: true,
              "class": "balanceTemplateContentPane",
              onClose: dojo.hitch(this, function() {
                parus.balance.TabContainer.selectChild((this.kind == "1") ? parus.balance.consolidatedPage : parus.balance.primaryPage);
                return true;
              })

            });

            templateGridPane.parentPane = contentPane;

            contentPane.setContent(borderContainer);

            parus.balance.TabContainer.addChild(contentPane);
            parus.balance.TabContainer.selectChild(contentPane);
          }

        }
        else parus.parus8.notificationMessage("Загрузка выполнена.");
        this.apply();
      })

    });



  },


  performLoad: function() {

    if (!this.validate()) return;

    if (!this.fileData.value) return;


    var distrByCatalog = this.distrByCatalog;
    var catalogTree = this.catalogTree;


    (new parus.treeDialog({

      model: parus.balance.catalogTreeStoreModel(),

      key: "BalanceReports",

      onDefaultNeeded: function() {
        var response = parus.database.execute("findAvailableCatalog", {
          UNITCODE: "BalanceReports"
        });
        return response ? parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "PATH") : null;
      },

      onSingle: function() {
        if (catalogTree.hiddenMode && !this.distrByCatalog.checked)

        return parus.parus8.nodeField(parus.parus8.rowNodes(parus.database.execute("singleCatalog", {
          "UNITCODE": "BalanceReports",
          "ACTION_CODE": "BLREPORTS_CUSTOM_IMPORT"
        }))[0], "CATALOG");

        else {
          var path = catalogTree.path;
          return catalogTree.model.getIdentity(path[path.length - 1]);
        }

      },

      onPerform: dojo.hitch(this, function(catalog) {

        this._performLoad(catalog);

        return true;
      })

    })).show();

  }


});