<?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($field) {

    if (isset($field)) {
      $this->field     = $field->field;
      $this->form  = $field->form;

//print_r ($field->tableHeader);        

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

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

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

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

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

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

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

  public function outputLayout() {
      
    $layout = "

<div id='div$this->field' $this->yScroll>
$this->timeHeader$this->label
<table id=table$this->field 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->field' class='filter$this->field'>      
  </tbody>  
</table>
</div>
    "; 


    return $layout;
  }	

  public function outputScript() {

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

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

function stopKeys$this->field (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->field').get(0).scrollHeight > $('#div$this->field').height()) {  
	  $('#table$this->field').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->field = (isNaN (str)) ? str : parseInt (str, 2);
// alert ('$this->field entry'); 
//  if (summaryId$this->field > 0)
//    get$this->field (summaryId$this->field);

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

});
    
$(document).on('click', '.row$this->field', function (e) {
//  $('.slide-panel').each(function(){ $(this).slideUp('slow'); });
   $('#upd-det'+selectId$this->field).html('');
  
  if (!$(this).prop('checked')) {
    selectId$this->field = 0;
  }
  else {  
    $('.row$this->field').prop('checked', false);
    $(this).prop('checked', true);
    selectId$this->field = $(this).val();
  }
//alert (preSender);
  var sender = preSender + '$this->form';
  var msgContent = [];
  msgContent.$this->form = [sender, selectId$this->field, 0, '$this->form', 'sum new row id'];
  newValue$this->form (msgContent);
});

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

function set$this->field (value, attr) {
//alert ('$this->field set:' + value + '-+-' + attr[0] + '-+-' + attr[1]);
  summaryId$this->field = value;
//  var gen = parseInt(attr,2);
//  var pos2 = parseInt('010',2);
//  var pos1 = parseInt('001',2);
  

//  if ((gen & 2) > 0)
//    get$this->field (value);
  
//  if (gen & 1 > 0)
//     $('.row$this->field[value='+attr[1]+']').click();
//     $('.row$this->field[value='+attr[1]+']').prop('checked', true);
}

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

function com$this->field (message) {
  $('#valmsg$this->field').html('');
  var key = Object.keys(message); 
logMessage ('<td>com msg</td><td>' + key + '</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->field = message[key][3];
//  index$this->field = message[key][3];
//alert ('com:$this->field:' + key + ':' + message[key][0] + ':' + 'sumid:' + summaryId$this->field + ' index:' +  message[key][3]); 

  if (key == 'fkeyId') {
    var sender = 'fky' + '$this->field';
    summaryId$this->field = message[key][1];
    selectId$this->field = 0;
    get$this->field (message[key][0], message[key][1], message[key][2]);
    var msgContent = [];
    msgContent.$this->field = [sender, message[key][1], message[key][2], '$this->field', 'set fkey'];
    newValue$this->form (msgContent);
  }  

  else if (key == 'summaryId') {
  preSender = 'cxt';
//alert ('sumid $this->field:' + key + ' sumid:' + message[key][1] + ' selid:' + message[key][2]);
//alert (' new idx:' + message[key][3] + ' sumid:' + message[key][1]);
    summaryId$this->field = message[key][1];
    var selectId = message[key][2];
//alert ('si $this->field summaryid:' + summaryId$this->field); 
//    $('.row$this->field[value='+selectId+']').click();
       
//      if (message[key][3] > 1)
//        selectId$this->field = 0;
      get$this->field (message[key][0], message[key][1], message[key][2]);

//    $('.row$this->field[value='+selectId+']').prop('checked', true);
//  var msgContent = [];
//  msgContent.$this->field = [summaryId$this->field, 110, 'sequent_frm', '001',  'new sumid'];
//  newValue$this->form (msgContent);
//  var msgContent = [];
//  msgContent.$this->form = [0, '', 'sequent_frm', '001',  'new sumid'];
//  newValue$this->form (msgContent);
    }  

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


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

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

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

function gen$this->field (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->field 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->field(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->field').innerHTML = totalDur;
//    document.getElementById ('hours$this->field').innerHTML = 
//      missedDur ? '<span style=color:red>check durations</span>' : totalDur;
  
  return inner;
}

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

  ";

  return $script;
  }
}

?>