 <?php 

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

  public function __construct($field) {

    if (isset($field)) {
      $this->size = $field->size;
      $this->scope = $field->scope;
      $this->form  = $field->form;
      $this->label = $field->label;
      $this->added = $field->added;

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

      if (isset($field->label1)) 
        $this->label1 = "<label>$field->label1</label>"; 
      else 
        $this->label1 = "";

      if (isset($field->label2)) 
        $this->label2 = "<label>$field->label2</label>"; 
      else 
        $this->label2 = "";

      if (isset($field->placeHolder1)) 
        $this->placeHolder1 = "placeHolder=\"$field->placeHolder1\""; 
      else 
        $this->placeHolder1 = "";

      if (isset($field->placeHolder2)) 
        $this->placeHolder2 = "placeHolder=\"$field->placeHolder2\""; 
      else 
        $this->placeHolder2 = "";
    }    
  }
  
  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->label</th>
        </tr>  
        <tr>
          <th style='width:20%;'></th>
          <th style='width:40%;'>$this->label1</th>
          <th style='width:40%;'>$this->label2</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', '.inp1$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());
}); 

$(document).on('change', '.inp2$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(dbRows) {
  var functions = JSON.parse('$this->functions');
  var functions0 = '';
  var functions1 = '';

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

  var layout = 
    '<tr id=0>' +
      '<td data-element=function class=field$this->scope style=width:20%;>' +
        '<select class=func$this->scope>' + functions0 + '</select>' +
      '</td>' +
      '<td data-element=input1 style=width:40%;>' + 
        '<input type=text class=inp1$this->scope $this->placeHolder1>' +
      '</td>' +
      '<td data-element=input2 style=width:40%;>' + 
        '<input type=text class=inp2$this->scope $this->placeHolder2>' +
      '</td>' +
    '</tr>';

  for (var l=0; l<dbRows.length; l++) {
    var rowId     = dbRows[l].id
    var inp1Value = dbRows[l].inp1val;
    var inp2Value = dbRows[l].inp2val;

    layout += 
      '<tr id=' + rowId + '>' +
      '<td data-element=function class=field$this->scope style=width:20%;>' +
        '<select class=func$this->scope>' + functions1 + '</select>' +
      '</td>' +
        '<td style=width:40%;>' + 
          '<input type=text class=inp1$this->scope value=\"' + inp1Value + '\">' +
        '</td>' +
        '<td style=width:40%;>' + 
          '<input type=text class=inp2$this->scope value=\"' + inp2Value + '\">' +
        '</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 colon    = '';
  var rowIds   = '';
  var elements = '';
  var values   = '';

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

    $(this).children('.field$this->form').each(function(){
      var element = $(this).attr('data-element');
      elements  += comma + colon + element;
      if (element == 'function')
        values += comma + colon + $(this).find('.func$this->scope option:selected').val();
      else if (element == 'input1')
        values += comma + colon + $(this).find('input').attr('data-value');
      else if (element == 'input2')
        values += comma + colon + $(this).find('input').attr('data-value');

      colon = ':';    
      comma = '';
    });
    comma = ',';
  });
  
  var params = 'selectid=' + selectId$this->scope + '&' + 
               'rowids=' + rowIds + '&' + 'elements=' + elements + '&' +
               'values=' + values + '&' ;

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

?>