dojo.provide("parus.balanceIndexConsComparationPrint");

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

dojo.require("parus.balanceIndexConsComparation");

dojo.mixin(parus, {

  balanceIndexConsComparationPrint: {

    print: function() {

      var query;
      var full;
      var handler;
      var title;

      dojo.xhrGet({

        url: "../.system/session.xml",
        handleAs: "xml",
        sync: true,
        preventCache: true,

        load: function(response) {

          if (!dojo.isIE) response.normalize();

          var LNodes = response.documentElement.getElementsByTagName("*");

          for (var i = 0; i != LNodes.length; i++)
          if (LNodes[i].nodeName == "query") query = LNodes[i].childNodes.item(0).nodeValue
          else if (LNodes[i].nodeName == "handler") handler = LNodes[i].childNodes.item(0).nodeValue
          else if (LNodes[i].nodeName == "title") title = LNodes[i].childNodes.item(0).nodeValue;

          return (response);

        }
      });

      parus.database.connected = handler;

      document.body.style.width = "260mm";

      document.body.insertAdjacentHTML("afterBegin", "<div>Сравнение состава показателей</div>");

      var titleNode = document.body.firstChild;

      titleNode.style.height = "auto";
      titleNode.style.fontWeight = "bold";
      titleNode.style.fontFamily = "Tahoma, Verdana, sans-serif";
      titleNode.style.margin = "10px";
      document.title = (!dojo.isIE || dojo.isIE >= 10) ? titleNode.textContent : titleNode.innerText;

      var table = document.body.appendChild(document.createElement("table"));

      table.width = "100%";
      table.border = "1";
      table.cellSpacing = "0";

      var row = table.insertRow(-1);
      row.style.fontWeight = "bold";

      query = dojo.fromJson(query);

      var store = parus.balanceIndexConsComparation.gridStore(query.comparationData);

      if (query.comparationData.result == "1") {
        row.insertCell(-1).appendChild(document.createTextNode("Разрез" + String.fromCharCode(160) + "(мнемокод)"));
        row.insertCell(-1).appendChild(document.createTextNode("Разрез" + String.fromCharCode(160) + "(наименование)"));
      }
      else {
        row.insertCell(-1).appendChild(document.createTextNode("Контрагент" + String.fromCharCode(160) + "(мнемокод)"));
        row.insertCell(-1).appendChild(document.createTextNode("Контрагент" + String.fromCharCode(160) + "(наименование)"));
      };

      var formatter = (query.comparationData.dataType == "1") ?

      (query.comparationData.fracPart ?
      function(data, rowIndex, column) {
        return data ?

        parus.parus8.formatIndexConsValue((data.toString()).split(".").join(","), query.comparationData.intPart, query.comparationData.skipUnsignificantZeroes ? null : query.comparationData.fracPart) : data;
      } : parus.parus8.formatFloat) : function(x) {
        return x
      };

      dojo.forEach(query.comparationData.items, function(item) {
        row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(item, "TITLE")));
      });


      var row = table.insertRow(-1);
      row.style.fontWeight = "bold";
      var cell = row.insertCell(-1)
      cell.appendChild(document.createTextNode("Значение" + String.fromCharCode(160) + "показателя"));
      cell.colSpan = 2;
      dojo.forEach(query.comparationData.items, function(item) {
        var cell = row.insertCell(-1);
        if (query.comparationData.dataType == "1") cell.align = "right";
        cell.appendChild(document.createTextNode(
        parus.parus8.nodeField(item, "VALUE") == null ? "" : formatter(parus.parus8.nodeField(item, "VALUE"))));
      });

      if (query.comparationData.dataType == "1") {

        var row = table.insertRow(-1);
        row.style.fontWeight = "bold";
        var cell = row.insertCell(-1);
        cell.appendChild(document.createTextNode("Итог" + String.fromCharCode(160) + "по" + String.fromCharCode(160) + "составу"));
        cell.colSpan = 2;
        dojo.forEach(query.comparationData.items, function(item) {
          var cell = row.insertCell(-1);
          if (query.comparationData.dataType == "1") cell.align = "right";
          cell.appendChild(document.createTextNode(parus.parus8.nodeField(item, "SUMMVALUE") == null ? "" : formatter(parus.parus8.nodeField(item, "SUMMVALUE"))));
        });

      }

      store.fetch({
        sort: query.order,
        query: query.conditions,
        onItem: function(dataItem) {

          var row = table.insertRow(-1);
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataItem, "CODE") || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataItem, "NAME") || String.fromCharCode(160)));

          dojo.forEach(query.comparationData.items, function(item) {
            var cell = row.insertCell(-1);
            if (query.comparationData.dataType == "1") cell.align = "right";
            v = parus.parus8.nodeField(dataItem, item["ID"]);
            cell.appendChild(document.createTextNode(v == null ? "" : formatter(v)));
          });

        }

      });


    }

  }
});