dojo.provide("parus.balanceUserProcParamsDialog");

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

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

dojo.require("parus.customEdit");

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

  useSavedValues: true,

  userproc: null,

  queryid: "userProcParams",

  _widgets: null,

  _blobsForValidation: null,

  _storeKey: null,

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


  saveFormLastValues: function() {
    var obj = this.paramValues() || {};

    for (var key in obj)
    if (dojo.isObject(obj[key])) delete obj[key];

    dojo.mixin(obj, {
      userSize: this.userSize
    });
    parus.userdata.set(this._storeKey + "-" + this.queryid + "-" + this.userproc, obj, true);
  },

  restoreFormLastValues: function() {

    var lastValues = parus.userdata.get(this._storeKey + "-" + this.queryid + "-" + this.userproc) || {};
    this.userSize = lastValues["userSize"] || null;
    if (this.useSavedValues) for (var key in lastValues) {
      var widget = this._widgets[key];
      if (widget) {
        if (widget.declaredClass == "dijit.form.CheckBox") widget.attr("checked", lastValues[key] == "1")
        else if (widget.declaredClass == "dijit.form.DateTextBox") widget.attr("value", dojo.date.locale.parse(lastValues[key], {
          selector: "date",
          datePattern: "dd.MM.yyyy"
        }))
        else widget.attr("value", lastValues[key])
      }
    }
  },

  show: function() {
    if (this._widgets.length) {
      this.restoreFormLastValues();
      this.inherited(arguments);
    }
    else this._noParameters();
  },

  _noParameters: function() {
    this.onApply(this);
  },

  paramValues: function() {

    var result = {};

    for (var key in this._widgets) {
      if (key != "length") {
        if (this._widgets[key].declaredClass == "dijit.form.CheckBox") result[key] = this._widgets[key].checked ? "1" : "0"
        else if (this._widgets[key].declaredClass == "parus.parus8.blobEditor") result[key] = this._widgets[key]
        else if (this._widgets[key].declaredClass == "parus.parus8.clobEditor") result[key] = this._widgets[key]
        else result[key] = parus.parus8.valueToExternalString(parus._docPropsValueValueFromWidget(this._widgets[key]));

      }
    }

    return result;

  },

  postCreate: function() {
    this.inherited(arguments);
    this._storeKey = this.declaredClass;

    var params = parus.parus8.rowNodes(parus.database.query(this.queryid, {
      "RN": this.userproc
    }));

    this._widgets = {
      length: 0
    };

    this._blobsForValidation = [];

    dojo.forEach(params, dojo.hitch(this, function(param) {

      var row = this.paramsTable.insertRow(-1);

      var titleCell = row.insertCell(-1);
      titleCell.appendChild(document.createElement("b")).
      appendChild(document.createTextNode(parus.parus8.nodeField(param, "NAME") + ":"));
      titleCell.width = "50%";

      var editCell = row.insertCell(-1);

      if (parus.parus8.nodeField(param, "DATATYPE") != "3") editCell.align = "right";

      var params = parus.customEditProperties(parus.parus8.nodeField(param, "DICTIONARY"), parus.parus8.nodeField(param, "DATATYPE"), parus.parus8.nodeField(param, "SUBTYPE") || "0", parus.parus8.nodeField(param, "REQUIRE") == "1", null, null, null);

      if (parus.parus8.nodeField(param, "DATATYPE") == "3") dojo.mixin(params.params, {
        checked: parus.parus8.nodeField(param, "VALUE") == "1"
      })
      else dojo.mixin(params.params, {
        value: parus.parus8.externalStringToValue(parus.parus8.nodeField(param, "VALUE"), parus.parus8.nodeField(param, "DATATYPE"))
      });

      dojo.mixin(params.params, {
        multiselect: !! (parus.parus8.nodeField(param, "DATATYPE") == "0"),
        arbitrary: true
      });

      var editWidget = new(params.constructor())(params.params, editCell.appendChild(document.createElement("div")));

      if (
      params.params.required && (
      parus.parus8.nodeField(param, "DATATYPE") == "5" || parus.parus8.nodeField(param, "DATATYPE") == "6")) {

        this._blobsForValidation.push(editWidget);

        dojo.mixin(editWidget, {
          onChange: dojo.hitch(this, function() {
            this.blobEditChange(editWidget)
          })
        });

      };

      parus._docPropsValueValueForWidget(editWidget, null, parus.parus8.nodeField(param, "DATATYPE"), parus.parus8.nodeField(param, "SUBTYPE") || "0");


      this._widgets["P" + parus.parus8.nodeField(param, "RN")] = editWidget;
      this._widgets.length++;

    }));

  },

  validate: function() {
    var valid = this.inherited(arguments);
    dojo.forEach(this._blobsForValidation, dojo.hitch(this, function(edit) {
      if (valid) valid = this.blobValidate(edit);
    }));
    return valid;
  },

  close: function() {
    dojo.forEach(this._blobsForValidation, function(edit) {
      dijit.hideTooltip(edit.domNode);
    });
    return this.inherited(arguments);
  },

  blobValidate: function(edit) {
    if (!edit.domNode.value) {
      dijit.showTooltip("Это обязательное значение", edit.domNode);
      dojo.addClass(edit.domNode, "dijitError");
      return false;
    }
    else {
      dojo.removeClass(edit.domNode, "dijitError");
      dijit.hideTooltip(edit.domNode);
      return true;
    }
  },

  blobEditChange: function(edit) {
    this.blobValidate(edit);
  }

});