dojo.provide("parus.balanceReportsConditionsDialog");

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");
dojo.require("dijit.TitlePane");
dojo.require("dijit.form.DateTextBox");
dojo.require("dijit.form.CheckBox");

dojo.require("parus.parus8");
dojo.require("parus.dialog");
dojo.require("parus.database");
dojo.require("parus.groupBox");
dojo.require("parus.dictEdit");

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

  consolidated: false,

  _docprops: null,

  setCheckBoxes: function(groupBox, condition) {
    var items = condition ? condition.split(";") : [];
    dojo.forEach(groupBox.widgets(), function(checkBox) {
      var cell = checkBox.domNode.parentNode;
      if (cell.style.display == "none") return;
      checkBox.attr("checked", (items.length == 0) || (dojo.indexOf(items, checkBox.numericValue.toString()) >= 0));
    });
  },

  getCheckBoxes: function(groupBox) {
    var items = [];
    var unchecked = false;
    dojo.forEach(groupBox.widgets(), function(checkBox) {
      var cell = checkBox.domNode.parentNode;
      if (cell.style.display == "none") return;
      if (checkBox.checked) items.push(checkBox.numericValue.toString())
      else unchecked = true;
    });
    if (unchecked) return items.join(";")
    else return "";
  },

  show: function() {
    if (this.consolidated) {

      this.mainContainer.domNode.style.width = "850px";

      var cells = this.statusTable.rows[0].cells;

      cells[2].style.display = "none";

      cells[4].style.display = "";
      cells[5].style.display = "";
      cells[6].style.display = "";

      cells[0].width = "10%";
      cells[1].width = "10%";
      cells[3].width = "10%";
      cells[4].width = "18%";

      cells[5].width = "18%";
      cells[6].width = "23%";
      cells[7].width = "11%";


    };
    this.inherited(arguments);

  },

  postCreate: function() {

    this.inherited(arguments);

    this._docprops = {};

    if (parus.balance._docprops.length) {
      this.requsitesPane.domNode.style.margin = "0";

      var tabs = new dijit.layout.TabContainer({
        region: "center",
        "class": "balanceInternalPages"
      });
      tabs.addChild(this.requsitesPane);

      var propsPane = new dijit.layout.ContentPane({
        title: "Свойства",
        "class": "dialogContentPane",
        style: "overflow-y:scroll"
      });
      var propsTable = document.createElement("table");
      propsTable.cellSpacing = "5";
      propsTable.width = "100%";

      dojo.forEach(parus.balance._docprops, dojo.hitch(this, function(docprop) {
        var row = propsTable.insertRow(-1);


        if (parus.parus8.nodeField(docprop, "DICTIONARY") || parus.parus8.nodeField(docprop, "DATATYPE") == "0") {


          var titleCell = row.insertCell(-1);
          titleCell.appendChild(document.createElement("b")).
          appendChild(document.createTextNode(parus.parus8.nodeField(docprop, "PROMPT") + ":"));

          var editCell = row.insertCell(-1);
          editCell.align = "right";
          editCell.colSpan = 3;

          var params = parus.customEditProperties(
          parus.parus8.nodeField(docprop, "DICTIONARY"), parus.parus8.nodeField(docprop, "DATATYPE") == "3" ? "4" : parus.parus8.nodeField(docprop, "DATATYPE"), parus.parus8.nodeField(docprop, "SUBTYPE"), false, 2000, parus.parus8.nodeField(docprop, "INTPART"), parus.parus8.nodeField(docprop, "FRACPART"));

          dojo.mixin(params.params, {
            multiselect: true,
            arbitrary: true
          });

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

          parus._docPropsValueValueForWidget(editWidget, null, parus.parus8.nodeField(docprop, "DATATYPE") == "3" ? "4" : parus.parus8.nodeField(docprop, "DATATYPE"), parus.parus8.nodeField(docprop, "SUBTYPE"));

          this._docprops[parus.parus8.nodeField(docprop, "RN")] = {
            dataType: parus.parus8.nodeField(docprop, "DATATYPE") == "3" ? "4" : parus.parus8.nodeField(docprop, "DATATYPE"),
            dataSubType: parus.parus8.nodeField(docprop, "SUBTYPE"),
            single: editWidget
          };



        }
        else {


          var titleCell = row.insertCell(-1);
          titleCell.appendChild(document.createElement("b")).
          appendChild(document.createTextNode(parus.parus8.nodeField(docprop, "PROMPT") + " с:"));


          var params = parus.customEditProperties(
          parus.parus8.nodeField(docprop, "DICTIONARY"), parus.parus8.nodeField(docprop, "DATATYPE") == "3" ? "4" : parus.parus8.nodeField(docprop, "DATATYPE"), parus.parus8.nodeField(docprop, "SUBTYPE"), false, 2000, parus.parus8.nodeField(docprop, "INTPART"), parus.parus8.nodeField(docprop, "FRACPART"));

          var editCell = row.insertCell(-1);
          editCell.align = "right";

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

          parus._docPropsValueValueForWidget(fromWidget, null, parus.parus8.nodeField(docprop, "DATATYPE") == "3" ? "4" : parus.parus8.nodeField(docprop, "DATATYPE"), parus.parus8.nodeField(docprop, "SUBTYPE"));

          titleCell = row.insertCell(-1);
          titleCell.appendChild(document.createElement("b")).
          appendChild(document.createTextNode("по :"));

          editCell = row.insertCell(-1);
          editCell.align = "right";

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

          parus._docPropsValueValueForWidget(toWidget, null, parus.parus8.nodeField(docprop, "DATATYPE") == "3" ? "4" : parus.parus8.nodeField(docprop, "DATATYPE"), parus.parus8.nodeField(docprop, "SUBTYPE"));

          this._docprops[parus.parus8.nodeField(docprop, "RN")] = {
            dataType: parus.parus8.nodeField(docprop, "DATATYPE") == "3" ? "4" : parus.parus8.nodeField(docprop, "DATATYPE"),
            dataSubType: parus.parus8.nodeField(docprop, "SUBTYPE"),
            from: fromWidget,
            to: toWidget
          };

        }

      }));


      propsPane.containerNode.appendChild(propsTable);
      tabs.addChild(propsPane);

      this.mainContainer.addChild(tabs);
      dojo.style(this.mainContainer.domNode, {
        height: (parus.parus8.styleSize(this.mainContainer.domNode.style.height) + 30) + "px"
      });
    };


  },

  clearConditions: function() {

    this.formCodeEdit.attr("value", null);
    this.agentCodeEdit.attr("value", null);
    this.agentNameEdit.attr("value", null);
    this.dateFromEdit.attr("value", null);
    this.dateToEdit.attr("value", null);

    dojo.forEach(this.statusGroupBox.widgets().
    concat(this.checkGroupBox.widgets()).
    concat(this.sentGroupBox.widgets()).
    concat(this.historyGroupBox.widgets()).
    concat(this.historyLastGroupBox.widgets()), function(checkBox) {
      checkBox.attr("checked", true);
    });

    for (var key in this._docprops) {
      var dp = this._docprops[key];
      dojo.forEach([dp.from, dp.single, dp.to], function(w) {
        if (w) {
          w.attr("value", null);
          if (w.subWidget) w.subWidget.attr("value", null);
        }
      });
    };
  },

  loadConditions: function(conditions) {

    this.formCodeEdit.attr("value", conditions["FORM_CODE"]);
    this.agentCodeEdit.attr("value", conditions["AGNABBR"]);
    this.agentNameEdit.attr("value", conditions["AGNNAME"]);

    this.dateFromEdit.attr("value", conditions["DATE_FROM"] ? dojo.date.locale.parse(conditions["DATE_FROM"].substring(2, 1000), {
      datePattern: "dd.MM.yyyy",
      selector: "date"
    }) : null);

    this.dateToEdit.attr("value", conditions["DATE_TO"] ? dojo.date.locale.parse(conditions["DATE_TO"].substring(2, 1000), {
      datePattern: "dd.MM.yyyy",
      selector: "date"
    }) : null);

    this.setCheckBoxes(this.statusGroupBox, conditions["STATE"]);
    this.setCheckBoxes(this.checkGroupBox, conditions["CHECKED"]);
    this.setCheckBoxes(this.sentGroupBox, conditions["SENT"]);
    this.setCheckBoxes(this.historyGroupBox, conditions["USE_HISTORY"]);
    this.setCheckBoxes(this.historyLastGroupBox, conditions["ONLY_LAST_HISTORY"]);

    var docPropsValues = conditions["DOCPROPS"];
    if (docPropsValues) {
      for (var key in docPropsValues) {

        var val = docPropsValues[key];
        var rn = key.substr(1, key.length);
        var def = this._docprops[rn];
        if (def && (def.dataType == val.dataType) && (def.dataSubType == val.dataSubType)) {
          if (def.single) parus._docPropsValueValueForWidget(def.single, val.value, def.dataType, def.dataSubType);
          if (def.from) parus._docPropsValueValueForWidget(def.from, val.from, def.dataType, def.dataSubType);
          if (def.to) parus._docPropsValueValueForWidget(def.to, val.to, def.dataType, def.dataSubType);

        }
      };
    };


  },

  saveConditions: function() {
    var result = {};

    result["FORM_CODE"] = this.formCodeEdit.value;
    result["AGNABBR"] = this.agentCodeEdit.value;
    result["AGNNAME"] = this.agentNameEdit.value;

    result["DATE_FROM"] = this.dateFromEdit.toString() != "" ? ">=" + dojo.date.locale.format(this.dateFromEdit.value, {
      selector: "date",
      datePattern: "dd.MM.yyyy"
    }) : "";
    result["DATE_TO"] = this.dateToEdit.toString() != "" ? "<=" + dojo.date.locale.format(this.dateToEdit.value, {
      selector: "date",
      datePattern: "dd.MM.yyyy"
    }) : "";

    result["STATE"] = this.getCheckBoxes(this.statusGroupBox);
    result["CHECKED"] = this.getCheckBoxes(this.checkGroupBox);
    result["SENT"] = this.getCheckBoxes(this.sentGroupBox);
    result["USE_HISTORY"] = this.getCheckBoxes(this.historyGroupBox);
    result["ONLY_LAST_HISTORY"] = this.getCheckBoxes(this.historyLastGroupBox);

    result["DOCPROPS"] = {};

    for (var key in this._docprops) {
      var dp = this._docprops[key];
      if (dp.single) result["DOCPROPS"]["P" + key] = {
        value: parus._docPropsValueValueFromWidget(dp.single),
        dataType: dp.dataType,
        dataSubType: dp.dataSubType
      }
      else result["DOCPROPS"]["P" + key] = {
        from: parus._docPropsValueValueFromWidget(dp.from),
        to: parus._docPropsValueValueFromWidget(dp.to),
        dataType: dp.dataType,
        dataSubType: dp.dataSubType
      }
    };

    return result;

  }


});