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

class SummaryTable  {
  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->iterator  = $entry->iterator;

//print_r ($entry->tableHeader);        

      if (null !== $entry->tableHeader)
        $this->tableHeader = $entry->tableHeader;
      else
        $this->tableHeader = '[]';

      if (isset($entry->indexMask))
       $this->indexMask = $entry->indexMask;
      else
        $this->indexMask = 0;

      if (isset($entry->label))
        $this->label = "<label>$entry->label</label><br>";
      else
        $this->label = "";

      if (isset($entry->summaryId))
        $this->summaryId = $entry->summaryId;
      else
        $this->summaryId = 0;

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

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

      $this->header = 0;   
      $this->timeHeader = "";
      if (isset($entry->timeHeader)) {
        $this->header = 1;   
        $this->timeHeader = 
          "<div style='border:1px solid #ccc;margin-top:5px;background:#dcdcdc'>
             $entry->timeHeader - Hours: 
             <div id='hours$this->scope' style='display:inline ;'></div>
           </div>";
      }      
    }
  }

  public function outputLayout() {
      
    $layout = "

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

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

      $layout .= "
      <th>$colLabel</th>  
      ";
    }    
  
    $layout .= "
    </tr>
  </thead>
  <tbody id='body$this->scope' class='filter$this->scope'>      
  </tbody>  
