<?php 

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

  public function __construct($field) {

    if (isset($field)) {
      $this->field    = $field->field;
      $this->form = $field->form;
      $this->label    = $field->label;
      $this->defLab   = explode ("_", $field->label)[0];
      $this->fieldId  = $field->fieldId;
      
      if (null == $field->rowList)
        $this->rowList = [];
      else
        $this->rowList  = $field->rowList;
      
      if (isset($field->panelLevel))
         $this->panelLevel = $field->panelLevel;
      else
        $this->panelLevel = "";

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

    $layout = "

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

  <button id='fdh$this->form' class='btn btn-inverse' value='$this->label' 
    onclick='exp$this->field(28);' style='float:$this->position;'>
    $this->defLab
  </button>

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

  }	

    public function outputScript() {

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

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

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

function gen$this->field (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->form' ;
    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->field (selectId) {

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

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

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

function download$this->field (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->field (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 exp$this->field (exportid) {
  var fileexp;
  var filename;

  var params = 'exportid=' + exportid; 
  var xhr = new XMLHttpRequest();

  xhr.open('POST', 'ajax/exp$this->field.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$this->field (filename, response);
    }
  }  
  xhr.send(params);
}

    ";
	  


    return $script;
  }
}

?>