dojo.provide("parus.balanceReportsFileImportDialog");

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

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

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

  _lastValues: null,

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

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

  saveFormLastValues: function() {

    this._lastValues["userProcEdit"] = this.userProcEdit.value;
    this._lastValues["binaryDataCheckBox"] = this.binaryDataCheckBox.attr("checked");
    this._lastValues["userSize"] = this.userSize;
    parus.userdata.set(this.declaredClass, this._lastValues);
  },

  restoreFormLastValues: function() {
    this._lastValues = parus.userdata.get(this.declaredClass) || {};
    this.userProcEdit.attr("value", this._lastValues["userProcEdit"]);
    this.binaryDataCheckBox.attr("checked", this._lastValues["binaryDataCheckBox"]);
    this.userSize = this._lastValues["userSize"] || null;
  },

  performLoad: function() {
    if (!this.validate()) return;

    if (!this.fileData.value) return;

    var response = parus.database.execute("FindUserProcedure", {
      CODE: this.userProcEdit.value
    });

    if (!response) return false;

    this.OKButton.attr("disabled", true);
    this.CancelButton.attr("disabled", true);

    var paramsDialog = new parus.balanceUserProcParamsDialog({

      useSavedValues: parus.parus8.optionValue("StoreUserProcsParams") != "0",

      userproc: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NRN"),

      onApply: dojo.hitch(this, function() {

        this.impparams.value = parus.xml.toXml({
          "PARAMS": {
            "PARAMS": paramsDialog.paramValues()
          }
        });
        this.unitcode.value = "BalanceReports";
        this.userprocValue.value = paramsDialog.userproc;

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

        var progressIndicator = new parus.progressDialog({
          message: "Выполняется загрузка"
        });

        progressIndicator.show();

        dojo.io.iframe.send({

          method: "POST",
          form: this.postForm,
          handleAs: (dojo.isIE >= 10) ? "html" : "xml",

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

            progressIndicator.close();
            progressIndicator.destroyRecursive();

            this.OKButton.attr("disabled", false);
            this.CancelButton.attr("disabled", false);

            if (response.documentElement.nodeName == "error") parus.parus8.errorMessage(parus.parus8.nodeField(response.documentElement, "message"))
            else(new dojo.DeferredList([this.dialog.hide()])).then(
            dojo.hitch(this, function() {
              this.apply();

              if (parus.parus8.nodeField(parus.parus8.rowNodes(response.documentElement)[0], "HASJOURNAL") == "1") {


                var dictionaryInfo = parus.dictionaries["MessagesJournal"];

                dojo.mixin(dictionaryInfo, {
                  dictionary: dictionaryInfo,
                  hideCancel: true,
                  gridConditions: {
                    IDENT: parus.parus8.nodeField(parus.parus8.rowNodes(response.documentElement)[0], "IDENT")
                  },
                  onApply: dojo.hitch(this, function(dialog) {
                    return true;
                  })
                });

                new parus.dictionaryDialog(dictionaryInfo).show();


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

            }));


          })


        });

        return true;
      }),

      onClose: dojo.hitch(this, function() {
        paramsDialog.saveFormLastValues();
        this.OKButton.attr("disabled", false);
        this.CancelButton.attr("disabled", false);
      })
    });

    paramsDialog.show();
  }


});