<?php 

class FilterTable   {
  private $scope;
  private $table;
  private $column;
  private $message;
 
  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope   = $entry->scope;
      $this->table   = $entry->table;
      $this->column  = $entry->column;
      $this->message = $entry->message;
   }
  }
  
  public function outputLayout() {
      
    $layout = "
<label for='$this->scope'>Filter</label>
<select id='options$this->scope' style='width:100px;height:26px' >
  <option value=none>None</option>
</select>
<div id=msg></div>
	";
	return $layout;
  }	

    public function outputScript() {

	  $script = "

var filterValue = 'None'; 
 
$('#options$this->scope').on('change',function(){
  filterValue = $(this).val();
  $('#msg').html(filterValue);  

  if (filterValue == 'none')
    $('.filter$this->message tr').prop('hidden', false); 
  else {
    $('.filter$this->message tr').prop('hidden', true); 
    $('.filter$this->message tr:contains(' + filterValue + ')').prop('hidden', false);  
  }  
});
    

function gen$this->scope (resp) {
  var value = '';
  var inner = '<option value=none>None</option>'
  for (var i=0; i<resp.length; i++) {
    var value = resp[i].$this->column;
    inner += '<option value=' + value + '>' + formatLabel(value) + '</option>';
  }
  return inner;
}    

    
$('#options$this->scope').mouseover(function(){

  var data = new FormData();
  data = 'table=$this->table' + '&' + 'column=$this->column';
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/getfilterlist.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);
      $('#options$this->scope').html(gen$this->scope(response));
      $('#options$this->scope option[value=' + filterValue + ']').attr('selected', 'selected');
    }
  }  
  xhr.send(data);
});  

    "; 
    return $script;
  }
}

?>