<?php 
defined('PUBLIC_ACCESS') or die('Restricted Access');

class TabularForm  {
  private $scope;
  private $iterator;
  private $summaryId;
  private $summary;
  private $props;
  private $formDefs;
  private $postMessages;
  private $yScroll;
  

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope     = $entry->scope;
      $this->selectId  = $entry->selectId;
      $this->summaryId = $entry->summaryId;
      $this->iterator  = $entry->iterator;

      if (isset ($entry->yScroll)) 
        $this->yScroll = "style='height:$entry->yScroll;overflow:auto;'";
      else
        $this->yScroll = "style='overflow:auto;'";

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

      $this->postMessages = "";
      if (null !== $entry->listeners) {
        $listeners = explode(",", $entry->listeners);
        for ($i=0; $i<count($listeners); $i++) {
          $this->postMessages .= "listener" . $listeners[$i] . "(msgContent); ";
        }
      }    
     
      if ((count($entry->summary)) > 0) {
        $this->summary  = $entry->summary;
        $firstRow = reset($this->summary);
        $row = get_object_vars($firstRow);
        $this->props = array_keys($row);

        if ($firstRow->id == 0)       
          $this->summary  = [];
      }
      else {
        $this->summary  = [];
        $this->props = [];
      }
    }
  }

  public function outputLayout() {
      
    $layout = "

<div id='summ-div$this->scope' $this->yScroll>
    
<table id=fth$this->scope class='table table-condensed'>
  <thead>
    <tr>
    ";

    for ($i=1; $i<count($this->props); $i++) {
      $colLabel = $this->props[$i];

      $layout .= "
      <th>$colLabel</th>  
      ";
    }    
  
    $layout .= "
    </tr>
  </thead>
  <tbody id='body$this->scope' class='filter$this->scope'>      
    ";
 
    foreach ($this->summary as $row) {
      $readonly = '';    
      $prop1 = $this->props[1];  
      $rowLabel = $row->$prop1;

      $layout .= "
    <tr>
      <td>
        <label><input class='summary$this->scope' type='checkbox'
          value='$row->id' $readonly> $rowLabel</label> 
	    <div id='upd-det$row->id'></div>
      </td>
      ";  

      for ($i=2; $i<count($this->props); $i++) {
        $prop = $this->props[$i];
        $cellValue = $row->$prop;
        $layout .= "
      <td $style>
        $cellValue 
      </td>
        ";    
      }
      $layout .= "
      </tr>
      ";
    }
 	  $layout .= "    
  </tbody>  
</table>
</div>
    "; 


    return $layout;
  }	

  public function outputScript() {

	$script = "

$(document).ready(function() {

  summaryId$this->scope = $this->summaryId;
  selectId$this->scope = $this->selectId;

  // passing a fixedOffset param will cause the table header to stick to the bottom of this element
  if ($('#summ-div$this->scope').get(0).scrollHeight > $('#summ-div$this->scope').height()) {  
	  $('#fth$this->scope').stickyTableHeaders({ scrollableArea: $('.scrollable-area')[0], 'fixedOffset': 2 });
  }
  var offset = $('.navbar').height();
//  $('html:not(.legacy) table').stickyTableHeaders({fixedOffset: offset});
//  $('table').stickyTableHeaders({fixedOffset: offset});


  if (selectId$this->scope > 0 && summaryId$this->scope > 0) {
    $('.summary$this->scope[value='+selectId$this->scope+']').click();
  }
});
    
$(document).on('click', '.summary$this->scope', function (e) {

//  $('.slide-panel').each(function(){ $(this).slideUp('slow'); });
  $('#upd-det'+selectId$this->scope).html('');
  
  if (!$(this).prop('checked')) {
    $('#del$this->iterator').prop('disabled', true);
    $('#fdh$this->iterator').html($('#fdh$this->iterator').prop('value').split('_')[0]);
    selectId$this->scope = 0;
  }
  else {  
    $('.summary$this->scope').prop('checked', false);
    $(this).prop('checked', true);
    $('#del$this->iterator').prop('disabled', false);
    $('#fdh$this->iterator').html($('#fdh$this->iterator').prop('value').split('_')[1]);
    selectId$this->scope = $(this).val();
  }	
  var msgContent = [];
  msgContent.$this->iterator = [selectId$this->scope,''];
  $this->postMessages
});

function notify$this->scope (msgId) {
  selectId$this->scope = msgId;
  get$this->scope (selectId$this->scope);   
}

function listener$this->scope (message) {
  var key = Object.keys(message); 
  $('#valmsg$this->scope').html('');
  if (message[key][0] != 0 && summaryId$this->scope != message[key][0]) { 
//alert ('st:' + key + ':' + message[key][0] + ' ' + summaryId$this->scope);
    if (key == '$this->iterator') {
      selectId = message[key][0];
    }  
    else {  
      summaryId$this->scope = message[key][0];
      selectId = 0;
    }  
    get$this->scope(selectId);
  }    

//  else if (cnfReply.reset)    
//    $('.detail-summ[value='+'selectId$this->scope'+']').click();
}

function gen$this->scope (resp) {
  var inner = '';
  var readonly = '';
  var formFields;
  for (var i=0; i<resp.length; i++) {
    if (i == 0) {
      formFields = Object.keys (resp[i]);
    }
    
    inner += '<tr>';
    for (var j=1; j < formFields.length; j++) { 
      if (j == 1) {
        inner += '<td><label><input class=summary$this->scope type=checkbox value=' + resp[i].id + readonly + '> ' + 
             resp[i][formFields[j]] + '</label><div id=upd-det' + resp[i].id + '></div>';
      }       
      else
        inner += '<td>' + resp[i][formFields[j]] + '</td>'; 
    }
    inner += '</tr>';
  }
  inner += '</tbody>';
  return inner;
}

function get$this->scope(selectId) {

  var params = 'summaryid=' + summaryId$this->scope ;
  
  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() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText)
      $('#body$this->scope').html(gen$this->scope(response));
      if (selectId$this->scope > 0) {  
        $('#upd-det'+selectId).html('<span class=\'glyphicon glyphicon-asterisk\'></span>');
        $('.summary$this->scope[value='+selectId+']').click();
      }
    }
  }  
  xhr.send(params);
}

  ";

  return $script;
  }
}

?>