<?php 

class ProcessReport  {
  private $scope;
  private $label;
  private $scopeId;
  private $rowList;

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->iterator = $entry->iterator;
      $this->label    = $entry->label;
      $this->scopeId  = $entry->scopeId;
      
      if (null == $entry->rowList)
        $this->rowList = [];
      else
        $this->rowList  = $entry->rowList;
      
    }    
  }
  
  public function outputLayout() {

    $layout = "

<input type='hidden' id='hid$this->scope' class='$this->iterator' name='$this->scope'>
<label for='list$this->scope' style='margin-bottom:0px;'>$this->label</label><br>
<div style='border:1px solid #ccc;'>
  <table class='table table-bordered' >
    <tbody id='list$this->scope'
    </tbody>
  </table>
 
  <table id='rep$this->scope' class='table table-bordered' >
  </table>
</div>
   ";
    return $layout;

  }	

    public function outputScript() {

	  $script = "
var selid = 0;
function notify$this->scope (msgId) {

  selId = msgId;
  $('#rep$this->scope').html(''); 
  getsetup$this->scope (msgId);
}

$('#hid$this->scope').on('chg$this->iterator', function() {
  var list =  $(this).val().split(',');
  var inner = '';
  var variable;
  for (var i=0; i<list.length; i++) {
    variable = list[i];  
    text = variable.substr(1);
  
    inner += '<tr><td>' + variable + '</td>' +
             '<td><input type=text id=crit' + text + ' class=$this->scope name=' + text + '></td></tr>';
  }

  $('#list$this->scope').html(inner);
});

function setup$this->scope (dbValues) {
  $('.$this->iterator').each(function(){
    if (typeof dbValues[$(this).prop('name')] != 'undefined') {
      if (($(this).prop('type') == 'text') || ($(this).prop('type') == 'hidden')) 
        $(this).prop('value', dbValues[$(this).prop('name')]).trigger('chg$this->iterator');
      else if ($(this).prop('type') == 'time')
        $(this).prop('value', dbValues[$(this).prop('name')]);
      else if ($(this).prop('type') == 'date')
        $(this).prop('value', dbValues[$(this).prop('name')]);
      else if($(this).is('textarea')) 
        $(this).val(dbValues[$(this).prop('name')]);
    }
  }); 
}

function getsetup$this->scope (selid) {

  var params = 'selid=' + selid;    

  var xhr = new XMLHttpRequest();

  xhr.open('POST', 'ajax/getsetup$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText)
      setup$this->scope(response);
    }
  }  
  xhr.send(params);
}

function download(fileName, text) {
  var element = document.createElement('a');
  var path = fileName + Math.floor(Date.now() / 1000) + '.exp';
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', path);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

function gen$this->scope (resp) {
  var inner = '';
  var rkeys;
  var highlight;
  var indicator;
  var style;
  var meta = new Array();
  for (var i=0; i<resp.length; i++) {
    if (i == 0) {
      rkeys = Object.keys (resp[i]);
      inner += '<thead><tr>'
      for (var j=0; j<rkeys.length; j++) {      
        inner += '<th>' + formatLabel(rkeys[j]) + '</th>';
      }  
      inner += '</tr></thead><tbody>';
    }
    
    inner += '<tr>';
    for (var j=0; j<rkeys.length; j++) { 
      inner += '<td>' + resp[i][rkeys[j]] + '</td>'; 
    }
    inner += '</tr>';
  }
  inner += '</tbody>';
  return inner;
}

function run$this->scope () {

  var params = 'selid=' + selId + '&' + 'values=';
  $('.querycriteria').each(function(){
    params += $(this).val() + ',';
  });    

  var xhr = new XMLHttpRequest();

  xhr.open('POST', 'ajax/run$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText)
      $('#rep$this->scope').html(gen$this->scope(response));
    }
  }  
  xhr.send(params);
}

function exp$this->scope () {

  var params = 'selid=' + selId + '&' + 'values=';
  $('.querycriteria').each(function(){
    params += $(this).val() + ',';
  });  
  var expFilename =  $('#repfilename').val();

  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/exp$this->scope.php', true);
  xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText)
      download(expFilename, response); 
    }
  }  
  xhr.send(params);
}
      ";
	  


    return $script;
  }
}

?>