<?php 

class SelectHourMin {

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

  public function __construct($entry) {
 
    if (isset($entry)) {
      $this->field    = $entry->field;
      $this->column   = $entry->column;
      $this->form = $entry->form;
      $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 id='lab$this->field'>$entry->label</label>";
      else
        $this->label = "";

      $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 = "";
      if (isset($entry->startButton))
        $this->startButton = 
          "<button id='start$this->field' class='punch punch-start'
          onclick='pun$this->field(\"start\");'>$entry->startButton
          </button>";
      $this->stopButton = "";
      if (isset($entry->stopButton))
        $this->stopButton = 
          "<button id='stop$this->field' class='punch punch-stop'
          onclick='pun$this->field(\"stop\");'>$entry->stopButton
          </button>";

      $this->pauseResume = "";
      if (isset($entry->pauseResume)) {
        $pause = explode ("_", $entry->pauseResume)[0];    
        $resume = explode ("_", $entry->pauseResume)[1];    
        $this->pauseResume = 
          "<button id='pause$this->field' class='punch punch-stop' 
          onclick='pun$this->field(\"pause\");'>$pause
          </button>
          <button id='resume$this->field' class='punch punch-start' 
          onclick='pun$this->field(\"resume\");'>$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 fkeyId$this->field = 0;
var selectId$this->field = 0;
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();
  send$this->field (hours + delimiter$this->field + minutes, delimiter$this->field);
});

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

// db value changes    
function set$this->field (dbValue, dbText) {
  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 send$this->field (value, text) {
//alert (value);
  var msgContent = [];
  msgContent.$this->field = [value, text, 'formentry', '$this->field', 'new field value'];
  newValue$this->form (msgContent);
}

function com$this->field (message) {
  var key = Object.keys(message);
//alert (key +  ' ' + message[key][0]);  
  if (key == 'updateId') {
    if (selectId$this->field > 0)
      pun$this->field ('duration');
  }    
  else if (key == 'selectId')
    selectId$this->field = message[key][0];
}  

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

function pun$this->field(button) {

  var params = 'button=' + button + '&' + '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);
      set$this->field (response, '');  
      send$this->field (response, '');  
    }
  }
  xhr.send(params);
}

    ";
  
    return $script;
  }
}  


?>