</table>
</div>
    "; 


    return $layout;
  }	

  public function outputScript() {

	$script = " 
	
var selectId$this->scope = 0;
var summaryId$this->scope = 0;
var fkeyId$this->scope = 0;
var index$this->scope = '$this->indexMask';
var sumIndex$this->scope = '$this->indexMask';

// alert (index$this->scope);

function stopKeys$this->scope (e) {
  if (!(e.key == 'ArrowLeft' || e.key == 'ArrowRight'))
    e.preventDefault();
}

$(document).ready(function() {

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

  var str = '$this->summaryId';
  summaryId$this->scope = (isNaN (str)) ? str : parseInt (str);
// alert ('$this->scope entry'); 
  if (summaryId$this->scope > 0)
    get$this->scope (summaryId$this->scope);

//  var msgContent = [];
//  msgContent.$this->scope = [summaryId$this->scope, '', 'sequent_frm', '001', 'entry sumid'];
//  msgContent.$this->scope = [summaryId$this->scope, '', 'sequent_upd', '001', 'entry sumid'];
//  newValue$this->iterator (msgContent);

});
    
$(document).on('click', '.row$this->scope', function (e) {
//  $('.slide-panel').each(function(){ $(this).slideUp('slow'); });
   $('#upd-det'+selectId$this->scope).html('');
  
  if (!$(this).prop('checked')) {
    selectId$this->scope = 0;
  }
  else {  
    $('.row$this->scope').prop('checked', false);
    $(this).prop('checked', true);
    selectId$this->scope = $(this).val();
  }
//alert ('$this->scope:' + ' i:' + index$this->scope);
  var source = (index$this->scope > 0) ? 'sequent_upd' : 'sequent_frm';
//  var source = 'sequent_upd';
//alert ('$this->scope:' + source + ' i:' + index$this->scope);
  var msgContent = [];
  msgContent.$this->iterator = [selectId$this->scope, '', source, '001',  'sum new row id'];
  newValue$this->iterator (msgContent);

});

$('.summarynote').keypress(function(event){
  event.preventDefault();
});

function set$this->scope (value, text) {
//alert ('set:' + value);
  summaryId$this->scope = value;
//  $('.row$this->scope[value='+selectId$this->scope+']').prop('checked', true);
}

multiFunction['set$this->scope'] = {
  set$this->scope: set$this->scope
}; 

function com$this->scope (message) {
  $('#valmsg$this->scope').html('');
  var key = Object.keys(message); 
logMessage ('<td>com msg</td><td>' + key +  '</td><td>$this->scope</td><td>' + message[key][0] + '</td><td>' + message[key][1] + '</td><td>' + message[key][2] + '</td><td>' + message[key][3] + '</td><td>' + message[key][4] + '</td>'); 
//alert (message[key][3]);
  sumIndex$this->scope = message[key][3];
//  index$this->scope = message[key][3];
//alert ('com:$this->scope:' + key + ':' + message[key][0] + ':' + 'sumid:' + summaryId$this->scope + ' index:' +  message[key][3]); 

  if (key == 'fkeyId') {
    summaryId$this->scope = message[key][0];
//alert ('fk $this->scope summaryid:' + summaryId$this->scope); 
    $('.row$this->scope[value='+selectId$this->scope+']').click();
//    $('.row$this->scope[value='+selectId$this->scope+']').prop('checked', false);
    selectId$this->scope = 0;
     
    get$this->scope (message[key][0]);
  }  

  else if (key == 'summaryId') {
//alert ('$this->scope:' + key + ' sumid:' + message[key][0] + ' ' + summaryId$this->scope);
//alert (' new idx:' + message[key][3] + ' sumid:' + message[key][0]);
      summaryId$this->scope = message[key][0];
//alert ('si $this->scope summaryid:' + summaryId$this->scope); 
//    $('.row$this->scope[value='+selectId$this->scope+']').click();
//    $('.row$this->scope[value='+selectId$this->scope+']').prop('checked', false);
       
      if (message[key][3] > 1)
        selectId$this->scope = 0;
      get$this->scope (summaryId$this->scope);
  var msgContent = [];
  msgContent.$this->scope = [summaryId$this->scope, '', 'sequent_frm', '001',  'new sumid'];
  newValue$this->iterator (msgContent);
//  var msgContent = [];
//  msgContent.$this->iterator = [0, '', 'sequent_frm', '001',  'new sumid'];
//  newValue$this->iterator (msgContent);
    }  

  else if (key == 'summaryId2') {
    summaryId$this->scope = message[key][0];
    $('.row$this->scope[value='+selectId$this->scope+']').click();
//    $('.row$this->scope[value='+selectId$this->scope+']').prop('checked', false);
       
    if (message[key][3] > 1)
      selectId$this->scope = 0;
    get$this->scope (summaryId$this->scope);
    var msgContent = [];
    msgContent.$this->scope = [summaryId$this->scope, '', 'sequent_frm', '001',  'new sumid'];
    newValue$this->iterator (msgContent);
  }  


  else if (key == 'selectId') {
    if (message[key][0] != selectId$this->scope) {
      selectId$this->scope = message[key][0];
//alert ('sel click:' + message[key][0] + ' ' + selectId$this->scope);
      $('.row$this->scope[value='+selectId$this->scope+']').prop('checked', true);
//      $('.row$this->scope[value='+selectId$this->scope+']').click();
    }
  }     

  else if (key == 'updateId') {
//alert ('$this->scope:' + key + ' selid:' + message[key][0] + ' ' + summaryId$this->scope);
    selectId$this->scope = message[key][0];
//alert ('$this->scope updateId'); 
     
    get$this->scope (summaryId$this->scope);
  }  
}

multiFunction['com$this->scope'] = {
  com$this->scope: com$this->scope
}; 

function gen$this->scope (resp) {
  var inner = '';
  var readonly = '';
  var missedDur = false;
  var totalDur = 0.0;
  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=row$this->scope type=checkbox value=' + resp[i].id + readonly + '> ' + 
             resp[i][formFields[j]] + '</label><div id=upd-det' + resp[i].id + '></div>';
      } 
      else if (formFields[j] == 'Note') {
        inner += '<td><input type=text onkeydown=stopKeys$this->scope(event) ' +
                  'style=max-width:300px; value=' + '\"' + resp[i][formFields[j]] + '\"' + 
                  ' </input></td>';
      }      
//      else if (formFields[j] == 'Note') {
//        inner += '<td><textarea class=summarynote wrap=off ' +
//                  'rows=1 style=max-width:100px;overflow:auto;>' +
//                  resp[i][formFields[j]] + '</textarea></td>';
//      }      
//      else if (formFields[j] == 'Note') {
//        inner += '<td><div style=overflow:auto;max-width:300px;>' +
//                  resp[i][formFields[j]] + '</div></td>';
//      }      
      else
        inner += '<td>' + resp[i][formFields[j]] + '</td>'; 

      if (formFields[j] == 'Duration') {
        var dur = Number(resp[i][formFields[j]]);
        totalDur += dur;
        if (dur == 0)
          missedDur = true;
      }
    }
    inner += '</tr>';
  }
  inner += '</tbody>';
  if ($this->header == 1)
    document.getElementById ('hours$this->scope').innerHTML = totalDur;
//    document.getElementById ('hours$this->scope').innerHTML = 
//      missedDur ? '<span style=color:red>check durations</span>' : totalDur;
  
  return inner;
}

function get$this->scope(summaryId) {
  var params = 'summaryid=' + summaryId + '&' + 
               'fkeyid=' + fkeyId$this->scope + '&' +
//               'index=' + index$this->scope + '&' +
               'index=' + sumIndex$this->scope + '&' +
               'field=$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)
 
      if (summaryId == 0)
        document.getElementById('body$this->scope').innerHTML = '';
      else
        document.getElementById('body$this->scope').innerHTML = gen$this->scope(response);
 
//alert ('$this->scope:' + selectId$this->scope);
      if (selectId$this->scope > 0) {
      
        $('.row$this->scope[value='+selectId$this->scope+']').prop('checked', true);
//        $('#upd-det'+selectId$this->scope).html('<span class=\'glyphicon glyphicon-asterisk\'></span>');
      }
    }
  }
  xhr.send(params);
}

  ";

  return $script;
  }
}

?>