dojo.provide("parus.balanceReportsExchangeImportDialog");

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

  kind: null,

  _lastValues: null,

  _files: null,

  applyProgressLabel: "Обмен отчетностью. Загрузка.",

  onLoad: function() {
    this._files = [this.fileData];
  },

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

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

  validate: function() {
    var valid = this.inherited(arguments);

    if (!this.fileData.disabled && valid) {
      if (!this.fileData.value) {
        dijit.showTooltip("Это обязательное значение", this.fileData);
        dojo.addClass(this.fileData, "dijitError");
        return false;
      }
      else {
        dojo.removeClass(this.fileData, "dijitError");
        dijit.hideTooltip(this.fileData);
      }
    }
    else {
      dojo.removeClass(this.fileData, "dijitError");
      dijit.hideTooltip(this.fileData);
    }

    var hasSlot = false;
    dojo.forEach(this._files, dojo.hitch(this, function(file) {
      if (!file.value) hasSlot = true;
    }));

    this.oneMoreFileButton.attr("disabled", hasSlot);

    return valid;
  },


  useJournalChanged: function() {
    if (this.useJournal.attr("checked")) {
      this.journalCatalog.attr("disabled", false);
      this.showJournal.attr("disabled", false);
      this.showJournalLabel.style.color = "";
      this.fileData.disabled = true;
      this.fileData.style.color = "#808080";
      this.fileData.value = null;
      this.reportTypeEdit.attr("disabled", true);
      this.reportTypeEdit.attr("value", "0");
      this.agentCode.attr("required", false);
    }
    else {
      this.journalCatalog.attr("disabled", true);
      this.journalCatalog.attr("value", null);
      this.showJournal.attr("disabled", true);
      this.showJournal.attr("checked", false);
      this.showJournalLabel.style.color = "#808080";
      this.fileData.disabled = false;
      this.fileData.style.color = "";
      this.reportTypeEdit.attr("disabled", false);
      this.agentCode.attr("required", true);
    }
  },


  exchangeKindChanged: function() {

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

    if (!response) return;

    var node = parus.parus8.rowNodes(response)[0];

    if (parus.parus8.nodeField(node, "NWEB_SIGN") == "1") {
      this.journalCatalog.attr("disabled", false);

      this.useJournal.attr("disabled", true);
      this.useJournal.attr("checked", true);
      this.useJournalLabel.style.color = "#808080";

      this.showJournal.attr("disabled", false);
      this.showJournalLabel.style.color = "";
      this.fileData.disabled = true;
      this.fileData.style.color = "#808080";
      this.fileData.value = null;
      this.reportTypeEdit.attr("disabled", true);
      this.reportTypeEdit.attr("value", "0");

    }
    else {
      this.journalCatalog.attr("disabled", true);
      this.journalCatalog.attr("value", null);

      this.useJournal.attr("disabled", false);
      this.useJournal.attr("checked", false);
      this.useJournalLabel.style.color = "";

      this.showJournal.attr("disabled", true);
      this.showJournal.attr("checked", false);
      this.showJournalLabel.style.color = "#808080";
      this.fileData.disabled = false;
      this.fileData.style.color = "";
      this.reportTypeEdit.attr("disabled", false);

    }
    this.validate();
  },

  saveFormLastValues: function() {
    this._lastValues["useJournal"] = this.useJournal.checked;
    this._lastValues["exchangeKind"] = this.exchangeKind.value;
    this._lastValues["showJournal"] = this.showJournal.checked;

    this._lastValues["reportDate"] = this.reportDate.value ? dojo.date.locale.format(this.reportDate.value, {
      selector: "date",
      datePattern: "dd.MM.yyyy"
    }) : "";

    this._lastValues["agentCode"] = this.agentCode.value;
    this._lastValues["reportTypeEdit"] = this.reportTypeEdit.value;
    this._lastValues["reportEdit"] = this.reportEdit.value;
    this._lastValues["canOverWrite"] = this.canOverWrite.attr("value");
    this._lastValues["recalcReports"] = this.recalcReports.checked;

    this._lastValues["journalCatalog"] = this.journalCatalog.value;
    parus.userdata.set(this.declaredClass, this._lastValues)
  },

  restoreFormLastValues: function() {
    this._lastValues = parus.userdata.get(this.declaredClass) || {};
    this.exchangeKind.attr("value", this._lastValues["exchangeKind"]);
    setTimeout(dojo.hitch(this, function() {
      this.useJournal.attr("checked", this._lastValues["useJournal"]);
      setTimeout(dojo.hitch(this, function() {
        this.showJournal.attr("checked", this._lastValues["showJournal"]);
        this.reportDate.attr("value", this._lastValues["reportDate"] ? dojo.date.locale.parse(this._lastValues["reportDate"], {
          datePattern: "dd.MM.yyyy",
          selector: "date"
        }) : null);
        this.agentCode.attr("value", this._lastValues["agentCode"]);
        this.reportTypeEdit.attr("value", this._lastValues["reportTypeEdit"]);
        this.reportEdit.attr("value", this._lastValues["reportEdit"]);
        this.canOverWrite.attr("value", this._lastValues["canOverWrite"]);
        this.recalcReports.attr("checked", this._lastValues["recalcReports"]);
        this.journalCatalog.attr("value", this._lastValues["journalCatalog"]);
      }), 100);
    }), 100);
  },

  fileDataChanged: function() {
    this.validate();
  },

  deleteFile: function(rowIndex, fileIndex) {
    return dojo.hitch(this, function() {
      this._files[fileIndex].value = null;
      this.validate();
    });
  },

  oneMoreFile: function() {

    var row = this.dataGroupBoxTable.insertRow(this.dataGroupBoxTableButtonRow.rowIndex);
    var cell = row.insertCell(-1);
    cell.colSpan = 2;
    cell.innerHTML = "<input size=\"32\" style=\"font-weight:bold\" onchange=\"dijit.getEnclosingWidget(this).dialogObject.fileDataChanged()\" type=\"file\" />";
    this._files.push(cell.firstChild);

    var cell = row.insertCell(-1);
    cell.align = "right";
    cell.width = "70";
    new dijit.form.Button({
      label: "Очистить",
      onClick: this.deleteFile(row.rowIndex, this._files.length - 1)
    }, cell.appendChild(document.createElement("span")));

    var c = dojo._getMarginSize(this.dialog.containerNode);
    c.h = c.h + 31;
    dojo.setMarginBox(this.dialog.containerNode, c.h);

    this.__sizeDialog.call(this.dialog);
    this.__positionDialog.call(this.dialog);
    this.validate();

    this.dataGroupBoxTable.rows[this.dataGroupBoxTable.rows.length - 1].scrollIntoView(true);

  }

});