$(document).ready(function () {
  $.tablesorter.addParser({
    id: 'date_parser',
    is: function (s, table, cell, $cell) {
      return false;
    },
    format: function (s, table, cell, cellIndex) {
      return $(cell).text().trim().split(".").reverse().join("-");
    },
    parsed: false,
    type: "text"
  });
  $("#allTable").tablesorter({
    sortList: [[0,1]],
    theme: "blue",
    widthFixed: true,
    widgets: ["zebra", "stickyHeaders"],
    fixedWidth: true,
    headers: {
      3: { sorter: "date_parser"},
      4: { sorter: "date_parser"}
    },
  });
});

function exportToRewizor(source, date, type) {
  blockUI("Trwa generowanie pliku...");
  $.ajax({
    url: "index.php?module=EcmInvoiceOuts&action=ecommerce&ajax=exportToRewizor&to_pdf=1&source="+source+"&date="+date+"&type="+type,
    type: "GET",
    success: function (data) {
      data = data.replace(/ {4}/g, "");
      data = data.replace(/\n/g, "\r\n");
      var bytes = new TextEncoder("windows-1252", { NONSTANDARD_allowLegacyEncoding: true }).encode(data);
      var filename = "rewizor-allegro.epp";
      var blob = new Blob([bytes], { type: "text/csv"});
      if (navigator.msSaveBlob) { // IE 10+
        navigator.msSaveBlob(blob, filename);
      } else {
        var link = document.createElement("a");
        if (link.download !== undefined) { // feature detection
          // Browsers that support HTML5 download attribute
          var url = URL.createObjectURL(blob);
          link.setAttribute("href", url);
          link.setAttribute("download", filename);
          link.style.visibility = "hidden";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        }
        $.unblockUI();
      }

      $.unblockUI();
    },
    error: function () {
      window.alert("Błąd eksportu.");
      $.unblockUI();
    }
  });
}

function blockUI($msg) {
  $.blockUI(
      {
        css: {
          border: 'none',
          padding: '15px',
          backgroundColor: '#000',
          '-webkit-border-radius': '10px',
          '-moz-border-radius': '10px',
          opacity: .5,
          'font-weight': 'bold',
          'font-size': '16px',
          color: '#fff',
        },
        message: $msg
      });
}

function copyToClipboard(text) {
  navigator.clipboard.writeText(text);
}