<?php 

class AddonFnSelect {
  private $scope;
  private $label;
  private $tblRows;
  private $control;

  public function __construct($entry) {

    if (isset($entry)) {
      $this->size = $entry->size;
      $this->scope = $entry->scope;
      $this->form  = $entry->form;
      $this->leftLabel = $entry->leftLabel;
      $this->leftSide = $entry->leftSide;

      if (isset($entry->functions))
        $this->functions = json_encode($entry->functions);
      else 
        $this->functions = "";

      if (isset($entry->selOptions))
        $this->selOptions = json_encode($entry->selOptions);
      else 
        $this->selOptions = "";

//print_r ("so:");
//print_r ($this->selOptions);
      if (isset($entry->placeHolder)) 
        $this->placeHolder = "placeholder=\"$entry->placeHolder\""; 
      else 
        $this->placeHolder = "";

      if (isset($entry->readOnly))
        $this->readOnly = $entry->readOnly;
      else 
        $this->readOnly = "";
    }    
  }
  
  public function outputLayout() {

    $layout = "
<div id='outer-container'>
  <div id='scrollable' style='height:" . $this->size . "em;'>
    <table class='table table-condensed'>
      <thead><tr>
        <th colspan='3'>$this->leftLabel</th>
      </tr></thead>    
      <tbody id='frm$this->scope'>
      </tbody>
    </table>
  </div>
  <button id='put$this->scope' class='btn btn-inverse' 
    onclick='put$this->scope();'>Enter</button>
</div>
    ";
    return $layout;
  }	

  public function outputScript() {

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

$(document).ready(function() {
  document.getElementById('put$this->scope').style.display= 'none';
 });

$(document).on('change', '.fn$this->scope', function (e) {
  $(this).closest('tr').prop('class', '$this->form');
  $(this).closest('td').prop('class', 'field$this->form');
}); 

$(document).on('change', '.sel$this->scope', function (e) {
  $(this).closest('tr').prop('class', '$this->form');
  $(this).closest('td').prop('class', 'field$this->form');
  $(this).attr('data-value', $(this).val());
}); 

function com$this->scope (message) {
  $('#valmsg$this->scope').html('');
  var key = Object.keys(message); 

  if (key == 'selectId') {
    selectId$this->scope = message[key][0];
    get$this->scope (selectId$this->scope);
    if (selectId$this->scope == 0)
      document.getElementById('put$this->scope').style.display= 'none';
    else
      document.getElementById('put$this->scope').style.display= 'block';
  }    
}

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

function gen$this->scope(rows) {
  var fnId;
  var text;
  var functions = JSON.parse('$this->functions');
  var selOptions = JSON.parse('$this->selOptions');
  var functions0 = '';
  var functions1 = '';
  var selections = '';

  for (var i=0; i<functions.length; i++) {
    if (functions[i].type == 0) {
      value = functions[i].id;
      text  = functions[i].text;
      functions0 += '<option value=' + value + '>' + text + '</option>';
    }  
    else if (functions[i].type == 1) {
      value = functions[i].id;
      text  = functions[i].text;
      functions1 += '<option value=' + value + '>' + text + '</option>';
    }  
  }    
  for (var j=0; j<selOptions.length; j++) {
    value = selOptions[j].id;
    text  = selOptions[j].text;
    selections += '<option value=' + value + selected + '>' + text + '</option>';
  }    

  var layout = 
    '<tr id=0>' +
      '<td data-element=function style=width:25%;>' +
        '<select class=fn$this->scope>' + functions0 + '</select>' +
      '</td>' +
      '<td data-element=select style=width:75%;>' + 
        '<select class=sel$this->scope>' + selections + '</select>' +
      '</td>' +
    '</tr>';
             
  for (var k=0; k<rows.length; k++) {
    var id = rows[k].id
    var fnValue = rows[k].fnValue;
    var selValue = rows[k].selValue;

    for (var l=0; l<selOptions.length; l++) {
      var selected = '';
      value = selOptions[l].id;
      text  = selOptions[l].text;

//alert (selValue + '==' + value) 

      if (selValue == value) 
        selected = ' selected';
      selections += '<option value=' + value + selected + '>' + text + '</option>';
    }    

    layout += 
      '<tr id=' + id + ' style=width:25%;>' +
        '<td class=field$this->scope data-element=function style=width:25%;>' +
          '<select class=fn$this->scope>' + functions1 + '</select>' +
        '</td>' +
        '<td data-element=select style=width:75%;>' + 
          '<select class=fn$this->scope>' + selections + '</select>' +
        '</td>' +
      '</tr>';
  }  
             
  return layout;
} 

function get$this->scope (selectId) {
  document.getElementById ('frm$this->scope').innerHTML = '';
  
  if (selectId > 0) {
    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() {
      if (xhr.readyState == 4 && xhr.status == 200) {
        var response = JSON.parse(xhr.responseText)
        document.getElementById('frm$this->scope').innerHTML = gen$this->scope(response);
      }
    }  
    xhr.send(params);
  }    
}

function put$this->scope () {
  var comma   = '';
  var rowIds  = '';
  var fnVals = '';
  var selVals = '';
  var elements = '';

  $('.$this->form').each(function() {
    rowIds  += comma + $(this).prop('id');
    $(this).children('.field$this->form').each(function(){
      var element = $(this).attr('data-element')
      elements  += comma + element;
      if (element == 'function')
        fnVals += comma + $(this).find('select option:selected').val();
      else if (element == 'select')
        selVals += comma + $(this).find('select option:selected').val();
    });
    comma = ',';
  });
  
  var params = 'selectid=' + selectId$this->scope + '&' + 
               'rowids=' + rowIds + '&' + 'elements=' + elements + '&' +
               'fnvals=' + fnVals + '&' + 'selvals=' + selVals;

  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/put$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)
      get$this->scope (response);
    }
  }  
  xhr.send(params);
}

    "; 
    return $script;
  }
}

?>