 <?php 

class AddonSelSel  {
  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->label = $entry->label;
      $this->added = $entry->added;
      $this->leftSide = $entry->leftSide;

      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='2'>$this->label</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', '.func$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');
}); 

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(rowValues, selOptions) {
  var value;
  var text;
  var selections = '';
  var functions = '<option value=1>$this->added</option>' +
                  '<option value=-1>Delete</option>';

  for (var j=0; j<selOptions.length; j++) {
    value = selOptions[j].id;
    text  = selOptions[j].text;
    selections += '<option value=' + value + '>' + text + '</option>';
  }    
  
  var layout = 
    '<tr id=0>' +
      '<td data-element=function  class=field$this->scope style=width:35%;>' +
        '<select class=func$this->scope><option value=0>Add</option></select>' +
      '</td>' +
      '<td data-element=select style=width:56%;>' + 
        '<select class=sel$this->scope>' + selections + '</select>' +
      '</td>' +
    '</tr>';

  for (var i=0; i<rowValues.length; i++) {
    var value = rowValues[i].id
    var text  = rowValues[i].selValue;

    layout += 
      '<tr id=' + value + '>' +
        '<td data-element=function style=width:35%;>' +
          '<select class=sel$this->form>' + functions + '</select>' +
        '</td>' +
        '<td class=field$this->form style=width:65%;>' + 
          '<input type=text class=inp$this->form value=\"' + text + '\">' +
        '</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 selOptions = JSON.parse(xhr.responseText)
        row$this->scope(selectId, selOptions);
      }
    }  
    xhr.send(params);
  }    
}

function row$this->scope (selectId, selOptions) {

  var params = 'selectid=' + selectId;

  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/row$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      var rowValues = JSON.parse(xhr.responseText)
      document.getElementById('frm$this->scope').innerHTML = gen$this->scope(rowValues, selOptions);
    }
  }  
  xhr.send(params);
}

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

  $('.$this->form').each(function() {
    rowIds  += comma + $(this).prop('id');

    $(this).children('.field$this->form').each(function(){
      var element = $(this).attr('data-element');
      if (element == 'function')
        funcVals += comma + $(this).find('.func$this->scope option:selected').val();
      else if (element == 'select')
        selVals += comma + $(this).find('.sel$this->scope option:selected').val();
      else
        selVals += comma + 0;
    });
    comma = ',';
  });
  
  var params = 'selectid=' + selectId$this->scope + '&' + 'rowids=' + rowIds + '&' +
               'funcvals=' + funcVals + '&' + '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;
  }
}

?>