<?php 

class MultiHourMin {

  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->label    = $entry->label;
      $this->iterator = $entry->iterator;
      $this->minutes  = explode (",",$entry->minList);
      list($this->begin, $this->end) = explode (",",$entry->hourRange);

      $colon = "";
      $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 ="<span style=font-weight:700;float:left;display:inline;>.</span>";
        
      }
    }
  }
  
  public function outputLayout() {

    $layout = "
<label style='margin-bottom:0px;' for='tab$this->scope'>$this->label</label><br>

<div style='border:1px solid grey;'>
  <input type='hidden' id=0 class='time$this->scope' name='$this->scope' value='00:00'> 
  <select id=0 name='hour_taken' class='hour$this->scope'
    style='height:25px;width:37px;direction:rtl;border:none;display:inline;'>
    ";
    for ($i=$this->begin; $i<=$this->end; $i++) {
      $inner = sprintf("%02d", $i);
      $layout .= "
    <option value=$inner>$inner</option>
      ";
    }
  
    $layout .= "
  </select>   
      $this->colon
  <select id=0 name='min_taken' class='min$this->scope'
    style='height:25px;width:43px;border:none;display:inline;'>
	";
    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>
</div>
  ";  

    return $layout;
  }

  public function outputScript() {
    $script = "
  
$('.min$this->scope').on('change', function() {
  rowid = $(this).prop('id');
  newMin = $(this).val();
  current = $('#'+rowid+'.time$this->scope').val(); 
  $('#'+rowid+'.time$this->scope').val(current.substring(0,3) + newMin);  
});

$('.hour$this->scope').on('change', function() {
  rowid = $(this).prop('id');
  newHour = $(this).val();
  current = $('#'+rowid+'.time$this->scope').val(); 
  $('#'+rowid+'.time$this->scope').val(newHour + current.substring(2));  
});

$(document).on('chg$this->iterator', '.time$this->scope', function(e,rowid) {

  var duration = $(this).val();
  var hours = String(duration.substring(0,2));
  var minutes = duration.substring(3,5);
  $('#'+rowid+'.hour$this->scope').find('option[value='+hours+']').prop('selected',true);
  $('#'+rowid+'.min$this->scope').find('option[value='+minutes+']').prop('selected',true);
});
    ";
    return $script;
  }

}

?>