<?php 

class TimeKeeper {

  private $scope;
  private $label;
  private $iterator;
  private $minutes;
  private $begin;
  private $end;
  private $colon;
  private $point;

  public function __construct($entry) {
 
    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->column   = $entry->column;
      $this->iterator = $entry->iterator;
      $this->entryNow = $entry->entryNow;
      $this->minutes  = explode (",",$entry->minList);
      list($this->begin, $this->end) = explode (",",$entry->hourRange);

      if (isset($entry->label))
//        $this->label = "<label style='vertical-align:45%;'>$entry->label</label>";
        $this->label = "<label>$entry->label</label>";
      else
        $this->label = "";

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

      $this->colon = "";
      $this->point = "";
      if ($entry->format == 'time') {
        $this->colon = "<span style=font-weight:800;float:left;display:inline;background:white;>:</span>";
      }  
      else if ($entry->format == 'duration') {
        $this->point = "&#8228;";
      }
      
      $this->startButton = "";
      $this->stopButton = "";
      $this->pauseResume = "";
      $this->timeType= "base";

      if (isset($entry->startButton)) {
        $this->timeType= "start";
        $this->startButton = 
          "<button class='punch punch-start but$this->scope'
          onclick='pun$this->scope(\"start\", \"00:00\", \"sequent_upd\");'>$entry->startButton
          </button>";
      }      

      else if (isset($entry->stopButton)) {
        $this->timeType= "stop";
        $this->stopButton = 
          "<button class='punch punch-stop but$this->scope'
          onclick='pun$this->scope(\"stop\", \"00:00\", \"sequent_upd\");'>$entry->stopButton
          </button>";
      }
      
      else if (isset($entry->duration)) {
        $this->timeType= "duration";
      }
      
      else if (isset($entry->pauseResume)) {
        $this->timeType= "break";
        $pause = explode ("_", $entry->pauseResume)[0];    
        $resume = explode ("_", $entry->pauseResume)[1];    
        $this->pauseResume = 
          "<button class='punch punch-stop but$this->scope' 
          onclick='pun$this->scope(\"pause\", \"00:00\", \"sequent_upd\");'>$pause
          </button>
          <button class='punch punch-start but$this->scope' 
          onclick='pun$this->scope(\"resume\", \"00:00\", \"sequent_upd\");'>$resume
          </button>";
      }
    }
  }
  
  public function outputLayout() {

    $layout = "
$this->stopButton$this->pauseResume$this->startButton
<label>$this->label</label>
<table style='height:28px;width:102px;border:1px solid grey; display: inline-block;'>
  <tr>
    <td>  
      <select id='hour$this->scope' name='hour$this->scope'
        style='height:19px;width:43px;direction:rtl;border:none;display:inline;padding-top:3px;'>
    ";
    for ($i=$this->begin; $i<$this->end; $i++) {
      $inner = sprintf("%02d", $i);
      $layout .= "
        <option value=$inner>$inner</option>
      ";
    }
  
    $layout .= "
      </select>   
    </td>
    <td>
      $this->colon
    </td>
    <td>
      <select id='min$this->scope' name='min$this->scope'
        style='height:19px;width:46px;border:none;display:inline;padding-top:3px;'>
	";
	
    for ($i=0; $i<count($this->minutes); $i++) {
      $selAttr = "";
      if ($this->minutes[$i] == $this->minValue)
        $selAttr = "selected";

      $inner = $this->minutes[$i];
      $layout .= "
        <option value=$inner $selAttr>$this->point$inner</option>
      ";
    }
  
    $layout .= "
      </select>
    </td>
  </tr>
</table>

  ";  

    return $layout;
  }

  public function outputScript() {

    $script = "

var blockPunch$this->scope = false;  
var selectId$this->scope = $this->selectId;
var delimiter$this->scope = ('$this->point' == '') ? ':' : '.';

// Minute dropdown changes  
$('#min$this->scope').on('change', function() {
  var hours = $('#hour$this->scope').val();
  var minutes = $(this).val();
  var time = hours + delimiter$this->scope + minutes
  base$this->scope (time, delimiter$this->scope);
});

// Hour dropdown changes  
$('#hour$this->scope').on('change', function() {
  var hours = $(this).val();
  var minutes =  $('#min$this->scope').val();
  var time = hours + delimiter$this->scope + minutes;
  base$this->scope (time, delimiter$this->scope);
});

// db value changes    
function set$this->scope (dbValue, dbText) {
//alert ('set:$this->scope: ' + dbValue);  

  var duration = String('0' + dbValue).slice(-5);
  var hours = String(duration.substring(0,2));
  var minutes = duration.substring(3,5);

  $('#hour$this->scope option[value='+hours+']').prop('selected',true);
  $('#min$this->scope option[value='+minutes+']').prop('selected',true);
}

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

function base$this->scope (value, text) {
//alert ('base:$this->scope-$this->timeType-' + '$this->column:' + value);  
  if ('$this->timeType' == 'base')
    send$this->scope (value, text, 'sequent_ent');
  else
    pun$this->scope ('$this->column', value, 'sequent_ent');  
}


function send$this->scope (value, text, source) {
//alert ('send:$this->scope-' + '$this->column:' + value);  
  var msgContent = [];
  msgContent.$this->scope = [value, text, source, '$this->scope', 'new field value'];
  newValue$this->iterator (msgContent);
}

function com$this->scope (message) {
  var key = Object.keys(message);
//alert ('$this->scope' + key + ' ' + message[key][0]);  

  if (key == 'selectId') {
    selectId$this->scope = message[key][0];
    blockPunch$this->scope = false;  
  }
  else if (key == 'timeSetting') {
    if (message[key][0] != '00:00')
      blockPunch$this->scope = true;
  }
  else if (key == 'timeUpdate') {
    pun$this->scope ('$this->column', '00:00', 'sequent_ent');
  }
  
  disenable$this->scope (selectId$this->scope, blockPunch$this->scope);
}  

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

function disenable$this->scope (selId, punch) {
//alert ('$this->scope ' + selId + ' ' + punch);
  if (selId == 0) {
    document.getElementById('hour$this->scope').disabled = true;
    document.getElementById('min$this->scope').disabled = true;
    $('.but$this->scope').prop('disabled', true);
  }
  else {
    document.getElementById('hour$this->scope').disabled = false;
    document.getElementById('min$this->scope').disabled = false;
    $('.but$this->scope').prop('disabled', punch);
  }
}

function pun$this->scope (set, time, sequent) {
  if (!(set == 'duration' && time == '00:00'))
    document.getElementById('timmsg$this->iterator').innerHTML = '';

  var params = 'set=' + set + '&' + 'time=' + time + '&' +
               'selectid=' + selectId$this->scope;
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/pun$this->iterator.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 (response[1] == 0) {
        document.getElementById('timmsg$this->iterator').innerHTML = response[2];
      }
      set$this->scope (response[0], '');  
      send$this->scope (response[0], '', 'sequent_ent');  
    }
  }
  xhr.send(params);
}

    ";
  
    return $script;
  }
}  


?>