<?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->defLab   = explode ("_", $entry->label)[0];
      $this->scopeId  = $entry->scopeId;
      
      if (null == $entry->rowList)
        $this->rowList = [];
      else
        $this->rowList  = $entry->rowList;
      
      if (isset($entry->panelLevel))
         $this->panelLevel = $entry->panelLevel;
      else
        $this->panelLevel = "";

      if (isset($entry->position) && $entry->position == "left")
        $this->position = "left";
      else
        $this->position = "right";
        
    }    
  }
  
  
  public function outputLayout() {

    $layout = "

<div style='border:1px solid #ccc;'>
  <table id='setup$this->scope' class='table table-bordered' >
  </table>

  <button id='fdh$this->iterator' class='btn btn-inverse' value='$this->label' 
    onclick='run$this->scope();' style='float:$this->position;'>
    $this->defLab
  </button>

  <table id='rep$this->scope' class='table table-bordered' >
  </table>
</div>
   ";
    return $layout;

  }	

    public function outputScript() {

	  $script = "
var selectId$this->scope = 0;

function com$this->scope (message) {
  var key = Object.keys(message); 
//alert ('com$this->scope:' + key + message[key][0]);
  if (key == 'selectId') { 
    selectId$this->scope = message[key][0];
    $('#rep$this->scope').html(''); 
    get$this->scope (selectId$this->scope);
  }    
}

multiFunction['com$this->scope'] = {
  com$this->scope: com$this->scope
}; 

function gen$this->scope (resp) {
  var inner = '';
  var rkeys = Object.keys (resp[0]);

  inner += '<thead><th>' + rkeys[0] + '</th><th>' + rkeys[1] +
           '</th><th>' + rkeys[2] + '</th></thead><tbody>';


  for (var i=0; i<resp.length; i++) {

    var attrClass = ' class=' + resp[i].Type + '$this->iterator' ;
    var attributes = '';
    var type = '';

    if (resp[i].Property.indexOf('Date') >= 0) {
      type = 'date';
      attributes = ' style=width:150px;';
    }  
    else if (resp[i].Property.indexOf('Time') >= 0) {
      type = 'time';
    }
    else if (resp[i].Property.indexOf('id') >= 0) {
      type = 'number';
    }
    else if (resp[i].Property.indexOf('Export') >= 0) {
      type = 'checkbox';
      if (resp[i].Value == 1)
        attributes = ' checked'; 
    }
    else {
      type = 'text';
    }

    inner += '<tr><td>' + resp[i].Property + '</td>' + 
             '<td><input type=' + type + ' name=\"' + resp[i].Property + '\"' + attrClass + attributes + ' value=\"' + resp[i].Value + '\"/></td>' +
             '<td>' + resp[i].Type + '</td></tr>';
  }
  inner += '</tbody>';
  return inner;
}

function get$this->scope (selectId) {

  if (selectId == 0) { 
    document.getElementById ('setup$this->scope').innerHTML = '';
    document.getElementById ('rep$this->scope').innerHTML = '';
    $('[class^=$this->panelLevel]').each(function(){ $(this).slideUp('slow'); });
  }

  else {
    var params = 'selectid=' + selectId;    
    var xhr = new XMLHttpRequest();

    xhr.open('POST', 'ajax/get$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)
        document.getElementById ('setup$this->scope').innerHTML = gen$this->scope(response);
      }
    }  
    xhr.send(params);
  }    
}

function download$this->scope (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 genrep$this->scope (resp) {
  var inner = '';
  var rkeys;
  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>' + 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 fileexp;
  var filename;

  var params = 'selectid=' + selectId$this->scope; 
  $('.Report$this->iterator').each(function(){
    params += '&' + $(this).attr('name') + '=';
    if ($(this).attr( 'name') == 'Export') {
      fileexp = $(this).is(':checked');
      params += fileexp;
    }  
    else if ($(this).attr( 'name') == 'File Name') {
      filename = $(this).val();
      params += filename;
    }
    
  });    
  var fields = '';
  var values = '';
  var comma = '';
  $('.Query$this->iterator').each(function(){
    fields +=  comma + $(this).attr( 'name');
    values +=  comma + $(this).val();
    comma = ',';
  });    
  params +=  '&' + 'fields=' + fields + '&' + 'values=' + values;

  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);
      if (fileexp) 
        download$this->scope (filename, response);
      else
        $('#rep$this->scope').html(genrep$this->scope(response));
    }
  }  
  xhr.send(params);
}

    ";
	  


    return $script;
  }
}

?>