<?php 

class AddonSelInpSel  {
  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;

      if (null !== $entry->topLabel)
        $this->label1 = "<tr><th colspan='3'>$entry->topLabel</th></tr>";
      else
        $this->label1 = '';

      if (null !== $entry->leftLabel && null !== $entry->rightLabel)
        $this->label2 = "<tr><th>$entry->leftLabel</th><th>$entry->rightLabel</th></th></tr>";
      else
        $this->label2 = '';

      if (null !== $entry->sel1Options)
        $this->sel1Options = json_encode($entry->sel1Options);
      else
        $this->sel1Options = '{}';
    
      if (null !== $entry->sel2Options)
        $this->sel2Options = json_encode($entry->sel2Options);
      else
        $this->sel2Options = '{}';

      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>
        $this->label1
        $this->label2
      </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', '.sel1$this->scope', function (e) {
  $(this).closest('tr').prop('class', '$this->form');
  $(this).closest('td').prop('class', 'field$this->form');
}); 

$(document).on('change', '.inp$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', '.sel2$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(rows) {
  var sel1Options = JSON.parse('$this->sel1Options');
  var sel2Options = JSON.parse('$this->sel2Options');
  var selections1 = '';
  var selections2 = '';

  for (var j=0; j<sel1Options.length; j++) {
    var value = sel1Options[j].id;
    var text  = sel1Options[j].text;
    selections1 += '<option value=' + value + '>' + text + '</option>';
  }    

  for (var j=0; j<sel2Options.length; j++) {
    var value = sel2Options[j].id;
    var text  = sel2Options[j].text;
    selections2 += '<option value=' + value + '>' + text + '</option>';
  }    
  
  var layout = 
    '<tr id=0>' +
      '<td data-element=select1 style=width:33%;>' +
        '<select class=sel1$this->scope>' + selections1 + '</select>' +
      '</td>' +
      '<td data-element=input style=width:34%;>' + 
        '<input type=text class=inp$this->scope $this->placeHolder>' +
      '</td>' +
      '<td data-element=select2 style=width:33%;>' +
        '<select class=sel2$this->scope>' + selections2 + '</select>' +
      '</td>' +
    '</tr>';
             
  for (var i=0; i<rows.length; i++) {
    var id = rows[i].id
    var selValue = rows[i].selval;
    var inpValue = rows[i].inpval;
    selections = '';

    for (var j=0; j<selOptions.length; j++) {
      optId = selOptions[j].id;
      text  = selOptions[j].text;

      var attributes = '';
      if (selValue == optId)
        attributes = ' selected';

      selections += '<option value=' + optId + attributes + '>' + text + '</option>';
    }    

    layout += 
      '<tr id=' + id + '>' +
        '<td class=field$this->scope data-element=select1 style=width:33%;>' +
          '<select class=sel1$this->form>' + selections1 +
          '<option value=-1>Delete</option></select>' +
        '</td>' +
        '<td style=width:34%;>' + 
          '<input type=text class=inp$this->form value=\"' + inpValue + '\">' +
        '</td>' +
        '<td class=field$this->scope data-element=select2 style=width:33%;>' +
          '<select class=sel2$this->form>' + selections2 +
        '</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 == 'select1') {
        values += comma + colon + $(this).find('.sel1$this->scope option:selected').val();
      }
      else if (element == 'input') {
        values += comma + colon + $(this).find('input').attr('data-value');
      }    
      else if (element == 'select2') {
        values += comma + colon + $(this).find('.sel2$this->scope option:selected').val();
      }

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

?>