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

class Timesheet  {
  private $idKey;
  private $scope;
  private $formDefs;
  private $scopeId;
  private $data;
  private $messageList;
  private $iterator;
  private $dropCols;  

  public function __construct($entry) {

    if (isset($entry)) {

      $this->idKey     = $entry->idKey;
      $this->scope     = $entry->scope;
      $this->scopeId   = $entry->scopeId;
      $this->subdetail = $entry->subdetail;
      $this->iterator  = $entry->iterator;
      $this->dropCols  = $entry->dropCols;
      
      if (null !== $entry->formDefs)
        $this->formDefs = json_encode($entry->formDefs);
      else
        $this->formDefs = '{}';
 
      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);
        $this->messageList = "";

        for ($i=0; $i<count($messages); $i++) {
          $this->messageList .= "notify" . $messages[$i] . "(dbRowId); ";
        }
      }
     
      if ((count($entry->data)) > 0) {
        $this->data  = $entry->data;
        $firstRow = reset($this->data);
        $line = get_object_vars($firstRow);
        $this->props = array_keys($line);

        if ($firstRow->id == 0)       
          $this->data  = [];

      }
      else {
        $this->data  = [];
        $this->props = [];
      }
    }
  }

  public function outputLayout() {
$cnt = count($this->data);
//print_r ("d4:$cnt "); 
      
    $layout = "

<div>

    ";

    $current = 0;
    $notFirst = false;
    $row = 0;
    foreach ($this->data as $line) {
      $row++;
      if ($current != $line->id) {
        if ($notFirst) {
          $layout .= "
    </tbody>  
  </table>
          ";
        }  
        $notFirst = true;
        $current = $line->id;
        $layout .= "
  <div style='border:1px solid #ccc;margin-top:5px;background:#dcdcdc'>
    $line->text - Hours: <div id='hours$line->id' style='display:inline;'>$line->total_dur</div>
  </div>
  <table class='table table-bordered'>
    <thead>
      <tr>
        ";
        for ($i=4; $i<count($this->props) - $this->dropCols; $i++) {
          $prop = $this->props[$i];
          $colLabel = ucfirst(str_replace("_", " ", $prop));
          $layout .= "
        <th>$colLabel</th>  
          ";
        }
        $layout .= "
        <td>
        </td>
      </tr>
    </thead>
    <tbody> 
        ";    
      }
      $layout .= "
      <tr>
        <td>
          <label><input id='$row' class='time-sheet' type='checkbox' value='$line->put_id'> $lineLabel 
			    <div id='upd-ind$row'></div></label>
        </td>
      "; 

      for ($i=5; $i<count($this->props) - $this->dropCols; $i++) {
        $prop = $this->props[$i];
        $cellValue = $line->$prop;
        if ($prop == "billable") { 
          $checked = "";
          if ($cellValue == "1") {
            $checked = "checked";
          }    
 
          if ($line->role_type == "travel") {
            $cellValue = "<input type=checkbox name=$prop class='upd$row' class='upd$row' $checked disabled
              onchange=\"putupdate($line->id, $line->put_id, $currValue, $(this).prop('checked'), 'billtravel')\">";       
          }
          else if ($line->role == "Absent") { 
            $cellValue = "<input type=checkbox name=$prop disabled>"; 
          }
          else {
            $cellValue = "<input type=checkbox name=$prop class='upd$row' $checked disabled
              onchange=\"putupdate($line->id, $line->put_id, $currValue, $(this).prop('checked'), 'billproject')\">";       
          }    
        }
        
        else if ($prop == "duration") {
          $currValue = $cellValue;

          if ($line->role_type == "travel") {
            $cellValue = "<input type='text' name='$prop' class='upd$row' value='$cellValue' disabled
              onchange=\"putupdate($line->id, $line->put_id, $currValue, $(this).val(), 'durtravel')\"       
              style='width:75px'>";          
          }      
          else if ($line->role == "Absent") {
            $cellValue = "<input type='text' name='$prop' value='$cellValue' disabled style='width:75px'>";          
          }      
          else {
            $cellValue = "<input type='text' name='$prop' class='upd$row' value='$cellValue' disabled
              onchange=\"putupdate($line->id, $line->put_id, $currValue, $(this).val(), 'durproject')\"       
              style='width:75px'>"; 
          }      
        }
        else if ($prop == "rationale") {
          if ($line->role_type == "travel") {
            $cellValue = "<input type='text' name='$prop' class='upd$row' value='$cellValue' disabled
              onchange=\"putupdate($line->id, $line->put_id, $currValue, $(this).val(), 'rattravel')\"       
              style='width:200px'>";          
          }      
          else if ($line->role == "Absent") {
            $cellValue = "<input type='text' disabled style='width:200px'>";          
          }      
          else {
            $cellValue = "<input type='text' name='$prop' class='upd$row' value='$cellValue' disabled
              onchange=\"putupdate($line->id, $line->put_id, $currValue, $(this).val(), 'ratproject')\"       
              style='width:200px'>";          
          }      
        }

        else if ($prop == "note") {

          if ($line->role_type == "travel") {
            $cellValue = "<input type='text' name='$prop' class='upd$row' value='$cellValue' disabled
              onchange=\"putupdate($line->id, $line->put_id, $currValue, $(this).val(), 'notetravel')\"       
              style='width:200px'>";          
          }
          else {
            $cellValue = "<input type='text' name='$prop' class='upd$row' value='$cellValue' disabled
              onchange=\"putupdate($line->id, $line->put_id, $currValue, $(this).val(), 'noteproject')\"       
              style='width:200px'>";          
          }      
        }
        $layout .= "
      <td>
        $cellValue 
      </td>
     
        ";    
      }
  	  $layout .= "    
      <td>
        <button class='btn btn value=$row inverse' onclick=commit($row)>Commit</button>
      </td>
      </tr>
      ";
    }
 	  $layout .= "    
    </tbody>  
  </table>
</div>
    ";
    return $layout;
  }	

  public function outputScript() {

	$script = "
  
var currentRow;  
$(document).on('click', '.time-sheet', function (e) {

  if (!$(this).prop('checked')) {
    $('.upd'+currentRow).each(function () {
      $(this).prop('disabled', true);
    });
    $(this).prop('checked', false);
  }
  else {
    $('.upd'+currentRow).each(function () {
      $(this).prop('disabled', true);
    });
    $('.time-sheet').prop('checked', false);
    $(this).prop('checked', true);
    currentRow = $(this).prop('id'); 
    $('.upd'+currentRow).each(function () {
      $(this).prop('disabled', false);
    });
  }
});

function commit (row) {
  $('.upd'+row).each(function () {
    $(this).prop('disabled', true);
  });
  $('.time-sheet#'+row).prop('checked', false);
//  $('#upd-ind'+row).html('<span class=\'glyphicon glyphicon-asterisk\'></span>');
}

function putupdate (pe_id, id, current, value, type) {
  var data = 'id=' + id + '&' + 'value=' + value + '&' + 'type=' + type;  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/puttimesheet.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 (type.indexOf('dur') > -1) {
        currTotal = parseFloat($('#hours'+pe_id).html());
        newTotal = currTotal - (current - value);
//alert (currTotal + ' ' + id);        
        $('#hours'+pe_id).html(newTotal);
      }
    }
  }  
  xhr.send(data);
}

 


  ";

  return $script;
  }
}

?>