dojo.provide("parus.balanceIndexConsPrint");

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

dojo.mixin(parus, {

  balanceIndexConsPrint: {

    print: function() {

      var query;
      var full;
      var handler;
      var title;
      var skipUnsignificantZeroes;
      var hiddencols;

      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;
          else if (LNodes[i].nodeName == "skipzero") skipUnsignificantZeroes = LNodes[i].childNodes.item(0).nodeValue == "1";
          else if (LNodes[i].nodeName == "hiddencols") hiddencols = 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";


      row.insertCell(-1).appendChild(document.createTextNode("Значение"));
      row.insertCell(-1).appendChild(document.createTextNode("Тип отчета"));
      row.insertCell(-1).appendChild(document.createTextNode("Контрагент (мнемокод)"));
      row.insertCell(-1).appendChild(document.createTextNode("Контрагент (наименование)"));
      row.insertCell(-1).appendChild(document.createTextNode("Состояние отчета"));
      row.insertCell(-1).appendChild(document.createTextNode("Отчет проверен"));
      row.insertCell(-1).appendChild(document.createTextNode("Отчет принят"));
      row.insertCell(-1).appendChild(document.createTextNode("Отчет отправлен в центр учета"));
      row.insertCell(-1).appendChild(document.createTextNode("Итог"));
      row.insertCell(-1).appendChild(document.createTextNode("Разрез"));
      row.insertCell(-1).appendChild(document.createTextNode("Состояние подотчета"));
      row.insertCell(-1).appendChild(document.createTextNode("Подотчет проверен"));

      row.insertCell(-1).appendChild(document.createTextNode("Значение (строка)"));
      row.insertCell(-1).appendChild(document.createTextNode("Значение (число)"));
      row.insertCell(-1).appendChild(document.createTextNode("Значение (дата)"));


      parus.database.execute("IndexCons", dojo.fromJson(query), dojo.hitch(this, function(response) {

        titleNode.innerHTML = response.documentElement.childNodes[0].childNodes[1].firstChild.nodeValue;

        var consItems =
        response.documentElement.childNodes[0].childNodes[0].childNodes.length ? parus.parus8.rowNodes(response.documentElement.childNodes[0].childNodes[0].childNodes[0]) : [];

        if (consItems.length == 0) {
          parus.parus8.errorMessage("Показатель не имеет состава.");
          return;
        };

        var fc = {
          intPart: response.documentElement.childNodes[0].childNodes[2].firstChild.childNodes[0].firstChild ? response.documentElement.childNodes[0].childNodes[2].firstChild.childNodes[0].firstChild.nodeValue : null,
          fracPart: skipUnsignificantZeroes ? null : (
          response.documentElement.childNodes[0].childNodes[2].firstChild.childNodes[1].firstChild ? response.documentElement.childNodes[0].childNodes[2].firstChild.childNodes[1].firstChild.nodeValue : null)
        };

        dojo.forEach(consItems, function(dataRow) {
          var row = table.insertRow(-1);
          row.style.fontSize = "smaller";
          var cell = row.insertCell(-1);
          cell.align = "right";
          cell.appendChild(document.createTextNode(dojo.hitch(fc, parus.parus8.formatFloatIntFrac)(parus.parus8.nodeField(dataRow, "VALUE")) || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(
          parus.balance.formatReportKind(parus.parus8.nodeField(dataRow, "KIND")) || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "AGNABBR") || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "AGNNAME") || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(
          parus.balance.formatReportStatus(parus.parus8.nodeField(dataRow, "STATE")) || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(
          parus.parus8.formatBoolean(parus.parus8.nodeField(dataRow, "CHECKED")) || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(
          parus.balance.formatReportAccepted(parus.parus8.nodeField(dataRow, "ACCEPTED")) || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(
          parus.parus8.formatBoolean(parus.parus8.nodeField(dataRow, "SENT")) || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(
          parus.parus8.formatBoolean(parus.parus8.nodeField(dataRow, "RESULT")) || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "CODE") || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(
          parus.balance.formatReportStatus(parus.parus8.nodeField(dataRow, "SRSTATE")) || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(
          parus.parus8.formatBoolean(parus.parus8.nodeField(dataRow, "SRCHECKED")) || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(
          parus.parus8.nodeField(dataRow, "STRVAL") || String.fromCharCode(160)));

          cell = row.insertCell(-1);
          cell.align = "right";
          cell.appendChild(document.createTextNode(dojo.hitch(fc, parus.parus8.formatFloatIntFrac)(parus.parus8.nodeField(dataRow, "NUMVAL")) || String.fromCharCode(160)));

          row.insertCell(-1).appendChild(document.createTextNode(
          parus.parus8.nodeField(dataRow, "DATEVAL") || String.fromCharCode(160)));
        })

        if (hiddencols) {
          var cols = ["VALUE", "KIND", "AGNABBR", "AGNNAME", "STATE", "CHECKED", "ACCEPTED", "SENT", "RESULT", "CODE", "SRSTATE", "SRCHECKED", "STRVAL", "NUMVAL", "DATEVAL"];
          dojo.forEach(hiddencols.split(","), function(col) {
            n = dojo.indexOf(cols, col);
            if (n >= 0) for (var i = 0; i < table.rows.length; i++) table.rows[i].cells[n].style.display = "none";

          });
        }

      }));

    }

  }
});