dojo.provide("parus.balanceReportAttachedDocumentActionDialog");

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

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

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

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

  grid: null,

  prn: null,

  rn: null,
  docCode: null,
  docType: null,
  catalog: null,
  note: null,
  unitcode: "BalanceReports",

  maxSize: null,

  _canChangeCatalog: false,

  _lastValues: null,

  postCreate: function() {
    this.inherited(arguments);
    this.noteInput.attr("value", null);
    dojo.mixin(this.docCodeInput, {
      dictButtonClick: function() {
        parus.database.execute("GetAttachedDocumentNextNumb", {
          PREFIX: this.textbox.value
        }, dojo.hitch(this, function(response) {
          this.setValue(parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "RESULT"));
        }));
        return true;
      }
    });

  },

  getFileSize: function() {
    if (this.fileDataInput.files) return this.fileDataInput.files[0].size;
    else return 0;
  },

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

    if (this.maxSize) {
      if (this.getFileSize() / 1024 > this.maxSize) {
        parus.parus8.errorMessage("Размер файла присоединённого документа превышает максимальный (" + this.maxSize + " килобайт).");
        return;
      }
    };

    var progressIndicator = new parus.progressDialog({
      message: "Загружается файл..."
    });
    progressIndicator.show();



    this.prnInput.value = this.prn;
    if (this.rn) this.rnInput.value = this.rn;

    this.unitcodeInput.value = this.unitcode;

    this.postForm.action = parus.database.getLoadAttachedDocumentUrl();

    this.postForm.method = "POST";
    this.postForm.encoding = "multipart/form-data";

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

      handle: dojo.hitch(this, function(response, ioArgs) {
        if (!dojo.isIE) response.normalize();

        if (response.documentElement.nodeName == "error") {
          parus.parus8.errorMessage(parus.parus8.nodeField(response.documentElement, "message"));
          response = null;
        };

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

        if (response) {
          if (this.grid) if (this.rn) parus.balance.refreshGrid(this.grid, "RN", this.rn)
          else parus.balance.refreshGrid(this.grid, "RN", parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "RN"));

          this.apply();
        };

      })
    });
  },

  onShow: function() {
    if (!this.rn) {
      this.restoreFormLastValues();
      this.docCodeInput.dictButtonClick();
    }

    else {
      this.docCodeInput.attr("value", this.docCode);
      this.docTypeInput.attr("value", this.docType);
      this.catalogInput.attr("value", this.catalog);
      this.noteInput.attr("value", this.note);

      this.docTypeInput.attr("readOnly", true);
      this.docCodeInput.attr("readOnly", true);
      this.catalogInput.attr("readOnly", true);

      this.fileDataInput.disabled = true;
    };

    setTimeout(dojo.hitch(this, function() {
      this._canChangeCatalog = true;
    }), 300);

  },

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

  saveFormLastValues: function() {

    this._lastValues["userSize"] = this.userSize;
    this._lastValues["docTypeInput"] = this.docTypeInput.attr("value");
    this._lastValues["catalogInput"] = this.catalogInput.attr("value");

    parus.userdata.set(this.declaredClass, this._lastValues);
  },

  restoreFormLastValues: function() {
    this._lastValues = parus.userdata.get(this.declaredClass) || {};

    this.userSize = this._lastValues["userSize"] || null;

    this.docTypeInput.attr("value", this._lastValues["docTypeInput"]);
    this.catalogInput.attr("value", this._lastValues["catalogInput"]);

  },

  docTypeChanged: function() {

    if (!this._canChangeCatalog) return;

    var info = parus.parus8.rowNodes(parus.database.query("AttachedDocumentTypesCatalog", {
      "CODE": this.docTypeInput.value
    }))[0];
    if (info) {
      this.catalogInput.attr("value", parus.parus8.nodeField(info, "CRNFLNKS_NAME"));
      this.maxSize = parus.parus8.nodeField(info, "MAX_SIZE");
    }
    else {
      this.catalogInput.attr("value", null);
    }
  }

});