<?php 

class SelectHourMin {

  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 id='lab$this->scope'>$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->scope' class='punch punch-start'
          onclick='pun$this->scope(\"start\");'>$entry->startButton
          </button>";
      $this->stopButton = "";
      if (isset($entry->stopButton))
        $this->stopButton = 
          "<button id='stop$this->scope' class='punch punch-stop'
          onclick='pun$this->scope(\"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->scope' class='punch punch-stop' 
          onclick='pun$this->scope(\"pause\");'>$pause
          </button>
          <button id='resume$this->scope' class='punch punch-start' 
          onclick='pun$this->scope(\"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->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 fkeyId$this->scope = 0;
var selectId$this->scope = 0;
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();
  send$this->scope (hours + delimiter$this->scope + minutes, delimiter$this->scope);
});

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

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

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

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

function pun$this->scope(button) {

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

    ";
  
    return $script;
  }
}  


?>