<?php 

class TimeKeeper {

  private $field;
  private $label;
  private $form;
  private $minutes;
  private $begin;
  private $end;
  private $colon;
  private $point;

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

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

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

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

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

      else if (isset($field->stopButton)) {
        $this->timeType= "stop";
        $this->stopButton = 
          "<button class='punch punch-stop but$this->field'
          onclick='pun$this->field(\"stop\", \"00:00\", \"sequent_upd\");'>$field->stopButton
          </button>";
      }
      
      else if (isset($field->duration)) {
        $this->timeType= "duration";
      }
      
      else if (isset($field->pauseResume)) {
        $this->timeType= "break";
        $pause = explode ("_", $field->pauseResume)[0];    
        $resume = explode ("_", $field->pauseResume)[1];    
        $this->pauseResume = 
          "<button class='punch punch-stop but$this->field' 
          onclick='pun$this->field(\"pause\", \"00:00\", \"sequent_upd\");'>$pause
          </button>
          <button class='punch punch-start but$this->field' 
          onclick='pun$this->field(\"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->field' name='hour$this->field'
        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->field' name='min$this->field'
        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->field = false;  
var selectId$this->field = $this->selectId;
var delimiter$this->field = ('$this->point' == '') ? ':' : '.';

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

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

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

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

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

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

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


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

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

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

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

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

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

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

    ";
  
    return $script;
  }
}  


?>