<?php 

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

  public function __construct($entry) {

    if (isset($entry)) {
      $this->size = $entry->size;
      $this->scope = $entry->scope;
      $this->Label = $entry->Label;

      if (isset($entry->entryRows)) 
        $this->entryRows = $entry->entryRows; 
      else 
        $this->entryRows = [];

      if (isset($entry->placeHolder)) 
        $this->placeHolder = "placeholder='$entry->placeHolder'"; 
      else 
        $this->placeHolder = "";

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

      $this->addonRow  = " \
      <tr id='add$this->scope'> \
        <td style='width=100%'> \
          <input type='text' $this->placeHolder> \
        </td> \
      </tr> \
          ";

      $this->addRow = str_replace ("\\", "", $this->addonRow);
    }    
  }
  
  public function outputLayout() {


    $layout = "
<div id='outer-container'>
  <div id='scrollable' style='height:" . $this->size . "em;'>
    <table class='table table-condensed'>
      <thead><tr>
        <th>$this->label</th>
      </tr></thead>    
      <tbody id='frm$this->scope'>
    ";    
    
    foreach ($this->entryRows as $row) {
      $layout .= "
        <tr id=$row->id class='addon$this->scope'>
          <td style='width=70%'> 
            <input type='text' value='$row->inpValue' $this->placeHolder> 
          </td> 
        </tr>
      ";
    }
                      
   $layout .= "
      </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;
var addonRow$this->scope = \"$this->addonRow\";

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

$(document).on('change', '#sel$this->scope', function (e) {
  $('#add$this->scope').prop('class', 'addon$this->scope');
  $('#add$this->scope').prop('id', 0);
  $(this).prop('id', '');
  $('#frm$this->scope').append (addonRow$this->scope);
});

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 id;
  var card_id;
  var text;
  var scope = '$this->scope';

  for (var i=0; i<rows.length; i++) {
    var id = rows[i].id
    var selValue = rows[i].selValue;
    var inpValue = rows[i].inpValue;

    $('#frm$this->scope').append (addonRow$this->scope);
    $('#add$this->scope').prop('class', 'addon$this->scope');
    $('#add$this->scope').find('select').prop('id', '');
    $('#add$this->scope').find('option[value='+selValue+']').prop('selected', true);
    $('#add$this->scope').find('input').val(inpValue);
    $('#add$this->scope').prop('id', id);
  }  
  $('#frm$this->scope').append (addonRow$this->scope);
//  $('#add$this->scope').prop('id', 0);
} 

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)
        gen$this->scope(response);
      }
    }  
    xhr.send(params);
  }    
}

function put$this->scope (selid) {
  var comma  = '';
  var ids = '';
  var selects = '';
  var inputs = '';

  $('.addon$this->scope').each(function() {
    ids     += comma + $(this).prop('id');
    selects += comma + $(this).find('select option:selected').val();
    inputs  += comma + $(this).find('input').val();
    comma = ',';
  });
  
  var params = 'selectid=' + selectId$this->scope + '&' + 'ids=' + ids + '&' + 
               'selects=' + selects + '&' + 'inputs=' + inputs;

  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;
  }
}

?>